8- Queens
queens(Board) :- permutation([1, 2, 3, 4, 5, 6, 7, 8], Board), safe(Board).
safe([]).
safe([Queen|Others]) :- safe(Others), noattack(Queen, Others, 1).
noattack(_, [], _).
noattack(Queen, [First|Others], Dist) :-
abs(Queen - First) =\= Dist,
Dist1 is Dist + 1,
noattack(Queen, Others, Dist1).
Input:
queens(X).
WaterJug Problem:
move([X, Y], [0, Y2]) :- X > 0, Y2 is X + Y, Y2 =< 4.
move([X, Y], [X2, 0]) :- Y > 0, X2 is X + Y, X2 =< 3.
move([X, Y], [0, Y]).
move([X, Y], [X, 0]).
move([X, Y], [3, Y]).
move([X, Y], [X, 4]).
solution([0, 2], _).
solution(State, History) :-
move(State, NewState),
write(NewState),nl,
\+ member(NewState, History),
solution(NewState, [NewState|History]).
Input:
solution([0,0],[]).
BFS:
s(a,b).
s(a,c).
s(b,d).
s(b,e).
s(c,f).
s(c,g).
start(Parent,Sol) :-
queue([Parent],[],Sol).
queue([],Sol,Sol).
queue([Parent|Q0],I0,Sol) :-
append(I0,[Parent],I1),
findall(Child,s(Parent,Child),Children),
append(Q0,Children,Q),
queue(Q,I1,Sol).
Input:
start(a,X).
DFS:
edge(a,b).
edge(a,c).
edge(b,d).
edge(b,e).
edge(c,f).
dfs(Node, Target, Path) :-
dfs_visit(Node, Target, [Node], Path).
dfs_visit(Node, Target, Visited, [Target|Visited]) :-
Node = Target.
dfs_visit(Node, Target, Visited, Path) :-
edge(Node, Next),
\+ member(Next, Visited),
dfs_visit(Next, Target, [Next|Visited], Path).
Input:
dfs(a,X,_).
8 puzzle problem:
move([T1, T2, T3, T4, T5, T6, T7, T8, 0], [T1, T2, T3, T4, T5, T6, T7, 0, T8]).
move([T1, T2, T3, T4, T5, T6, T7, T8, 0], [T1, T2, T3, T4, T5, T6, 0, T8, T7]).
move([T1, T2, T3, T4, T5, T6, T7, T8, 0], [T1, T2, T3, T4, T5, 0, T7, T8, T6]).
move([T1, T2, T3, T4, T5, T6, T7, T8, 0], [T1, T2, T3, T4, 0, T6, T7, T8, T5]).
move([T1, T2, T3, T4, T5, T6, T7, T8, 0], [T1, T2, T3, 0, T5, T6, T7, T8, T4]).
move([T1, T2, T3, T4, T5, T6, T7, T8, 0], [T1, T2, 0, T4, T5, T6, T7, T8, T3]).
move([T1, T2, T3, T4, T5, T6, T7, T8, 0], [T1, 0, T3, T4, T5, T6, T7, T8, T2]).
move([T1, T2, T3, T4, T5, T6, T7, T8, 0], [0, T2, T3, T4, T5, T6, T7, T8, T1]).
solution([1, 2, 3, 4, 5, 6, 7, 8, 0], _).
solution(State, History) :-
move(State, New),
write(New),nl,
\+ member(New, History),
solution(New, [New|History]).
Input:
solution([1,2,3,4,5,6,7,8,0],[]).
Travelling Salesman:
city(1, 2, 3).
city(2, 1, 4).
city(2, 3, 1).
city(3, 2, 5).
city(3, 4, 2).
city(4, 3, 1).
route(Cities, Distance) :-
findall(D, (permutation(Cities, Perm), dist(Perm, D)), Distances),
min(Distance, Distances).
dist([_], 0).
dist([A,B|T], D) :-
city(A, B, D1),
dist([B|T], D2),
D is D1 + D2.
min(X, [X]).
min(M, [H|T]) :- min(M, T), M =< H.
min(H, [H|_]).
Input:
route([1,2,3,4], X).
Facts:
(Write any program where facts and logics are used, two example programs are given. Write a
program similar to those examples, also give multiple queries/inputs to the program. Another
example can be found in Lab Record.)
EG1:
driving(sam,car).
driving(jack,bike).
driving(john,car).
driving(john,bike).
racing(sam,john,car).
convicts(X,Y):-
driving(X,Z),
driving(Y,Z),
racing(X,Y,Z).
Input (Multiple)
driving(sam,X).
driving(X,car).
convicts(X,Y).
EG2:
kills(cipher,42).
kills(pj,27).
kills(pixy,40).
ace(X):-
kills(X,Y),Y>30.
Input (Multiple)
Kills(cipher,X).
ace(pixy).
ace(X).
Expert System Medical Diagnosis
% Define symptoms and their corresponding diseases
symptom(fever, flu).
symptom(cough, flu).
symptom(headache, flu).
symptom(runny_nose, flu).
symptom(sore_throat, flu).
symptom(stomach_pain, food_poisoning).
symptom(diarrhea, food_poisoning).
symptom(vomiting, food_poisoning).
% Find the symptoms patient has, and based on symptoms find disease
diagnosis(Patient, Disease) :-
symptoms(Patient, Symptoms),
diagnose(Symptoms,Disease).
diagnose([Symptom|_],Disease):-
findall(X,symptom(Symptom,X),Disease).
% Define the symptoms of a patient
symptoms(Patient, Symptoms) :-
findall(Symptom, (symptom(Symptom, _), has_symptom(Patient, Symptom)), Symptoms).
% Define the symptoms a patient has
has_symptom(patient1, fever).
has_symptom(patient1, cough).
has_symptom(patient1, headache).
has_symptom(patient1, runny_nose).
Input:
diagnosis(patient1, Disease).