home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / EFFO / pd3.lzh / SBPROLOG2.2 / CMPLIB / CMPLIB_SRC / $convrhs1.P < prev    next >
Text File  |  1991-08-10  |  6KB  |  139 lines

  1. /* *****************************************************************
  2. *                                   *
  3. *    SB-Prolog                           *
  4. *    Copyright SUNY at Stony Brook, 1986               *
  5. *                                   *
  6. ****************************************************************** */
  7.  
  8. /*-----------------------------------------------------------------
  9. SB-Prolog is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY.  No author or distributor
  11. accepts responsibility to anyone for the consequences of using it
  12. or for whether it serves any particular purpose or works at all,
  13. unless he says so in writing.  Refer to the SB-Prolog General Public
  14. License for full details.
  15.  
  16. Everyone is granted permission to copy, modify and redistribute
  17. SB-Prolog, but only under the conditions described in the
  18. SB-Prolog General Public License.   A copy of this license is
  19. supposed to have been given to you along with SB-Prolog so you
  20. can know your rights and responsibilities.  It should be in a
  21. file named COPYING.  Among other things, the copyright notice
  22. and this notice must be preserved on all copies. 
  23. ------------------------------------------------------------------ */
  24.  
  25.  
  26. /* ---------------------------- $convrhs1.P ---------------------------- */
  27.  
  28. /* **********************************************************************
  29. $convrhs1_export([$convrhs/12]).
  30.  
  31. $convrhs1_use($meta,[_,$univ/2,$length/2]).
  32. $convrhs_use($aux1,[_,_,_,_,_,_,_,_,$logical_or/3]).
  33. $convrhs_use($computil1,[_,_,_,_,$max/3,_,_,_,_,$length1/2,
  34.                 $prefix_list/3,_,_,_,_,_,_,_,_,_]).
  35. $convrhs1_use($inline1,[$inline/2]).
  36. $convrhs1_use($blist,[_,_,$member1/2]).
  37. $convrhs1_use($getclauses,[_,_,$attach/2]).
  38. $convrhs1_use($targlist1,[_,$targlist/10,$targlist_is/8]).
  39. ********************************************************************** */
  40.  
  41.  
  42. /*  The last 3 parameters of convrhs are as follows: NFlag is set to 1
  43.     if there is need for normalization in the clause, i.e. when execution
  44.     paths fork.  AFlag is set to 1 if it is clear that an "allocate" instr
  45.     must be generated, e.g. when a disjunction is present.  LFlag is 1 if
  46.     the subtree convrhs is processing contains a last literal of the clause,
  47.     0 if it does not (this is necessary to be able to handle variables
  48.     which occur only in the last literals of the clause, in different
  49.     disjunctive branches -- we have to be able to recognize them as temps.  */
  50.  
  51. $convrhs((Goal,Goals),and(Rgoal,_,Rgoals),VidTbl,
  52.         Pathin,Pathout,St,Litin,Litout,BindList,NFlag,AFlag,LFlag) :-
  53.     !,
  54.     $convrhs(Goal,Rgoal,VidTbl,
  55.         Pathin,Pathmid,St,Litin,Litmid,BindList,NFlag0,AFlag0,0),
  56.     $convrhs(Goals,Rgoals,VidTbl,
  57.         Pathmid,Pathout,St,Litmid,Litout,BindList,NFlag1,AFlag1,LFlag),
  58.     $logical_or(NFlag0,NFlag1,NFlag),
  59.     $logical_or(AFlag0,AFlag1,AFlag).
  60.  
  61. $convrhs(';'(Goal,Goals),or(Rgoal,_,Rgoals),VidTbl,
  62.             Pathin,Pathout,St,Litin,Litout,BindList,1,1,LFlag) :-
  63.     !,
  64.     $length1(BindList,BLen),
  65.     $convrhs(Goal,Rgoal,VidTbl,
  66.                 Pathin,Pathmid,St,Litin,Litout1,BindList,_,_,LFlag),
  67.     Pathmid1 is Pathmid + 1, $prefix_list(BindList,BLen,BindList1),
  68.     $convrhs(Goals,Rgoals,VidTbl,
  69.             Pathmid1,Pathout,St,Litin,Litout2,BindList1,_,_,LFlag),
  70.     $max(Litout1, Litout2, Litout).
  71.  
  72. $convrhs(if_then_else(Test,Goal1,Goal2),if_then_else(RTest,_,RGoal1,RGoal2),
  73.         VidTbl,Pin,Pout,St,Lin,Lout,BList,1,AFlag,LFlag) :-
  74.     !,
  75.     $convrhs_test(Test,RTest,VidTbl,Pin,St,Lin,Lmid0,BList),
  76.     $length1(BList,BLen),
  77.     $convrhs(Goal1,RGoal1,VidTbl,
  78.             Pin,Pmid1,St,Lmid0,Lout1,BList,_,AFlag1,LFlag),
  79.     $prefix_list(BList,BLen,BList1),
  80.     $convrhs(Goal2,RGoal2,VidTbl,
  81.             Pmid1,Pout,St,Lmid0,Lout2,BList1,_,AFlag2,LFlag),
  82.     $max(Lout1,Lout2,Lout),
  83.     $logical_or(AFlag1,AFlag2,AFlag).
  84. $convrhs(Goal,Rgoal,VidTbl,Pathno,Pathno,St,Litin,Litout,BindList,0,0,LFlag) :-
  85.     $convrhs_nextlitno(Goal,Litin,Litout),
  86.     $convgoal(Goal,Rgoal,Pathno,VidTbl,St,Litin,BindList,LFlag).
  87.  
  88. /*      "convrhs_test" converts the test for an if-then-else        */
  89.  
  90. $convrhs_test(','(T1,T2),and(RT1,_,RT2),VidTbl,Pathno,St,Lin,Lout,BList) :-
  91.     !,
  92.     $convrhs_test(T1,RT1,VidTbl,Pathno,St,Lin,Lmid0,BList),
  93.     $convrhs_test(T2,RT2,VidTbl,Pathno,St,Lmid0,Lout,BList).
  94. $convrhs_test(';'(T1,T2),or(RT1,_,RT2),VidTbl,Pathno,St,Lin,Lout,BList) :-
  95.     !,
  96.     $convrhs_test(T1,RT1,VidTbl,Pathno,St,Lin,Lout0,BList),
  97.     $convrhs_test(T2,RT2,VidTbl,Pathno,St,Lin,Lout1,BList),
  98.     $max(Lout0,Lout1,Lout).
  99. $convrhs_test(T,RT,VidTbl,Pathno,St,Lin,Lout,BList) :-
  100.     $convrhs_nextlitno(T,Lin,Lout),
  101.     $convgoal(T,RT,Pathno,VidTbl,St,Lin,BList,0).
  102.  
  103. $convrhs_nextlitno(Goal, Litnum, NextLitnum) :-
  104.     $univ(Goal,[Pred|Args]),
  105.     $length(Args, Arity),
  106.     (($inline(Pred, Arity), NextLitnum  is Litnum) ;
  107.      NextLitnum is Litnum + 1).    
  108.  
  109. $convgoal(Goal,'_call'(is,Args,P),Pathno,VidTbl,St,Litno,BindList,LFlag) :-
  110.     $univ(Goal,[is|Unargs]), !,
  111.     St = st(Clist,Vlist),
  112.     $length(Unargs,2),
  113.     $member1(nv(NVars),P), $member1(nv(NVars),P1),
  114.     $attach(c(Litno,2,P1),Clist),
  115.     $member1(inline,P1),
  116.     $targlist_is(Unargs,Args,VidTbl,St,Pathno,Litno,1,LFlag).
  117. $convgoal(Goal,TGoal,Pathno,VidTbl,St,Litno,BindList,LFlag) :-
  118.     $univ(Goal,[Pred|Unargs]),
  119.     $length(Unargs,Npars),
  120.     St=st(Clist,Vlist),
  121.     $member1(nv(NVars),P), $member1(nv(NVars),P1),
  122.     $attach(c(Litno,Npars,P1),Clist),
  123.     (($inline(Pred,Npars), $member1(inline,P1)) ; true),
  124.     $targlist(Unargs,Args,VidTbl,St,Pathno,Litno,1,UniList,BindList,LFlag),
  125.     N1 is Npars+1, 
  126.     $constr_goal(Pred,Args,P,N1,Pathno,Litno,NVars,Vlist,UniList,TGoal).
  127.  
  128. $constr_goal(Pred,Args,P,_,_,_,NVars,_,[],'_call'(Pred,Args,P)) :-
  129.     $member1(nv(NVars),P).
  130. $constr_goal(Pred,Args,P,N,Path,L,NVars,Vlist,[(V=Str)|UniRest],
  131.             and('_call'(=, [V, Str], NV), _, TGoal1)) :-
  132.     V = v(Vid,Prag), Prag = vrec(_,_,_,_),
  133.     $member1(v(Vid,Occlist),Vlist),
  134.     gennum(Vno), $member1(o(Vno,Path,L,N,t,Prag),Occlist),
  135.     N1 is N+1, $member1(internal,NV), $member1(nv(NVars),NV),
  136.     $constr_goal(Pred,Args,P,N1,Path,L,NVars,Vlist,UniRest,TGoal1).
  137.  
  138. /* ---------------------------- $convrhs1.P --------------------------- */
  139.