izilotti
izilotti

Reputation: 4927

PROLOG with lambda expressions

Is there any Java PROLOG implementation that supports lambda expressions? I know that there are Java implementations of other languages that support lambda expressions, such as LISP and Clojure, but I really need a PROLOG implementation.

http://en.wikipedia.org/wiki/Comparison_of_Prolog_implementations

Upvotes: 9

Views: 2503

Answers (4)

user502187
user502187

Reputation:

There are basically two approaches around for lambda expressions in Prolog, that only address the invocation of lambda expressions and not higher order unification:

  • global-by-default: The variables in the body of the lambda expression are global by default, if they are not mentioned by an extra binder.

  • local-by-default: The variables in the body of the lambda expression are local by default, if they are not mentioned by an extra binder.

Representatives of local-by-default are for example Ulrich Neumerkel's library(lambda) or the lambda expressions found in Logtalk. The global-by-default approach is currently followed by the lambda expressions found in Jekejeke Prolog.

Both approaches allow to model the same mathematical lambda expressions and solve the following problems by some further syntax:

  • Control of sharing or non-sharing of variables across multiple invocations.

  • Forcing of alpha conversion for binders and local variables in the body of the lambda expression.

Here is an example, factorial via the Y combinator:

  • global-by-default: Jekejeke Prolog
    ?- Y    = F\X^call(X\call(F,call(X,X)),X\call(F,call(X,X))),
       Fact = F\J^H^M^N^N\J^H^M^M\J^H^(N=0,M=1;N>0,H is N-1,call(F,H,J),M is N*J),
       call(Y,Fact,10,R). 
    R = 3628800.
    ?- Y    = \F^call([F]+\X^call(F,call(X,X)),[F]+\X^call(F,call(X,X))),
       Fact = \F^([F]+\N^([N,F]+\M^(N=0,M=1;N>0,H is N-1,call(F,H,J),M is N*J))),
       call(Y,Fact,10,R).
    R = 3628800.

Bye

P.S.: The distinction global-by-default and local-by-default is borrowed from, page 10:
The Language Features and Architecture of B-Prolog
Neng-Fa Zhou, Theory and Practice of Logic Programming, 2011
http://www.sci.brooklyn.cuny.edu/~zhou/papers/tplp11sips.pdf

Upvotes: 2

Paulo Moura
Paulo Moura

Reputation: 18663

Lean Prolog is implemented in Java and can run Logtalk, which makes lambda expressions available to to all supported backend Prolog compilers. For an overview of Logtalk's lambda expressions syntax see e.g.:

https://logtalk.org/2009/12/08/lambda-expressions-in-logtalk.html/

For usage examples see:

https://github.com/LogtalkDotOrg/logtalk3/tree/master/examples/lambdas

Upvotes: 5

Erik Kaplun
Erik Kaplun

Reputation: 38217

In SWI-Prolog there is library(yall) (https://www.swi-prolog.org/pldoc/man?section=yall), which even supports currying:

?- use_module(library(yall)).

?- maplist([X,Y]>>(Y #= 2*X), [1,2,3], Ys).
Ys = [2, 4, 6].

?- maplist([X,Y]>>([Z]>>(Z #= X * Y)), [1,2,3], [3,4,5], Ys).
Ys = [3, 8, 15].

?- maplist([X,Y,Z]>>(Z #= X * Y), [1,2,3], [3,4,5], Ys).
Ys = [3, 8, 15].

?- maplist([X]>>([Y]>>([Z]>>(Z #= X * Y))), [1,2,3], [3,4,5], Ys).
Ys = [3, 8, 15].

Same with library(lambda):

?- maplist(\X^(\Y^(Y #= 2*X)), [1,2,3], Ys).
Ys = [2, 4, 6].

?- maplist(\X^Y^(\Z^(Z #= X * Y)), [1,2,3], [3,4,5], Ys).
Ys = [3, 8, 15].

?- maplist(\X^Y^Z^(Z #= X * Y), [1,2,3], [3,4,5], Ys).
Ys = [3, 8, 15].

?- maplist(\X^(\Y^(\Z^(Z #= X * Y))), [1,2,3], [3,4,5], Ys).
Ys = [3, 8, 15].

However the lambdas of library(lambda) can also be used to "extract" the values of only some variables of a query:

?- Xs = [1,2,3,4,5,6,7], member(X, Xs), 0 #= X mod 2, Y #= X * 2.
Xs = [1, 2, 3, 4, 5, 6, 7],
X = 2,
Y = 4 ;
Xs = [1, 2, 3, 4, 5, 6, 7],
X = 4,
Y = 8 ;
Xs = [1, 2, 3, 4, 5, 6, 7],
X = 6,
Y = 12 ;
false.

?- Y+\(Xs = [1,2,3,4,5,6,7], member(X, Xs), 0 #= X mod 2 Y #= X * 2).
Y = 4 ;
Y = 8 ;
Y = 12 ;
false.

Upvotes: 2

user1812457
user1812457

Reputation:

There is an Prolog lambda implementation by Ulrich Neumerkel. SWI-Prolog for example supports it. If you did a search in Stackoverflow:

[swi-prolog] lambda

you can also find quite a few answers using it for solutions.

Also, the web-page that explains it all

Upvotes: 3

Related Questions