This repository has been archived by the owner on Nov 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
syntax.pl
239 lines (212 loc) · 12.3 KB
/
syntax.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/* Copyright [2021] Initial copyright holders by country:
LodgeIT (AU), AORA Law (UK), Bob Kowalski (UK), Miguel Calejo (PT), Jacinto Dávila (VE)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
:- module(_,[
op(1195,fx, user:(++)), % for hypothetical facts and rules
op(1190,xfx,user:(if)),
op(1187,xfx,user:(then)),
op(1187,xfx,user:(must)),
op(1185,fx,user:(if)),
op(1185,xfy,user:else),
op(1000,xfy,user:and), % same as ,
op(1050,xfy,user:or), % same as ;
op(900,fx,user:not), % same as \+
op(700,xfx,user:in),
op(600,xfx,user:on),
op(1150,xfx,user:because), % to support because(on(p,t),why) if ...
op(700,xfx,user:at), % note vs. negation...incompatible with LPS fluents
% date operators
op(700,xfx,user:is_not_before),
op(700,xfx,user:before),
op(700,xfx,user:after),
taxlog2prolog/3,
semantics2prolog/3
]).
:- use_module(kp_loader,[kp_location/3,my_xref_defined/3]).
:- use_module(library(prolog_xref)).
:- use_module(library(prolog_colour)).
:- use_module(library(pengines)).
/*
Transforms source rules into our "no time on heads" representation, using a body wrapper to carry extra information:
taxlogBody(RealBody,HasTimeOnHead,Time,URL,Why)
P on T if Body --> P :- taxlogBody(Body,true,T,'',[])
P on T because Why :- PrologBody --> P :- taxlogBody(PrologBody,true,T,'',Why)
P if Body --> P :- taxlogBody(Body,false,_,'',[])
Admissible variants with a specific URL:
P on T at URL if Body --> P :- taxlogBody(Body,true,T,URL,[])
P at URL if Body --> P :- taxlogBody(Body,false,_,URL,[])
*/
taxlog2prolog(if(function(Call,Result),Body), neck(if)-[delimiter-[head(meta,Call),classify],SpecB], (function(Call,Result):-Body)) :- !,
taxlogBodySpec(Body,SpecB).
taxlog2prolog(if(at(on(H,T),Url),B), neck(if)-[delimiter-[delimiter-[SpecH,classify],classify],SpecB], (H:-taxlogBody(B,true,T,Url,[]))) :- !,
taxlogHeadSpec(H,SpecH), taxlogBodySpec(B,SpecB).
taxlog2prolog(if(at(H,Url),B), neck(if)-[delimiter-[SpecH,classify],SpecB], (H:-taxlogBody(B,false,_T,Url,[]))) :- !,
taxlogHeadSpec(H,SpecH), taxlogBodySpec(B,SpecB).
taxlog2prolog(if(on(H,T),B), neck(if)-[delimiter-[SpecH,classify],SpecB], (H:-taxlogBody(B,true,T,'',[]))) :- !,
taxlogHeadSpec(H,SpecH), taxlogBodySpec(B,SpecB).
taxlog2prolog(if(H,B),neck(if)-[SpecH,SpecB],(H:-taxlogBody(B,false,_,'',[]))) :- !,
taxlogHeadSpec(H,SpecH), taxlogBodySpec(B,SpecB).
taxlog2prolog((because(on(H,T),Why):-B), neck(clause)-[ delimiter-[delimiter-[SpecH,classify],classify], SpecB ], (H:-taxlogBody(call(B),true,T,'',Why))) :- Why\==[], !,
taxlogHeadSpec(H,SpecH), taxlogBodySpec(B,SpecB).
taxlog2prolog(mainGoal(G,Description),delimiter-[Spec,classify],(mainGoal(G,Description):-(_=1->true;GG))) :- !, % hack to avoid 'unreferenced' highlight in SWISH
functor(G,F,N), functor(GG,F,N), % avoid "Singleton-marked variable appears more than once"
taxlogBodySpec(G,Spec).
taxlog2prolog((example(T,Sequence):-Body), neck(clause)-[delimiter-[classify,Spec],classify],(example(T,Sequence):-Body)) :- !,
(Sequence==[]->Spec=classify ; (Spec=list-SeqSpec, scenarioSequenceSpec(Sequence,SeqSpec))).
taxlog2prolog(example(T,Sequence),delimiter-[classify,Spec],example(T,Sequence)) :- !,
(Sequence==[]->Spec=classify ; (Spec=list-SeqSpec, scenarioSequenceSpec(Sequence,SeqSpec))).
taxlog2prolog(question(X,QuestionTerm),delimiter-[classify,classify],question(X,QuestionTerm)) :- !.
taxlog2prolog(question(X,QuestionTerm,Answer),delimiter-[classify,classify,classify],question(X,QuestionTerm,Answer)) :- !.
taxlog2prolog(irrelevant_explanation(G),delimiter-[Spec],irrelevant_explanation(G)) :- !,
taxlogBodySpec(G,Spec).
taxlog2prolog(query(Name,Goal),delimiter-[classify,classify],query(Name,Goal)).
% extending to cover new structural changes at semantical level
semantics2prolog(if(H,B),neck(if)-[SpecH,SpecB],(H:-B)) :- !,
SpecH=classify, SpecB=classify.
%taxlogHeadSpec(H,SpecH), taxlogBodySpec(B,SpecB).
%semantics2prolog(mainGoal(G,Description),delimiter-[Spec,classify],(mainGoal(G,Description):-(_=1->true;GG))) :- !, % hack to avoid 'unreferenced' highlight in SWISH
% functor(G,F,N), functor(GG,F,N), % avoid "Singleton-marked variable appears more than once"
% taxlogBodySpec(G,Spec).
semantics2prolog(example(T,Sequence),delimiter-[classify,Spec],example(T,Sequence)) :-
pengine_self(SwishModule),
declare_facts_as_dynamic(SwishModule, [example(_,_)]), !,
Spec = classify. % just a hack - scenarioSequenceSpec must be different for prolog's scenarios
%(Sequence==[]->Spec=classify ; (Spec=list-SeqSpec, scenarioSequenceSpec(Sequence,SeqSpec))).
semantics2prolog(query(Name,Goal),delimiter-[classify,classify],query(Name,Goal)) :-
pengine_self(SwishModule),
declare_facts_as_dynamic(SwishModule, [query(_,_)]), !.
semantics2prolog(metapredicates(Assumptions), delimiter-[classify,classify],metapredicates([N])) :-
pengine_self(SwishModule), lists:length(Assumptions,N),
declare_facts_as_dynamic(SwishModule, Assumptions), !.
semantics2prolog(predicates(Assumptions), delimiter-[classify,classify],predicates([N])) :-
pengine_self(SwishModule), lists:length(Assumptions,N),
declare_facts_as_dynamic(SwishModule, Assumptions), !.
%print_message(informational, "asserted: ~w"-[Assumptions]).
semantics2prolog(events(Assumptions), delimiter-[classify,classify],events([N])) :-
pengine_self(SwishModule), lists:length(Assumptions,N),
declare_facts_as_dynamic(SwishModule, [happens(_,_), initiates(_,_,_), terminates(_,_,_)|Assumptions]), !.
semantics2prolog(fluents(Assumptions), delimiter-[classify,classify],fluents([N])) :-
pengine_self(SwishModule), lists:length(Assumptions,N),
declare_facts_as_dynamic(SwishModule, [it_is_illegal(_,_)|Assumptions]), !.
% assuming one example -> one scenario -> one list of facts. % deprecated
% declare_dynamic(Module, [scenario(Facts, _)]) :- declare_facts_as_dynamic(Module, Facts).
declare_facts_as_dynamic(_, []) :- !.
declare_facts_as_dynamic(M, [F|R]) :- functor(F, P, A), % facts are the templates now
dynamic([M:P/A], [thread(local), discontiguous(true)]), declare_facts_as_dynamic(M, R).
% note: keep the above cases coherent with kp_loader:system_predicate/1
scenarioSequenceSpec([S|Scenarios],[Spec|Specs]) :- !,
scenarioSpec(S,Spec),
scenarioSequenceSpec(Scenarios,Specs).
scenarioSequenceSpec([],[]).
scenarioSpec(scenario(Facts,Assertion),delimiter-[FactsSpec,Spec]) :-
(Facts==[] -> FactsSpec=classify ; (factsSpecs(Facts,FS), FactsSpec=list-FS)),
taxlogBodySpec(Assertion,Spec).
factsSpecs(Facts,classify) :- var(Facts), !.
factsSpecs([Fact_|Facts],[FactSpec|Specs]) :- !,
(Fact_= -Fact -> FactSpec= delimiter-[FS] ; Fact_= '++'(Fact) -> FactSpec= delimiter-[FS]; (Fact=Fact_,FactSpec=FS)),
nonvar(Fact),
(Fact=if(H,B)->(
taxlogHeadSpec(H,FSH),taxlogBodySpec(B,FSB),FS=neck(if)-[FSH,FSB]);
taxlogHeadSpec(Fact,FS) ),
factsSpecs(Facts,Specs).
factsSpecs([],[]).
taxlogHeadSpec(H,head(Class, H)) :- current_source(UUID),
!,
xref_module(UUID,Me),
(H=on(RealH,_T)->true;H=RealH),
(xref_called(_Other,Me:RealH, _) -> (Class=exported) ;
xref_called(UUID, RealH, _By) -> (Class=head) ;
Class=unreferenced).
taxlogHeadSpec(H,head(head, H)).
:- multifile swish_highlight:style/3.
swish_highlight:style(neck(if), neck, [ text(if) ]).
% :- thread_local current_module/1.
% :- multifile prolog_colour:directive_colours/2.
% prolog_colour:directive_colours((:- module(M,_)),null) :-
% mylog(detected_module/M), % NOT CALLED AT ALL???
% retractall(current_module(_)), assert(current_module(M)), fail.
% this must be in sync with the interpreter i(...) and prolog:meta_goal(...) hooks
taxlogBodySpec(V,classify) :- var(V), !.
taxlogBodySpec(and(A,B),delimiter-[SpecA,SpecB]) :- !,
taxlogBodySpec(A,SpecA), taxlogBodySpec(B,SpecB).
taxlogBodySpec((A,B),delimiter-[SpecA,SpecB]) :- !,
taxlogBodySpec(A,SpecA), taxlogBodySpec(B,SpecB).
taxlogBodySpec(or(A,B),delimiter-[SpecA,SpecB]) :- !,
taxlogBodySpec(A,SpecA), taxlogBodySpec(B,SpecB).
taxlogBodySpec((A;B),delimiter-[SpecA,SpecB]) :- !,
taxlogBodySpec(A,SpecA), taxlogBodySpec(B,SpecB).
taxlogBodySpec(must(if(I),M),delimiter-[delimiter-SpecI,SpecM]) :- !,
taxlogBodySpec(I,SpecI), taxlogBodySpec(M,SpecM).
taxlogBodySpec(not(G),delimiter-[Spec]) :- !,
taxlogBodySpec(G,Spec).
taxlogBodySpec((\+G),delimiter-[Spec]) :- !,
taxlogBodySpec(G,Spec).
taxlogBodySpec(then(if(C),else(T,Else)),delimiter-[delimiter-[SpecC],delimiter-[SpecT,SpecE]]) :- !,
taxlogBodySpec(C,SpecC), taxlogBodySpec(T,SpecT), taxlogBodySpec(Else,SpecE).
taxlogBodySpec(then(if(C),Then),delimiter-[delimiter-[SpecC],SpecT]) :- !,
taxlogBodySpec(C,SpecC), taxlogBodySpec(Then,SpecT).
taxlogBodySpec(forall(C,Must),control-[SpecC,SpecMust]) :- !,
taxlogBodySpec(C,SpecC), taxlogBodySpec(Must,SpecMust).
taxlogBodySpec(setof(_X,G,_L),control-[classify,SpecG,classify]) :- !,
taxlogBodySpec(G,SpecG).
taxlogBodySpec(bagof(_X,G,_L),control-[classify,SpecG,classify]) :- !,
taxlogBodySpec(G,SpecG).
taxlogBodySpec(_^G,delimiter-[classify,SpecG]) :- !,
taxlogBodySpec(G,SpecG).
% this is needed only to deal with multiline instances of aggregate... (or of any predicate of our own colouring, apparently:-( )
taxlogBodySpec(aggregate(_X,G,_L),control-[classify,SpecG,classify]) :- !,
taxlogBodySpec(G,SpecG).
taxlogBodySpec(aggregate_all(_X,G,_L),control-[classify,SpecG,classify]) :- !,
taxlogBodySpec(G,SpecG).
taxlogBodySpec(findall(_X,G,_L),control-[classify,SpecG,classify]) :- !,
taxlogBodySpec(G,SpecG).
% questions are no longer goals, just annotations for (rendering unknown) goal literals
%taxlogBodySpec(question(_,_),delimiter-[classify,classify]). % to avoid multiline colouring bug
%taxlogBodySpec(question(_),delimiter-[classify]).
taxlogBodySpec(M:G,delimiter-[classify,SpecG]) :- !, taxlogBodySpec(at(G,M),delimiter-[SpecG,classify]).
taxlogBodySpec(at(G_,M_),Spec) :- nonvar(M_), nonvar(G_), !, % assuming atomic goals
atom_string(M,M_), %TODO: this might be cleaned up/refactored with the next clauses:
(G_=on(G,_) -> Spec=delimiter-[delimiter-[SpecG,classify],classify]; (G=G_, Spec=delimiter-[SpecG,classify])),
(my_xref_defined(M,G,_)-> SpecG=goal(imported(M),G)-classify ; SpecG=goal(undefined,G)-classify).
taxlogBodySpec(on(G,_T),delimiter-[SpecG,classify] ) :- !,
taxlogBodySpec(G,SpecG).
taxlogBodySpec(G,Spec) :-
(compound(G)->Spec=goal(Class,G)-classify;Spec=goal(Class,G)),
current_source(UUID), taxlogGoalSpec(G, UUID, Class),
!.
taxlogBodySpec(_G,classify).
taxlogGoalSpec(G, UUID, Class) :-
(my_xref_defined(UUID, G, Class) -> true ;
%prolog_colour:built_in_predicate(G)->Class=built_in ;
my_goal_classification(G,Class) -> true;
Class=undefined).
:- if(current_prolog_flag(version_data,swi(8, 2, _, _))).
my_goal_classification(G,Class) :-
prolog_colour:call_goal_classification(G, Class).
:- elif(( current_prolog_flag(version_data,V), V@>= swi(8, 3, 0, []))).
my_goal_classification(G,Class) :-
prolog_colour:call_goal_classification(G, _Module, Class).
:- else.
:- print_message(error,"You need SWI-Prolog 8.2 or later"-[]), halt(1).
:- endif.
:- if(current_module(swish)). %%% only when running with the SWISH web server:
% hack to find the editor (e.g. its module name) that triggered the present highlighting
current_source(UUID) :-
swish_highlight:current_editor(UUID, _TB, source, Lock, _), mutex_property(Lock,status(locked(_Owner, _Count))), !.
current_source(UUID) :-
%mylog('Could not find locked editor, going with the first one'),
swish_highlight:current_editor(UUID, _TB, source, _Lock, _), !.
:- else. %% barebones SWI-Prolog:
% find the module in the file being coloured (which has been xref'd already)
current_source(Source) :-
prolog_load_context(source,File), kp_location(Source,File,false).
:- endif.