home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / prolog / modprolg / mod-prol.lha / Prolog / modlib / src / $funrel.P < prev    next >
Encoding:
Text File  |  1992-05-27  |  6.1 KB  |  175 lines

  1. /************************************************************************
  2. *                                    *
  3. * The SB-Prolog System                            *
  4. * Copyright SUNY at Stony Brook, 1986; University of Arizona, 1987    *
  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.  *                                                                          *
  27.  * This file has been changed by to include Modules Extensions              *
  28.  * Changes by : Brian Paxton 1991/92                                        *
  29.  * Last update : June 1992                                                  *
  30.  *                                                                          *
  31.  * Organisation : University of Edinburgh.                                  *
  32.  * For : Departments of Computer Science and Artificial Intelligence        * 
  33.  *       Fourth Year Project.                                               *
  34.  *                                                                          *
  35.  ****************************************************************************/
  36.  
  37. /* $funrel.P */
  38. /* the predicates in this file are called from $getclauses: they
  39.    traverse a clause and convert functional syntax in arithmetic
  40.    expressions to relational expressions.  For example,
  41.    
  42.        X is 1 + sqrt(Y)
  43.  
  44.    is converted to
  45.  
  46.     square(Z, Y), X is 1 + Z
  47.  
  48.    The set of "evaluable functors", and the predicates they are
  49.    converted to, are defined in the table $fun_to_rel/3 at the
  50.    end of this file.
  51. */
  52.  
  53. $funrel_export([$fun_rel/2,$expand_body/2,$fun_rel/3,$expand_body/3]).
  54.  
  55. % $funrel_use : $bmeta $meta
  56.  
  57. $fun_rel(Old, New) :- $fun_rel(Old,New,perv).
  58.  
  59. $fun_rel((H :- B), (H :- B1), Tag) :- !, $expand_body(B, B1, Tag).
  60. $fun_rel(T, T,_).
  61.  
  62. $expand_body(Old, New) :-
  63.     $expand_body(Old, New, perv).
  64.  
  65. $expand_body((A1,B1), (A2,B2), Tag) :-
  66.      !, $expand_body(A1,A2,Tag), $expand_body(B1,B2,Tag).
  67. $expand_body((A1;B1), (A2;B2), Tag) :-
  68.      !, $expand_body(A1,A2,Tag), $expand_body(B1,B2,Tag).
  69. $expand_body((A1 -> B1), (A2 -> B2), Tag) :-
  70.      !, $expand_body(A1,A2,Tag), $expand_body(B1,B2,Tag).
  71. $expand_body('=:='(A1,B1), G, _) :-
  72.      !, $funrel_proc_exp(A1,A2,G1), $funrel_proc_exp(B1,B2,G2),
  73.      $chk_goals(G1,G2,'=:='(A2,B2), G).
  74. $expand_body('=/='(A1,B1), G, _) :-
  75.      !, $funrel_proc_exp(A1,A2,G1), $funrel_proc_exp(B1,B2,G2),
  76.      $chk_goals(G1,G2,'=/='(A2,B2), G).
  77. $expand_body('>='(A1,B1), G, _) :-
  78.      !, $funrel_proc_exp(A1,A2,G1), $funrel_proc_exp(B1,B2,G2),
  79.      $chk_goals(G1,G2,'>='(A2,B2), G).
  80. $expand_body('>'(A1,B1), G, _) :-
  81.      !, $funrel_proc_exp(A1,A2,G1), $funrel_proc_exp(B1,B2,G2),
  82.      $chk_goals(G1,G2,'>'(A2,B2), G).
  83. $expand_body('=<'(A1,B1), G, _) :-
  84.      !, $funrel_proc_exp(A1,A2,G1), $funrel_proc_exp(B1,B2,G2),
  85.      $chk_goals(G1,G2,'=<'(A2,B2), G).
  86. $expand_body('<'(A1,B1), G, _) :-
  87.      !, $funrel_proc_exp(A1,A2,G1), $funrel_proc_exp(B1,B2,G2),
  88.      $chk_goals(G1,G2,'<'(A2,B2), G).
  89. $expand_body(not(A1), not(A2), Tag) :-
  90.      !, $expand_body(A1,A2,Tag).
  91. $expand_body(is(A,Exp1), B, _) :- 
  92.      !, $funrel_proc_exp(Exp1,Exp2,G,Conv),
  93.      (Conv =:= 1 ->
  94.           (var(Exp2) ->
  95.                (A = Exp2, B = G) ;
  96.            B = (G,is(A,Exp2))
  97.       ) ;
  98.       B = is(A,Exp1)
  99.      ).
  100.  
  101. /* New bits for modules 
  102.  
  103.    The predicates that have to be passed the tag of the current structure
  104.    are processed here. */
  105.  
  106. $expand_body(current_structure(X),current_structure(X,Tag),Tag) :- !.
  107.  
  108. $expand_body(name(X,Y),name(X,Y,Tag),Tag) :- !.
  109.  
  110. $expand_body(read(X),read(X,Tag),Tag) :- !.
  111. $expand_body(read(X,Y),read(X,Y),_) :- 
  112.     $isa_structuretag(Y), !.
  113. $expand_body(read(X,Y),read(X,Y,Tag),Tag) :- !.
  114.  
  115. $expand_body(compound(X,Y,Z),compound(X,Y,Z,Tag),Tag) :- !.
  116.  
  117. $expand_body(bldstr(X,Y,Z),bldstr(X,Y,Z,Tag),Tag) :- !.
  118.  
  119. $expand_body('=..'(X,Y),'=..'(X,Y,Tag),Tag) :- !.
  120.  
  121. $expand_body(structure(X,Y),structure(X,Y,Tag),Tag) :- !.
  122. $expand_body(structure(X,Y,Z),structure(X,Y,Z,Tag),Tag) :- !.
  123.  
  124. $expand_body(T,T,_).
  125.  
  126. $funrel_proc_exp(E1,E2,G) :- $funrel_proc_exp(E1,E2,G,_).
  127.  
  128. $funrel_proc_exp(E1,E3,G,C) :-
  129.      $structure(E1) ->
  130.           ($functor(E1,F,N), $functor(E2,F,N),
  131.            $funrel_proc_subexps(N,E1,E2,true,G1,0,C0),
  132.            ($fun_to_rel(E2,G2,E3) ->
  133.            (C = 1,
  134.                 (G1 = true -> G = G2 ; G = (G1,G2))
  135.            ) ;
  136.            (E3 = E2, G = G1, C = C0)
  137.            )
  138.           ) ;
  139.       (E3 = E1, G = true, C = 0).
  140.  
  141. $funrel_proc_subexps(N,Ein,Eout,Gin,Gout,Cin,Cout) :-
  142.      N =:= 0 ->
  143.           (Cin = Cout, Gin = Gout) ;
  144.       (arg(N,Ein,T1), arg(N,Eout,T2),
  145.        $funrel_proc_exp(T1,T2,G,C0),
  146.        N1 is N-1,
  147.        (Gin = true -> Gmid = G ; Gmid = (Gin,G)),
  148.        (Cin =:= 1 -> C1 = 1 ; C1 = C0),
  149.        $funrel_proc_subexps(N1,Ein,Eout,Gmid,Gout,C1,Cout)
  150.       ).
  151.  
  152. $chk_goals(A,B,C,D) :-
  153.      A ?= true ->
  154.           (B ?= true ->
  155.            D = C ;
  156.            D = (B,C)
  157.           ) ;
  158.       (B ?= true ->
  159.            D = (A,C) ;
  160.            D = (A,B,C)
  161.           ).
  162.  
  163. % $fun_to_rel(T1, T2, Z) is true iff T2 represents the "relational syntax"
  164. % version of the "functional syntax" expression "Z = T1".
  165.  
  166. $fun_to_rel(sqrt(X), square(Y,X), Y).
  167. $fun_to_rel(square(X), square(X,Y), Y).
  168. $fun_to_rel(integer(X), floor(X,Y), Y).
  169. $fun_to_rel(float(X), floor(Y,X), Y).
  170. $fun_to_rel(exp(X), exp(X,Y), Y).
  171. $fun_to_rel(ln(X), exp(Y,X), Y).
  172. $fun_to_rel(sin(X), sin(X,Y), Y).
  173. $fun_to_rel(arcsin(X), sin(Y,X), Y).
  174.  
  175.