Why is [] = _. true in Prolog? - prolog

I am learning Prolog, and do not understand why ?- [] = _. returns true in SWI-Prolog.
[] means an empty list, and _ means it is not empty, right?
Could someone explain the above logic?

_ is a logical variable, just like X or anything else that starts with an underscore or a capital letter. Free variables (i.e., variables that are not already bound to some term) can be unified with anything. A goal like [] = X says "unify X with []", with the effect that, if this succeeds, every use of X will refer to the term [].
With _ as the variable this is the same as for X, with the exception that _ is the anonymous variable: It cannot be reused, its name does not matter, and different occurrences of _ refer to different variables. So _ can never be bound before encountering the goal [] = _. Thus this unification succeeds, which is why you get the answer true.
_ does not by itself mean "not empty". But you may be confused by its use as a placeholder: L = [_,_,_] means that L is a list of three elements (that we know nothing about). In this sense _ means "there is something here". But it has to be inside the list for this meaning.

Related

how does variable calculating the value in prolog's functor?

The trace processing of the functor 'flatten2' with input list: "[4, [3, [2, [1,[ ] ] ] ] ]" (Screenshot of the tracing process)
The above is the screenshot of me calling functor flatten2 with input list "[4, [3, [2, [1,[ ] ] ] ] ]", and a variable 'X'.
The below is the function that I was tracing, stolen from this question: Flatten a list in Prolog.
So my essential question is that during the recursion, what happens to the variable X and its value? Why is the 'X' showed as '_295' in the "call 1", how does prolog grammar calculating this value?
flatten2([], []) :- !.
flatten2([L|Ls], FlatL) :-
!,
flatten2(L, NewL),
flatten2(Ls, NewLs),
append(NewL, NewLs, FlatL).
flatten2(L, [L]).
X is just a local name for "globally visible stuff" that is held in the "term store".
X designates (or names or denotes) either:
Concrete content: A term, which a generally tree. The variable is called "bound" or "instantiated". The leaves of the tree are either concrete content (atoms, numbers etc.) or cells with no content (see below). The inner nodes are called compound terms. If X designates a term, then the query nonvar(X) succeeds. When X is printed, it "disappears": The content is printed instead.
A cell with no content (I like to call this a "hole"), which is meant to be take up a term eventually. In that case the variable is called "unbound" or "uninstantiated". If X is such an unbound variable, i.e. if it designates a hole, then the query var(X) succeeds. When X is printed, its name is printed.
Confusingly (and I should add, rather sloppily), a "variable name" is commonly also called a "term". That's a Prolog tripmine, these two concepts should be held apart but they are not.
If you write your Prolog clause, you will use nice variable names.
flatten2(L, [L]).
Remember these variables names have no particular significance and they are local to the clause.
When Prolog runs, it has to pull new variable names that are distinct from any other names "out of the hat". These fresh variable names look like _295. Different implementations have different conventions here.
An example where new variable names have to be created to describe a list that contains a member foo somewhere (on at least one place). List templates of increasing length are generated. At each place of the list except the place holding foo (a concrete term), there is a "cell without content"/"hole". To express this, a random new variable name distinct from any other variable name is generated and printed. The variable name is probably directly derived from the hole address.
?- member(foo,L).
L = [foo|_23302] ;
L = [_23300, foo|_24038] ;
L = [_23300, _24036, foo|_24774] ;
L = [_23300, _24036, _24772, foo|_25510] ;
L = [_23300, _24036, _24772, _25508, foo|_26246] ;
L = [_23300, _24036, _24772, _25508, _26244, foo|_26982]

Evaluate term in Prolog and write it

Hi I am learning prolog (using swi-prolog 7 on Mac) but I cannot find enough docs/tutorials to explain how to print out a result of a term evaluation. I tried code below but it always prints out ERROR: prolog/test.pl:2: user:main: false for any arguments.
#!/usr/bin/env swipl
:- initialization(main, main).
fun1(A, B):- A < B.
fun2([A1 | A2]):- A3 is fun1(A1, A2), write(A3).
main(args) :- fun2(args).
How can I write result of fun1 to stdout in SWI-Prolog?
Perhaps this?
fun1(A,B,Value) :-
(
A < B
->
Value = true
;
Value = false
).
fun2(A1,A2) :-
fun1(A1, A2, Value ),
format('Result: ~w~n',[Value]).
Example run:
?- fun2(1,2).
Result: true
true.
In Prolog you have to think of the result of each line as being true or false and then possibly binding a value to a variable or something more complex as state.
The code in the question was returning that the predicate was true or false but not binding a value to a variable or altering state. By adding Value as an additional parameter and then binding a value in the predicate, the value in Value was able to be used for display.
EDIT
Question from OP in comment
I have never seen -> is it documented somewhere? Sorry if it is a noobie question.
No it is not a noobie question, and actually it was wise of you to just ask instead of festering on it.
See: Control Predicates
In particular ->/2 or (:Condition -> :Action) is often used with ;/2 and together they work like an if then else, e.g.
if then else syntax:
NB This is not Prolog syntax but a syntax common in many imperative programming languages.
if (<condition>) then
<when true>
else
<when false>
-> ; syntax:
This is Prolog syntax.
(
<condition>
->
<when true>
;
<when false>
)
EDIT
Question from OP in comment
When I run this code without the init block and main, so just in interactive mode, then it works. When I try to make a script out of it I get the same error ERROR: prolog/test.pl:2: user:main: false
First
main(args) :- fun2(args).
args is a value but needs to be a variable and in Prolog variables by default start with a capital letter.
main(Args) :- fun2(Args).
Next, Args as received in main/1 is a list, but fun2/2 expects two separate parameters. So by deconstructing Args into a list of two items with Args = [A1,A2] the items in the list can be used as individual items to be passed as parameters to fun2/2.
main(Args) :-
Args = [A1,A2],
fun2(A1,A2).
Example run from top level.
?- main([1,2]).
Result: true
true.
I leave it as an exercise to check if this works as needed from the command line.

Binding vs Assignment

I've read a number of articles on the difference between assignment and binding, but it hasn't clicked yet (specifically in the context of an imperative language vs one without mutation).
I asked in IRC, and someone mentioned these 2 examples illustrate the difference, but then I had to go and I didn't see the full explanation.
Can someone please explain how/why this works in a detailed way, to help illustrate the difference?
Ruby
x = 1; f = lambda { x }; x = 2; f.call
#=> 2
Elixir
x = 1; f = fn -> x end; x = 2; f.()
#=> 1
I've heard this explanation before and it seems pretty good:
You can think of binding as a label on a suitcase, and assignment as a
suitcase.
In other languages, where you have assignment, it is more like putting a value in a suitcase. You actually change value that is in the suitcase and put in a different value.
If you have a suitcase with a value in it, in Elixir, you put a label on it. You can change the label, but the value in the suitcase is still the same.
So, for example with:
iex(1)> x = 1
iex(2)> f = fn -> x end
iex(3)> x = 2
iex(4)> f.()
1
You have a suitcase with 1 in it and you label it x.
Then you say, "Here, Mr. Function, I want you to tell me what is in this suitcase when I call you."
Then, you take the label off of the suitcase with 1 in it and put it on another suitcase with 2 in it.
Then you say "Hey, Mr. Function, what is in that suitcase?"
He will say "1", because the suitcase hasn't changed. Although, you have taken your label off of it and put it on a different suitcase.
After a while, I came up with the answer that is probably the best explanation of the difference between “binding” and “assignment”; it has nothing in common with what I have written in another answer, hence it’s posted as a separate answer.
In any functional language, where everything is immutable, there is no meaningful difference between terms “binding” and “assignment.” One might call it either way; the common pattern is to use the word “binding,“ explicitly denoting that it’s a value bound to a variable. In Erlang, for instance, one can not rebound a variable. In Elixir this is possible (why, for God’s sake, José, what for?)
Consider the following example in Elixir:
iex> x = 1
iex> 1 = x
The above is perfectly valid Elixir code. It is evident, one cannot assign anything to one. It is neither assignment nor binding. It is matching. That is how = is treated in Elixir (and in Erlang): a = b fails if both are bound to different values; it returns RHO if they match; it binds LHO to RHO if LHO is not bound yet.
In Ruby it differs. There is a significant difference between assignment (copying the content,) and binding (making a reference.)
Elixir vs Ruby might not be the best contrast for this. In Elixir, we can readily "re-assign" the value of a previously assigned named variable. The two anonymous-function examples you provided demonstrate the difference in how the two languages assign local variables in them. In Ruby, the variable, meaning the memory reference, is assigned, which is why when we change it, the anonymous function returns the current value stored in that memory-reference. While in Elixir, the value of the variable at the time the anonymous function is defined (rather than the memory reference) is copied and stored as the local variable.
In Erlang, Elixir's "parent" language, however, variables as a rule are "bound." Once you've declared the value for the variable named X, you are not allowed to alter it for the remainder of the program and any needed alterations would need to be stored in new named variables. (There is a way to reassign a named variable in Erlang but it is not the custom.)
Binding refers to particular concept used in expression-based languages that may seem foreign if you're used to statement-based languages. I'll use an ML-style example to demonstrate:
let x = 3 in
let y = 5 in
x + y
val it : int = 8
The let... in syntax used here demonstrates that the binding let x = 3 is scoped only to the expression following the in. Likewise, the binding let y = 5 is only scoped to the expression x + y, such that, if we consider another example:
let x = 3 in
let f () =
x + 5
let x = 4 in
f()
val it : int = 8
The result is still 8, even though we have the binding let x = 4 above the call to f(). This is because f itself was bound in the scope of the binding let x = 3.
Assignment in statement-based languages is different, because the variables being assigned are not scoped to a particular expression, they are effectively 'global' for whatever block of code they're in, so reassigning the value of a variable changes the result of an evaluation that uses the same variable.
The easiest way to understand the difference, would be to compare the AST that is used by the language interpreter/compiler to produce machine-/byte-code.
Let’s start with ruby. Ruby does not provide the AST viewer out of the box, so I will use RubyParser gem for that:
> require 'ruby_parser'
> RubyParser.new.parse("x = 1; f = -> {x}; x = 2; f.()").inspect
#=> "s(:block, s(:lasgn, :x, s(:lit, 1)),
# s(:lasgn, :f, s(:iter, s(:call, nil, :lambda), 0, s(:lvar, :x))),
# s(:lasgn, :x, s(:lit, 2)), s(:call, s(:lvar, :f), :call))"
The thing we are looking for is the latest node in the second line: there is x variable inside the proc. In other words, ruby expects the bound variable there, named x. At the time the proc is evaluated, x has a value of 2. Hence the the proc returns 2.
Let’s now check Elixir.
iex|1 ▶ quote do
...|1 ▶ x = 1
...|1 ▶ f = fn -> x end
...|1 ▶ x = 2
...|1 ▶ f.()
...|1 ▶ end
#⇒ {:__block__, [],
# [
# {:=, [], [{:x, [], Elixir}, 1]},
# {:=, [], [{:f, [], Elixir}, {:fn, [], [{:->, [], [[], {:x, [], Elixir}]}]}]},
# {:=, [], [{:x, [], Elixir}, 2]},
# {{:., [], [{:f, [], Elixir}]}, [], []}
# ]}
Last node in the second line is ours. It still contains x, but during a compilation stage this x will be evaluated to it’s currently assigned value. That said, fn -> not_x end will result in compilation error, while in ruby there could be literally anything inside a proc body, since it’ll be evaluated when called.
In other words, Ruby uses a current caller’s context to evaluate proc, while Elixir uses a closure. It grabs the context it encountered the function definition and uses it to resolve all the local variables.

Getting specific data from Fact

I know that the Title ,does not mean anything , however , how to get 'calculas1' word and put it in list from this fact using prolog :
hasMark(calculas1 , 78 , 110).
In Prolog, you can query a fact this way:
hasMark(Subject, X, Y).
Which, in the case of your example, would yield:
Subject = calculus1
X = 78
Y = 110
The easiest way to collect them would be using the builtin, findall:
findall(Subject, hasMark(Subject, _, _), Subjects).
This will yield:
Subjects = [calculus1, ...]
The ... are other subjects, if you have them. Notice that I used _ for two of the parameters. That means I don't care what those values are. Any variable that starts with a _, including that character by itself, means "don't care".
You can also use a predicate like this:
add_subject( MyList, [Subject | MyList] ) :-
hasMark(Subject, _, _).
So you can query:
add_subject( [], List ).
And get:
List = [calculus1].
Note that a naming convention with underscores and lower case is more commonly used in Prolog than "camelCase". So it would be preferred to name your facts has_mark.

variable is only used once

I am using ancient Turbo Prolog since it is included in our curriculum. Why is this program not working?
domains
disease, indication = symbol
Patient = string
Fe,Ra,He,Ch,Vo,Ru = char
predicates
hypothesis(Patient,disease)
symptom(Patient,indication,char)
response(char)
go
clauses
go:-
write("What is patient's name?"),
readln(Patient),
symptom(Patient,fever,Fe),
symptom(Patient,rash,Ra),
symptom(Patient,head_ache,He),
symptom(Patient,chills,Ch),
symptom(Patient,runny_nose,Ru),
symptom(Patient,head_ache,He),
symptom(Patient,vomit,Vo),
hypothesis(Patient,Disease),
write(Patient," probably has ", Disease , "."),nl.
go:-
write("Sorry unable to seem to be diagnose disease"),nl.
symptom(Patient,Fever,Feedback) :-
Write("Does " , Patient , " have " , Fever , "(y/n) ?"),
response(Reply),
Feedback = Reply.
hypothesis(Patient, chicken_pox) :-
Fe = Ra = He = Ch = 'y'.
hypothesis(Patient, caner) :-
Ru = Ra = He = Vo = 'y'.
hypothesis(Patient, measles) :-
Vo = Ra = Ch = Fe = He = 'y'.
response(Reply):-
readchar(Reply),
write(Reply),nl.
I get the warning variable is only used at all lines which contains symtoms. Isn't the parameter passing call by reference? When i pass Fe to symptoms the value should be copied to Fe and when i compare it in hypothesis it should work accordingly. = operator in Turbo Prolog works very strangely. When it is not bound to any variable, the statement a = 3 will assign 3 to a and when a already contains a value a = 5 will check whether a's value is 5 or not.
Kindly help me why is the program not working?
Thanks in advance :)
The trouble is not with your symptoms/3 predicate, they will bind (unify) their 3rd argument to what response/1 gives. The problem is that these values are never passed into your hypothesis/2 procedure in go/0 so they are never used to try and generate a hypothesis. Prolog doesn't have global variables so you have to explicitly pass all values, though you can keep things in the database which can easily cause problems if you are not careful.
This means that in hypothesis/2 you are not testing the values of Fe, Ra, He, etc but binding local variables with the same names. This is also why you get warnings of the variables only being referenced once, you bind them but never use them. Remember they are local, all variables are local to the clause in which they occur.
All this applies to standard prolog, I have never used Turbo Prolog.

Resources