% Generic puzzle solver % ---- Try possibillities against given facts and assert those that fit % Each component can only occur once in a fact solution(X):- possibility(X), unknown(X), givens(X), assertz(fact(X)). % --- the following avoids a warning ---- :-style_check(-singleton). % --- the following avoids an exception ---- :-dynamic(fact/1). % normally X will be a structure with components representing different % facets of the entities involved. % --- two utilities: if-then-, and if-and-only-if if(P,Q):- P->Q;true. iff(P,Q):-(P,Q); not( P ), not( Q ). % Test case % A Sample Logic Problem. [ Original logic problems, July 1990, p 5] % Angela, David and Mae are the young stars in a talent show. % Their ages are 5,7, and 8. One has last name Starr. % Miss Grant is three years older than Angela. % The child whose last name is Diamond is seven years old % first(angela). first(david). first(mae). male(david). female(angela). female(mae). age(5). age(7). age(8). last(diamond). last(grant). last(starr). possibility(child(First,Last,Age)):-first(First), last(Last), age(Age). givens(child(First,Last,Age)):- if(male(First), Last\=grant), if(Last=grant, female(First)), iff(Last=diamond,Age=7), iff(Last=grant,Age=8), iff(First=angela,Age=5). unknown(child(First,Last,Age)):- not( fact(child(First,_,_))), not( fact(child(_,Last,_))), not( fact(child(_,_,Age))). go:-solution(X), fail; listing(fact). :- write('Puzzle loaded'), nl,nl. :- write('Input time(go). to start solving it... and see how fast it is.'), nl,nl.