Why is the x free in the lambda - lambda-calculus

I have following lambda expression:
x λ x. x
This is a function application, but why x is free variable?

x is not free in the lambda, but it is free outside of the lambda. Or, more simply, in
x (\y. y)
x is free but y is not. Variables are free just when there is no enclosing lambda that binds them; since there is no lambda enclosing the x at all, there is certainly no lambda enclosing the x that also binds x.

Related

Free Variable in Prolog

Can anyone explain the concept of free variables in Prolog. Is it similar to anonymous variables ? Or is there a difference. Also could be great if an example is given to explain.
tl;dr:
free is a notion to distinguish universally bound (free in clause notation) from existentially bound variables in setof/3, bagof/3, etc. - some people use free to mean "currently not instantiated" and some use it to denote an output argument that's meant to be instantiated by the predicate but that's not how the standard uses it.
long version:
I will quote the Prolog standard on the definition:
7.1.1.4 Free variables set of a term
The free variables set, FVt of a term T with respect to a
term v is a set of variables defined as the set difference
of the variable set (7.1.1.1) of T and BV where BV is a
set of variables defined as the union of the variable set of
v and the existential variables set (7.1.1.3) of T.
where they explicitly note:
The concept of a free variables set is required when defining
bagof/3 (8.10.2) and setof/3 (8.10.3).
Perhaps as a background: in logic, a free variable is one that is not bound by a quantifier (e.g. x is bound and y is free in ∀x p(x,y) ). A (pure) prolog clause head(X) :- goal1(X), goal2(X). can be read as the logical formula ∀X goal1(X) ∧ goal2(X) → head(X). In practice, as long as we use fresh variables whenever we try to unify a goal with a clause, we can just disregard the universal quantifiers. So for our purposes we can treat X in the clause above as free.
This is all and well until meta-predicates come in: say we are interested in the set of first elements in a list of tuples:
?- setof(X, member(X-Y, [1-2, 2-2, 1-3]), Xs).
Y = 2,
Xs = [1, 2] ;
Y = 3,
Xs = [1].
But we get two solutions: the ones where Y=2 and those where Y=3. What I'd actually want to say is: there exists some Y such that X-Y is a member of the list. The Prolog notation for this pattern is to write Var^Term:
?- setof(X, Y^member(X-Y, [1-2, 2-2, 1-3]), Xs).
Xs = [1, 2].
In the first example, both X and Y are free, in the second example X is free and Y is bound.
If we write this as a formula we get setof(X, ∃Y member(X-Y, [1-2, 2-3, 1-3]), Xs) which is not a first order formula anymore (there is an equivalent first order one but this is where the name meta predicate comes in). Now the problem is that the Var^Term notation is purely syntactical - internally there is only one type of variable. But when we describe the behaviour of setof and friends we need to distinguish between free and existentially bound variables. So unless you are using metapredicates, all of your variables can be considered as free (1).
The Learning Prolog link provided by #Reema Q Khan is a bit fuzzy in its use of free. Just looking at the syntax, X is free in X=5, X is 2 + 3. But when we run this query, as soon as we get to the second goal, X has been instantiated to 5 so we are actually running the query 5 is 2 + 3 (2). What is meant in that context is that we expect is/3 to unify its first argument (often called "output" argument). To make sure this always succeeds we would pass a variable here (even though it's perfectly fine not to do it). The text tries to describe this expectation as "free variable" (3).
(1) ok, formally, anything that looks like Var^Term considers Var existentially bound but without meta-predicates this doesn't matter.
(2) I believe there is a clash in notation that some texts use "X is bound to 5" here, which might increase the confusion.
(3) What the should say is that they expect that the argument has not been instantiated yet but even that does not capture the semantics correctly - Paulo Moura already gave the initial ground example 5 is 3 + 2.
Maybe this can help. (If I have prepared it, I might as well post it! Still hard to read, needs simplification.)
In fact, you need to distinguish whether you talk about the syntax of the program or whether you talk about the runtime state of the program.
The word "variable" takes on slightly different meanings in both cases. In common usage, one does not make a distinction, and the understanding this fluent usage provides is good enough. But for beginners, this may be a hurdle.
In logic, the word "variable" has the meaning of "a symbol selected from the set of variable symbols", and it stands for the possibly infinite set of terms it may take on while fulfilling any constraints given by the logical formulae it participates in. This is not the "variable" used in reasoning about an actual programs.
Free Variable:
"is" is a build-in arithmetic evaluator in Prolog. "X is E" requires X to be free variable and E to be arithmetic expression that is possible to evaluate. E can contain variables but these variables has to be bound to numbers, e.g., "X=5, Y is 2*X" is correct Prolog goal.
More Explanation:
http://kti.ms.mff.cuni.cz/~bartak/prolog.old/learning/LearningProlog11.html
Anonymous Variable:
The name of every anonymous variable is _ .
More Explanation:
https://dobrev.com/help/tut/The_anonymous_variable.html#:~:text=The%20anonymous%20variable%20is%20an,of%20_denotes%20a%20distinct%20variable%20.

Is there a difference between `nonvar(X)` and `\+var(X)`?

Prolog has the following to check current state of computation:
var/1: var(#Term): "True if Term currently is a free variable."
nonvar/1: nonvar(#Term): "True if Term currently is not a free variable."
Is there some difference between nonvar(X) and \+var(X)? Probably not?
P.S.
From the ISO standard:
nonvar/1: nonvar(X) is true iff X is not a member of the set V (7.1.1). (p.69)
var/1 : var(X) is true iff X is a member of the V (7.1.1.). (p.67)
On page 27:
7.1.1. Variable
A variable is a member of a set V (see 6.1.2 a). While a goal is being executed, unification
may cause a variable to become unified with another term.
This should probably be rewritten slightly to bring in the idea of "X is the name of a currently fresh/unconstrained/unrefined variable".
No difference other than possibly on performance on a naive Prolog system (with \+ var(X) being slower than nonvar(X)).
Btw, with a clause such as:
a(X) :- \+ var(X).
The Logtalk linter reports:
* Suspicious call: \+var(A) instead of nonvar(A)

when to stop reduction in lambda caculus

I am confused about how the lambda reduction terminates.For example,the number 2 is written as
\xy.xxy
Why shouldn't we continue to apply beta substitution rule and make it like
\xy.(x)xy
=>\ab.axy
=>\b.yx
=>y
This is clearly wrong.But i don't know why.Can anyone help me?thx a lot!
If you're having trouble making substitutions, I recommend you use fully expanded forms
\xy.xxy
is not
\xy.(x)xy
it is
λx. λy. (x x) y
which can be eta-reduced to
λx. λy. (x x) y
λx. (x x)
λx. x x
which is in head normal form and cannot be further reduced
beta substitution wasn't necessary in this particular problem

beta reduction: correct way to replace bound variables?

say I have the following example of a lambda expression with \x meant to represent lambda x
What would the beta reduction of the following be?
(\x.\x.(x x)) \z.z
My first instinct would have been for this to be
\x.(\z.z \z.z)
But someone I spoke with was of the opinion that the second \x would also be replaced with \z.z
which would mean it is really
\(\z.z).(\z.z \z.z)
Can someone please clarify what the correct approach would be. I can't say I really understand the second approach.
Actually, (\x.\x.(x x)) \z.z is alpha equivalent to (\x.\y.(y y)) \z.z because variables are always bound to the closest lambda abstraction.
This means (\x.\x.(x x)) \z.z is beta equivalent to \x.(x x).
Substitution is never done on lambda abstractions, the \x. part.

How can I avoid passing variables through multiple functions?

While doing GUI programming (in Perl), I often have the case where I have a main package/function x that calls a package/function y, which in turn calls package/function z. But, when I have a variable in x (e.g. a reference to a widget) that I need to access in z, I have to then pass it through y because I don't want to use globals.
Is there a better way to do this? As my program gets bigger, I seem to have more and more variables being passed through packages/functions just so that I can access them in sub packages/functions.
You seem to think this is a bug. It's a feature! It means that if you come back to the program in months or years, you know how and where your data is being used.
Keep in mind that it is not in general empty information to pass a variable a through y to get to z. The reason is that y might instead generate its own version of a; from looking at x or z alone you wouldn't be able to tell with global variables. If you pass it through y, at least you know that y starts out with something generated by x.
Keep in mind that you also might find that you need to refactor your code:
x produces a
x calls y with a
y produces b
y calls z with a and b
could be changed to
x produces a
x calls y
y produces and returns b
x calls z with a and b
in some cases.

Resources