rfe-p> exec elements

relfun
rfe-p> style prolog
rfe-p> inter
rfi-p> destroy
rfi-p> 
rfi-p> %------------------------------------------------------------------------
rfi-p> %   Validation and Exploration of the Periodic System of the Elements:
rfi-p> %
rfi-p> %                       A Relfun Knowledge Base
rfi-p> %
rfi-p> %------------------------------------------------------------------------
rfi-p> 
rfi-p> 
rfi-p> % SEE COMMENTS EARLY IN FILE elements.rfp FOR INTRODUCTORY EXPLANATIONS !
rfi-p> 
rfi-p> % read knowledge base elements.rfp:
rfi-p> 
rfi-p> consult "/home/rfm/RELFUN/RFM/demo/sampler/elements" 
Reading file "/home/rfm/RELFUN/RFM/demo/sampler/elements.rfp"
rfi-p> 
rfi-p> size
(clauses 223)
(literals 396)
rfi-p> 
rfi-p> % Each atomic entry has the following structure (the emphasis on
rfi-p> % chemical properties derives from our application considerations):
rfi-p> 
rfi-p> % atm(<number>, 
rfi-p> %     name[<symbol>, <engl>],
rfi-p> %     prop[<m metal, g gaseous, sm semimetal, sc semiconductor,
rfi-p> %                                                nyd not yet determined>,
rfi-p> %          eco[<electron configuration of last three shells>],
rfi-p> %          <atom weight>,
rfi-p> %          oxi[<sequence of possible oxidations>]]).
rfi-p> 
rfi-p> % suppose there was an ill-formed atm entry (this one indeed we had):
rfi-p> rx atm(61, name[pm, promethium], prop[m, eco[23, 8, 2], 145, oxi[3]]).
rfi-p> az atm(61, name[pm, promethium], prop[m, eco[145], 23, 8, 2, oxi[3]]).
rfi-p> 
rfi-p> % this could be detected using an atm signature declaration,
rfi-p> % corresponding to the above structure, for syntactic validation:
rfi-p> 
rfi-p> pause()
true
rfi-p> 
rfi-p> % without signature 'filter' all atoms qualify:
rfi-p> length(tupof(atm( | _)))
107
rfi-p> 
rfi-p> 
rfi-p> % assert signature clause at the beginning of the knowledge base:
rfi-p> sl
rfi-l> a0 (sg (atm $integerp
                   (name $symbolp $symbolp)
                   (prop (dom m g sm sc nyd)
                         (eco $integerp $integerp $integerp)
                         $numberp
                         id ) ) )         ;; interpreter: (oxi | id)
rfi-l> sp
rfi-p> 
rfi-p> % with signature 'filter' one atom doesn't qualify:
rfi-p> length(tupof(atm( | _)))
106
rfi-p> 
rfi-p> % ill-formed atoms localized by missint on the list of element numbers:
rfi-p> listing missint
missint([]) :-& [].
missint([E]) :-& [].
missint([F,S|R]) :- S is +(F,1) & missint([S|R]).
missint([F,S|R]) :-& tup(+(F,1)|missint(tup(+(F,1),S|R))).
rfi-p> missint(tupof(atm(Eno | _), Eno))
[61]
rfi-p> 
rfi-p> % ill-formed query fails because of the signature test:
rfi-p> atm(61, name[pm, promethium], prop[m, eco[145], 23, 8, 2, oxi[3]])
unknown
rfi-p> 
rfi-p> % well-formed query fails because not in the knowledge base:
rfi-p> atm(61, name[pm, promethium], prop[m, eco[23, 8, 2], 145, oxi[3]])
unknown
rfi-p> 
rfi-p> % let us restore the well-formed atm entry:
rfi-p> rx atm(61, name[pm, promethium], prop[m, eco[145], 23, 8, 2, oxi[3]]).
rfi-p> az atm(61, name[pm, promethium], prop[m, eco[23, 8, 2], 145, oxi[3]]).
rfi-p> 
rfi-p> % well-formed query now succeeds:
rfi-p> atm(61, name[pm, promethium], prop[m, eco[23, 8, 2], 145, oxi[3]])
true
rfi-p> 
rfi-p> pause()
true
rfi-p> 
rfi-p> % find description of the atom with number 45:
rfi-p> 
rfi-p> atm(45 | Description)
true
Description=[name[rh,rhodium],prop[m,eco[18,16,1],102.9055,oxi[3]]]
rfi-p> 
rfi-p> % enumerate the atoms having less shells than a preceding one
rfi-p> % (third eco shell is 0 and atomic number is greater than 10):
rfi-p> 
rfi-p> atm(Number,
           name[Symb, Engl],
           prop[Kind,
                eco[Shell-a, Shell-b, 0],
                Weight,
                Oxi]),
       >(Number, 10),
       discovered[Engl]
discovered[palladium]
Number=46
Symb=pd
Engl=palladium
Kind=m
Shell-a=18
Shell-b=18
Weight=106.42
Oxi=oxi[2,4]
rfi-p> more                        % request more solutions
unknown
rfi-p> 
rfi-p> pause()
true
rfi-p> 
rfi-p> % find all noble gases (their oxidation is 0) and show their outer
rfi-p> % electron configuration (which 'explains' the non-oxidation property):
rfi-p> 
rfi-p> tupof(atm(_, name[_, Engl], prop[g, Eco, _, oxi[0]]), found[Engl, Eco])
[found[helium,eco[2,0,0]],
 found[neon,eco[2,8,0]],
 found[argon,eco[2,8,8]],
 found[krypton,eco[8,18,8]],
 found[xenon,eco[18,18,8]],
 found[radon,eco[32,18,8]]]
rfi-p> 
rfi-p> pause()
true
rfi-p> 
rfi-p> % enumerate the metals using a nullary, non-deterministic function:
rfi-p> 
rfi-p> listing metal
metal() :- 
         atm(Eno,name[Abbr,Full],prop[m,Eco,Weight,Oxi]) &
         [Eno,Abbr,Full].
rfi-p> 
rfi-p> metal()
[3,li,lithium]
rfi-p> more                        % request more solutions
[4,be,beryllium]
rfi-p> more
[11,na,sodium]
rfi-p> pause()
true
rfi-p> 
rfi-p> % enumerate 'large' metals:
rfi-p> 
rfi-p> [Eno | Rest] is metal(), >(Eno, 20), large-metal[|Rest]
large-metal[sc,scandium]
Eno=21
Rest=[sc,scandium]
rfi-p> more
large-metal[ti,titanium]
Eno=22
Rest=[ti,titanium]
rfi-p> more
large-metal[v,vanadium]
Eno=23
Rest=[v,vanadium]
rfi-p> more
large-metal[cr,chronium]
Eno=24
Rest=[cr,chronium]
rfi-p> more
large-metal[mn,manganese]
Eno=25
Rest=[mn,manganese]
rfi-p> more
large-metal[fe,ferrum]
Eno=26
Rest=[fe,ferrum]
rfi-p> pause()
true
rfi-p> 
rfi-p> % the symbol-to-number and "new"-group functions:
rfi-p> 
rfi-p> listing symbno 
symbno(Symb) :- atm(Eno,name[Symb,_],_) & Eno.
rfi-p> pause()
true
rfi-p> listing newgroup-num
newgroup-num(dom[1,3,11]) :-& 1.
newgroup-num(2) :-& 18.
newgroup-num(dom[4,12]) :-& 2.
newgroup-num(bnd[Eno,dom[5,6,7,8,9,10]]) :- element(Eno) & +(Eno,8).
newgroup-num(Eno) :- 
         element(Eno), <=(13,Eno), <=(Eno,57) & +(1,-(Eno,1)18.).
newgroup-num(Eno) :- 
         element(Eno), <=(72,Eno), <=(Eno,89) & +(1,-(Eno,69)18.).
newgroup-num(Eno) :- element(Eno), <=(104,Eno), <=(Eno,107) & -(Eno,100).
rfi-p> pause()
true
rfi-p> listing newgroup-sym
newgroup-sym(Symb) :-& newgroup-num(symbno(Symb)).
rfi-p> pause()
true
rfi-p> 
rfi-p> % find out "new" group number for given element magnesium:
rfi-p> 
rfi-p> newgroup-sym(mg)                                 % for element symbol mg
2
rfi-p> 
rfi-p> listing englno
englno(Engl) :- atm(N,name[_,Engl],_) & N.
rfi-p> 
rfi-p> Eno is englno(magnesium), newgroup-num(Eno)      % for element number 12
2
Eno=12
rfi-p> pause()
true
rfi-p> 
rfi-p> % give an element of "new" group 2:
rfi-p> 
rfi-p> listing newgroup-elements   % inverts function newgroup-num, hence englno
newgroup-elements(G) :- G is newgroup-num(N), N is englno(E) & E.
rfi-p> 
rfi-p> newgroup-elements(2)
beryllium
rfi-p> more
magnesium
rfi-p> more
calcium
rfi-p> pause()
true
rfi-p> 
rfi-p> % map "new" to "old" groups:
rfi-p> 
rfi-p> listing new2old
new2old(1) :-& main[i].
new2old(2) :-& main[ii].
new2old(3) :-& auxiliary[iii].
new2old(4) :-& auxiliary[iv].
new2old(5) :-& auxiliary[v].
new2old(6) :-& auxiliary[vi].
new2old(7) :-& auxiliary[vii].
new2old(8) :-& auxiliary[viii].
new2old(9) :-& auxiliary[viii].
new2old(10) :-& auxiliary[viii].
new2old(11) :-& auxiliary[i].
new2old(12) :-& auxiliary[ii].
new2old(13) :-& main[iii].
new2old(14) :-& main[iv].
new2old(15) :-& main[v].
new2old(16) :-& main[vi].
new2old(17) :-& main[vii].
new2old(18) :-& main[viii].
rfi-p> 
rfi-p> new2old(2)
main[ii]
rfi-p> new2old(4)
auxiliary[iv]
rfi-p> pause()
true
rfi-p> 
rfi-p> % explore the main-auxiliary-main structure of the new groups:
rfi-p> 
rfi-p> tupof(Maux[Oldno] is new2old(Newno), new-maux[Newno, Maux])
[new-maux[1,main],
 new-maux[2,main],
 new-maux[3,auxiliary],
 new-maux[4,auxiliary],
 new-maux[5,auxiliary],
 new-maux[6,auxiliary],
 new-maux[7,auxiliary],
 new-maux[8,auxiliary],
 new-maux[9,auxiliary],
 new-maux[10,auxiliary],
 new-maux[11,auxiliary],
 new-maux[12,auxiliary],
 new-maux[13,main],
 new-maux[14,main],
 new-maux[15,main],
 new-maux[16,main],
 new-maux[17,main],
 new-maux[18,main]]
rfi-p> 
rfi-p> pause()
true
rfi-p> 
rfi-p> % define old2new as the inversion of the new2old group function:
rfi-p> 
rfi-p> az old2new(Old) :- Old is new2old(New) & New.
rfi-p> 
rfi-p> old2new(auxiliary[v])
5
rfi-p> more
unknown
rfi-p> 
rfi-p> old2new(auxiliary[viii])
8
rfi-p> more
9
rfi-p> more
10
rfi-p> more
unknown
rfi-p> 
rfi-p> pause()
true
rfi-p> 
rfi-p> % give an element of "old" main group ii:
rfi-p> 
rfi-p> listing oldgroup-sym
oldgroup-sym(Symb) :-& new2old(newgroup-num(symbno(Symb))).
oldgroup-sym(Symb) :- 
         Eno is symbno(Symb), >=(Eno,58), <=(Eno,71) & lanthanide.
oldgroup-sym(Symb) :- 
         Eno is symbno(Symb), >=(Eno,90), <=(Eno,103) & actinide.
rfi-p> 
rfi-p> main[ii] is oldgroup-sym(Symb), Symb   % inverts function oldgroup-sym
be
Symb=be
rfi-p> more
mg
Symb=mg
rfi-p> pause()
true
rfi-p> 
rfi-p> % enumerate max. numbers of electrons on all shells via free function call:
rfi-p> 
rfi-p> listing maxelec
maxelec(1) :-& 2.
maxelec(2) :-& 8.
maxelec(3) :-& 18.
maxelec(dom[4,5]) :-& 32.
rfi-p> 
rfi-p> maxelec(Sh)
2
Sh=1
rfi-p> more
8
Sh=2
rfi-p> more
18
Sh=3
rfi-p> more
32
Sh=bnd[Anon60159*1,dom[4,5]]
rfi-p> more
unknown
rfi-p> pause()
true
rfi-p> 
rfi-p> % the shell-filling functions for computing electron configurations:
rfi-p> 
rfi-p> listing inner-shells
inner-shells(Eno) :- <=(1,Eno), <=(Eno,18) & 0.
inner-shells(Eno) :- <=(19,Eno), <=(Eno,36) & 1.
inner-shells(Eno) :- <=(37,Eno), <=(Eno,54) & 2.
inner-shells(Eno) :- <=(55,Eno), <=(Eno,86) & 3.
inner-shells(Eno) :- <=(87,Eno), <=(Eno,last-atm()) & 4.
rfi-p> pause()
true
rfi-p> listing inner-shell-fill
inner-shell-fill(L,0) :-& L.
inner-shell-fill(L,S) :- 
         <(0,S) & inner-shell-fill(tup(maxelec(S)|L),-(S,1)).
rfi-p> pause()
true
rfi-p> listing eleconf-sym
eleconf-sym(Symb) :- 
         atm(Eno,name[Symb,_],prop[_,eco[L1,L2,L3],_,_]) &
         inner-shell-fill([L1,L2,L3],inner-shells(Eno)).
rfi-p> pause()
true
rfi-p> listing eleconf-num
eleconf-num(Eno) :- 
         atm(Eno,_,prop[_,eco[L1,L2,L3],_,_]) &
         inner-shell-fill([L1,L2,L3],inner-shells(Eno)).
rfi-p> pause()
true
rfi-p> 
rfi-p> % compute electron configurations of elements magnesium and unnilseptium:
rfi-p> 
rfi-p> eleconf-sym(mg)
[2,8,2]
rfi-p> 
rfi-p> tup(englno(unnilseptium), eleconf-num(englno(unnilseptium)))
[107,[2,8,18,32,32,13,2]]
rfi-p> pause()
true
rfi-p> 
rfi-p> % generate and filter the two versions of the periodic table:
rfi-p> 
rfi-p> listing select-all
select-all(_,_).
rfi-p> pause()
true
rfi-p> listing select-metals
select-metals(_,prop[m,_,_,_]).
rfi-p> pause()
true
rfi-p> listing select-light-metals
select-light-metals(Eno,prop[m,_,_,_]) :- light-metal(Eno).
rfi-p> pause()
true
rfi-p> 
rfi-p> listing newtable
newtable(Filter-p) :-& list-new(last-atm(),Filter-p,[]).
rfi-p> 
rfi-p> % switch to emulator mode:
rfi-p> 
rfi-p> emul 
Collecting modules for the emulator:
sortbase workspace 
rfe-p> 
rfe-p> % compile knowledge base:
rfe-p> 
rfe-p> compile 
rfe-p> 
rfe-p> % create "new" table of all elements:
rfe-p> newtable(select-all)
[group[1,[hydrogen,lithium,sodium,potassium,rubidium,caesium,francium]],
 group[2,[beryllium,magnesium,calcium,strontium,barium,radium]],
 group[3,[scandium,yttrium,lanthanum,actinium]],
 group[4,[titanium,zirconium,hafnium,unnilquadium]],
 group[5,[vanadium,niobium,tantalum,unnilpentium]],
 group[6,[chronium,molybdenum,tungsten,unnihexium]],
 group[7,[manganese,technetium,rhenium,unnilseptium]],
 group[8,[ferrum,ruthenium,osmium]],
 group[9,[cobalt,rhodium,iridium]],
 group[10,[nickel,palladium,platinum]],
 group[11,[copper,silver,gold]],
 group[12,[zinc,cadmium,mercury]],
 group[13,[boron,aluminium,galium,indium,thallium]],
 group[14,[carbon,silicon,germanium,tin,lead]],
 group[15,[nitrogen,phosphorus,arsenic,antimony,bismuth]],
 group[16,[oxygen,sulfur,selenium,tellurium,polonium]],
 group[17,[fluorine,chlorine,bromine,iodine,astatine]],
 group[18,[helium,neon,argon,krypton,xenon,radon]]]
rfe-p> 
rfe-p> % create "new" table of metals:
rfe-p> newtable(select-metals)
[group[1,[lithium,sodium,potassium,rubidium,caesium,francium]],
 group[2,[beryllium,magnesium,calcium,strontium,barium,radium]],
 group[3,[scandium,yttrium,lanthanum,actinium]],
 group[4,[titanium,zirconium,hafnium]],
 group[5,[vanadium,niobium,tantalum]],
 group[6,[chronium,molybdenum]],
 group[7,[manganese,technetium,rhenium]],
 group[8,[ferrum,ruthenium,osmium]],
 group[9,[cobalt,rhodium,iridium]],
 group[10,[nickel,palladium,platinum]],
 group[11,[copper,silver,gold]],
 group[12,[zinc,cadmium,mercury]],
 group[13,[aluminium,galium,indium,thallium]],
 group[14,[tin,lead]],
 group[16,[polonium]],
 group[17,[astatine]]]
rfe-p> 
rfe-p> % create "new" table of light metals:
rfe-p> newtable(select-light-metals)
[group[1,[lithium,sodium,potassium,rubidium,caesium,francium]],
 group[2,[beryllium,magnesium,calcium,strontium,barium]],
 group[3,[scandium,yttrium]],
 group[4,[titanium]],
 group[13,[aluminium]]]
rfe-p> 
rfe-p> inter
rfi-p> pause()
true
rfi-p> 
rfi-p> listing oldtable
oldtable(Filter-p) :-& list-old(last-atm(),Filter-p,[]).
rfi-p> 
rfi-p> emul
Collecting modules for the emulator:
sortbase workspace 
rfe-p> 
rfe-p> % create "old" view on all elements (their representation
rfe-p> % remains the same, only their aggregation is changing):
rfe-p> oldtable(select-all)
[group[main[i],
       [hydrogen,lithium,sodium,potassium,rubidium,caesium,francium]],
 group[auxiliary[i],[copper,silver,gold]],
 group[main[ii],[beryllium,magnesium,calcium,strontium,barium,radium]],
 group[auxiliary[ii],[zinc,cadmium,mercury]],
 group[main[iii],[boron,aluminium,galium,indium,thallium]],
 group[auxiliary[iii],[scandium,yttrium,lanthanum,actinium]],
 group[main[iv],[carbon,silicon,germanium,tin,lead]],
 group[auxiliary[iv],[titanium,zirconium,hafnium,unnilquadium]],
 group[main[v],[nitrogen,phosphorus,arsenic,antimony,bismuth]],
 group[auxiliary[v],[vanadium,niobium,tantalum,unnilpentium]],
 group[main[vi],[oxygen,sulfur,selenium,tellurium,polonium]],
 group[auxiliary[vi],[chronium,molybdenum,tungsten,unnihexium]],
 group[main[vii],[fluorine,chlorine,bromine,iodine,astatine]],
 group[auxiliary[vii],[manganese,technetium,rhenium,unnilseptium]],
 group[main[viii],[helium,neon,argon,krypton,xenon,radon]],
 group[auxiliary[viii],
       [ferrum,
        cobalt,
        nickel,
        ruthenium,
        rhodium,
        palladium,
        osmium,
        iridium,
        platinum]]]
rfe-p> 
rfe-p> % create "old" table of metals:
rfe-p> oldtable(select-metals)
[group[main[i],[lithium,sodium,potassium,rubidium,caesium,francium]],
 group[auxiliary[i],[copper,silver,gold]],
 group[main[ii],[beryllium,magnesium,calcium,strontium,barium,radium]],
 group[auxiliary[ii],[zinc,cadmium,mercury]],
 group[main[iii],[aluminium,galium,indium,thallium]],
 group[auxiliary[iii],[scandium,yttrium,lanthanum,actinium]],
 group[main[iv],[tin,lead]],
 group[auxiliary[iv],[titanium,zirconium,hafnium]],
 group[auxiliary[v],[vanadium,niobium,tantalum]],
 group[main[vi],[polonium]],
 group[auxiliary[vi],[chronium,molybdenum]],
 group[main[vii],[astatine]],
 group[auxiliary[vii],[manganese,technetium,rhenium]],
 group[auxiliary[viii],
       [ferrum,
        cobalt,
        nickel,
        ruthenium,
        rhodium,
        palladium,
        osmium,
        iridium,
        platinum]]]
rfe-p> 
rfe-p> % create "old" table of light metals:
rfe-p> oldtable(select-light-metals)
[group[main[i],[lithium,sodium,potassium,rubidium,caesium,francium]],
 group[main[ii],[beryllium,magnesium,calcium,strontium,barium]],
 group[main[iii],[aluminium]],
 group[auxiliary[iii],[scandium,yttrium]],
 group[auxiliary[iv],[titanium]]]
rfe-p> 
rfe-p> inter
rfi-p> pause()
true
rfi-p> emul
Collecting modules for the emulator:
sortbase workspace 
rfe-p> 
rfe-p> % in both views not all elements are included, lanthanides and actanides
rfe-p> % have extra representations:
rfe-p> 
rfe-p> tup(table-lan(), table-act())
[[cerium,
  praseodymium,
  nitrogen,
  promethium,
  samarium,
  europium,
  gadolinium,
  terbium,
  dysprosium,
  holmium,
  erbium,
  thullium,
  ytterbium,
  lutetium],
 [thorium,
  protoactinium,
  uranium,
  neptunium,
  plutonium,
  americium,
  curium,
  berkelium,
  californium,
  einsteinium,
  fermium,
  mendelevium,
  nobelium,
  lawrencium]]
rfe-p> 
rfe-p> inter
rfi-p> pause()
true
rfi-p> emul
Collecting modules for the emulator:
sortbase workspace 
rfe-p> 
rfe-p> % check knowledge base for inconsistencies in electron configuration,
rfe-p> % performing semantic validation (atomic = proton = electron  number):
rfe-p> 
rfe-p> check-all-eleconfs()
consistent[]
rfe-p> 
rfe-p> inter
rfi-p> 
rfi-p> listing check-all-eleconfs
check-all-eleconfs() :- element(Eno), corrupt-eleconf(Eno) & inconsistent[Eno].
check-all-eleconfs() :-& consistent[].
rfi-p> 
rfi-p> pause()
true
rfi-p> 
rfi-p> listing corrupt-eleconf
corrupt-eleconf(Eno) :- /=(Eno,sum(eleconf-num(Eno))).
rfi-p> 
rfi-p> % sample for failing the consistency-check:
rfi-p> 
rfi-p> % remove a fact from the knowledge base:
rfi-p> 
rfi-p> rx atm(92,
              name[u, uranium],
              prop[m,
                   eco[21, 9, 2],
                   238.0289,
                   oxi[3, 4, 5, 6]]).
rfi-p> 
rfi-p> % replace it with a 'wrong' fact
rfi-p> 
rfi-p> az atm(92,
              name[u, uranium],
              prop[m,
                   eco[21, 9, 3],             % 3 instead of 2
                   238.0289,
                   oxi[3, 4, 5, 6]]).
rfi-p> 
rfi-p> listing atm(92 | X)
sg(atm($integerp,
       name($symbolp,$symbolp),
       prop(dom(m,g,sm,sc,nyd),
            eco($integerp,$integerp,$integerp),
            $numberp,
            _)))
atm(92,name[u,uranium],prop[m,eco[21,9,3],238.0289,oxi[3,4,5,6]]).
rfi-p> 
rfi-p> emul 
Collecting modules for the emulator:
sortbase workspace 
rfe-p> compile 
rfe-p> 
rfe-p> check-all-eleconfs()
inconsistent[92]
rfe-p> 
rfe-p> more
consistent[]