Professional Documents
Culture Documents
Prolog: The Language of Logic
Prolog: The Language of Logic
• test1(X,Y):- sat(and(not(X),X)).
• test2(X,Y):- sat(and(X,not(Y))).
List Operator [H |T]
• [a,b,c] is a list in Prolog.
• [H|T] = [a,b,c] results in
– H = a i.e. the head of list
– T = [b,c] i.e. the tail of the list.
• membership definition
– member(H,[H|T]). % base case first. Why?
– member(H,[_|T]):- member(H,T).
– Use it.
Member Tests
• ?- member(3,X).
– X = [3| _G109]. % _G.., system generated
variable
– X= [_G11,3| _]. % etc.
• ?- member(X,Y).
– X = _G131, Y= [_G131|, _G321].
Permutation & Insert
• insert(X,L, [X|L]).
• insert(X,[H|T],[H|T1]):- insert(X,T,T1).
• perm([],[]).
• perm([H|T],P):-perm(T,T1),insert(H,T1,P).
DFS
% solve(goal, solution Path)
% s(state, successor-state)
dfs(N,[N]) :- goal(N).
dfs(N,[N|Sol1]):- s(N,N1), dfs(N1,Sol1).
?- dfs(a,N).
N = [a, b, e, i] ;
N = [a, c, f] ;
Limitations
• 2nd order: Can’t ask what is relationship
between heart and lungs?
• Probabilities: What is likelihood of fire
destroying Julian?