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

  1. %    $Id: expanders.lf,v 1.2 1994/12/08 23:57:43 duchier Exp $    
  2. module("expanders") ?
  3.  
  4. import("accumulators") ?
  5.  
  6.  
  7. add_man(expanders,
  8.      "module(""expanders"")
  9.  
  10.       This module contains some useful clause expanders.") ?
  11.  
  12. %%% map a pred on a list
  13.  
  14. add_man(:-&,
  15.      "Head :-& Tail.
  16.  
  17.       This expander lets the user write a list traversal in one clause.
  18.  
  19.       ex:
  20.       foo(X) :-& process(X).
  21.  
  22.          is translated into:
  23.  
  24.       foo([]) :- !.
  25.       foo([X|Tail]) :-
  26.           process(X),
  27.           foo(Tail).
  28.  
  29.       The ""list"" argument is always the first one. The expanded predicate may
  30.       have other arguments: they are passed to the recursive call.
  31.  
  32.       ex:
  33.       add_feats(X,Term) :-& Term.X = @.
  34.  
  35.          is translated into:
  36.  
  37.       add_feats([]) :- !.
  38.       add_feats([X|Tail],Term) :- Term.X = @, add_feats(Tail,Term).
  39.  
  40.  
  41.       Rules may end with ""."" only if at least one option of expand load is 
  42.       true. Otherwise, they must end with ""?"".") ?
  43.  
  44. public(:-&) ?
  45. op(1200,xfx,:-&) ?
  46. non_strict(:-&) ?
  47.  
  48. (X :-& Y) :-
  49.     pred_list_expander(@(X,Y),in_clauses => Clauses,out_clauses => []),
  50.     maprel(assert,Clauses).
  51.  
  52. public(pred_list_expander) ?
  53. associate_expanders(:-&,pred_list_expander,:-) ?
  54. pred_list_expander(X :- Y,in_clauses => In,out_clauses => Out) :-
  55.     Head1 = root_sort(X) & @([]),
  56.     Args = get_args(X),
  57.     X <- root_sort(X) & @([X.1|Tail]) & copy_pointer(Args),
  58.     RecCall = root_sort(X) & @(Tail) & copy_pointer(Args),
  59.     In = [(Head1 :- !),(X :- Y,RecCall)|Out].
  60.     
  61. get_args(X) ->
  62.     get_args_2(features(X),X).
  63.  
  64. get_args_2([1|B],X) -> get_other_args(B,X).
  65. get_args_2([A|B],X) -> T | T.A = X.A, T = get_args_2(B,X).
  66. get_args_2([]) -> @.
  67.  
  68. get_other_args([A|B],X) -> T | T.A = X.A, T = get_other_args(B,X).
  69. get_other_args([]) -> @.
  70.  
  71.  
  72.  
  73. add_man(->&,
  74.      "Head ->& Tail.
  75.  
  76.       This expander maps 'Tail' on a list.
  77.  
  78.       ex:
  79.       foo(X) ->& process(X).
  80.  
  81.          is translated into:
  82.  
  83.       foo([X|Tail]) -> [process(X)|foo(Tail)].
  84.       foo([]) -> [].
  85.  
  86.       The ""list"" argument is always the first one. The expanded predicate may
  87.       have other arguments: they are passed to the recursive call.
  88.  
  89.       ex:
  90.       map_plus(X,Val) ->&  X + Val.
  91.  
  92.          is translated into:
  93.  
  94.       map_plus([X|Tail],Val) ->  [X+Val|map_plus(Tail,Val)].
  95.       map_plus([]) -> [].
  96.  
  97.       Rules may end with ""."" only if at least one option of expand load is 
  98.       true. Otherwise, they must end with ""?"".") ?
  99.  
  100.  
  101. public(->&) ?
  102. op(1200,xfx,->&) ?
  103. non_strict(->&) ?
  104.  
  105. (X ->& Y) :-
  106.     func_list_expander(X -> Y,in_clauses => List,out_clauses => []),
  107.     Cl1 = List.1,Cl2 = List.2 .1,
  108.     assert(Cl1),
  109.     assert(Cl2).
  110.  
  111. associate_expanders(->&,func_list_expander,->) ?
  112. func_list_expander(X -> Y,in_clauses => In, out_clauses => Out) :-
  113.     Head1 = root_sort(X) & @([]),
  114.     Args = get_args(X),
  115.     X <- root_sort(X) & @([X.1|Tail]) & copy_pointer(Args),
  116.     RecCall = root_sort(X) & @(Tail) & copy_pointer(Args),
  117.     In = [(X -> [Y|RecCall]),
  118.           (Head1 -> [])|Out].
  119.  
  120.  
  121.