home *** CD-ROM | disk | FTP | other *** search
/ Black Box 4 / BlackBox.cdr / proglang / pie2.arj / CHEM.PRO < prev    next >
Text File  |  2000-01-01  |  4KB  |  172 lines

  1. /* CHEM.PRO */
  2. /* Run demos by calling "start(1) */
  3. /* Molecular query predicates and examples */
  4. /* description of 3-chloro-toluene molecules:*/
  5.  
  6. atomic_weight(hydrogen,1.008).
  7. atomic_weight(carbon,12.011).
  8. atomic_weight(chlorine,35.45).
  9.  
  10. atom_specs(h1,hydrogen,[c1]).
  11. atom_specs(h2,hydrogen,[c3]).
  12. atom_specs(h3,hydrogen,[c3]).
  13. atom_specs(h4,hydrogen,[c5]).
  14. atom_specs(h5,hydrogen,[c3]).
  15. atom_specs(h6,hydrogen,[c6]).
  16. atom_specs(h7,hydrogen,[c7]).
  17. atom_specs(c1,carbon,[c2,c4,h1]).
  18. atom_specs(c2,carbon,[c1,c5,ch]).
  19. atom_specs(c3,carbon,[c4,h2,h3,h5]).
  20. atom_specs(c4,carbon,[c1,c3,c6]).
  21. atom_specs(c5,carbon,[c2,c7,h4]).
  22. atom_specs(c6,carbon,[c4,c7,h6]).
  23. atom_specs(c7,carbon,[c5,c6,h7]).
  24. atom_specs(ch,chlorine,[c2]).
  25.  
  26. /* Predicates to find bonds and identify elements */
  27.  
  28. bonded(A1,A2) :-
  29.      atom_specs(A1,_,Neighbors),
  30.      member1(A2,Neighbors).
  31.  
  32. member1(X,[X|_]).
  33. member1(X,[_|Y]) :- !,member1(X,Y).
  34.  
  35. element(A1, Element) :- atom_specs(A1, Element,_).
  36.  
  37. /* Predicates to identify particular substructures */
  38.  
  39. methyl(C) :-
  40.      element(C,carbon),
  41.      bonded(C,H1), element(H1,hydrogen),
  42.      bonded(C,H2), element(H2,hydrogen), H1\==H2,
  43.      bonded(C,H3), element(H3,hydrogen), H3\==H1,
  44.           H3\==H2.
  45. /*
  46. ethyl(C1,C2) :-
  47.     element(C1,carbon),
  48.     bonded(C1,H1), element(H1,hydrogen),
  49.     bonded(C1,H2), element(H2,hydrogen), H1\==H2,
  50.     bonded(C1,C2), element(C2,carbon),C1\==C2,
  51.     methyl(C2).
  52. */
  53. ethyl(C1,C2):-
  54.     methyl(C2),
  55.     bonded(C2,C1),element(C1,carbon),C1\==C2,
  56.     bonded(C1,H1), element(H1,hydrogen),
  57.     bonded(C1,H2), element(H2,hydrogen), H1\==H2.
  58.     
  59.  
  60.  
  61. six_membered_carbon_ring([A1,A2,A3,A4,A5,A6]) :-
  62.      element(A1,carbon), bonded(A1,A2),
  63.      element(A2,carbon), bonded(A2,A3), A1\==A3,
  64.      element(A3,carbon), bonded(A3,A4),
  65.           not member1(A4,[A1,A2,A3]),
  66.      element(A4,carbon), bonded(A4,A5),
  67.           not member1(A5,[A1,A2,A3,A4]),
  68.      element(A5,carbon), bonded(A5,A6),
  69.      element(A6,carbon), bonded(A6,A1),
  70.           not member1(A6,[A1,A2,A3,A4,A5]).
  71.  
  72. meth_carbon_ring([C|Ring]) :-
  73.      six_membered_carbon_ring(Ring),
  74.      member1(A,Ring), bonded(A,C), methyl(C).
  75.  
  76. hydroxide(O) :- element(O,oxygen),
  77.                     bonded(O,H),
  78.                  element(H,hydrogen).
  79.  
  80. chlorine(C):-
  81.     element(Ch,chlorine),
  82.     bonded(Ch,C).
  83.  
  84. calc_atomic_weight(_):-
  85.     initialize_atomic_weight,
  86.     atom_specs(_,Spec,_),
  87.     add_weight(Spec),
  88.     fail.
  89.     
  90. calc_atomic_weight(Weight):-
  91.     retract(weight(Weight)).
  92.     
  93. add_weight(Spec):-
  94.     retract(weight(W1)),
  95.     atomic_weight(Spec,Atom_weight),
  96.     W2 is W1 + Atom_weight,
  97.     assert(weight(W2)).
  98.     
  99. count_types(Spec,_):-
  100.     initialize_counter,
  101.     atom_specs(_,Spec,_),
  102.     count_atom,
  103.     fail.
  104.     
  105. count_types(_,Count):-
  106.     retract(count(Count)).
  107.     
  108. count_atom:-
  109.     retract(count(A)),
  110.     A1 is A + 1,
  111.     assert(count(A1)).
  112.  
  113. initialize_atomic_weight:-
  114.     retract(weight(_)),
  115.     fail.
  116.     
  117. initialize_atomic_weight:-
  118.     assert(weight(0)).
  119.  
  120. initialize_counter:-
  121.     retract(count(_)),
  122.     fail.
  123.     
  124. initialize_counter:-
  125.     assert(count(0)).
  126.  
  127. /* Demonstrations */
  128.  
  129. demo(1.0) :- write("Searching for a methyl group..."),nl,
  130.           methyl(X),
  131.           write("Found one centered on atom:"),write(X),nl.
  132.           
  133. demo(2.0) :- write("Searching for a six-membered carbon ring..."),nl,
  134.           six_membered_carbon_ring(List),
  135.           write("Found one containing atoms:"),write(List),nl.
  136.  
  137. demo(3.0) :- write("Searching for a Chlorine atom..."),nl,
  138.     chlorine(Ch),
  139.     write("Found one bonded to atom:"),write(Ch),nl.
  140.  
  141. demo(4.0) :- write("Calculating atomic weight..."),nl,
  142.     calc_atomic_weight(Weight),
  143.     write("The atomic weight is ",Weight),nl.
  144.     
  145. demo(5.0) :- write("Searching for a ethyl group..."),nl,
  146.           ethyl(X1,X2),
  147.           write("Found one consisting of atoms:"),write(X1," and ",X2),nl.
  148.           
  149. demo(6.0):-write("Counting the atoms..."),nl,
  150.     count_types(carbon,A1),  write("Carbon    = ",A1),nl,
  151.     count_types(hydrogen,A2),write("Hydrogen  = ",A2),nl,
  152.     count_types(chlorine,A3),write("Chlorine  = ",A3),nl.
  153.  
  154. start(A):-
  155.     demo(A),
  156.     nl,
  157.     A1 is A + 1,!,
  158.     start(A1).
  159.     
  160. start(A):-
  161.     A < 7,!,
  162.     write("Demo failed"),
  163.     nl,nl,
  164.     A1 is A + 1,
  165.     start(A1).
  166.     
  167. start(_).
  168.  
  169.     
  170.     
  171.     
  172.