home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Black Box 4
/
BlackBox.cdr
/
proglang
/
pie2.arj
/
CHEM.PRO
< prev
next >
Wrap
Text File
|
2000-01-01
|
4KB
|
172 lines
/* CHEM.PRO */
/* Run demos by calling "start(1) */
/* Molecular query predicates and examples */
/* description of 3-chloro-toluene molecules:*/
atomic_weight(hydrogen,1.008).
atomic_weight(carbon,12.011).
atomic_weight(chlorine,35.45).
atom_specs(h1,hydrogen,[c1]).
atom_specs(h2,hydrogen,[c3]).
atom_specs(h3,hydrogen,[c3]).
atom_specs(h4,hydrogen,[c5]).
atom_specs(h5,hydrogen,[c3]).
atom_specs(h6,hydrogen,[c6]).
atom_specs(h7,hydrogen,[c7]).
atom_specs(c1,carbon,[c2,c4,h1]).
atom_specs(c2,carbon,[c1,c5,ch]).
atom_specs(c3,carbon,[c4,h2,h3,h5]).
atom_specs(c4,carbon,[c1,c3,c6]).
atom_specs(c5,carbon,[c2,c7,h4]).
atom_specs(c6,carbon,[c4,c7,h6]).
atom_specs(c7,carbon,[c5,c6,h7]).
atom_specs(ch,chlorine,[c2]).
/* Predicates to find bonds and identify elements */
bonded(A1,A2) :-
atom_specs(A1,_,Neighbors),
member1(A2,Neighbors).
member1(X,[X|_]).
member1(X,[_|Y]) :- !,member1(X,Y).
element(A1, Element) :- atom_specs(A1, Element,_).
/* Predicates to identify particular substructures */
methyl(C) :-
element(C,carbon),
bonded(C,H1), element(H1,hydrogen),
bonded(C,H2), element(H2,hydrogen), H1\==H2,
bonded(C,H3), element(H3,hydrogen), H3\==H1,
H3\==H2.
/*
ethyl(C1,C2) :-
element(C1,carbon),
bonded(C1,H1), element(H1,hydrogen),
bonded(C1,H2), element(H2,hydrogen), H1\==H2,
bonded(C1,C2), element(C2,carbon),C1\==C2,
methyl(C2).
*/
ethyl(C1,C2):-
methyl(C2),
bonded(C2,C1),element(C1,carbon),C1\==C2,
bonded(C1,H1), element(H1,hydrogen),
bonded(C1,H2), element(H2,hydrogen), H1\==H2.
six_membered_carbon_ring([A1,A2,A3,A4,A5,A6]) :-
element(A1,carbon), bonded(A1,A2),
element(A2,carbon), bonded(A2,A3), A1\==A3,
element(A3,carbon), bonded(A3,A4),
not member1(A4,[A1,A2,A3]),
element(A4,carbon), bonded(A4,A5),
not member1(A5,[A1,A2,A3,A4]),
element(A5,carbon), bonded(A5,A6),
element(A6,carbon), bonded(A6,A1),
not member1(A6,[A1,A2,A3,A4,A5]).
meth_carbon_ring([C|Ring]) :-
six_membered_carbon_ring(Ring),
member1(A,Ring), bonded(A,C), methyl(C).
hydroxide(O) :- element(O,oxygen),
bonded(O,H),
element(H,hydrogen).
chlorine(C):-
element(Ch,chlorine),
bonded(Ch,C).
calc_atomic_weight(_):-
initialize_atomic_weight,
atom_specs(_,Spec,_),
add_weight(Spec),
fail.
calc_atomic_weight(Weight):-
retract(weight(Weight)).
add_weight(Spec):-
retract(weight(W1)),
atomic_weight(Spec,Atom_weight),
W2 is W1 + Atom_weight,
assert(weight(W2)).
count_types(Spec,_):-
initialize_counter,
atom_specs(_,Spec,_),
count_atom,
fail.
count_types(_,Count):-
retract(count(Count)).
count_atom:-
retract(count(A)),
A1 is A + 1,
assert(count(A1)).
initialize_atomic_weight:-
retract(weight(_)),
fail.
initialize_atomic_weight:-
assert(weight(0)).
initialize_counter:-
retract(count(_)),
fail.
initialize_counter:-
assert(count(0)).
/* Demonstrations */
demo(1.0) :- write("Searching for a methyl group..."),nl,
methyl(X),
write("Found one centered on atom:"),write(X),nl.
demo(2.0) :- write("Searching for a six-membered carbon ring..."),nl,
six_membered_carbon_ring(List),
write("Found one containing atoms:"),write(List),nl.
demo(3.0) :- write("Searching for a Chlorine atom..."),nl,
chlorine(Ch),
write("Found one bonded to atom:"),write(Ch),nl.
demo(4.0) :- write("Calculating atomic weight..."),nl,
calc_atomic_weight(Weight),
write("The atomic weight is ",Weight),nl.
demo(5.0) :- write("Searching for a ethyl group..."),nl,
ethyl(X1,X2),
write("Found one consisting of atoms:"),write(X1," and ",X2),nl.
demo(6.0):-write("Counting the atoms..."),nl,
count_types(carbon,A1), write("Carbon = ",A1),nl,
count_types(hydrogen,A2),write("Hydrogen = ",A2),nl,
count_types(chlorine,A3),write("Chlorine = ",A3),nl.
start(A):-
demo(A),
nl,
A1 is A + 1,!,
start(A1).
start(A):-
A < 7,!,
write("Demo failed"),
nl,nl,
A1 is A + 1,
start(A1).
start(_).