Escolar Documentos
Profissional Documentos
Cultura Documentos
Prolog & Logic today: Status today for Logic and Logic
Prolog is used for prototypes
programming:
Some larger commercial systems (links to .exe and formal verification of safety critical systems
users often not aware that system is in Prolog). (landnings wheels in air plains, train
Compilation and Interpretation systems (Erlang used signals, medical hardware, etc).
at Ericsson is initially written in Prolog).
Graphic packets, fast execution, good interfaces to
Hardware design (TTL-logic circuit,
other programming languages (calls to/from e.g.
C++/Java).
true=1=5 volt, false=0=0 volt), predicate
logic, temporal logic, modal logic
Used in natural language processing, interpretation,
translation etc.
Good interfaces to web applications, web pages can
use prolog (exv. LPA-Prolog – html – php - mySQL).
Logic History
400 fK The first foundation was built by Aristoteles (axiom, premise, Some Important Notes (Logic)
conclusion “Alla valar är däggdjur" osv).
1930 Subject stabilized [Gödel, Church, Russel, etc.]. (Predicate logic
completely explored Church-Turing-sentence => predicate logic ==
calculable functions) Predicate name is a name standing for both
1960 Automatic Theorem Proving & AI. property and relation.
1965 Resolution and unification [Robinson]. Resolution: an effective
conclusion drawing but rules must be written in a special form. Predicate name is followed by arguments.
1972 The first Prolog-implementation [Colmerauer].
Arity gives the number of arguments of the
1974 Logic programming (SLD-resolution) [Kowalski] (Algoritm = logic +
control, let computers decide how the problem should be solved, i.e., predicate.
execution ord). Arity= 0: propositional logic (no argument) SUNNY
1977 Negation (SLDNF-resolution) [Clark]. (SLDNF completed negative
assertion “A penguin can not fly"). Arity= 1: property man(peter)
1978 Edinburgh Prolog [Warren] (most common syntax today). Arity= 2: relation lenght(peter,1.95)
1981 Japanese announce its 5:e-generation project: robotic, AI, etc, with
logic programming (”main result”: increased use of computers in Arguments are called terms. A term can be
Japan with Kanji, romaji). constant, variable or a structure.
1995 – Constraint Logic Programming (CLP), Agent Programming (logic
bubbles), …
olive_oil(primo_grande).
quality(primo_grande, extra_vergine). Structure
year(primo_grande, 2007).
acidity(primo_grande,high). 3 other ways to write the same thing:
acidity(primo_grande,spain).
color(primo_grande,dark_green). oil(primo_grande, 2007, high, vergine, spain, dark_green).
Do we have a extra vergine olive oil produced after oil(primo_grande, 2007, [acidity, high],[quality, vergin], [origin,
2006? spain], [color, dark_green).
| ?- olive_oil(O), quality(O, extra_vergine), year(O,Y), Y>2006.
oil(primo_grande, 2007, flavor(acidity(high), quality(vergin),
Y = 2007, origin(spain), color(dark_green))).
O = primo_grande ? ;
no
| ?- Warning: easy to misunderstand structure,
there is no function in Prolog!
olive_oil(primo_grande).
olive_oil( primo_grande).
More Rules man(peter
man( peter).).
olive_oil(primo_grande). likes(M, W) :-
:- man(M), olive_oil(W).
olive_oil(W).
quality(primo_grande, extra_vergine).
year(primo_grande, 2007). What does Peter like?
| ?- likes(peter, X).
human(peter). X = primo_grande? ;
no
% For any man M and O it is valid that …. | ?- \+ likes(peter, primo_grande).
% M likes O. No
| ?- \+ likes(lisa, primo_grande).
likes (M, O) :- human(M), olive_oil(O).
Yes
Negation, ”it is not true”, written as ”\+”
Task (assignment)
man(adam). man(kalle). man(lasse).
woman(lisa). woman(eva). woman(mia).
Idea to Solve Assignment)
parent(adam,lasse). % adam is parent of lasse
parent(eva,kalle). parent(eva,lisa). parent(lisa,lasse). cousin(K1, K2):-
parent(kalle,knut). Test i Prolog
parent(P1, K1),
?- cusin(X,Y).
cusin(X,Y).
parent(P2, K2),
father(F,C):- man(F), parent(F,C). X = lasse
siblings(P1,P2).
mother(M,C):- woman(M), parent(M,C).
Y = knut ;
1) Add the fact that kalle has one daughter mia. siblings(S1,S2):- no
parent(X,S1),
2) Draw a family tree.
parent(X,S2),
3) Write a rule to define relation cousin, e.g. find out
who mia’s cousin is. S1 \== S2. % S1 and S2 must not be the same person,
extra: 3a) write a rule to define grand mother. 3b) use % one can not be siblings of himself☺
that rule to show that eva is lasses grand mother.
Apply the relation mother with two arguments (to
show the arity of a predicate one usually write
mother/2).
Lists
List
[ ] : one empty list ?- [a,b,c] = [Head|Tail].
[1, 2, abba] : a list containing 3 elements Head=a
Separate head and tail
Tail=[b,c]
[[11, 12, 13], [21, 22, 23], [31, 32, 33]]
| ?- [a] = [H|T].
:a 3x3 matrix (a list which contains 3
H=a
lists which...).
T=[]
Head of list, tail of list.
These lists are identical
?- [a,b,c] = [a|[b,c]] = [a|[b|[c]]] = [a|[b|[c|[]]]].
Yes
Trace A is B.
| ? - trace, X is sin(4/5), X>2.
Expression B is calculated and unified with
1 1 Call: _59 is sin(4/5) ? <ENTER> variable (or constant) A.
1 1 Exit: 0.71735609 is sin(4/5) ? | ? - Area is 3.14 * 5 * 5.
<ENTER> Area = 78.5 ? <ENTER>
2 1 Call: 0.71735609 > 2 ? <ENTER> yes
| ? - 4 is 4.
2 1 Fail: 0.71735609 > 2 ? <ENTER> • As the right side of ”is” has
yes to be calculable, it must not
1 1 Redo: 0.71735609 is sin(4/5) ? contain unbounded variable.
| ? – X is 2 + M.
<ENTER>
ERROR
1 1 Fail: _59 is sin(4/5) ? <ENTER> | ? – M = 2, X is 2 + M.
”Redo” means that Prolog interpretor backtracks (backs one X=4
or more steps to find alternative solutions).
Example of unification -1
Unification ”=” continue | ?- xx(left, 123, X) = xx(A,B,B).
A = left,
two structures can be unified if B = 123,
X = 123 ? ;
they have same constructor
(name) | ?- cbc = X.
X = cbc ?
They have same arity
| ?- +(5,5) = 5 + 5
Arguments can be unified yes
Failed example:
| ?- X = 5 + 5, X = 10.
no
CWA: All those which are not explicitly stated are not
true. ?- X = z, \+ member(X,[a,b,c]).
yes
Negation as Failure: ?- X = c, \+ member(X,[a,b,c]).
NF: Whatever Prolog program can not derive in finite
no
time is false.
door(s,a).
Search i prolog door(a,b).
door(b,c).
Search, new door e-b
door(c,d).
door(d,e).
door(s,a). d e f door(e,b). %now loop can happen d e f
door(a,b). door(e,f).
door(b,c).
go(A,B):-door(A,B).
door(c,d). s go(A,C):-door(A,X), go(X,C).
s
door(d,e).
c b a c b a
door(e,f). ?- go(s,f).
Stack overflow
?- go(s,f). go2(A,B,V,P):-door(A,B), V = P.
Yes go2(A,B,V,P):-door(A,X), \+ member (X, V), go2(X,B, [X|V], P).
?- go2(s,f, [], P).
P = [a,b,c,d,e,f]
Meta-logic predicate
These are what are required to write a
Prolog contains a uppsättning predicate which is not
strict predicate logic. One can also write program which Prolog program.
change its own code (important property within AI).
?- write(”hello world ”), writeln(5).
hello world 5 The rests is hard work to train yourself
var(X) – fails if X is instantialized. to jump out of imperative
Example:
?− X = Y, var(X).
programming. Think in a declarative
yes way in Prolog programming.
?- Z = hej, var(Z).
no
?− var(sun).
no Read some Prolog tutorials on web!
•
•
integer( X) - if X is bound to an integer.
atomic(X) - if X is bound to a constant End