-
Notifications
You must be signed in to change notification settings - Fork 0
/
interpretation.pl
261 lines (231 loc) · 6.58 KB
/
interpretation.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
:-module(interpretation, [interpretation/2
]).
:-use_module(metasplain).
/** <module> Generate program interpretations.
*/
%! interpretation(+Program,-Interpretation) is det.
%
% Form an Interpretation for a Program.
%
% Interpretation is a string of expressions in a Controlled
% Natural Language, derived from the structure of the program in
% combination with a number of user-provided interpretation
% strings.
%
interpretation(Ps,Is):-
var(Is)
,!
,predicate_interpretations(Ps,Is_Cs)
,program_interpretation(Is_Cs,Is).
interpretation(Ps,Is):-
var(Ps)
,interpretation_program(Is,Ps).
%! predicate_interpretations(+Program,-Interpretations) is det.
%
% Form Interpretations for all predicates in a Progarm.
%
predicate_interpretations(Ps,Is):-
predicate_interpretations(Ps,[],Is).
%! predicate_interpretations(+Program,+Acc,-Interpretations) is
%! det.
%
% Business end of predicate_interpretations/2
%
predicate_interpretations([],Acc,Is):-
reverse(Acc,Is)
,!.
predicate_interpretations([C|Cs],Acc,Bind):-
select_definition(C,[C|Cs],Ds,Rs)
,clause_predicate_symbol(C,S/A)
,clause_interpretations(S/A,Ds,Is)
,predicate_interpretations(Rs,[Is|Acc],Bind).
%! clause_interpretations(+Predicate,+Definition,-Interpretations)
%! is det.
%
% Form Interpretations for all clauses of a Predicate.
%
clause_interpretations(S/A,Ds,Is):-
clause_interpretations(S/A,Ds,[],Is).
%! clause_interpretations(+Predicate,+Definition,+Acc,-Interpretations)
%! is det.
%
% Business end of clause_interpretations/3
%
clause_interpretations(S/A,[],Acc,S/A-Is):-
reverse(Acc,Is)
,!.
clause_interpretations(S/A,[C|Cs],Acc,Bind):-
configuration:variable_interpretation(false)
,!
,clause_literals(C,Ls)
,literals_symbols(Ls,Ss)
,once(clause_metarule(C,Ss,M))
,configuration:interpretation_string(M,Is)
,once(phrase(clause_interpretation(Is),Ss))
,cleaned_interpretation(Is, Is_)
,clause_interpretations(S/A,Cs,[Is_|Acc],Bind).
clause_interpretations(S/A,[C|Cs],Acc,Bind):-
configuration:variable_interpretation(true)
,clause_literals(C,Ls)
,literals_symbols(Ls,Ss)
,once(clause_metarule(C,Ss,M))
,term_variables(Ls,Vs)
% Avoid binding variables in other clauses.
% Because they're usually not standardised apart!
,copy_term(Vs,Vs_)
,numbervars(Vs_)
,configuration:interpretation_string(M,Ss,Vs_,Is)
,varnumbers(Is,Is_1)
,cleaned_interpretation(Is_1, Is_2)
,clause_interpretations(S/A,Cs,[Is_2|Acc],Bind).
%! clause_interpretation(?Symbols) is nondet.
%
% Interpret a list of predicate Symbols.
%
clause_interpretation([]) --> [].
clause_interpretation([S|Ss]) --> [S], clause_interpretation(Ss).
clause_interpretation([_S|Ss]) --> clause_interpretation(Ss).
%! cleaned_interpretation(+Interpretation,-Cleaned) is det.
%
% Cleanup an Interpretation string.
%
% Currently, this just removes underscores between symbols'
% substrings.
%
cleaned_interpretation(Is,Cs):-
cleaned_interpretation(Is,[],Cs).
%! cleaned_interpretation(+Interpretation,+Acc,-Cleaned) is det.
%
% Business end of cleaned_interpretation/2.
%
cleaned_interpretation([],Acc,Cs):-
reverse(Acc,Cs_)
,flatten(Cs_,Cs).
cleaned_interpretation([S|Is],Acc,Bind):-
configuration:interpretation_cleanup(remove_underscores)
,atom(S)
,!
,atomic_list_concat(Ss,'_',S)
,cleaned_interpretation(Is,[Ss|Acc],Bind).
cleaned_interpretation([S|Is],Acc,Bind):-
cleaned_interpretation(Is,[S|Acc],Bind).
%! program_interpretation(+Program,-Interpretation) is det.
%
% Form an Interpretation for a Program.
%
program_interpretation(Ps,Is):-
program_interpretation(Ps,[],Is).
%! program_interpretation(+Program,+Acc,-Interpretation) is det.
%
% Business end of program_interpretation/2.
%
program_interpretation([],Acc,Is):-
reverse(Acc,Is)
,!.
program_interpretation([_S/_A-[Is]|Ps],Acc,Bind):-
!
,numbervars(Is)
,findall(A
,(member(T,Is)
,format(atom(A),'~w',[T])
)
,As)
,atomic_list_concat(As,' ',I)
,program_interpretation(Ps,[I|Acc],Bind).
program_interpretation([_S/_A-Is|Ps],Acc,Bind):-
configuration:explanation_connectives([connective(C)])
,atomic_list_concat([' ',C,' '],'',C_)
,merged_interpretations(Is,Ms)
,numbervars(Ms)
% Could generalise...
,findall(As
,(member(Ts,Ms)
,findall(A
,(member(T,Ts)
,format(atom(A),'~w',[T])
)
,As)
)
,AS)
,findall(CI_
,(member(CI,AS)
,atomic_list_concat(CI,' ',CI_)
)
,Cs)
,atomic_list_concat(Cs,C_,Is_)
,program_interpretation(Ps,[Is_|Acc],Bind).
%! merged_interpretations(+Interpretations,-Merged) is det.
%
% Merge a list of Interpretations, removing repeating tokens.
%
merged_interpretations(Is,Is):-
configuration:merge_interpretations(false)
,!.
merged_interpretations([I|Is],[I|Ms]):-
configuration:merge_interpretations(true)
,merged_interpretations(I,Is,[],Ms).
%! merged_interpretations(+First,+Interpretations,+Acc,-Merged) is
%! det.
%
% Business end of merged_interpretations/2.
%
merged_interpretations(_,[],Acc,Ms):-
reverse(Acc,Ms)
,!.
merged_interpretations(I1,[I2|Is],Acc,Bind):-
string_difference(I1,I2,Ds)
,merged_interpretations(I1,Is,[Ds|Acc],Bind).
%! string_difference(+String_1,+String_2,-Difference) is det.
%
% The Difference of two strings with a common prefix.
%
string_difference(Ss1,Ss2,Ds):-
string_difference_(Ss1,Ss2,Ds).
%! string_difference_(+String_1,+String_2,+Acc,-Difference) is det.
%
% Business end of string_difference/3
%
string_difference_([T|Ss1],[T|Ss2],Bind):-
!
,string_difference_(Ss1,Ss2,Bind).
string_difference_(_Ss1,Ss,Ss).
%! interpretation_program(+Interpretation,-Program) is nondet.
%
% Reconstruct a program from an Interpretation.
%
interpretation_program(Is,Ps):-
interpretation_clauses(Is, Cs)
,interpretation_program(Cs,[],Ps).
%! interpretation_clauses(+Interpretation,-Clauses) is det.
%
% Split an Interpretation to a list of interpreted Clauses.
%
interpretation_clauses(Is,Cs):-
interpretation_clauses(Is,[],Cs).
%! interpretation_clauses(+Interpretation,+Acc,-Clauses) is det.
%
% Business end of interpretation_clauses/2
%
interpretation_clauses([],Acc,Cs):-
reverse(Acc,Cs_)
,flatten(Cs_, Cs)
,!.
interpretation_clauses([I|Is], Acc,Bind):-
explanation_connectives([connective(C)])
% Ensure no spaces are left at clause limits.
,atomic_list_concat([' ',C,' '],'',C_)
% Split predicate names around connectives.
,atomic_list_concat(Ci,C_,I)
,interpretation_clauses(Is,[Ci|Acc],Bind).
%! interpretation_program(+Interpretation,+Acc,-Program) is nondet.
%
% Business end of interpretation_program/2.
%
interpretation_program([],Acc,Ps):-
reverse(Acc,Ps)
,!.
interpretation_program([I|Is],Acc,Bind):-
atomic_list_concat(Ts,' ',I)
,interpretation_string(M,HO,_FO,Ts)
,clause_metarule(C,HO,M)
,interpretation_program(Is,[C|Acc],Bind).