home *** CD-ROM | disk | FTP | other *** search
Prolog Source | 1987-02-26 | 5.4 KB | 108 lines |
- /*
- USING TURBO PROLOG TO SOLVE LOGIC PROBLEMS -- AN ALTERNATIVE
- BY: RICHARD S. NOWAKOWSKI
-
- This is an alternative solution to Garry Vass's "logic problem". For a
- statement of the problem, its clues, an excellent analysis and Vass's solution
- see the accompanying file SOLVLOG.PRO by Garry Vass.
-
- My approach to this type of problem is to use a simple, list structure and
- to let Prolog's unification and bactracking mechanisms find the solution. The
- three key predicates, i.e., member, append and permute, are all standard C&M
- fare. To run this program, give the goal "final" during interactive mode.
- Then, the start time, the solution, and the end time will be displayed. On my 8
- Mhz AT compatible this takes 0.11 seconds to find and print the solution,
- whereas Vass's version takes about 17 seconds.
-
- I am a newcomer to Prolog, and there is no doubt that my solution could be
- improved and/or generalized. Leave a message for me with your comments on the
- SSNJ BBS (201-729-7410) or on the Turbo Prolog SIG on the POLICE STATION BBS
- (201-963-3115). */
-
-
- DOMAINS
- list = symbol* /* lists for names, items */
- intlist = integer* /* list for costs */
- set = set (symbol, symbol, symbol, integer) /* data structure for each purchase */
- setlist = set* /* list for solutions */
-
- PREDICATES
- member (set, setlist) /* determine membership in a list */
-
- append (list, list, list) /* append two lists */
- append (intlist, intlist, intlist)
-
- permute (list, list) /* rearranges a list */
- permute (intlist, intlist)
-
- fnamelist (list) /* use lists for facts */
- lnamelist (list)
- itemlist (list)
- costlist (intlist)
-
- isvalidsolution (setlist) /* generates valid solutions */
- resolution (setlist) /* resolution of facts with clues */
- final /* timing loop and printing control */
-
- CLAUSES
- member (X, [X|_]). /* standard list processing predicate */
- member (X, [_|Tail]) :- member (X, Tail).
-
- append ([], L, L). /* standard list processing predicate */
- append ([X|L1], L2, [X|L3]) :- append (L1, L2, L3).
-
- permute ([], []). /* standard list processing predicate */
- permute (L, [H|T]) :- append(V,[H|U],L),
- append(V,U,W),
- permute(W,T).
-
- fnamelist ([garry, john, ron]). /* facts for each data type */
- lnamelist ([vass, ross, clark]).
- itemlist ([modem, monitor, keyboard]).
- costlist ([150, 200, 250]).
-
- isvalidsolution ([set (FName1, LName1, Item1, Cost1), /* Each valid solution is a list */
- set (FName2, LName2, Item2, Cost2), /* with three members. */
- set(FName3, LName3, Item3, Cost3)]) :-
- fnamelist ([FName1, FName2, FName3]), /* Order of first names is fixed. */
- lnamelist (LNamelist), /* Last names can be in any order.*/
- permute (LNamelist, [LName1, LName2, LName3]),
- itemlist (Itemlist), /* Items can be in any order. */
- permute (Itemlist, [Item1, Item2, Item3]),
- costlist (Costlist), /* Costs can be in any order. */
- permute (Costlist, [Cost1, Cost2, Cost3]).
-
- resolution (Solution) :- isvalidsolution (Solution), /* Solution must be valid. */
- member (set (_,_,monitor,150), Solution), /* Clue 1: monitor is cheapest. */
- member (set (ron,_,Item4ron,_), Solution), /* Clue 2: Ron did not buy */
- Item4ron<>"modem", /* the modem. */
- member (set (garry, _, _, Cost4garry), Solution), /* Clue 3: Gary spent less */
- member (set (_, ross, Item4ross, Cost4ross), Solution), /* than Ross, and Ross */
- Cost4garry < Cost4ross, /* did not buy the */
- Item4ross <> "keyboard", /* the keyboard. */
- member (set (_, clark, _, 250), Solution). /* Clue 4: Clark paid $250. */
-
- final:- time (Starthrs, Startmins, Startsecs,Starthunds),nl, /* Begin timing */
- write ("Beginning at ",Starthrs,":",Startmins,":",
- Startsecs,":",Starthunds), nl,
- resolution ([set (FName1, LName1, Item1, Cost1), /* Find solution */
- set (FName2, LName2, Item2, Cost2),
- set(FName3, LName3, Item3, Cost3)]),
- write (FName1," ",LName1," purchased a ", /* Write solution */
- Item1," for $",Cost1), nl,
- write (FName2," ",LName2," purchased a ",
- Item2," for $",Cost2), nl,
- write (FName3," ",LName3," purchased a ",
- Item3," for $",Cost3), nl,
- time (Endhrs,Endmins,Endsecs,Endhunds), /* End timing */
- write ("Ending at ",Endhrs,":",Endmins,":",
- Endsecs,":",Endhunds), nl.
-
-
- /*
- goal
- final.
- */
-
- /* remove comment marks around goal for autorun or to compile to EXE file */
-