Skip to content

Commit a0b3ec9

Browse files
committed
added support for negation and basic relational and arithmetic operators
1 parent c2b8eed commit a0b3ec9

File tree

4 files changed

+178
-48
lines changed

4 files changed

+178
-48
lines changed

parser.pl

+38-3
Original file line numberDiff line numberDiff line change
@@ -89,27 +89,58 @@
8989
atom_ast(H),
9090
[punct(":")],
9191
[punct("-")],
92-
atom_ast(B),
92+
sep,
93+
literal(B),
94+
sep,
9395
body_ast(T),
9496
[punct(".")].
9597

9698
clause(cl(1,H,[B|T])) -->
9799
atom_ast(H),
98100
[punct(":")],
99101
[punct("-")],
100-
atom_ast(B),
102+
sep,
103+
literal(B),
101104
body_ast(T),
102105
[punct(".")].
103106

104107
body_ast([]) --> [].
105108
body_ast([Atom|T]) -->
109+
sep,
106110
[punct(",")],
107-
atom_ast(Atom),
111+
sep,
112+
literal(Atom),
113+
sep,
108114
body_ast(T).
109115

116+
sep -->
117+
{true}.
118+
sep -->
119+
[cntrl("\n")].
120+
121+
literal(Atom) -->
122+
atom_ast(Atom).
123+
literal(not(Atom)) -->
124+
[word("not")],
125+
atom_ast(Atom).
126+
110127
atom_ast(Atom) -->
111128
[word(X)],
112129
{atom_codes(Atom,X)}.
130+
atom_ast(Atom) -->
131+
[word(X)],
132+
[punct(A)],
133+
[word(Y)],
134+
{word_to_term(X,Atom1),word_to_term(Y,Atom2),atom_codes(Op,A),
135+
Atom =.. [(Op)|[Atom1,Atom2]]}.
136+
atom_ast(Atom) -->
137+
[word(X)],
138+
[punct(A)],
139+
[punct(B)],
140+
[word(Y)],
141+
{word_to_term(X,Atom1),word_to_term(Y,Atom2),
142+
string_concat(A,B,C),atom_codes(Op,C),
143+
Atom =.. [(Op)|[Atom1,Atom2]]}.
113144
atom_ast(Atom) -->
114145
[word(P)],
115146
[punct("(")],
@@ -126,6 +157,7 @@
126157
{word_to_term(H,Arg)},
127158
[punct(",")],
128159
args_ast(T).
160+
%missing: lists and functions in general...
129161

130162
comment_ast([]) --> [].
131163
comment_ast([H|T]) -->
@@ -134,6 +166,9 @@
134166
comment_ast([H|T]) -->
135167
[punct(H)],
136168
comment_ast(T).
169+
comment_ast([H|T]) -->
170+
[other(H)],
171+
comment_ast(T).
137172

138173
pred_ast([]) --> [].
139174
pred_ast([(Pred,N)|R]) -->

temp.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
[[call(0,ancestor(anna,fanny),"anna is the ancestor of fanny"),call(1,parent(anna,fanny),"anna is the parent of fanny"),call(2,mother(anna,fanny),"anna is the mother of fanny")],[call(0,ancestor(daniel,fanny),"daniel is the ancestor of fanny"),call(1,parent(daniel,fanny),"daniel is the parent of fanny"),call(2,mother(daniel,fanny),"daniel is the mother of fanny")],[call(0,ancestor(tim,fanny),"tim is the ancestor of fanny"),call(1,parent(tim,anna),"tim is the parent of anna"),call(2,mother(tim,anna),"tim is the mother of anna"),call(1,ancestor(anna,fanny),"anna is the ancestor of fanny"),call(2,parent(anna,fanny),"anna is the parent of fanny"),call(3,mother(anna,fanny),"anna is the mother of fanny")],[call(0,ancestor(celine,fanny),"celine is the ancestor of fanny"),call(1,parent(celine,daniel),"celine is the parent of daniel"),call(2,father(celine,daniel),"celine is the father of daniel"),call(1,ancestor(daniel,fanny),"daniel is the ancestor of fanny"),call(2,parent(daniel,fanny),"daniel is the parent of fanny"),call(3,mother(daniel,fanny),"daniel is the mother of fanny")]]
1+
[[call(0,intraocularLens,"intraocular lens are recommended"),call(1,correctiveLens,"needs corrective lenses"),call(2,shortSighted,"is short sighted"),not_call(2,laserSurgery,"did not undergo laser surgery"),call(3,shortSighted,"is short sighted"),call(3,tightOnMoney,"is tight on money"),call(4,student,"is a student"),not_call(4,richParents,"has not rich parents"),not_call(1,glasses,"does not wear glasses"),call(2,correctiveLens,"needs corrective lenses"),call(3,shortSighted,"is short sighted"),not_call(3,laserSurgery,"did not undergo laser surgery"),call(4,shortSighted,"is short sighted"),call(4,tightOnMoney,"is tight on money"),call(5,student,"is a student"),not_call(5,richParents,"has not rich parents"),call(2,caresPracticality,"cares about practicality"),call(3,likesSports,"likes sports"),not_call(1,contactLens,"does not wear contact lenses"),call(2,correctiveLens,"needs corrective lenses"),call(3,shortSighted,"is short sighted"),not_call(3,laserSurgery,"did not undergo laser surgery"),call(4,shortSighted,"is short sighted"),call(4,tightOnMoney,"is tight on money"),call(5,student,"is a student"),not_call(5,richParents,"has not rich parents"),call(2,afraidToTouchEyes,"is afraid to touch eyes")]]

tracer.pl

+68-42
Original file line numberDiff line numberDiff line change
@@ -5,47 +5,7 @@
55
:- dynamic fresh_vars/1.
66
:- dynamic verbose/0.
77

8-
main :-
9-
prolog_flag(argv,ArgV),
10-
get_options(ArgV,Options,_RemArgV), !,
11-
%(member(verbose,Options) -> (assert_verbose, print(Options),nl)
12-
% ; (member(very_verbose,Options) -> (assert_verbose,assert_very_verbose, print(Options),nl) ; true)),
13-
((member(format(F),Options), assert(cli_format(F)),fail)
14-
; true),
15-
((member(file(File),Options), assert(cli_initial_file(File)),fail)
16-
; true),
17-
main_cli.
18-
19-
:- dynamic cli_initial_file/1.
20-
:- dynamic cli_format/1.
21-
:- dynamic cli_option/1.
22-
23-
main_cli :-
24-
cli_initial_file(File),
25-
%cli_format(Format),
26-
!,
27-
xgen(File).
28-
29-
get_options([],Rec,Rem) :- !,Rec=[],Rem=[].
30-
get_options(Inputs,RecognisedOptions,RemOptions) :-
31-
(recognise_option(Inputs,Flag,RemInputs)
32-
-> (RecognisedOptions = [Flag|RecO2],
33-
assert(cli_option(Flag)), %%print(Flag),nl,
34-
RemO2 = RemOptions)
35-
; (Inputs = [H|RemInputs], RemOptions = [H|RemO2], RecO2 = RecognisedOptions)
36-
),
37-
get_options(RemInputs,RecO2,RemO2).
38-
39-
recognise_option(Inputs,Flag,RemInputs) :-
40-
recognised_option(Heads,Flag),
41-
append(Heads,RemInputs,Inputs).
42-
43-
recognised_option(['-file',NT],file(NT)).
44-
%recognised_option(['-format',F],format(F)).
45-
46-
/*
47-
xgen(filename)
48-
*/
8+
%%%%%%%%%%%%%%%%%%%%% tracer.pl %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
499

5010
:- dynamic cl/3.
5111
:- dynamic reading/3.
@@ -96,7 +56,7 @@
9656
retractall(explanations(_)),
9757
retractall(counter(_)),assertz(counter(0)),
9858
read_problog_program(File,Program),
99-
%print(Program).
59+
%print(Program),
10060
file_base_name(File,FileNameExt),
10161
string_concat(FileName,'.pl',FileNameExt),
10262
assertz(filename(FileName)),
@@ -116,6 +76,15 @@
11676
(reading(A,String,Vars) -> format(string(S),String,Vars)
11777
; term_string(A,S)),
11878
process_trace(R,RT).
79+
process_trace([not_call(N,A)|R],[not_call(N,A,S)|RT]) :-
80+
(reading(not(A),String,Vars) -> format(string(S),String,Vars)
81+
; term_string(A,AS), string_concat("not ",AS,S)),
82+
process_trace(R,RT).
83+
process_trace([failed_call(N,A)|R],[failed_call(N,A,S2)|RT]) :-
84+
(reading(A,String,Vars) -> format(string(S),String,Vars)
85+
; term_string(A,S)),
86+
string_concat("(failed) ", S, S2),
87+
process_trace(R,RT).
11988

12089
run(Q) :-
12190
findall(Trace,eval(0,Q,Trace),Traces),
@@ -142,6 +111,14 @@
142111

143112
print_trace([]).
144113
print_trace([call(N,_A,S)|R]) :-
114+
% tab(N*3),format("~p~n",[A]),
115+
tab(N*3),format(S),nl,
116+
print_trace(R).
117+
print_trace([not_call(N,_A,S)|R]) :-
118+
% tab(N*3),format("~p~n",[A]),
119+
tab(N*3),format(S),nl,
120+
print_trace(R).
121+
print_trace([failed_call(N,_A,S)|R]) :-
145122
% tab(N*3),format("~p~n",[A]),
146123
tab(N*3),format(S),nl,
147124
print_trace(R).
@@ -150,12 +127,55 @@
150127
eval(N,[ret|R],Trace) :-
151128
M is N-1,
152129
eval(M,R,Trace).
130+
eval(N,[A|R],[call(N,A)|Trace]) :-
131+
predicate_property(A,built_in),
132+
\+ predicate_name(A,"not/1"),
133+
!,
134+
call(A),
135+
eval(N,R,Trace).
136+
eval(N,[not(A)|R],[not_call(N,A)|Trace]) :-
137+
ground(A),
138+
\+ cl(_,A,_Body),
139+
eval(N,R,Trace).
140+
eval(N,[not(A)|R],[not_call(N,A)|Trace]) :-
141+
ground(A),
142+
cl(_,A,Body),
143+
M is N+1,
144+
eval_not(M,Body,Trace1),
145+
!,
146+
eval(N,R,Trace2),
147+
append(Trace1,Trace2,Trace).
153148
eval(N,[A|R],[call(N,A)|Trace]) :-
154149
cl(_,A,Body),
155150
append(Body,[ret|R],RB),
156151
M is N+1,
157152
eval(M,RB,Trace).
158153

154+
eval_not(N,[ret|R],Trace) :-
155+
M is N-1,
156+
eval_not(M,R,Trace).
157+
eval_not(N,[not(A)|_R],[call(N,A)|Trace]) :-
158+
ground(A),
159+
cl(_,A,Body),
160+
M is N+1,
161+
eval(M,Body,Trace).
162+
eval_not(N,[not(A)|R],[not_call(N,A)|Trace]) :-
163+
ground(A),
164+
cl(_,A,Body),
165+
M is N+1,
166+
\+ eval(M,Body,_Trace),
167+
eval_not(N,R,Trace).
168+
eval_not(N,[A|_R],[failed_call(N,A)]) :-
169+
\+ cl(_,A,_Body).
170+
eval_not(N,[A|R],[call(N,A)|Trace]) :-
171+
ground(A),
172+
cl(_,A,Body),
173+
M is N+1,
174+
eval(M,Body,Trace1),!,
175+
eval_not(N,R,Trace2),
176+
append(Trace1,Trace2,Trace).
177+
178+
159179
%% findall(SortedExplanation,
160180
%% (cl(P,Atom,Body),
161181
%% add_empty_ancestors(Body,BodyAn),
@@ -254,6 +274,12 @@
254274
replace_vars(R,RT).
255275

256276
rvars([],[],_,_).
277+
rvars([not(Atom)|R],[not(NAtom)|RR],VarList,N) :-
278+
!,
279+
Atom=..[Pred|Args],
280+
rvars_args(Args,VarList,N,ArgsVars,NVarList,NN),
281+
NAtom =.. [Pred|ArgsVars],
282+
rvars(R,RR,NVarList,NN).
257283
rvars([Atom|R],[NAtom|RR],VarList,N) :-
258284
Atom=..[Pred|Args],
259285
rvars_args(Args,VarList,N,ArgsVars,NVarList,NN),

tracer_shell.pl

+71-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
:- dynamic verbose/0.
1010

1111
main :-
12-
current_prolog_flag(argv,ArgV),
12+
%current_prolog_flag(argv,ArgV),
13+
current_prolog_flag(os_argv,[_,_|ArgV]),
1314
get_options(ArgV,Options,RemArgV), !,
1415
(RemArgV = [FileName]
1516
-> assert(cli_initial_file(FileName))
@@ -56,6 +57,8 @@
5657
recognised_option(['-o',F],output(F)).
5758
recognised_option(['-g',F],goal(F)).
5859

60+
%%%%%%%%%%%%%%%%%%%%% tracer.pl %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
61+
5962
:- dynamic cl/3.
6063
:- dynamic reading/3.
6164
:- dynamic comment/1.
@@ -105,7 +108,7 @@
105108
retractall(explanations(_)),
106109
retractall(counter(_)),assertz(counter(0)),
107110
read_problog_program(File,Program),
108-
%print(Program).
111+
%print(Program),
109112
file_base_name(File,FileNameExt),
110113
string_concat(FileName,'.pl',FileNameExt),
111114
assertz(filename(FileName)),
@@ -125,6 +128,15 @@
125128
(reading(A,String,Vars) -> format(string(S),String,Vars)
126129
; term_string(A,S)),
127130
process_trace(R,RT).
131+
process_trace([not_call(N,A)|R],[not_call(N,A,S)|RT]) :-
132+
(reading(not(A),String,Vars) -> format(string(S),String,Vars)
133+
; term_string(A,AS), string_concat("not ",AS,S)),
134+
process_trace(R,RT).
135+
process_trace([failed_call(N,A)|R],[failed_call(N,A,S2)|RT]) :-
136+
(reading(A,String,Vars) -> format(string(S),String,Vars)
137+
; term_string(A,S)),
138+
string_concat("(failed) ", S, S2),
139+
process_trace(R,RT).
128140

129141
run(Q) :-
130142
findall(Trace,eval(0,Q,Trace),Traces),
@@ -151,6 +163,14 @@
151163

152164
print_trace([]).
153165
print_trace([call(N,_A,S)|R]) :-
166+
% tab(N*3),format("~p~n",[A]),
167+
tab(N*3),format(S),nl,
168+
print_trace(R).
169+
print_trace([not_call(N,_A,S)|R]) :-
170+
% tab(N*3),format("~p~n",[A]),
171+
tab(N*3),format(S),nl,
172+
print_trace(R).
173+
print_trace([failed_call(N,_A,S)|R]) :-
154174
% tab(N*3),format("~p~n",[A]),
155175
tab(N*3),format(S),nl,
156176
print_trace(R).
@@ -159,12 +179,55 @@
159179
eval(N,[ret|R],Trace) :-
160180
M is N-1,
161181
eval(M,R,Trace).
182+
eval(N,[A|R],[call(N,A)|Trace]) :-
183+
predicate_property(A,built_in),
184+
\+ predicate_name(A,"not/1"),
185+
!,
186+
call(A),
187+
eval(N,R,Trace).
188+
eval(N,[not(A)|R],[not_call(N,A)|Trace]) :-
189+
ground(A),
190+
\+ cl(_,A,_Body),
191+
eval(N,R,Trace).
192+
eval(N,[not(A)|R],[not_call(N,A)|Trace]) :-
193+
ground(A),
194+
cl(_,A,Body),
195+
M is N+1,
196+
eval_not(M,Body,Trace1),
197+
!,
198+
eval(N,R,Trace2),
199+
append(Trace1,Trace2,Trace).
162200
eval(N,[A|R],[call(N,A)|Trace]) :-
163201
cl(_,A,Body),
164202
append(Body,[ret|R],RB),
165203
M is N+1,
166204
eval(M,RB,Trace).
167205

206+
eval_not(N,[ret|R],Trace) :-
207+
M is N-1,
208+
eval_not(M,R,Trace).
209+
eval_not(N,[not(A)|_R],[call(N,A)|Trace]) :-
210+
ground(A),
211+
cl(_,A,Body),
212+
M is N+1,
213+
eval(M,Body,Trace).
214+
eval_not(N,[not(A)|R],[not_call(N,A)|Trace]) :-
215+
ground(A),
216+
cl(_,A,Body),
217+
M is N+1,
218+
\+ eval(M,Body,_Trace),
219+
eval_not(N,R,Trace).
220+
eval_not(N,[A|_R],[failed_call(N,A)]) :-
221+
\+ cl(_,A,_Body).
222+
eval_not(N,[A|R],[call(N,A)|Trace]) :-
223+
ground(A),
224+
cl(_,A,Body),
225+
M is N+1,
226+
eval(M,Body,Trace1),!,
227+
eval_not(N,R,Trace2),
228+
append(Trace1,Trace2,Trace).
229+
230+
168231
%% findall(SortedExplanation,
169232
%% (cl(P,Atom,Body),
170233
%% add_empty_ancestors(Body,BodyAn),
@@ -263,6 +326,12 @@
263326
replace_vars(R,RT).
264327

265328
rvars([],[],_,_).
329+
rvars([not(Atom)|R],[not(NAtom)|RR],VarList,N) :-
330+
!,
331+
Atom=..[Pred|Args],
332+
rvars_args(Args,VarList,N,ArgsVars,NVarList,NN),
333+
NAtom =.. [Pred|ArgsVars],
334+
rvars(R,RR,NVarList,NN).
266335
rvars([Atom|R],[NAtom|RR],VarList,N) :-
267336
Atom=..[Pred|Args],
268337
rvars_args(Args,VarList,N,ArgsVars,NVarList,NN),

0 commit comments

Comments
 (0)