Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binders in Prolog

Tags:

lambda

prolog

I know Prolog has no built-in binders to represent e.g. λx (x=1) but I was wondering if it was possible to implement them. In some predicates like setof/3, the behaviour is quite close: the variable X in the answer substitution for the query

?- setof(X, member(X,[a,a,c]), Xs).
Xs = [a, c].

is unbound and we can give it a value to our liking:

?- setof(X, member(X,[a,a,c]), Xs), X = b.
X = b,
Xs = [a, c].

But if we instantiate X earlier, we lose solutions:

?- X = b, setof(X, member(X,[a,a,c]), Xs).
false.

What I would like to happen is that setof binds X, i.e. for the evaluation of setof, X is treated like a fresh variable. To make this a bit more concrete: Is it possible to give an implementation binding_setof such that

?- X = b, binding_setof(X, member(X,[a,a,c]), Xs).
X = b,
Xs = [a, c].

?

P.S.: I am aware of languages like λProlog which were built to solve this issue, but I am interested in a Prolog solution.

Edit: I have tried to solve the problem with library(lambda). I aimed to create an anonymous variable and apply it to \X^setof(X, member(X,[a,c,c], Xs) via call/N. Since the binding of Xs is undone outside of it, the toplevel does not report it. It's still possible to see it by adding format('~w',[Xs]) into the setof but I leave it out for clarity. Again, the call

?- call(\X^setof(X,member(X,[a,c,c]),Xs), _), X=b.
X = b.

succeeds, but

?- X = b, call(\X^setof(X,member(X,[a,c,c]),Xs), _).
false.

fails. This is consistent with the comment in the source that a lambda bound variable must not occur outside the lambda expression.

like image 463
lambda.xy.x Avatar asked May 05 '17 17:05

lambda.xy.x


People also ask

What is postponed binding in Prolog?

Most Prolog dialects will invent a name like "_14" for such unbound variables, to print out if they must. Postponing of binding in Prolog interpreters has the advantage that binding is only done when truly necessary to answer a query. This saves on the computer's overhead cost of binding, for one thing.

What is an example of a variable in Prolog?

A variable in Prolog is a string of letters, digits, and underscores ( _ ) beginning either with a capital letter or with an underscore. Examples: X , Sister , _ , _thing , _y47 , First_name , Z2 The variable _ is used as a "don't-care" variable, when we don't mind what value the variable has.

What is underscore in Prolog?

A single underscore ( _ ) denotes an anonymous variable and means "any term". Unlike other variables, the underscore does not represent the same value everywhere it occurs within a predicate definition. A compound term is composed of an atom called a "functor" and a number of "arguments", which are again terms.

How does Prolog try to satisfy a predicate?

Goals relating to user-defined predicates are evaluated by examining the database of rules and facts loaded by the user. Prolog attempts to satisfy a goal by matching it with the heads of clauses in the database, working from top to bottom. ?-dog(X). might be matched with the fact dog(fido).

What is binding of variables in Prolog?

This process is called binding the variables to values. For example, Prolog can unify the terms cat (A), and cat (mary) by binding variable A to atom mary that means we are giving the value mary to variable A. Prolog can unify person (Kevin, dane) and person (L, S) by binding L and S to atom kevin and dane, respectively.

What is the Prolog operator?

The prolog operator is a symbolic character to work arithmetic, logical, and comparison operations. It is a symbol to take action between two values and objects for a programming language. The prolog operator is an expression to perform two or more than the two values in the arithmetic, logical, comparison, and another format.

How do you unify variables in Prolog?

For example, Prolog can unify the terms cat (A), and cat (mary) by binding variable A to atom mary that means we are giving the value mary to variable A. Prolog can unify person (Kevin, dane) and person (L, S) by binding L and S to atom kevin and dane, respectively. In starting, all variables have no value.

What are the basic components of Prolog programming?

So we will move on to the first step of our Prolog Programming. Knowledge Base − This is one of the fundamental parts of Logic Programming. We will see in detail about the Knowledge Base, and how it helps in logic programming. Facts, Rules and Queries − These are the building blocks of logic programming.


1 Answers

The Golog interpreter contains a solution to locally scoping variables, they use a pi/2 compound and a sub/4 predicate. We can adapt pi/2 to a predicate, their license precludes distributing their interpreter so you'll need to get sub/4 from them (free), linked above.

pi(V, E) :- 
    sub(V, _, E, E1), 
    call(E1).

?- X = d, pi(x, setof(x, member(x, [a, a, c]), Xs)).
X = d,
Xs = [a, c].

It works "most of the time"; I've had issues when working with CLP libraries.

like image 85
Paul Brown Avatar answered Oct 11 '22 19:10

Paul Brown