Article View: comp.lang.prolog
Article #112Re: A simple logic problem.
From: Paul Tarau
Date: Mon, 27 Jul 1992 03:46
Date: Mon, 27 Jul 1992 03:46
93 lines
3010 bytes
3010 bytes
/* MUSIC MEN Three friends like different kinds of music. From the clues given below, can you identify them, say how old each is, and work out his musical preference? Clues: 1. Rob is older than Queen, who likes classical music. 2. The pop-music fan, who is not Prince, is not 24. 3. Leon, who is not King, is 25. 4. Mark's musical preference is not jazz. Knowledge: "this is what we know of the world." Names : Leon, Mark, Rob. Surnames : King, Prince, Queen. Ages : 24, 25, 26. Music : Classical, Jazz, Pop. % solution Leon Prince, 25, jazz. Mark Queen, 24, classical. Rob King, 26, pop. */ % Well, I simply cannot resist to so much music and royalty... solve(D0):- data(D0), % 1. Rob is older than Queen, who likes classical music. older(RAge,QAge), pick(D0,D1,_,[queen,QAge,classic]), pick(D1,_,rob,[_,RAge,_]), % 2. The pop-music fan, who is not Prince, is not 24. pick(D0,E1,_,[_,_,pop]), pick(E1,E2,_,[prince,_,_]), pick(E2,_,_,[_,24,_]), % 3. Leon, who is not King, is 25. pick(D0,F1,leon,[_,25,_]), pick(F1,_,_,[king,_,_]), % 4. Mark's musical preference is not jazz. pick(D0,G1,mark,[_,_,_]), pick(G1,_,_,[_,_,jazz]). older(26,25). older(25,24). older(26,24). data([ [_-king,_-prince,_-queen], % surnames [_-24,_-25,_-26], % ages [_-classic,_-jazz,_-pop] % musical preferences ]). select(X,[X|Xs],Xs). select(X,[Y|Xs],[Y|Ys]):-select(X,Xs,Ys). pick([],[],_,[]). pick([Xs|Xss],[Ys|Yss],Name,[A|As]):- select(Name-A,Xs,Ys), pick(Xss,Yss,Name,As). go:- statistics(runtime,_), solve(X), statistics(runtime,[_,T]), write(X),nl,write(time=T),nl,fail ; true. % ?-go. /* This is a pure Prolog solution, to make it a little bit more interesting. The method is useful for other puzzles on small finite domains (as usually given by humans to humans). I am wondering if someone tried to compile formulas with negation or constraints to something that looks like "solve/1". I hope it is also fine food for hungry partial evaluators. As is, it takes exactly the same time in emulated Sicstus 2.1 and in BinProlog (140ms on a Sparc IPC). I would be curious to know how much it takes on faster Prologs and how fast it can be made by partial evaluation. Do constraint logic programming languages perform significantly better on this kind of problem than a partially evaluated Prolog solution? Paul Tarau tarau@info.umoncton.ca */
Technical Details
Message-ID:
<1992Jul27.034607.19386@IRO.UMontreal.CA>Path:
arc1-ca.pugleaf.net!sparky!uunet!bonnie.concordia.ca!IRO.UMontreal.CA!kovic.IRO.UMontreal.CA.IRO.UMontreal.CA!tarauReferences:
<tp847807.712050871@oak>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!bonnie.concordia.ca!IRO.UMontreal.CA!kovic.IRO.UMontreal.CA.IRO.UMontreal.CA!tarau From: tarau@IRO.UMontreal.CA (Paul Tarau) Newsgroups: comp.lang.prolog Subject: Re: A simple logic problem. Message-ID: <1992Jul27.034607.19386@IRO.UMontreal.CA> Date: 27 Jul 92 03:46:07 GMT References: <tp847807.712050871@oak> Sender: news@IRO.UMontreal.CA Reply-To: tarau@IRO.UMontreal.CA (Paul Tarau) Organization: Universite de Montreal Lines: 93