/* ***************************************************************** * * * SB-Prolog * * Copyright SUNY at Stony Brook, 1986 * * * ****************************************************************** */ /*----------------------------------------------------------------- SB-Prolog is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY. No author or distributor accepts responsibility to anyone for the consequences of using it or for whether it serves any particular purpose or works at all, unless he says so in writing. Refer to the SB-Prolog General Public License for full details. Everyone is granted permission to copy, modify and redistribute SB-Prolog, but only under the conditions described in the SB-Prolog General Public License. A copy of this license is supposed to have been given to you along with SB-Prolog so you can know your rights and responsibilities. It should be in a file named COPYING. Among other things, the copyright notice and this notice must be preserved on all copies. ------------------------------------------------------------------ */ /* translcuts1.P contains routines for source-to-source transformation of clauses during preprocessing. */ /* ********************************************************************** $translcuts1_export([$transl_cuts/5,$transl_softcuts/5,$find_prag/4]). $translcuts1_use($meta,[$functor/3,$univ/2,$length/2]). $translcuts1_use($aux1,[_,_,_,_,$umsg/1,_,_,$gensym_pred/2,_]). $translcuts1_use($blist,[$append/3,$member/2,_]). $translcuts1_use($tindex1,[$tindex/2]). $translcuts1_use($cond1,[$cond/5,_,_,$inline_neg/2]). ********************************************************************** */ $transl_cuts(P,N,Clauses,PragList,[PredDef1 | PredDefRest]) :- $functor(Head,P,N), N1 is N + 1, $gensym_pred(P,NP), $univ(Head,[_|Args]), $append(Args,[B],NArgs), $univ(NLit,[NP|NArgs]), $transl_cut_clauses(NP,Clauses,NewCls), $find_prag(P,N,PragList,Prag), PredDef1 = pred(P,N,1,1,[rule(Head,('_$savecp'(B),NLit),0,0)],Prag), PredDef2 = pred(NP,N1,1,1,NewCls,Prag), $tindex(PredDef2,PredDefRest). $transl_softcuts(P,N,VarCall,InterpVarCall,Call) :- var(VarCall), !, ((Call =:= 0, $translcuts_naked_var_msg(P,N)) ; (Call =\= 0) ), InterpVarCall = ('_$savecp'(X), '_$interp'(VarCall,X)). $transl_softcuts(P,N,','(A,B), ','(NA,NB),Call) :- !, $transl_softcuts(P,N,A,NA,Call), $transl_softcuts(P,N,B,NB,Call). $transl_softcuts(P,N,'->'(A,B),NB,Call) :- !, $transl_cuts_ifthenelse(P,N,A,B,fail,NB,Call). $transl_softcuts(P,N,';'('->'(A,B),C),NB,Call) :- $transl_cuts_ifthenelse(P,N,A,B,C,NB,Call). $transl_softcuts(P,N,';'(A,B),if_then_else(Test,TP,FP),Call) :- $cond(A,B,Test,TP0,FP0), !, $transl_softcuts(P,N,TP0,TP,Call), $transl_softcuts(P,N,FP0,FP,Call). $transl_softcuts(P,N,';'(A,B), ';'(NA,NB),Call) :- !, $transl_softcuts(P,N,A,NA,Call), $transl_softcuts(P,N,B,NB,Call). $transl_softcuts(P,N,not(Goal), NewGoal,Call) :- !, ( ($inline_neg(Goal, NewGoal)) ; (NewGoal = ('_$savecp'(B),((NegGoal,'_$cutto'(B),fail) ; true)), $transl_softcuts(P,N,Goal, NegGoal,Call) ) ). $transl_softcuts(P,N,call(Goal),NewGoal,_) :- !, $transl_softcuts(P,N,Goal,NewGoal,1). $transl_softcuts(P,N,Goal,Goal,_). $transl_cut_clauses(_,[],[]). $transl_cut_clauses(P,[C1|CRest],[NC1|NCRest]) :- $transl_cut_clauses1(P,C1,NC1), $transl_cut_clauses(P,CRest,NCRest). $transl_cut_clauses1(P,fact(Fact,CFlag),fact(NewFact,CFlag)) :- $univ(Fact,[P1|Args]), $append(Args,[_],NArgs), $univ(NewFact,[P|NArgs]). $transl_cut_clauses1(P,rule(H,B,CFlag0,CFlag1),rule(NH,NB,CFlag0,CFlag1)) :- $univ(H,[P1|Args]), $append(Args,[V],NArgs), $univ(NH,[P|NArgs]), $transl_hardcuts(P,N,V,B,NB,0). $transl_hardcuts(P,N,V,VarCall,InterpVarCall,Call) :- var(VarCall), !, ((Call =:= 0, $translcuts_naked_var_msg(P,N)) ; (Call =\= 0) ), InterpVarCall = ('_$savecp'(X), '_$interp'(VarCall,X)). $transl_hardcuts(P,N,V,','(A1,B1),','(A2,B2),Call) :- !, $transl_hardcuts(P,N,V,A1,A2,Call), $transl_hardcuts(P,N,V,B1,B2,Call). $transl_hardcuts(P,N,V,'->'(A,B),NB,Call) :- $transl_cuts_ifthenelse(P,N,A,B,fail,NB,Call). $transl_hardcuts(P,N,V,';'('->'(A,B),C),NB,Call) :- $transl_cuts_ifthenelse(P,N,A,B,C,NB,Call). $transl_hardcuts(P,N,V,';'(A1,B1),if_then_else(Test,TP,FP),Call) :- $cond(A1,B1,Test,TP0,FP0), !, $transl_hardcuts(P,N,V,TP0,TP,Call), $transl_hardcuts(P,N,V,FP0,FP,Call). $transl_hardcuts(P,N,V,';'(A1,B1),';'(A2,B2),Call) :- !, $transl_hardcuts(P,N,V,A1,A2,Call), $transl_hardcuts(P,N,V,B1,B2,Call). $transl_hardcuts(P,N,V,not(Goal),NewGoal,Call) :- !, ( ($inline_neg(Goal, NewGoal)) ; (NewGoal = ('_$savecp'(B),((NegGoal,'_$cutto'(B),fail) ; true)), $transl_hardcuts(P,N,B,Goal, NegGoal,Call) ) ). $transl_hardcuts(P,N,V,'!','_$cutto'(V),_) :- !. $transl_hardcuts(P,N,V,call(Goal),NewGoal,_) :- !, $transl_hardcuts(P,N,V,Goal,NewGoal,1). $transl_hardcuts(_,_,_,L,L,_). $transl_contains_cut(X) :- var(X), !, fail. $transl_contains_cut(','(A,B)) :- $transl_contains_cut(A) ; $transl_contains_cut(B). $transl_contains_cut(';'(A,B)) :- $transl_contains_cut(A) ; $transl_contains_cut(B). $transl_contains_cut('->'(A,B)) :- $transl_contains_cut(A) ; $transl_contains_cut(B). $transl_contains_cut(not(C)) :- $transl_contains_cut(C). $transl_contains_cut(call(C)) :- $transl_contains_cut(C). $transl_contains_cut('!'). $translcuts_naked_var_msg(P,N) :- $umsg(['*** Warning: naked variable being called in',P,'/',N,'***']). $translcuts_inline_ifthenelse(P,N,A,B,C,NewBody,Call) :- (($transl_contains_cut(B), !, NewBody = ','('_$savecp'(X),if_then_else(NA,NB0,NC)), $transl_hardcuts(P,N,X,A,NA,Call), $transl_hardcuts(P,N,X,B,NB0,Call), $transl_hardcuts(P,N,X,C,NC,Call) ) ; (NewBody = if_then_else(NA,NB0,NC), $transl_softcuts(P,N,A,NA,Call), $transl_softcuts(P,N,B,NB0,Call), $transl_softcuts(P,N,C,NC,Call) ) ). $transl_cuts_ifthenelse(P,N,A,B,C,NB,Call) :- $transl_all_inlines(A), $translcuts_inline_ifthenelse(P,N,A,B,C,NB,Call). $transl_cuts_ifthenelse(P,N,A,B,C,NB,Call) :- NB = ','('_$savecp'(X),';'((T1,'_$cutto'(X),NB0),NC)), $transl_hardcuts(P,N,X,A,T1,Call), $transl_hardcuts(P,N,X,B,NB0,Call), $transl_hardcuts(P,N,X,C,NC,Call). /* at this point, a test is considered to be inline (and hence a candidate for transformation to "if-then-else" iff it is either an inline test, or a conjunction of inline tests, or a disjunction of inline tests. We could set it up so that arbitrary constructs involving inlines are allowed, but this complicates code generation quite a bit, and it's not clear that more complex constructs are encountered frequently in practice. */ $transl_all_inlines(','(G1,G2)) :- !, $transl_all_inlines_conj(G1), $transl_all_inlines_conj(G2). $transl_all_inlines(';'(G1,G2)) :- !, $transl_all_inlines_disj(G1), $transl_all_inlines_disj(G2). $transl_all_inlines(A) :- $functor(A,Pred,Arity), $inline_test(Pred,Arity). $transl_all_inlines_conj(','(G1,G2)) :- !, $transl_all_inlines_conj(G1), $transl_all_inlines_conj(G2). $transl_all_inlines_conj(A) :- $functor(A,Pred,Arity), $inline_test(Pred,Arity). $transl_all_inlines_disj(';'(G1,G2)) :- !, $transl_all_inlines_disj(G1), $transl_all_inlines_disj(G2). $transl_all_inlines_disj(A) :- $functor(A,Pred,Arity), $inline_test(Pred,Arity). $find_prag(P,N,PragList,Prag) :- $member(prag(P,N,Prag),PragList), !. $find_prag(_,_,_,[]). $inline_test('>',2). $inline_test('>=',2). $inline_test('=<',2). $inline_test('<',2). $inline_test('=:=',2). $inline_test('=\=',2). $inline_test(var,1). $inline_test(nonvar,1). $inline_test(true,0). /* ---------------------------- $translcuts1.P ---------------------------- */