% Generic puzzle solver % ---- Try possibillities against given facts and assert those that fit % Each component can only occur once in a fact solution(X):- clear_facts, possibility(X), new_attributes(X), givens(X), assertz(fact(X)). clear_facts:-abolish(fact,1), assert( fact(dummy_entity) ). % --- the following avoids an exception ---- :-dynamic(fact/1). :-style_check(-singleton). go:-solution(X), fail; listing(fact). % normally X will be a structure with components representing different % facets or attributes 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 ). :- write('Input time(go). to solving puzzle... and see how fast it is.'), nl,nl. % 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). new_attributes(child(First,Last,Age)):- not( fact(child(First,_,_)) ), not( fact(child(_,Last,_)) ), not( fact(child(_,_,Age)) ). :- write('Puzzle loaded'), nl,nl.