home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / CLIPPER / MISC / AIE8907.ZIP / RDBMS.CDE < prev    next >
Encoding:
Text File  |  1988-05-04  |  6.5 KB  |  219 lines

  1. ------------------------------------------
  2. BOX 1
  3.  
  4. PROMOTION TABLE (RDBMS)
  5.  
  6. CareerField     Selects  Eligible  Cycle
  7. avionics          66       326     fy83a5
  8. personnel        229      1397     fy85a6
  9. linguist          39       123     fy84a7
  10.  
  11. PROMOTION TABLE (PROLOG)
  12.  
  13. waps(avionics,66,326,fy83a5).
  14. waps(personnel,229,1397,fy85a6).
  15. waps(linguist,39,123,fy84a7).
  16.  
  17. PROMOTION RATE TABLE
  18. Cycle           RATE
  19. fy83a5           .17
  20. fy84a7           .28
  21. fy85a6           .18
  22.  
  23. PROMOTION RATE TABLE (PROLOG)
  24. p_rate(fy83a5,0.17).
  25. p_rate(fy84a7,0.28).
  26. p_rate(fy85a6,0.18).
  27. ------------------------------------------
  28. BOX 2
  29.  
  30. To identify those career fields which may
  31. have a promotion advantage:
  32.  
  33. advantage(CareerField) :-
  34.   waps(CareerField,Selects,Eligible,_,Cycle),
  35.   p_rate(Cycle,Rate),
  36.   Selects / Eligible >= Rate+0.05.
  37.  
  38. To identify career fields which may have
  39. abnormal selection rates due to the small
  40. size of the career field:
  41.  
  42. small_cell(CareerField) :-
  43.   waps(CareerField,Selects,Eligible,_,Cycle),
  44.   Eligible < 100,
  45.   p_rate(Cycle,Rate),
  46.   SelectRate = Selects / Eligible,
  47.   not(Rate-0.005 <= SelectRate <= Rate+0.005).
  48. ------------------------------------------
  49. BOX 3
  50. relation_1
  51.  
  52. SSAN  RANK        CareerField
  53. 3     lieutenant  pilot
  54. 1     captain     mathematician
  55. 2     major       pilot
  56.  
  57. relation_2
  58.  
  59. SSAN  RANK        CareerField
  60. 2     major       pilot
  61. 3     lieutenant  pilot
  62. 1     captain     mathematician
  63.  
  64. relation_3
  65.  
  66. SSAN  RANK        CareerField
  67. 3     lieutenant  pilot
  68. 4     colonel     commander
  69.  
  70. relation_4
  71.  
  72. SSAN  RANK        CareerField
  73. 4     colonel     commander
  74. 3     lieutenant  pilot
  75.  
  76. relation_5
  77.  
  78. SSAN  CareerField     RANK
  79. 3     pilot           lieutenant
  80. 1     mathematician   captain
  81. 2     pilot           major
  82.  
  83. ----------------------------------------------
  84. BOX 4
  85.         A Simple Display Loop
  86.  
  87.      % Display the instances of relation r
  88. display :-
  89.     r(X, Y, Z),
  90.     display_hlpr(X, Y, Z),
  91.     fail.
  92. display.
  93.  
  94. Here's how this
  95. loop works.  Each time Prolog can find an
  96. <I>X<R>, <I>Y<R> and <I>Z<R> which
  97. satisfy <I>r<R>, it does so.  Prolog
  98. passes these values to display_hlpr,
  99. which writes them to the screen.  Then
  100. the <I>fail<R> causes Prolog to
  101. BACKTACK.  It goes back to the last
  102. point where a choice was made -- namely
  103. <I>r(X, Y, Z)<R>, where the values of
  104. the variables were chosen.  Prolog looks
  105. for another choice for these variables
  106. in the as yet unsearched part of its
  107. database.  If it finds one, it repeats
  108. the display and failure in another cycle
  109. through the loop.  If it finds no more
  110. values satisfying <I>r<R>, it
  111. abandonsthe first <I>display<R> clause
  112. and goes on to the second clause, namely
  113. <I>display.<R> This clause does not
  114. display anything, but as a clause stored
  115. in Prolog's database of
  116. assumed-to-be-true logical statements,
  117. <I>display.<R> is logically true.  This
  118. causes <I>display<R> as a Prolog goal
  119. (Prolog procedure call or statement to
  120. proved) to be satisfied.  This
  121. satisfaction, essentially a boolean
  122. value for <I>display<R> of TRUE, is
  123. returned to the caller of the predicate
  124. (Prolog procedure) or to the user if the
  125. user directly asked for <I>display<R>.
  126.  
  127. ---------------------------------------------------------
  128. Box 5
  129. Example of Quotient in Prolog
  130.  
  131.      % r6 / r7
  132. r6_div_r7( Name) :-
  133.      % find a name in r6
  134.    r6( Name, _),
  135.      % make sure it appears with all possible r7's
  136.    r6_div_r7_hlpr( Name).
  137.  
  138.      % succeed if Name can be extended in all r7
  139.      % ways so result is in r6
  140. r6_div_r7_hlpr( Name ) :-
  141.      % get possible values of Expertise
  142.    r7( Expertise ),
  143.      % if some Name -- Expertise pair is not in r6
  144.    ( not r6( Name, Expertise),
  145.      % then fail and quit
  146.      !, fail
  147.      % else fail to get the next Expertise to try
  148.     ; fail).
  149.      % succeed when all Expertise have been tried
  150. r6_div_r7_hlpr( _) .
  151.  
  152. --------------------------------------
  153. Box 6
  154. Synthesizing Quotient Rules
  155.  
  156.            % rule for quotient
  157. define_goal(  Query,      %%% user query
  158.                Goal) :-   %%% name and arity of synthesized rule
  159.            %%% is Query a quotient?
  160.       functor( Query, '/', 2),    !,
  161.            %%% if so, get its arguments
  162.       arg( 1, Query, R1),       !,
  163.       arg( 2, Query, R2),       !,
  164.            %%% define subgoals
  165.       define_goal( R1, Goal1),
  166.       define_goal( R2, Goal2),
  167.            %%% and get their names and arities
  168.       get_name_and_arity( Goal1, Name1, Arity1),
  169.       get_name_and_arity( Goal2, Name2, Arity2),
  170.            %%% create a name for the quotient predicate
  171.       build_name( Name1, Name2, '/', Quotient_name),
  172.            %%%  get arity of quotient
  173.       Quotient_arity is Arity1 - Arity2,
  174.            %%%  make sure it's positive
  175.       ( Quotient_arity > 0
  176.         ;
  177.         write($ Quotient can not be defined.$), nl, !, fail
  178.       ),
  179.            %%% generate call to quotient
  180.       functor( Quotient_call, Quotient_name, Quotient_arity),
  181.            %%% get call to denominator
  182.       functor( Denominator_call, Name2, Arity2 ),
  183.            %%% get args of Denominator
  184.       Denominator_call =.. [ _ | Denominator_args],
  185.            %%% get args of quotient
  186.       Quotient_call =.. [ _ | Quotient_args],
  187.            %%% get args to Numerator
  188.       append( Quotient_args, Denominator_args, Numerator_args),
  189.            %%% build call to numerator
  190.       Numerator_call =.. [ Name1 | Numerator_args],
  191.            %%% get name of quotient helper
  192.       concat( Quotient_name, $_hlpr$, S_hlpr_name),
  193.       atom_string( Hlpr_name, S_hlpr_name),
  194.            %%% build call to helper predicate
  195.       Hlpr_call =..[ Hlpr_name | Quotient_args],
  196.            %%% build main helper rule
  197.       Hlpr_rule_1 =
  198.           ( Hlpr_call :- Denominator_call,
  199.                          ( not Numerator_call, !, fail;
  200.                            fail)),
  201.            %%% build loop terminator helper rule
  202.       Hlpr_rule_2 = Hlpr_call,
  203.            %%%  remove old clauses with same name as helper
  204.       retractall( Hlpr_call),
  205.            %%%  define the helper predicate
  206.       assertz(Hlpr_rule_1),
  207.       assertz(Hlpr_rule_2),
  208.            %%%  define quotient rule
  209.       Quotient_rule = ( Quotient_call :- Numerator_call, Hlpr_call),
  210.            %%% remove all old predicates of this name
  211.       retractall( Quotient_call),
  212.            %%% define the quotient predicate
  213.       assertz(Quotient_rule),
  214.            %%% set output to name and arity of this predicate
  215.       Goal =  Quotient_name / Quotient_arity.
  216.  
  217. --------------------------------------------------
  218.  
  219. ri