pizarraLaboProlog.plMarch 13, 2026
.plPro of Prolog
Contra of Prolog
swiplswipl in a terminal
swipl is installed??- halt. stops the session (also Control+D, but it is uglier.)swipl aware of pizarraLaboProlog.pl
swipl pizarraLaboProlog.pl in a terminal, or?- [pizarraLaboProlog]., or?- consult('pizarraLaboProlog.pl').padre?
?- listing(padre).A, …, Z,a, …, z,0, …,9, and _, that begins with a lower-case letter.' and '.atom/1 to check
_._ (the anonymous variable)var/1 to check
( and a sequence of arguments separated by , and ending with ). Arguments can be atoms, variables, numbers or compund terms.The number of arguments that a compund term has is called its arity.
compound/1 or functor/3 to check
.:- and then atoms or compunds terms separated by , (the body) and ending with .A Prolog program is a sequence of clauses.
A query is a sequence of atoms or compund terms (separated by ,) and ending with .
true/0: always truefalse/0: (or fail/0) always false=/2: =(X,Y) (or X = Y) true iff X and Y can be unified\=/2: X \= Y true iff X=Y is false\+: \+ X true when X cannot be proved truewrite(X): writes the term X and is truenl/0: newline, and also truedisplay/1 to look into the internal representation of terms
What is ?- display(X = Y).?
=/2 & proof searchGiven two expressions give values to the variables so that they become the same.
?- f(X,g(X),a) = f(a,Y,Z).
The exception is the variable _: each of its instances is intended to mean a new fresh variable.
?- p(_,2,2) = p(1,Y,_).
?- 3 = 1 + 2.
?- tio(A,B). ?- padre(Y,pedro), write(Y), nl, fail.
?- trace, tio(A,B). ?- trace, padre(Y,pedro), write(Y), nl, fail.
The empty list is []. A list is a sequence (, separated) of compound terms (including lists). E.g. [a, b, [], f(a), g(f(a,X)), [a,b,c], X] is a list.
The first element of a list is the head, the rest the tail ([H | T])
What is the output of (and how you read) the following queries?
[H | T] = [a, b, [], f(a), g(f(a,X)), [a,b,c], X].[H1,H2 | T] = [a, b, [], f(a), g(f(a,X)), [a,b,c], X].[H1,H2,H3 | T] = [a, b].[H | T]=[].[H | T]=[a].Predicate second/2 to obtain the second element of a list? second([_,Elem|_], Elem)
member(?Elem, ?List)
True if Elem is a member of List.
Let’s re-implement it as pert/2:
How pert/2 definition differs from member/2?
append(?List1, ?List2, ?List1AndList2)
List1AndList2 is the concatenation of List1 and List2
Let’s re-implement it as conc/3:
How conc/3 definition differs from append/3?
last(?List, ?Last)
Succeeds when Last is the last element of List.
Let’s re-implement it as ultim/2:
X is the last of the list L if L is the concatenation of some list and [X]
subset(+SubSet,+Set)
True if all elements of SubSet belong to Set as well.
Let’s re-implement it as subcjt/2:
subconjunt/2 below does the same, but also allows to generate subsets in the first element
= is unification (aka mattern matching). So how we do arithmetic?
is/2
X is Expr where Expr is an expression that can be evaluated, if it has variables, all of them are instanciated.
Caution
Expr is X is not allowed. The expression always goes to the right and the variable to assign to the left.
length(?List,?Length)
True if Length represents the number of elements in List.
Let’s re-implement it as mida/2