home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Interactive Guide / c-cplusplus-interactive-guide.iso / c_ref / csource4 / 297_01 / exampl3.spr < prev    next >
Encoding:
Text File  |  1979-12-31  |  2.6 KB  |  96 lines

  1. /* exampl3.spr */
  2. /* Exploring combinations of relations and rules.
  3.    Rules are more complicated than facts. They are prolog's 
  4.    equivalent of procedures.
  5.    A rule has a head and some conditions.
  6.    In Small Prolog a rule is a list whose first element is the head
  7.    and the following elements are its conditions.
  8.  
  9.    You can ask a question if it matches the head of a rule or 
  10.    a fact or if it's a builtin. Otherwise its garanteed to "fail".
  11.    Of course you can also group several questions into a list of 
  12.    questions.
  13.    Matching the head of a rule is more sophisticated
  14.    than matching a fact, because rule heads generally contain
  15.    variables.
  16.    Once the first goal of the query and the head of the rule
  17.    have matched (ie unified) the goals of the rule are added
  18.    to the list of goals to solve.
  19.    All variables in rules are local so there is never any 
  20.    conflict.
  21.  
  22.    Once you know prolog well it is fun trying to encode high school
  23.    science. But it takes considerable maturity and one does notice
  24.    that prolog can be awkward.
  25.  */
  26.  
  27. (density sea_water 1.O3)
  28. (density milk 1.O3)
  29. (density olive_oil 0.92)
  30. (density petroleum 0.80)
  31.  
  32. (density almond_wood 0.99)
  33. (density apricot_wood 0.89)
  34. (density apple_wood 0.88)
  35. (density walnut_wood 0.8)
  36. (density maple 0.7)
  37. (density poplar 0.45)
  38. (density balsa 0.12)
  39. (density iridium 22.64)
  40. (density lead 11.34)
  41. (density plutonium 19.7)
  42. (density copper 8.94)
  43. (density steel 7.8)
  44. (density mercury 13.6)
  45. (density gold 19.3)
  46. (density diamond 3.51)
  47. (density glass 2.5)
  48. (density sugar 1.6)
  49. (density fat 0.94)
  50.  
  51. (solid almond_wood)
  52. (solid apricot_wood)
  53. (solid apple_wood)
  54. (solid walnut_wood)
  55. (solid maple)
  56. (solid poplar)
  57. (solid balsa)
  58. (solid iridium)
  59. (solid lead)
  60. (solid plutonium)
  61. (solid copper)
  62. (solid steel)
  63. (solid gold)
  64. (solid diamond)
  65. (solid glass) /* at least, to the layman. In fact it's a viscous liquid */
  66. (solid sugar)
  67. (solid fat)
  68.  
  69. (liquid sea_water)
  70. (liquid pure_water)
  71. (liquid olive_oil)
  72. (liquid petroleum)
  73. (liquid milk)
  74.  
  75. (water_soluble water)
  76. (water_soluble sea_water)
  77. (water_soluble sugar)
  78. (water_soluble milk)
  79.  
  80. /* this is a rule */
  81. /* It has an informal declarative interpretation as follows:
  82.   A thing can float on water if its density is less than 1.0 .
  83.   Now try the query 
  84.   (can_float_on_water X).
  85.  */
  86. ((can_float_on_water Thing )/* head */
  87.  /* if the following are true */
  88.      (density Thing Density)
  89.       (rless Density 1.0)
  90. )
  91.  
  92. /* actually you would probably add an extra condition :
  93.    (not (water_soluble Thing))
  94.   but that's jumping ahead a little.
  95.  */
  96.