Post on 29-Jan-2020
Logic Programming
Logic Programming
fac(1,1) fac(n,n×m)
fac(n-1,m)
ex) factorial program query
?- fac(5,120).yes?- fac(5,X).X=120
cf) in procedural programming
x=1;for (i=1;i<=n;i++) x=x*i;
프로그램 = 추론규칙
구하고자 하는 것이 무엇인지를 명시
어떻게 구하는지를 명시
예) 타입시스템
•타입시스템 (추론규칙)
•올바른 타입을 가지는 프로그램이란 무엇인가?
•타입 유추 알고리즘
•입력 프로그램의 타입을 어떻게 찾는가?
Γ � E : τ
Γ � n : ι
Γ(x) = τ
Γ � x : τ
Γ + x : τ1 � E : τ2
Γ � λx.E : τ1 → τ2
Γ � E1 : τ1 → τ2 Γ � E2 : τ1
Γ � E1 E2 : τ2
Γ � E1 : ι Γ � E2 : ιΓ � E1+E2 : ι
E → n | x | λx.E | E E | E + E
v → n | λx.EC → [] | C E | v C | C + E | v + C
∅ � ff1 f2
f1 ∧ f2
T· · ·
let fac = rec f λn.if n = 0 then 1 else n ∗ (f (n− 1)) in (fac 2)
σ � 1 ⇒ 1· · ·
σ{x �→ 1} � x + 2 ⇒ 3σ � let x = 1 in x + 2 ⇒ 3
σ � λx.x ⇒ �λx.x, σ� σ � 1 ⇒ 1 σ{x �→ 1} � x ⇒ 1σ � (λx.x) 1 ⇒ 1
σ � λx.z x ⇒ �λx.z x, σ� σ � λy.y ⇒ �λy.y,σ�
σ{x �→ �λy.y,σ�} � z ⇒ �λk.k, σ��σ{x �→ �λy.y,σ�} � x ⇒ �λy.y,σ�σ�{k �→ �λy.y,σ� � k ⇒ �λy.y,σ�}σ{x �→ �λy.y,σ�} � z x ⇒ �λy.y,σ�
σ(= {z �→ �λk.k, σ��}) � (λx.z x) (λy.y) ⇒ �λy.y,σ�
σ � λx.E ⇒ λx.E
σ � E1 ⇒ λx.E� σ � E2 ⇒ v σ{x �→ v} � E� ⇒ v�
σ � E1 E2 ⇒ v�
λx.E
E1 E2
S = {x1 �→ Y1, . . . , xn �→ Yn}S E
1
Introduction to Prolog
• Programming in logic
• The first and most popular logic programming language
• First-order predicate logic: Turing complete
• Apps) natural language processing, theorem proving, expert systems, games, web programming, etc
Defining Relations by Facts
parent(tom,bob).parent(pam,bob).parent(tom,liz).parent(bob,ann).parent(bob,pat).parent(pat,jim).
pam tom
bob
ann pat
jim
liz
Defining Relations by Facts
pam tom
bob
ann pat
jim
liz
?- parent(bob,pat).yes
?- parent(liz,pat).no
?- parent(tom,ben).no
Defining Relations by Facts
pam tom
bob
ann pat
jim
liz
?- parent(X,pat).X=tom
?- parent(bob,X).X=annX=pat
?- parent(X,Y).X=pam Y=bobX=tom Y=bob...
Defining Relations by Facts
pam tom
bob
ann pat
jim
liz
?- parent(Y,jim), parent(X,Y).X=bob Y=pat
?- parent(X,Y), parent(Y,jim).X=bob Y=pat
?- parent(tom,X), parent(X,Y).X=bob Y=ann
?- parent(X,ann), parent(X,pat).X=bob
Defining Relations by Rules
female(pam).male(tom).male(bob).female(liz).female(pat).female(ann).male(jim).
pam tom
bob
ann pat
jim
liz
female male
female female
femalemale
male
Defining Relations by Rules
offspring(Y,X):-parent(X,Y).
pam tom
bob
ann pat
jim
liz
female male
female female
femalemale
male
offspring(Y,X)
parent(X,Y)
inference rule
in prolog
?- offspring(liz,tom).yes
offspring(liz,tom):-parent(tom,liz).
Defining Relations by Rules
grandparent(X,Z):- parent(X,Y), parent(Y,Z).
pam tom
bob
ann pat
jim
liz
female male
female female
femalemale
male
grandparent(X,Z)
parent(X,Y) parent(Y,Z)
inference rule
in prolog
Defining Relations by Rules
sister(X,Y):- parent(Z,X), parent(Z,Y), female(X).
pam tom
bob
ann pat
jim
liz
female male
female female
femalemale
male
sister(X,Y)
parent(Z,X) parent(Z,Y) female(X)
inference rule
in prolog
Defining Relations by Rules
?- sister(ann,pat).yes
?- sister(X,pat).X=annX=pat
pam tom
bob
ann pat
jim
liz
female male
female female
femalemale
male
sister(X,Y):- parent(Z,X), parent(Z,Y), female(X).
Defining Relations by Rules
?- sister(ann,pat).yes
?- sister(X,pat).X=ann
pam tom
bob
ann pat
jim
liz
female male
female female
femalemale
male
sister(X,Y):- parent(Z,X), parent(Z,Y), female(X), different(X,Y).
Recursive Rules
predecessor(X,Z):- parent(X,Z).
predecessor(X,Z):- parent(X,Y), predecessor(Y,Z).
predecessor(X,Z)
parent(X,Y) predecessor(Y,Z)
inference rule
in prolog
predecessor(X,Z)
parent(X,Z)
?- predecessor(pam,X).X=bobX=annX=patX=jim
How prolog answers
parent(tom,bob).parent(pam,bob).parent(tom,liz).parent(bob,ann).parent(bob,pat).parent(pat,jim).
predecessor(X,Z):- parent(X,Z).
predecessor(X,Z):- parent(X,Y), predecessor(Y,Z).
predecessor(tom,pat)
parent(tom,pat)
no
parent(tom,Y)predecessor(Y,pat)
predecessor(bob,pat)
Y=bob
parent(bob,pat)
yes
Declarative/Procedural Meanings
• Declarative meanings
• What is the output of the program?
• Procedural meanings
• How is this output obtained?
Data objects in Prolog
data objects (=terms)
simple objects structures
constants
atoms numbers
variables
tom,bob,nil,x_,x25,... 1, 1313, 0, -97, 3.14,...
X,Y,Z,...
Structured Objects
• Structured object = tree
• Objects that have several components.
date(1, may, 2011)
functor arguments
date
1 may 2011
point(1,1)seg(point(1,1),point(2,3))triangle(point(4,2),point(6,4),point(7,1))
(a+b)*(c-5) = *(+(a,b), -(c,5))
Matching• Two terms match if they are unifiable
• Prolog finds the most general unifier
?- date(D, M, 2011) = date(D1, may, Y1).D = D1M = mayY1 = 2001
?- date(D, M, 2011) = date(D1, may, 2144)fail
D = 1D = 1M = mayY1 = 2001
Prolog and Logic
• Syntax: first-order predicate logic, clause form
• Procedural meaning: resolution principle for mechanical theorem proving (Robinson’65)
• Matching = unification in logic
• occur check
?- X = f(X).X = f(X) or X = f(f(f(f(f(f(f(f(f(...
Lists[ann, tennis, tom, skiing]= .(ann, .(tennis, .(tom, .(skiing, []))))
[ann] = .(ann,[])
?- Hobbies1 = .(tennis, .(music, [])), Hobbies2 = [skiing, food], L = [ann, Hobbies1, tom, Hobbies2].Hobbies1 = [tennis, music]Hobbies2 = [skiing, food]L = [ann,[tennis,music],tom,[skiing,food]]
[a,b,c] = [a|[b,c]] = [a,b,|[c]] = [a,b,c|[]]
List operations
member(X,L) : X is a member of list L
conc(L1,L2,L3) : L3 is the concatenation of L1 and L2
add(X,L1,L2) : add(X,L,[X|L)
del(X,L,L1) : L1 is equal to L with X removed
sublist(S,L) : S is a sublist of L
permutation (L,P) : P is a permutation of L
Membership
member(X,L) is true if X occurs in L
?- member(b,[a,b,c]).yes
?- member(b,[a,[b,c]]).no
?- member([b,c],[a,[b,c]]).yes
member(X,[X|Tail]).member(X,[Head|Tail]) :- member(X,Tail).
Concatenationconc(L1,L2,L3) is true if L1 and L2
are two lists and L3 is their concatenation
?- conc([a,b],[c,d],[a,b,c,d]).yes
?- conc([a,b,c],[1,2,3],L).L = [a,b,c,1,2,3]
conc([],L,L).conc([X|L1],L2,[X|L3]) :- conc(L1,L2,L3).
X L1 L2
L3
[X|L3]
Other uses of conc
?- conc(L1,L2,[a,b,c]).L1 = []L2 = [a,b,c];
L1 = [a]L2 = [b,c];
L1 = [a,b]L2 = [c];
L1 = [a,b,c]L2 = []
decompose a given list into two lists
Other uses of conc
?- conc(Before,[may|After], [jan,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec]).Before = [jan,feb,mar,apr]After = [jun,jul,aug,sep,oct,nov,dec]
?- conc(_,[Pred,may,Succ|_], [jan,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec]).Pred = aprSucc = jun
?- L1 = [a,b,z,z,c,z,z,z,d,e], conc(L2,[zzz,|_],L1).L2 = [a,b,z,z,c]L1 = [a,b,z,z,c,z,z,z,d,e]
look for a certain pattern in a list
Other uses of conc
member(X,L) is true if X occurs in L
member(X,[X|Tail]).member(X,[Head|Tail]) :- member(X,Tail).
member(X,L) :- conc(_,[X|_],L).
vs.
Deleting an item del(X,L,L1) is true if L1 is equal to L
with the item X removed
del(X,[X|Tail],Tail).del(X,[Y|Tail],[Y,Tail]) :- del(X,Tail,Tail1)
?- del(a,[a,b,a,a],L).L = [b,a,a];L = [a,b,a];L = [a,b,a];
?- del(a,L,[1,2,3]).L = [a,1,2,3];L = [1,a,2,3];L = [1,2,a,3];L = [1,2,3,a];
(Quiz1) Inserting an itemDefine insert(X,L,L1)
: insert(X,L,L1) is true if L1 is equal to L with the item X inserted (at any place)
?- insert(a,[b,c],[a,b,c]).yes
?- insert(a,[b,c],L).L = [a,b,c];L = [b,a,c];L = [b,c,a];
?- insert(a,L,[a,b,c]).L = [b,c]
Sublist
sublist(S,L) is true if S is a sublist of L
?- sublist([c,d,e],[a,b,c,d,e]).yes
?- sublist([c,e],[a,b,c,d,e,f]).no
L1 L2
L
Ssublist(S,L):- sublist(L1,L3,L), sublist(S,L2,L3).
L3
Sublist
?- sublist (S,[a,b,c]).S = [];S = [a];S = [a,b];S = [a,b,c];S = [];S = [b];S = [b,c];S = [];S = [c];S = [];
Permutations
permutation([],[]).permutation([X|L],P):- permutation(L,P1), insert(X,P1,P).
?- permutation ([a,b,c],P).P = [a,b,c];P = [b,c,a];P = [b,c,a];P = [a,c,b];P = [c,a,b];
permutation(L,P) is true if P is a permutation of L
(Quiz2). Reverse
Define the relation
reverse(List,ReversedList)
that reverses list.
?- reverse([a,b,c,d],[d,c,b,a]).yes
(Quiz3). PalindromeDefine the predicate
palindrome(List)
that is true if List is a palindrome. A list is palindrome if it reads the same in the forward and in the backward direction.
?- palindrome([m,a,d,a,m]).yes
?- permutation([a,a,b,b,c],L), palindrome(L).L = [a,b,c,b,a];L = [b,a,c,a,b];...
The eight queens problem
1 2 3 4 5 6 7 8
1 2
3 4
5 6
7 8
Board templateX/Y: a queen sitting at (X,Y)
1 2 3 4 5 6 7 8
1 2
3 4
5 6
7 8
[1/2, 2/4, 3/6,4/8,5/3,6/1,7/7,8/5]
[1/Y1, 2/Y2, 3/Y3,4/Y4,5/Y5,6/Y6,7/Y7,8/Y8]
Solutionsolution(S) is true if position S is safe
solution([X/Y|Others]):- solution(Others), member(Y,[1,2,3,4,5,6,7,8]), noattack(X/Y,Others).
noattack(_,[]).noattack(X/Y,[X1/Y1|Others]):- Y =\= Y1, Y1-Y =\= X1-X, Y1-Y =\= X-X1, noattack(X/Y,Others).
template([1/Y1,2/Y2,3/Y3,4/Y4,5/Y5,6/Y6,7/Y7,8/Y8]).
?- template(S), solution(S).S = [1/4,2/2,3/7,4/3,5/6,6/8,7/5,8/1]...