Advertisement
logicmoo

Untitled

May 30th, 2015
409
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.     Author:        Ulrich Neumerkel
  3.     E-mail:        ulrich@complang.tuwien.ac.AT
  4.     Copyright (C): 2009 Ulrich Neumerkel. All rights reserved.
  5.  
  6. Redistribution AND use in source AND binary forms, with OR without
  7. modification, are permitted provided that the following conditions are
  8. met:
  9.  
  10. 1. Redistributions of source CODE must retain the above copyright
  11.    notice, this list of conditions AND the following disclaimer.
  12.  
  13. 2. Redistributions in binary form must reproduce the above copyright
  14.    notice, this list of conditions AND the following disclaimer in the
  15.    documentation AND/OR other materials provided with the distribution.
  16.  
  17. THIS SOFTWARE IS PROVIDED BY Ulrich Neumerkel ``AS IS'' AND ANY
  18. EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  20. PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Ulrich Neumerkel OR
  21. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  22. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  23. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  24. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  25. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  26. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28.  
  29. The views AND conclusions contained in the software AND documentation
  30. are those of the authors AND should NOT be interpreted AS representing
  31. official policies, either expressed OR implied, of Ulrich Neumerkel.
  32.  
  33.  
  34.  
  35. */
  36.  
  37. :- module(rec_lambda, [
  38.            (^)/3, (^)/4, (^)/5, (^)/6, (^)/7, (^)/8, (^)/9,
  39.            (\)/1, (\)/2, (\)/3, (\)/4, (\)/5, (\)/6, (\)/7,
  40.            (+\)/2, (+\)/3, (+\)/4, (+\)/5, (+\)/6, (+\)/7,
  41.                    (recl)/0, (recl)/1, (recl)/2, (recl)/3, (recl)/4, (recl)/5, (recl)/6,
  42.                    ctn/2,
  43.                    recval/2,
  44.                    letrec/2,
  45.            op(201,xfx,+\)]).
  46.  
  47. /** <module> Lambda expressions
  48.  
  49. This library provides lambda expressions TO simplify higher order
  50. programming based on call/N.
  51.  
  52. Lambda expressions are represented by ordinary Prolog terms.
  53. There are two kinds of lambda expressions:
  54.  
  55.     Free+\X1^X2^ ..^XN^Goal
  56.  
  57.          \X1^X2^ ..^XN^Goal
  58.  
  59. The second is a shorthand FOR t+\X1^X2^..^XN^Goal.
  60.  
  61. Xi are the parameters.
  62.  
  63. Goal is a goal OR continuation. Syntax note: Operators within Goal
  64. require parentheses due TO the low precedence of the ^ operator.
  65.  
  66. Free contains variables that are valid outside the scope of the lambda
  67. expression. They are thus free variables within.
  68.  
  69. All other variables of Goal are considered local variables. They must
  70. NOT appear outside the lambda expression. This restriction is
  71. currently NOT checked. Violations may lead TO unexpected bindings.
  72.  
  73. In the following example the parentheses around X>3 are necessary.
  74.  
  75. ==
  76. ?- use_module(library(lambda)).
  77. ?- use_module(library(apply)).
  78.  
  79. ?- maplist(\X^(X>3),[4,5,9]).
  80. true.
  81. ==
  82.  
  83. In the following X is a variable that is shared by both instances of
  84. the lambda expression. The second query illustrates the cooperation of
  85. continuations AND lambdas. The lambda expression is in this case a
  86. continuation expecting a further argument.
  87.  
  88. ==
  89. ?- Xs = [A,B], maplist(X+\Y^dif(X,Y), Xs).
  90. Xs = [A, B],
  91. dif(X, A),
  92. dif(X, B).
  93.  
  94. ?- Xs = [A,B], maplist(X+\dif(X), Xs).
  95. Xs = [A, B],
  96. dif(X, A),
  97. dif(X, B).
  98. ==
  99.  
  100. The following queries are all equivalent. TO see this, use
  101. the fact f(x,y).
  102. ==
  103. ?- call(f,A1,A2).
  104. ?- call(\X^f(X),A1,A2).
  105. ?- call(\X^Y^f(X,Y), A1,A2).
  106. ?- call(\X^(X+\Y^f(X,Y)), A1,A2).
  107. ?- call(call(f, A1),A2).
  108. ?- call(f(A1),A2).
  109. ?- f(A1,A2).
  110. A1 = x,
  111. A2 = y.
  112. ==
  113.  
  114. Further discussions
  115. http://www.complang.tuwien.ac.AT/ulrich/Prolog-inedit/ISO-Hiord
  116.  
  117. @tbd Static expansion similar TO apply_macros.
  118. @author Ulrich Neumerkel
  119. */
  120.  
  121.  
  122. :- meta_predicate ctn(?,?).
  123. ctn(I,O):-copy_term_nat(I,O).
  124.  
  125.  
  126. :- meta_predicate
  127.       letrec(+,+),
  128.       recval(+,?),
  129.     recl(),
  130.     recl(?),
  131.     recl(?,?),
  132.     recl(?,?,?),
  133.     recl(?,?,?,?),
  134.     recl(?,?,?,?,?),
  135.     recl(?,?,?,?,?,?).
  136.  
  137.  
  138. letrec(N,rec_lambda(FC)):-!, atom_concat('$rec_lambda',N,Key), b_setval(Key,FC),!.
  139. % TODO only DO this IF the lamda will need it
  140. letrec(N,user:FC):-!, once(( atom_concat('$rec_lambda',N,Key), b_setval(Key,FC))).
  141. letrec(N,FC):- once((atom_concat('$rec_lambda',N,Key), b_setval(Key,FC))).
  142.  
  143.  
  144. recval(N,FC):- atom_concat('$rec_lambda',N,Key), nb_current(Key,FC),!,FC\=[].
  145.  
  146. recl :-recval(0,FC),call(FC).
  147. recl(V1):-recval(1,FC),call(FC,V1).
  148. recl(V1,V2):-recval(2,FC),call(FC,V1,V2).
  149. recl(V1,V2,V3):-recval(3,FC),call(FC,V1,V2,V3).
  150. recl(V1,V2,V3,V4):-recval(4,FC),call(FC,V1,V2,V3,V4).
  151. recl(V1,V2,V3,V4,V5):-recval(5,FC),call(FC,V1,V2,V3,V4,V5).
  152. recl(V1,V2,V3,V4,V5,V6):-recval(6,FC),call(FC,V1,V2,V3,V4,V5,V6).
  153.  
  154.  
  155.  
  156.  
  157. :- meta_predicate no_hat_call(0).
  158.  
  159. :- meta_predicate
  160.     ^(?,0,?),
  161.     ^(?,1,?,?),
  162.     ^(?,2,?,?,?),
  163.     ^(?,3,?,?,?,?),
  164.     ^(?,4,?,?,?,?,?),
  165.     ^(?,5,?,?,?,?,?,?),
  166.     ^(?,6,?,?,?,?,?,?,?).
  167.  
  168.  
  169.  
  170. ^(V1,Goal,V1) :-
  171.    letrec(1,Goal),
  172.    no_hat_call(Goal).
  173. ^(V1,Goal,V1,V2) :-
  174.     letrec(2,Goal),
  175.    call(Goal,V2).
  176. ^(V1,Goal,V1,V2,V3) :-
  177.     letrec(3,Goal),
  178.    call(Goal,V2,V3).
  179. ^(V1,Goal,V1,V2,V3,V4) :-
  180.     letrec(4,Goal),
  181.    call(Goal,V2,V3,V4).
  182. ^(V1,Goal,V1,V2,V3,V4,V5) :-
  183.     letrec(5,Goal),
  184.    call(Goal,V2,V3,V4,V5).
  185. ^(V1,Goal,V1,V2,V3,V4,V5,V6) :-
  186.     letrec(6,Goal),
  187.    call(Goal,V2,V3,V4,V5,V6).
  188. ^(V1,Goal,V1,V2,V3,V4,V5,V6,V7) :-
  189.     letrec(7,Goal),
  190.    call(Goal,V2,V3,V4,V5,V6,V7).
  191.  
  192. :- meta_predicate
  193.     \(0),
  194.     \(1,?),
  195.     \(2,?,?),
  196.     \(3,?,?,?),
  197.     \(4,?,?,?,?),
  198.     \(5,?,?,?,?,?),
  199.     \(6,?,?,?,?,?,?).
  200.  
  201. \(FC) :-
  202.    letrec(0,FC),
  203.    ctn(FC,C),no_hat_call(C).
  204. \(FC,V1) :-
  205.    letrec(1,FC),
  206.    ctn(FC,C),call(C,V1).
  207. \(FC,V1,V2) :-
  208.    letrec(2,FC),
  209.    ctn(FC,C),call(C,V1,V2).
  210. \(FC,V1,V2,V3) :-
  211.    letrec(3,FC),
  212.    ctn(FC,C),call(C,V1,V2,V3).
  213. \(FC,V1,V2,V3,V4) :-
  214.    letrec(4,FC),
  215.    ctn(FC,C),call(C,V1,V2,V3,V4).
  216. \(FC,V1,V2,V3,V4,V5) :-
  217.    letrec(5,FC),
  218.    ctn(FC,C),call(C,V1,V2,V3,V4,V5).
  219. \(FC,V1,V2,V3,V4,V5,V6) :-
  220.    letrec(6,FC),
  221.    ctn(FC,C),call(C,V1,V2,V3,V4,V5,V6).
  222.  
  223. :- meta_predicate
  224.     +\(?,0),
  225.     +\(?,1,?),
  226.     +\(?,2,?,?),
  227.     +\(?,3,?,?,?),
  228.     +\(?,4,?,?,?,?),
  229.     +\(?,5,?,?,?,?,?),
  230.     +\(?,6,?,?,?,?,?,?).
  231.  
  232. +\(GV,FC) :-
  233.    letrec(0,FC),
  234.    ctn(GV+FC,GV+C),no_hat_call(C).
  235. +\(GV,FC,V1) :-
  236.    letrec(1,FC),
  237.    ctn(GV+FC,GV+C),call(C,V1).
  238. +\(GV,FC,V1,V2) :-
  239.    letrec(2,FC),
  240.    ctn(GV+FC,GV+C),call(C,V1,V2).
  241. +\(GV,FC,V1,V2,V3) :-
  242.    letrec(3,FC),
  243.    ctn(GV+FC,GV+C),call(C,V1,V2,V3).
  244. +\(GV,FC,V1,V2,V3,V4) :-
  245.    letrec(4,FC),
  246.    ctn(GV+FC,GV+C),call(C,V1,V2,V3,V4).
  247. +\(GV,FC,V1,V2,V3,V4,V5) :-
  248.    letrec(5,FC),
  249.    ctn(GV+FC,GV+C),call(C,V1,V2,V3,V4,V5).
  250. +\(GV,FC,V1,V2,V3,V4,V5,V6) :-
  251.    letrec(6,FC),
  252.    ctn(GV+FC,GV+C),call(C,V1,V2,V3,V4,V5,V6).
  253.  
  254.  
  255. %% no_hat_call(:Goal)
  256. %
  257. % Like call, but issues an error FOR a goal (^)/2.  Such goals are
  258. % likely the result of an insufficient number of arguments.
  259.  
  260. no_hat_call(MGoal) :-
  261.    strip_module(MGoal, _, Goal),
  262.    (  nonvar(Goal),
  263.       Goal = (_^_)
  264.    -> throw(error(existence_error(lambda_parameters,Goal),_))
  265.    ;  call(MGoal)
  266.    ).
  267.  
  268. % I would like TO replace this by:
  269. % V1^Goal :- throw(error(existence_error(lambda_parameters,V1^Goal),_)).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement