-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsyllogisms.pl
123 lines (88 loc) · 2.8 KB
/
syllogisms.pl
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
%% File: syllogisms.pl
%% Name: Antonio Vespoli
%% Date: 25/11/2020
%%
%% This program is a solution to Prolog Assessed Exercise 5 'Syllogisms'
%% The exercise is to develop a parser and meta-interpreter for syllogistic
%% sentences, and use these to build a tool to determine the validity of a
%% syllogistic argument.
%% ---------------------------- Step 1 ---------------------------------------%%
%% opposite(+L, -Opp)
%% Opposite is returned by first matching form and no backtracking is performed
opposite([a,B,is,Article,C], [some,B,is,not,Article,C]).
opposite([every,B,is,Article,C], [some,B,is,not,Article,C]).
opposite([a,B,is,C], [some,B,is,not,C]).
opposite([every,B,is,C], [some,B,is,not,C]).
opposite([some,B,is,not,Article,C], [a,B,is,Article,C]).
opposite([some,B,is,not,C], [a,B,is,C]).
opposite([no,B,is,Article,C], [some,B,is,Article,C]).
opposite([no,B,is,C], [some,B,is,C]).
opposite([some,B,is,C], [no,B,is,C]).
opposite([some,B,is,Article,C], [no,B,is,Article,C]).
%% ---------------------------- Step 2 ---------------------------------------%%
%% syllogism(-Clauses)
% Each DCG rule converts a sentence in the respective list of clauses
article --> [a].
article --> [every].
optionalArticle --> [a].
optionalArticle --> [].
syllogism(Cl) -->
article,
[B,is],
optionalArticle,
[C],
{
Cl1 =.. [C,X],
Cl2 =.. [B,X],
Cl = [(Cl1 :- Cl2)]
}.
syllogism(Cl) -->
[some,B,is,not],
optionalArticle,
[C],
{
Cl1 =.. [some,B,C],
Cl2 =.. [B,Cl1],
Cl3 =.. [C,Cl1],
Cl = [ (Cl2 :- true), ( Cl3 :- true) ]
}.
syllogism(Cl) -->
[no,B,is],
optionalArticle,
[C],
{
Cl1 =.. [B,X],
Cl2 =.. [C,X],
Cl = [ (false :- Cl1,Cl2) ]
}.
syllogism(Cl) -->
[some,B,is],
optionalArticle,
[C],
{
Cl1 =.. [not,C],
Cl2 =.. [some,B,Cl1],
Cl3 =.. [B,Cl2],
Cl4 =.. [C,Cl2],
Cl = [ (Cl3 :- true), (false :- Cl4) ]
}.
%% ---------------------------- Step 3 ---------------------------------------%%
%% translate(+N)
% Searches premises and conclusion, applies opposite, gets corresponding clauses and asserts them
translate(N) :-
p(N,A),
p(N,B),
B \= A,
c(N,C),
opposite(C,OC),
phrase(syllogism(ClauseList1),A),
phrase(syllogism(ClauseList2),B),
phrase(syllogism(ClauseList3),OC),
append(ClauseList1,ClauseList2,ClauseList4),
append(ClauseList4,ClauseList3,ClauseList5),
assertall(N,ClauseList5).
%% ---------------------------- Step 4 ---------------------------------------%%
%% eval(+N, +Calls)
% Recursively evaluates the clause and succeds if each condition in Calls is derivable from the clauses
%% ---------------------------- Step 5 ---------------------------------------%%
%% test(+N)