home *** CD-ROM | disk | FTP | other *** search
- BOX 4ì
- ááááááááA Simple Display Loop
-
- ááááá% Display the instances of relation rì
- display :-ì
- áááár(X, Y, Z),ì
- áááádisplay_hlpr(X, Y, Z),ì
- ááááfail.ì
- display.
-
- Here's how thisì
- loop works. Each time Prolog can find anì
- <I>X<R>, <I>Y<R> and <I>Z<R> whichì
- satisfy <I>r<R>, it does so. Prologì
- passes these values to display_hlpr,ìèwhich writes them to the screen. Thenì
- the <I>fail<R> causes Prolog toì
- BACKTACK. It goes back to the lastì
- point where a choice was made -- namelyì
- <I>r(X, Y, Z)<R>, where the values ofì
- the variables were chosen. Prolog looksì
- for another choice for these variablesì
- in the as yet unsearched part of itsì
- database. If it finds one, it repeatsì
- the display and failure in another cycleì
- through the loop. If it finds no moreì
- values satisfying <I>r<R>, itì
- abandonsthe first <I>display<R> clauseì
- and goes on to the second clause, namelyì
- <I>display.<R> This clause does notì
- display anything, but as a clause storedì
- in Prolog's database ofì
- assumed-to-be-true logical statements,ì
- <I>display.<R> is logically true. Thisì
- causes <I>display<R> as a Prolog goalì
- (Prolog procedure call or statement toì
- proved) to be satisfied. Thisì
- satisfaction, essentially a booleanì
- value for <I>display<R> of TRUE, isì
- returned to the caller of the predicateì
- (Prolog procedure) or to the user if theì
- user directly asked for <I>display<R>.
-
- ---------------------------------------------------------ì
- Box 5ì
- Example of Quotient in Prolog
-
- ááááá% r6 / r7ì
- r6_div_r7( Name) :-ì
- ááááá% find a name in r6ì
- ááár6( Name, _),ì
- ááááá% make sure it appears with all possible r7'sì
- ááár6_div_r7_hlpr( Name).
-
- ááááá% succeed if Name can be extended in all r7ì
- ááááá% ways so result is in r6ì
- r6_div_r7_hlpr( Name ) :-ì
- ááááá% get possible values of Expertiseì
- ááár7( Expertise ),ì
- ááááá% if some Name -- Expertise pair is not in r6ì
- ááá( not r6( Name, Expertise),ì
- ááááá% then fail and quitì
- ááááá!, failì
- ááááá% else fail to get the next Expertise to tryì
- áááá; fail).ì
- ááááá% succeed when all Expertise have been triedì
- r6_div_r7_hlpr( _) .
-
- --------------------------------------ì
- Box 6ìèSynthesizing Quotient Rules
-
- ááááááááááá% rule for quotientì
- define_goal( Query, %%% user queryì
- áááááááááááááááGoal) :- %%% name and arity of synthesized ruleì
- ááááááááááá%%% is Query a quotient?ì
- ááááááfunctor( Query, '/', 2), !,ì
- ááááááááááá%%% if so, get its argumentsì
- ááááááarg( 1, Query, R1), !,ì
- ááááááarg( 2, Query, R2), !,ì
- ááááááááááá%%% define subgoalsì
- áááááádefine_goal( R1, Goal1),ì
- áááááádefine_goal( R2, Goal2),ì
- ááááááááááá%%% and get their names and aritiesì
- ááááááget_name_and_arity( Goal1, Name1, Arity1),ì
- ááááááget_name_and_arity( Goal2, Name2, Arity2),ì
- ááááááááááá%%% create a name for the quotient predicateì
- áááááábuild_name( Name1, Name2, '/', Quotient_name),ì
- ááááááááááá%%% get arity of quotientì
- ááááááQuotient_arity is Arity1 - Arity2,ì
- ááááááááááá%%% make sure it's positiveì
- áááááá( Quotient_arity > 0ì
- áááááááá;ì
- ááááááááwrite($ Quotient can not be defined.$), nl, !, failì
- áááááá),ì
- ááááááááááá%%% generate call to quotientì
- ááááááfunctor( Quotient_call, Quotient_name, Quotient_arity),ì
- ááááááááááá%%% get call to denominatorì
- ááááááfunctor( Denominator_call, Name2, Arity2 ),ì
- ááááááááááá%%% get args of Denominatorì
- ááááááDenominator_call =.. [ _ | Denominator_args],ì
- ááááááááááá%%% get args of quotientì
- ááááááQuotient_call =.. [ _ | Quotient_args],ì
- ááááááááááá%%% get args to Numeratorì
- ááááááappend( Quotient_args, Denominator_args, Numerator_args),ì
- ááááááááááá%%% build call to numeratorì
- ááááááNumerator_call =.. [ Name1 | Numerator_args],ì
- ááááááááááá%%% get name of quotient helperì
- ááááááconcat( Quotient_name, $_hlpr$, S_hlpr_name),ì
- ááááááatom_string( Hlpr_name, S_hlpr_name),ì
- ááááááááááá%%% build call to helper predicateì
- ááááááHlpr_call =..[ Hlpr_name | Quotient_args],ì
- ááááááááááá%%% build main helper ruleì
- ááááááHlpr_rule_1 =ì
- áááááááááá( Hlpr_call :- Denominator_call,ì
- ááááááááááááááááááááááááá( not Numerator_call, !, fail;ì
- áááááááááááááááááááááááááááfail)),ì
- ááááááááááá%%% build loop terminator helper ruleì
- ááááááHlpr_rule_2 = Hlpr_call,ì
- ááááááááááá%%% remove old clauses with same name as helperì
- ááááááretractall( Hlpr_call),ì
- ááááááááááá%%% define the helper predicateì
- ááááááassertz(Hlpr_rule_1),ì
- ááááááassertz(Hlpr_rule_2),ì
- ááááááááááá%%% define quotient ruleìèááááááQuotient_rule = ( Quotient_call :- Numerator_call, Hlpr_call),ì
- ááááááááááá%%% remove all old predicates of this nameì
- ááááááretractall( Quotient_call),ì
- ááááááááááá%%% define the quotient predicateì
- ááááááassertz(Quotient_rule),ì
- ááááááááááá%%% set output to name and arity of this predicateì
- ááááááGoal = Quotient_name / Quotient_arity.
-
- --------------------------------------------------
-