home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / lifeos2.zip / LIFE-1.02 / TESTS / LF / DISSOLVE.LF < prev    next >
Text File  |  1996-06-04  |  9KB  |  263 lines

  1. %
  2. % module(DissolvePattern).
  3. %
  4. % load(name_top_vars)?
  5. %
  6. %
  7. %        Dissolving a psi-term (not only patterns for a Life function call)
  8. %
  9. %
  10. %
  11.  
  12. %
  13. % The sort structure of dissolved forms
  14. %
  15.  
  16. diss_form <| @.
  17.  
  18. sort_eqn    := diss_form(var=>X,srt=>Y).            % s must match the root_sort_n of X (i.e. X:s)
  19. feature_eqn := diss_form(var1=>X,feat=>Y,var2=>Z).  % X.L is equal to Y
  20. coref_eqn   := diss_form(var1=>X,var2=>Y).          % X = Y
  21.  
  22. %
  23. % dissolve a psi-term (without executing it: this is why there is
  24. %                      the non_strict declaration).
  25. %
  26. % jolidissolve takes a string instead of a psi-term.
  27. non_strict(dissolve_psi)?
  28.  
  29. jolidissolve(X)   -> @ | W = parse(X), dissolve_psi(W).
  30. dissolve_psi(Psi) -> @ | (DissForm,Bindings) = dissolve((Dummy,Psi),[]), prettyprint(DissForm).
  31.  
  32. non_strict(dissolve)?
  33.  
  34. dissolve((V,Psi),Dejavu) ->
  35.         cond(assq(Psi,Dejavu)&bool(Name),
  36.              ([coref_eqn(var1=>V,var2=>Name)],Dejavu),
  37.              ([sort_eqn(var=>V,srt =>my_root_sort(Psi))| SubstEqn], NewDejavu) % generate the root sort constraint
  38.                 |                                       % generate the feature constraints by examining recursively the subtrees:
  39.                   (  FL = features_n(Psi),              % get the feature list
  40.              P=project_n(2=>Psi),
  41.                      FV = map(P,FL),% get the feature values list
  42.                      % FV = map(project_n & @(2=>Psi),FL),% get the feature values list
  43.                      (SubstEqn,NewDejavu) =
  44. flatten_accumulate(dissolve_features_of(psi=>Psi,var=>V),
  45.            FL,[(Psi,V)|Dejavu]))).
  46. % flatten_accumulate(dissolve_features_of & @(psi=>Psi,var=>V),FL,[(Psi,V)|Dejavu]))).
  47.  
  48. %
  49. % Find a "quoted" root_sort in case it is top
  50. %
  51.  
  52. my_root_sort(X) -> cond(var(X),"@",root_sort_n(X)).
  53.  
  54. %
  55. %
  56. non_strict(dissolve_features_of)?
  57.  
  58. dissolve_features_of(psi=>_,var=>_,[],Dejavu) -> ([],Dejavu).
  59.  
  60. dissolve_features_of(psi=>Psi,var=>V,Feature,Dejavu) ->
  61.         ([feature_eqn(var1=>V,feat=>Feature,var2 => NewVar)|EqnRest],NewDejavu)
  62.              |   FeatureValue = project_n(Feature,Psi),
  63.                  (EqnRest,NewDejavu) = dissolve((NewVar,FeatureValue),Dejavu).
  64.           
  65. %
  66. % Human readable output for debugging ...
  67. %
  68.                        
  69. %
  70. % Name the top variables, then print normally the psi-term.
  71. %
  72.  
  73. prettyprint(L) :- setq(my_gen_sym,1), pretty(name_vars(L)).
  74.  
  75. pretty([]):- !.
  76. pretty([X:diss_form|Rest]):- pretty_inst(X),pretty(Rest),!.
  77.  
  78. pretty_inst(sort_eqn(var=>X,srt=>Y)) :- write(X),write(":"),write(Y),pretty_sep,!.
  79. pretty_inst(feature_eqn(var1=>X,feat=>Y,var2=>Z)) :- write(X),write("."),write(Y),
  80.                                                      write("="),write(Z),pretty_sep,!.
  81. pretty_inst(coref_eqn(var1=>X,var2=>Y)) :- write(X),write("="),write(Y),pretty_sep,!.
  82.  
  83. pretty_sep:- write(", "),!.
  84.  
  85. %
  86. % Functional utilities to handle lists and associationb lists... better in a library
  87. %
  88.  
  89. %
  90. % assqq(X,L) is true iff  X occurs in the association  list L, in  which case
  91. % has the associated value as feature 1
  92. %
  93.  
  94. assq(X,[])            -> false.
  95. assq(X,[(Y,Value)|T]) -> cond(X===Y,true(Value),assq(X,T)).
  96.  
  97. %
  98. % Accumulate function: takes a function f:A->B->C, a list L of A, a B, and returns
  99. % the result of applying f to the each element of L, and to the progressively modified B
  100. %
  101.  
  102. flatten_accumulate(F,[],B) -> ([],B).
  103. flatten_accumulate(F,[Hd|T],B) -> (Flat,NewB) | (Nv,NB) = F(Hd,B), (Nl,NewB) = flatten_accumulate(F,T,NB), Flat = append(Nv,Nl).
  104.  
  105. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  106. %
  107. % to be done ...
  108. %
  109.  
  110. %
  111. % export(dissolve_psi).
  112. %
  113.  
  114. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  115. % FILE. . . . . /udir/dicosmo/life/code/name_top_vars
  116. % EDIT BY . . . Roberto Di Cosmo
  117. % ON MACHINE. . Prl341
  118. % STARTED ON. . Thu Sep 24 16:10:30 1992
  119. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  120.  
  121. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  122.  
  123. % Take a psi-term and return a copy of it  where all variables satisfying the
  124. % predicate "var"   (i.e.,  by  this time, being    of   sort @)    are named
  125. % consistently, by moving  them down  the sort hierarchy  to the  appropriate
  126. % string sort.
  127.  
  128. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  129.  
  130. % load(repair_list)?
  131.  
  132. %
  133. % The function genvar yields a new variable name each time it is called
  134. %
  135.  
  136. setq(my_gen_sym,0)?
  137.  
  138. genvar -> strcon("U",int2str(N)) | N = my_gen_sym, setq(my_gen_sym,N+1).
  139.  
  140. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  141.  
  142. % The  function `name_vars' takes a psi-term  and returns a  copy of it where
  143. % all variables satisfying the predicate "var" (i.e., by  this time, being of
  144. % sort  @) are named  consistently, by moving   them down to  the appropriate
  145. % string sort.
  146.  
  147. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  148.  
  149. name_vars(X) -> Y | Y = copy_term(X), name_top_vars(Y,[]).
  150.  
  151. name_vars_from_to(X,N,M) -> Y | setq(my_gen_sym,N),
  152.                                 Y = name_vars(X),
  153.                                 M = my_gen_sym.
  154.  
  155. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  156.  
  157. %
  158. % Since  the  root_sort of  a  list is  not "cons",  with  features "head" and
  159. % "tail", this function needs to use the  repaired life  version  of features
  160. % called features_n to navigate in every psi-term uniformly
  161. %
  162. % We  need here also a   table of already  seen elements  to avoid looping on
  163. % cyclic structures; the updated table is given back as the last argument
  164. %
  165.  
  166. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  167.  
  168. name_top_vars(X,Table,[X|Table])
  169.        :- var(X),!, X = genvar.
  170. name_top_vars(X,Table,NewTable)
  171.        :- name_top_vars_body(features_n(X),X,[X|Table],NewTable).
  172.  
  173. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  174.  
  175. % The   function `name_top_vars_body'  takes a  list   of pairs  of  the form
  176. % (attribute,psi-term) and a table  and returns a pair  made up of  a similar
  177. % list and a new table.
  178.  
  179. name_top_vars_body([],X,Table,Table)
  180.        :- !.
  181. name_top_vars_body([A|T],X,Table,NewTable)
  182.        :- Z=project_n(A,X),
  183.             cond(memq(Z,Table),
  184.                  name_top_vars_body(T,X,Table,NewTable),
  185.                  (name_top_vars(Z,Table,NT), name_top_vars_body(T,X,NT,NewTable))).
  186.  
  187. %
  188. % true iff X occurs in the list passed as second argument
  189. %
  190.  
  191. memq(X,[])    -> false.
  192. memq(X,[Y|T]) -> cond(X===Y,true,memq(X,T)).
  193.  
  194. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  195. % FILE. . . . . /udir/dicosmo/life/code/repair_list
  196. % EDIT BY . . . Roberto Di Cosmo
  197. % ON MACHINE. . Prl341
  198. % STARTED ON. . Thu Sep 18 16:10:30 1992
  199. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  200.  
  201. %
  202. % Repair buggy list sort definitions
  203. %
  204.  
  205. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  206.  
  207. % The built ins of the current version of  Wild_Life treat lists in a special
  208. % way, that renders it impossible to write programs  that navigate prsi terms
  209. % in a general uniform way.
  210. %
  211. % This file  provides the necessary   (not necessarily  efficient) workaround
  212. % while waiting for the definitive solution (in the compiler?).
  213. %
  214. % The functions features, root_sort, project, listify_body and bodify_list are
  215. % provided here  in a _n form that  works uniformly  on  psi-terms, even when
  216. % they are lists.
  217. %
  218.  
  219. %
  220. % Examples of applications are  to be found in dissolve.life, wam_bruno.life,
  221. % name_top_vars.life
  222. %
  223.  
  224. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  225.  
  226. %
  227. % To  avoid residuation  on variables, I fire  the function anyway, and  then
  228. % check what I have in hand, in the internal functions
  229. %
  230.  
  231. features_n(X) -> N | X:==@, N=features(X),!; N=features_internal(X).
  232. features_internal([])    -> [].
  233. features_internal([X|T]) -> [hd,tl].
  234. features_internal(X)     -> features(X).
  235.  
  236. root_sort_n(X) -> N | X:==@, N=@,!; N=root_sort_internal(X).
  237. root_sort_internal([])    -> nil.
  238. root_sort_internal([X|T]) -> cons.
  239. root_sort_internal(X)     -> root_sort(X).
  240.  
  241. project_n(A,X) -> N | X:==@, N=X.A,!; N=project_internal(A,X).
  242. project_internal(hd,[X|T]) -> X.
  243. project_internal(tl,[X|T]) -> T.
  244. project_internal(tl,[])    -> [].
  245. project_internal(A,X)      -> X.A.
  246.  
  247. %
  248. % Rebuild listify_body and  bodify_list using  the  repaired sort  navigation
  249. % functions
  250. %
  251.  
  252. listify_body_n(A) -> map(feature_subterm_n(2 => A),features_n(A)).
  253.  
  254. feature_subterm_n(A,B) -> A, project_n(A,B).
  255.  
  256. %
  257. % Specially handle new list sort manipulation
  258. %
  259. bodify_list_n([])                -> @.
  260. bodify_list_n([(hd, X),(tl, Y)]) -> [X|Y].
  261. bodify_list_n([(A, B)|C:@])      -> D:bodify_list_n(C) | B = project_n(A,D).
  262.