home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / lang / prolog / 1450 < prev    next >
Encoding:
Internet Message Format  |  1992-07-26  |  2.8 KB

  1. Path: sparky!uunet!bonnie.concordia.ca!IRO.UMontreal.CA!kovic.IRO.UMontreal.CA.IRO.UMontreal.CA!tarau
  2. From: tarau@IRO.UMontreal.CA (Paul Tarau)
  3. Newsgroups: comp.lang.prolog
  4. Subject: Re: A simple logic problem.
  5. Message-ID: <1992Jul27.034607.19386@IRO.UMontreal.CA>
  6. Date: 27 Jul 92 03:46:07 GMT
  7. References: <tp847807.712050871@oak>
  8. Sender: news@IRO.UMontreal.CA
  9. Reply-To: tarau@IRO.UMontreal.CA (Paul Tarau)
  10. Organization: Universite de Montreal
  11. Lines: 93
  12.  
  13. /*
  14. MUSIC MEN
  15.  
  16. Three friends like different kinds of music.  From the clues given
  17. below, can you identify them, say how old each is, and work out
  18. his musical preference?
  19.  
  20. Clues: 
  21. 1.      Rob is older than Queen, who likes classical music.
  22. 2.      The pop-music fan, who is not Prince, is not 24.
  23. 3.      Leon, who is not King, is 25.
  24. 4.      Mark's musical preference is not jazz.
  25.  
  26. Knowledge: "this is what we know of the world."
  27. Names           : Leon, Mark, Rob.
  28. Surnames        : King, Prince, Queen.
  29. Ages            : 24, 25, 26.
  30. Music           : Classical, Jazz, Pop.
  31.  
  32. % solution
  33.  
  34. Leon Prince, 25, jazz.
  35. Mark Queen, 24, classical.
  36. Rob King, 26, pop.
  37. */
  38.  
  39. % Well, I simply cannot resist to so much music and royalty...
  40.  
  41. solve(D0):-
  42.     data(D0),
  43.  
  44.     % 1.      Rob is older than Queen, who likes classical music.
  45.     older(RAge,QAge),
  46.     pick(D0,D1,_,[queen,QAge,classic]),
  47.     pick(D1,_,rob,[_,RAge,_]),
  48.  
  49.     % 2.      The pop-music fan, who is not Prince, is not 24.
  50.     pick(D0,E1,_,[_,_,pop]),
  51.     pick(E1,E2,_,[prince,_,_]),
  52.     pick(E2,_,_,[_,24,_]),
  53.  
  54.     % 3.      Leon, who is not King, is 25.
  55.     pick(D0,F1,leon,[_,25,_]),
  56.     pick(F1,_,_,[king,_,_]),
  57.  
  58.     % 4.      Mark's musical preference is not jazz.
  59.     pick(D0,G1,mark,[_,_,_]),
  60.     pick(G1,_,_,[_,_,jazz]).
  61.  
  62. older(26,25).
  63. older(25,24).
  64. older(26,24).
  65.     
  66. data([    
  67.     [_-king,_-prince,_-queen],    % surnames
  68.     [_-24,_-25,_-26],        % ages
  69.     [_-classic,_-jazz,_-pop]    % musical preferences
  70. ]).
  71.  
  72. select(X,[X|Xs],Xs).
  73. select(X,[Y|Xs],[Y|Ys]):-select(X,Xs,Ys).
  74.  
  75. pick([],[],_,[]).
  76. pick([Xs|Xss],[Ys|Yss],Name,[A|As]):-
  77.     select(Name-A,Xs,Ys),
  78.     pick(Xss,Yss,Name,As).
  79.  
  80. go:-
  81.     statistics(runtime,_),
  82.     solve(X),
  83.     statistics(runtime,[_,T]),
  84.     write(X),nl,write(time=T),nl,fail
  85. ;    true.
  86.  
  87. %  ?-go.
  88.  
  89. /*
  90. This is a pure Prolog solution, to make it a little bit
  91. more interesting. The method is useful for other puzzles on
  92. small finite domains (as usually given by humans to humans).
  93. I am wondering if someone tried to compile formulas with
  94. negation or constraints to something that looks like "solve/1".
  95. I hope it is also fine food for hungry partial evaluators.
  96. As is, it takes exactly the same time in emulated Sicstus 2.1
  97. and in BinProlog (140ms on a Sparc IPC). I would be curious to know 
  98. how much it takes on faster Prologs and how fast it can be
  99. made by partial evaluation. Do constraint logic programming
  100. languages perform significantly better on this kind of problem
  101. than a partially evaluated Prolog solution?
  102.  
  103. Paul Tarau
  104. tarau@info.umoncton.ca
  105. */
  106.