generated from daytonaio/Sample-Template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.pro
49 lines (40 loc) · 1.48 KB
/
main.pro
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
% Facts about parent relationships
parent(john, mary). % john is parent of mary
parent(john, tom). % john is parent of tom
parent(mary, ann). % mary is parent of ann
parent(mary, pat). % mary is parent of pat
parent(tom, jim). % tom is parent of jim
% Rules
% X is grandparent of Z if X is parent of Y and Y is parent of Z
grandparent(X, Z) :-
parent(X, Y),
parent(Y, Z).
% X is sibling of Y if they share a parent and are not the same person
sibling(X, Y) :-
parent(Z, X),
parent(Z, Y),
X \= Y.
% X is ancestor of Y if X is parent of Y or
% X is parent of someone who is ancestor of Y
ancestor(X, Y) :-
parent(X, Y).
ancestor(X, Y) :-
parent(X, Z),
ancestor(Z, Y).
% Example queries (in interactive mode):
% ?- grandparent(john, ann). % Is john grandparent of ann?
% ?- sibling(ann, pat). % Are ann and pat siblings?
% ?- ancestor(john, jim). % Is john ancestor of jim?
% ?- findall(X, parent(mary, X), Children). % List all mary's children
% Example queries (in script mode):
main :-
write('Test grandparent(john, ann): '),
(grandparent(john, ann) -> write(true) ; write(false)), nl,
write('Test sibling(ann, pat): '),
(sibling(ann, pat) -> write(true) ; write(false)), nl,
write('Test ancestor(john, jim): '),
(ancestor(john, jim) -> write(true) ; write(false)), nl,
write('Mary''s children: '),
findall(X, parent(mary, X), Children),
write(Children), nl.
:- initialization(main).