home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / lifeos2.zip / LIFE-1.02 / EXAMPLES / MACHINE.LF < prev    next >
Text File  |  1996-06-04  |  4KB  |  261 lines

  1. % Copyright 1992 Digital Equipment Corporation
  2. % All Rights Reserved
  3.  
  4. % Execute the query 'machine?' to run this program.
  5. % This program emulates the snack machine at PRL.
  6. % The program is in French.
  7.  
  8. module("machine") ?
  9. public( machine,gini,perrier,oasis,coca,coca_light,cake,lion,bounty,
  10.         raider,kit_kat,rien) ?
  11.  
  12. :: boisson(prix => 5).
  13.  
  14. gini <| boisson.
  15. perrier <| boisson.
  16. oasis <| boisson.
  17. coca <| boisson.
  18. coca_light <| boisson.
  19.  
  20. cake <| snack.
  21. lion <| snack.
  22. bounty <| snack.
  23. raider <| snack.
  24. kit_kat <| snack.
  25.  
  26. snack <| a_vendre.
  27. boisson <| a_vendre.
  28. rien <| a_vendre.
  29.  
  30. :: cake(prix => 3).
  31. :: lion(prix => 3).
  32. :: bounty(prix => 3.5).
  33. :: raider(prix => 3.5).
  34. :: kit_kat(prix => 4).
  35.  
  36.  
  37. :: piece.
  38.  
  39. piece <| objet.
  40. boisson <| objet.
  41. snack <| objet.
  42.  
  43. :: objet(quantite => N:int, prix => real).
  44.  
  45. global(arret_machine) ?
  46.  
  47. dynamic(machine_contient)?
  48.  
  49. machine_contient(
  50.     [
  51.     cake(quantite => 3),
  52.     lion(quantite => 7),
  53.     bounty(quantite => 2),
  54.     raider(quantite => 3),
  55.     kit_kat(quantite => 7),
  56.     gini(quantite => 2),
  57.     perrier(quantite => 9),
  58.     oasis(quantite => 2),
  59.     coca(quantite => 1),
  60.     coca_light(quantite => 1),
  61.     piece(prix => 0.5,quantite => 1),
  62.     piece(prix => 1,quantite => 3),
  63.     piece(prix => 2,quantite => 2),
  64.     piece(prix => 5,quantite => 2),
  65.     piece(prix => 10,quantite => 1)
  66.     ]).
  67.  
  68.  
  69. machine :-
  70.     (
  71.         arret_machine = get_choice,
  72.         machine2
  73.     ;
  74.         succeed
  75.     ).
  76.  
  77. machine2 :- 
  78.     write('Distributeur de Snacks et Boissons de DEC PRL.'),
  79.     nl,
  80.     nl,
  81.     machine_contient(Contenu:list),
  82.     write('La machine contient:'),
  83.     nl,
  84.     nl,
  85.     affiche(Contenu),
  86.     nl,
  87.     lire(Commande),
  88.     write('Vous avez commande un ',
  89.           root_sort(Commande:a_vendre(prix => P))),
  90.     ajouter(root_sort(Commande) & @(prix => P,quantite => -1)),
  91.     nl,
  92.     nl,
  93.     write('Vous devez payer ',P,'F.'),
  94.     nl,
  95.     nl,
  96.     payer(P,Commande),
  97.     nl,
  98.     write('-----------------------------------------------------'),
  99.     nl,
  100.     fail.
  101.  
  102. machine2 :- machine2.
  103.  
  104.  
  105. lire(C:a_vendre(quantite => 1)) :-
  106.     machine_contient(Choses),
  107.     repeter,
  108.     (
  109.             write('Que voulez vous acheter ? '),
  110.         read_halt(C),
  111.         nl,
  112.         (
  113.             C :== rien,
  114.             set_choice(arret_machine),
  115.             fail
  116.         ;
  117.             dans(C,Choses),
  118.             !
  119.         )
  120.     ;
  121.         write('Il n''y en a pas a vendre...'),
  122.         nl,
  123.         fail
  124.     ).
  125.  
  126.  
  127. payer(N:real,C) :-
  128.     N<0,
  129.     !,
  130.     (
  131.         trouver_pieces(-N,L),
  132.         !,
  133.         write('La machine rend:'),
  134.         rendre_pieces(L)
  135.     ;
  136.         write('Il n''y a pas assez de monnaie...'),
  137.         nl,
  138.         rendre_le_plus(-N-0.5)
  139.     ),
  140.     nl,
  141.     write('Prenez votre ',root_sort(C),' dans le distributeur SVP.'),
  142.     nl,
  143.     nl,
  144.     ! .
  145.  
  146. rendre_le_plus(N) :-
  147.     N=<0,
  148.     write('La machine ne vous rend RIEN !!!'),
  149.     nl,
  150.     ! .
  151.  
  152. rendre_le_plus(N) :-
  153.     trouver_pieces(N,L),
  154.     !,
  155.     write('La machine ne peut rendre que ',N,
  156.           'F au mieux, soit:'),
  157.     rendre_pieces(L),
  158.     ! .
  159.  
  160. rendre_le_plus(N) :-
  161.     rendre_le_plus(N-0.5).    
  162.  
  163.  
  164. payer(0,C) :- 
  165.     write('Le compte est bon.'),
  166.     nl,
  167.     write('Veuillez prendre votre ',
  168.           root_sort(C),' dans le distributeur...'),
  169.     nl,
  170.     nl,
  171.     ! .
  172.  
  173. payer(P,C) :-
  174.     write('Donnez une piece de F ? '),
  175.     read_halt(N:real),
  176.     N={0.5;1;2;5;10},
  177.     !,
  178.     ajouter(piece(prix => N,quantite => 1)),
  179.     payer(P-N,C).
  180.  
  181. payer(P,C) :-
  182.     write('Inacceptable.'),
  183.     nl,
  184.     write('Recommencez.'),
  185.     nl,
  186.     payer(P,C).
  187.  
  188.  
  189. ajouter(C) :-
  190.     machine_contient(Choses),
  191.     retract(machine_contient),
  192.     mettre(C,Choses,Contenu),
  193.     assert(machine_contient(Contenu)).
  194.  
  195.  
  196. mettre( C:@(prix => P,quantite => R),
  197.     [A:@(prix => P,quantite => Q)|Reste],
  198.     [B:@(prix => P,quantite => Q+R)|Reste]) :- 
  199.     A=root_sort(C),
  200.     B=root_sort(C),
  201.     !.
  202.  
  203. mettre(C,[A|B],[A|B2]) :- mettre(C,B,B2).
  204.  
  205.  
  206. trouver_pieces(N,somme_egale(piece_dans(Choses),N)) :- 
  207.     machine_contient(Choses).
  208.  
  209.  
  210. piece_dans([]) -> [].
  211. piece_dans([piece(prix => P,quantite => Q)|B]) ->
  212.     apres(piece_dans(B),Q,P).
  213. piece_dans([A|B]) -> piece_dans(B).
  214.  
  215. apres(C,0,A) -> C.
  216. apres(C,N,P) -> apres([P|C],N-1,P).
  217.  
  218.     
  219.     
  220. somme_egale(list,0) -> [].
  221. somme_egale([],N) -> {}.
  222. somme_egale([A|B],N) -> {[A|somme_egale(B,N-A)];somme_egale(B,N)}.
  223.  
  224.  
  225.  
  226. rendre_pieces([]) :- nl.
  227. rendre_pieces([N|Reste]) :-
  228.     ajouter(piece(prix => N,quantite => -1)),
  229.     write(' ',N,'F'),
  230.     rendre_pieces(Reste).
  231.     
  232.  
  233.  
  234. affiche([]) :- ! .
  235. affiche([A:@(quantite => 0)|B]) :-
  236.     !,
  237.     affiche(B).
  238.  
  239. affiche([A|B]) :-
  240.     write('       ',A),
  241.     nl,
  242.     affiche(B).
  243.  
  244. dans(A,[ B:@(quantite => N:int) | @]) :-
  245.     N>0,
  246.     A=root_sort(B).
  247.  
  248. dans(A,[B|C]) :- dans(A,C).
  249.  
  250. repeter.
  251. repeter :- 
  252.     write('Recommencez...'),
  253.     nl,
  254.     repeter.
  255.  
  256. read_halt(C) :-
  257.     read_token(D),
  258.     end_check(D, C).
  259.  
  260. end_check(end_of_file, _) :- nl,set_choice(arret_machine),fail.
  261. end_check(C,C).