home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / CLIPPER / MISC / AIE8906.ZIP / CODE.PRO < prev    next >
Encoding:
Text File  |  1988-05-04  |  4.8 KB  |  133 lines

  1. BOX 4ì
  2. ááááááááA Simple Display Loop
  3.  
  4. ááááá% Display the instances of relation rì
  5. display :-ì
  6. áááár(X, Y, Z),ì
  7. áááádisplay_hlpr(X, Y, Z),ì
  8. ááááfail.ì
  9. display.
  10.  
  11. Here's how thisì
  12. loop works.  Each time Prolog can find anì
  13. <I>X<R>, <I>Y<R> and <I>Z<R> whichì
  14. satisfy <I>r<R>, it does so.  Prologì
  15. passes these values to display_hlpr,ìèwhich writes them to the screen.  Thenì
  16. the <I>fail<R> causes Prolog toì
  17. BACKTACK.  It goes back to the lastì
  18. point where a choice was made -- namelyì
  19. <I>r(X, Y, Z)<R>, where the values ofì
  20. the variables were chosen.  Prolog looksì
  21. for another choice for these variablesì
  22. in the as yet unsearched part of itsì
  23. database.  If it finds one, it repeatsì
  24. the display and failure in another cycleì
  25. through the loop.  If it finds no moreì
  26. values satisfying <I>r<R>, itì
  27. abandonsthe first <I>display<R> clauseì
  28. and goes on to the second clause, namelyì
  29. <I>display.<R> This clause does notì
  30. display anything, but as a clause storedì
  31. in Prolog's database ofì
  32. assumed-to-be-true logical statements,ì
  33. <I>display.<R> is logically true.  Thisì
  34. causes <I>display<R> as a Prolog goalì
  35. (Prolog procedure call or statement toì
  36. proved) to be satisfied.  Thisì
  37. satisfaction, essentially a booleanì
  38. value for <I>display<R> of TRUE, isì
  39. returned to the caller of the predicateì
  40. (Prolog procedure) or to the user if theì
  41. user directly asked for <I>display<R>.
  42.  
  43. ---------------------------------------------------------ì
  44. Box 5ì
  45. Example of Quotient in Prolog
  46.  
  47. ááááá% r6 / r7ì
  48. r6_div_r7( Name) :-ì
  49. ááááá% find a name in r6ì
  50. ááár6( Name, _),ì
  51. ááááá% make sure it appears with all possible r7'sì
  52. ááár6_div_r7_hlpr( Name).
  53.  
  54. ááááá% succeed if Name can be extended in all r7ì
  55. ááááá% ways so result is in r6ì
  56. r6_div_r7_hlpr( Name ) :-ì
  57. ááááá% get possible values of Expertiseì
  58. ááár7( Expertise ),ì
  59. ááááá% if some Name -- Expertise pair is not in r6ì
  60. ááá( not r6( Name, Expertise),ì
  61. ááááá% then fail and quitì
  62. ááááá!, failì
  63. ááááá% else fail to get the next Expertise to tryì
  64. áááá; fail).ì
  65. ááááá% succeed when all Expertise have been triedì
  66. r6_div_r7_hlpr( _) .
  67.  
  68. --------------------------------------ì
  69. Box 6ìèSynthesizing Quotient Rules
  70.  
  71. ááááááááááá% rule for quotientì
  72. define_goal(  Query,      %%% user queryì
  73. áááááááááááááááGoal) :-   %%% name and arity of synthesized ruleì
  74. ááááááááááá%%% is Query a quotient?ì
  75. ááááááfunctor( Query, '/', 2),    !,ì
  76. ááááááááááá%%% if so, get its argumentsì
  77. ááááááarg( 1, Query, R1),       !,ì
  78. ááááááarg( 2, Query, R2),       !,ì
  79. ááááááááááá%%% define subgoalsì
  80. áááááádefine_goal( R1, Goal1),ì
  81. áááááádefine_goal( R2, Goal2),ì
  82. ááááááááááá%%% and get their names and aritiesì
  83. ááááááget_name_and_arity( Goal1, Name1, Arity1),ì
  84. ááááááget_name_and_arity( Goal2, Name2, Arity2),ì
  85. ááááááááááá%%% create a name for the quotient predicateì
  86. áááááábuild_name( Name1, Name2, '/', Quotient_name),ì
  87. ááááááááááá%%%  get arity of quotientì
  88. ááááááQuotient_arity is Arity1 - Arity2,ì
  89. ááááááááááá%%%  make sure it's positiveì
  90. áááááá( Quotient_arity > 0ì
  91. áááááááá;ì
  92. ááááááááwrite($ Quotient can not be defined.$), nl, !, failì
  93. áááááá),ì
  94. ááááááááááá%%% generate call to quotientì
  95. ááááááfunctor( Quotient_call, Quotient_name, Quotient_arity),ì
  96. ááááááááááá%%% get call to denominatorì
  97. ááááááfunctor( Denominator_call, Name2, Arity2 ),ì
  98. ááááááááááá%%% get args of Denominatorì
  99. ááááááDenominator_call =.. [ _ | Denominator_args],ì
  100. ááááááááááá%%% get args of quotientì
  101. ááááááQuotient_call =.. [ _ | Quotient_args],ì
  102. ááááááááááá%%% get args to Numeratorì
  103. ááááááappend( Quotient_args, Denominator_args, Numerator_args),ì
  104. ááááááááááá%%% build call to numeratorì
  105. ááááááNumerator_call =.. [ Name1 | Numerator_args],ì
  106. ááááááááááá%%% get name of quotient helperì
  107. ááááááconcat( Quotient_name, $_hlpr$, S_hlpr_name),ì
  108. ááááááatom_string( Hlpr_name, S_hlpr_name),ì
  109. ááááááááááá%%% build call to helper predicateì
  110. ááááááHlpr_call =..[ Hlpr_name | Quotient_args],ì
  111. ááááááááááá%%% build main helper ruleì
  112. ááááááHlpr_rule_1 =ì
  113. áááááááááá( Hlpr_call :- Denominator_call,ì
  114. ááááááááááááááááááááááááá( not Numerator_call, !, fail;ì
  115. áááááááááááááááááááááááááááfail)),ì
  116. ááááááááááá%%% build loop terminator helper ruleì
  117. ááááááHlpr_rule_2 = Hlpr_call,ì
  118. ááááááááááá%%%  remove old clauses with same name as helperì
  119. ááááááretractall( Hlpr_call),ì
  120. ááááááááááá%%%  define the helper predicateì
  121. ááááááassertz(Hlpr_rule_1),ì
  122. ááááááassertz(Hlpr_rule_2),ì
  123. ááááááááááá%%%  define quotient ruleìèááááááQuotient_rule = ( Quotient_call :- Numerator_call, Hlpr_call),ì
  124. ááááááááááá%%% remove all old predicates of this nameì
  125. ááááááretractall( Quotient_call),ì
  126. ááááááááááá%%% define the quotient predicateì
  127. ááááááassertz(Quotient_rule),ì
  128. ááááááááááá%%% set output to name and arity of this predicateì
  129. ááááááGoal =  Quotient_name / Quotient_arity.
  130.  
  131. --------------------------------------------------
  132.  
  133.