home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 5 / DATAFILE_PDCD5.iso / utilities / b / binprolog / !BinPro330 / progs / puzzle < prev    next >
Encoding:
Text File  |  1993-04-30  |  2.6 KB  |  103 lines

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