foltl.pl 35.1 KB
Newer Older
Thierry Martinez's avatar
FOLTL    
Thierry Martinez committed
1
2
3
:- module(
  foltl,
  [
Thierry Martinez's avatar
Thierry Martinez committed
4
    % Grammar
Thierry Martinez's avatar
Thierry Martinez committed
5
6
7
    foltl/1,
    foltl_predicate/1,
    foltl_expression/1,
Thierry Martinez's avatar
Thierry Martinez committed
8
9
    parameter_between_interval/1,
    variable_objective/1,
FAGES Francois's avatar
FAGES Francois committed
10
11
    time_value/1,
    time_value_list/1,
Thierry Martinez's avatar
Thierry Martinez committed
12
    % Biocham commands
Thierry Martinez's avatar
FOLTL    
Thierry Martinez committed
13
    validity_domain/1,
14
    satisfaction_degree/2,
Thierry Martinez's avatar
Thierry Martinez committed
15
16
17
    (ltl_pattern)/1,
    list_ltl_patterns/0,
    delete_ltl_pattern/1,
FAGES Francois's avatar
FAGES Francois committed
18
    expand_ltl/1,
Thierry Martinez's avatar
Thierry Martinez committed
19
    % Public API
20
    expand_formula/2,
Thierry Martinez's avatar
FOLTL    
Thierry Martinez committed
21
    validity_domain/2,
22
    satisfaction_degree/3,
FAGES Francois's avatar
FAGES Francois committed
23
    generate_domain_cpp/1,
24
    generate_domain_cpp/2,
25
    generate_objective/1,
FAGES Francois's avatar
FAGES Francois committed
26
27
    free_variable_index/2,
    column/2,
Thierry Martinez's avatar
FOLTL    
Thierry Martinez committed
28
    op(700, xfy, '<>'),
FAGES Francois's avatar
FAGES Francois committed
29
30
    op(700, xfy, '<<'),
    op(700, xfy, '>>'),
31
    op(800, yfx, '/\\'), % left parentheses for the reversed order used below
FAGES Francois's avatar
FAGES Francois committed
32
    op(900, yfx, '\\/'), % idem
Thierry Martinez's avatar
Thierry Martinez committed
33
    op(1010, fx, ltl_pattern)
Thierry Martinez's avatar
FOLTL    
Thierry Martinez committed
34
35
36
  ]
).

37
38
39
40
41
42
43
% linting
:- use_module(doc).
:- use_module(biocham).
:- use_module(objects).
:- use_module(reaction_rules).


FAGES Francois's avatar
doc    
FAGES Francois committed
44
:- doc('Firt-Order Linear Time Logic with linear constraints over the reals, FO-LTL(Rlin), can be used to specify semi-qualitative semi-quantitative constraints on the dynamical behavior of the system, in a much more flexible manner than by curves to fit \\cite{RBFS11tcs}. The syntax of FO-LTL(Rlin) formulas is given below together with some useful abbreviations \\cite{FT14book}:').
FAGES Francois's avatar
FAGES Francois committed
45

FAGES Francois's avatar
ppl    
FAGES Francois committed
46
47
48
:- doc('The \\texttt{foltl_magnitude} option (default 5) is the multiplicative factor used for the strong comparison operators \\texttt{<<} and \\texttt{>>} over positive numbers, \\texttt{A<<B} means \\texttt{5*A<B}.').

%:- doc('The \\texttt{precision} option of the numerical solver (default 6, i.e. $10^{-6}$) is the additive term used for the strict comparison operators  \\texttt{<} and \\texttt{>} over real numbers.').
Thierry Martinez's avatar
FOLTL    
Thierry Martinez committed
49

FAGES Francois's avatar
foltl    
FAGES Francois committed
50
51
52
53
:- initial('option(foltl_magnitude: 5)').


:- devdoc('TODO: split this file into two and create ppl.pl (structure similar to ctl.pl/nusmv.pl) I failed because of unextricable errors with grammar_map as always...
FAGES Francois's avatar
FAGES Francois committed
54
55
56
\\begin{itemize}
\\item add_ltl for creating FO-LTL(Rlin) specifications
\\item generate_ltl for interesting patterns with objective values
FAGES Francois's avatar
FAGES Francois committed
57
\\item in ppl.pl: move from here the cpp program generation for ppl and compute robustness as penetration depth in addition to satisfaction degree
FAGES Francois's avatar
FAGES Francois committed
58
59
60
\\item in tables.pl or perhaps numerical_simulation.pl about traces: move from here the little time:value list grammar, and add trace simplification keeping only the time points that are extremal for some variable
\\end{itemize}').

FAGES Francois's avatar
doc    
FAGES Francois committed
61
:- devdoc('\\section{Grammar}').
FAGES Francois's avatar
doc    
FAGES Francois committed
62

Thierry Martinez's avatar
Thierry Martinez committed
63
64
65
:- grammar(foltl).


Thierry Martinez's avatar
Thierry Martinez committed
66
67
foltl('X'(F)) :-
  foltl(F).
Thierry Martinez's avatar
Thierry Martinez committed
68

Thierry Martinez's avatar
Thierry Martinez committed
69
70
foltl('F'(F)) :-
  foltl(F).
Thierry Martinez's avatar
Thierry Martinez committed
71

Thierry Martinez's avatar
Thierry Martinez committed
72
foltl('G'(F)) :-
Thierry Martinez's avatar
Thierry Martinez committed
73
74
  foltl(F).

Thierry Martinez's avatar
Thierry Martinez committed
75
foltl(not(F)) :-
Thierry Martinez's avatar
Thierry Martinez committed
76
  foltl(F).
Thierry Martinez's avatar
Thierry Martinez committed
77
78
79
80
81

foltl('U'(F, G)) :-
  foltl(F),
  foltl(G).

FAGES Francois's avatar
FAGES Francois committed
82
83
84
85
foltl('R'(F, G)) :-
  foltl(F),
  foltl(G).

Thierry Martinez's avatar
Thierry Martinez committed
86
87
88
foltl('W'(F, G)) :-
  foltl(F),
  foltl(G).
Thierry Martinez's avatar
Thierry Martinez committed
89

Thierry Martinez's avatar
Thierry Martinez committed
90
91
92
foltl(F /\ G) :-
  foltl(F),
  foltl(G).
Thierry Martinez's avatar
Thierry Martinez committed
93

Thierry Martinez's avatar
Thierry Martinez committed
94
95
96
97
98
99
foltl(F \/ G) :-
  foltl(F),
  foltl(G).

foltl(exists(X, F)) :-
  name(X),
Thierry Martinez's avatar
Thierry Martinez committed
100
101
  foltl(F).

Thierry Martinez's avatar
Thierry Martinez committed
102
103
104
foltl(F => G) :-
  foltl(F),
  foltl(G).
105
% :- doc(' \\emphright{\\texttt{f => g} is equivalent to \\texttt{not f \\\\/ g}}').
Thierry Martinez's avatar
Thierry Martinez committed
106
107
108
109

foltl(G <=> F) :-
  foltl(F),
  foltl(G).
110
% :- doc('  \\emphright{\\texttt{f <=> g} is equivalent to \\texttt{f => g /\\\\ g => f}}').
Thierry Martinez's avatar
Thierry Martinez committed
111

FAGES Francois's avatar
FAGES Francois committed
112
113
114
115
foltl(false).

foltl(true).

Thierry Martinez's avatar
Thierry Martinez committed
116
117
118
foltl(FunctionApplication) :-
  function_application(untyped, FunctionApplication).

Thierry Martinez's avatar
Thierry Martinez committed
119
120
121
foltl(F) :-
  foltl_predicate(F).

Thierry Martinez's avatar
Thierry Martinez committed
122

Thierry Martinez's avatar
Thierry Martinez committed
123
124
125
126
127
128
129
130
131
132
133
134
135
:- grammar(foltl_predicate).


foltl_predicate(E = F) :-
  foltl_expression(E),
  foltl_expression(F).


foltl_predicate(E <> F) :-
  foltl_expression(E),
  foltl_expression(F).


FAGES Francois's avatar
FAGES Francois committed
136
137
138
139
140
141
142
143
144
145
foltl_predicate(E << F) :-
  foltl_expression(E),
  foltl_expression(F).


foltl_predicate(E >> F) :-
  foltl_expression(E),
  foltl_expression(F).


Thierry Martinez's avatar
Thierry Martinez committed
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
foltl_predicate(E < F) :-
  foltl_expression(E),
  foltl_expression(F).


foltl_predicate(E <= F) :-
  foltl_expression(E),
  foltl_expression(F).


foltl_predicate(E > F) :-
  foltl_expression(E),
  foltl_expression(F).


foltl_predicate(E >= F) :-
  foltl_expression(E),
  foltl_expression(F).


David Rosenblueth's avatar
David Rosenblueth committed
166
167
168
:- grammar(foltl_expression).


Thierry Martinez's avatar
Thierry Martinez committed
169
170
171
172
173
174
175
176
177
178
179
180
foltl_expression(E + F) :-
  foltl_expression(E),
  foltl_expression(F).

foltl_expression(E - F) :-
  foltl_expression(E),
  foltl_expression(F).

foltl_expression(E * F) :-
  foltl_expression(E),
  foltl_expression(F).

181
182
183
184
foltl_expression(E ^ F) :-
  foltl_expression(E),
  foltl_expression(F).

Thierry Martinez's avatar
Thierry Martinez committed
185
186
187
188
foltl_expression(E / F) :-
  foltl_expression(E),
  foltl_expression(F).

Thierry Martinez's avatar
Thierry Martinez committed
189
190
191
foltl_expression(E) :-
  number(E).

Thierry Martinez's avatar
Thierry Martinez committed
192
193
194
foltl_expression(FunctionApplication) :-
  function_application(foltl_expression, FunctionApplication).

Thierry Martinez's avatar
Thierry Martinez committed
195
196
197
foltl_expression(E) :-
  name(E).

198
199
200
foltl_expression([E]) :-
  name(E).

Thierry Martinez's avatar
Thierry Martinez committed
201

FAGES Francois's avatar
foltl    
FAGES Francois committed
202
203
204
205
206
207
208
209
210
211
212
213
214

:- grammar(time_value).

time_value(Time:Value) :-
  time(Time),
  number(Value).


:- grammar(time_value_list).

time_value_list(L):-
  list(time_value, L).

Thierry Martinez's avatar
Thierry Martinez committed
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
:- grammar(parameter_between_interval).


parameter_between_interval(Min <= Parameter <= Max) :-
  number(Min),
  parameter_name(Parameter),
  number(Max).


:- grammar(variable_objective).


variable_objective(Variable -> Value) :-
  name(Variable),
  number(Value).

FAGES Francois's avatar
foltl    
FAGES Francois committed
231
232
233
234
235
236
237
238
239
240
241
242
:- doc('Furthermore, some useful abbreviations have been predefined with the following FO-LTL(Rlin) formula patterns:').

:- initial('ltl_pattern forall(X, Formula) = not(exists(X, not(Formula)))').
:- initial('ltl_pattern reachable(Formula) = F(Formula)').
:- initial('ltl_pattern steady(Formula) = G(Formula)').


:- doc('\\emph{Curves.}').

:- initial('ltl_pattern curve(Molecule, Time_Value_List) = curve(Molecule, Time_Value_list)').


FAGES Francois's avatar
FAGES Francois committed
243
:- doc('\\emph{Sequence of events} \\doi{MRMFJ08bi}.').
FAGES Francois's avatar
foltl    
FAGES Francois committed
244
245
246
247
248
249
250
251
252
253

:- initial('ltl_pattern occurs(Formula) = F(Formula)').
:- initial('ltl_pattern excludes(Formula) = G(not(Formula))').
:- initial('ltl_pattern invariates(Formula) = G(Formula)').
:- initial('ltl_pattern weak_sequence(Formula1, Formula2) = F(Formula1 /\\ F(Formula2))').
:- initial('ltl_pattern exact_sequence(Formula1, Formula2) = F(Formula1 /\\ X(Formula2))').
:- initial('ltl_pattern sequence(Formula1, Formula2) = G(U(Formula1,Formula2))').
:- initial('ltl_pattern consequence(Formula1, Formula2) = G(Formula1  => F(Formula2))').
:- initial('ltl_pattern implication(Formula1, Formula2) = G(Formula1 => Formula2)').

FAGES Francois's avatar
FAGES Francois committed
254
:- doc('\\emph{Thresholds, global extrema, amplitudes} \\cite{FT14book}.').
FAGES Francois's avatar
foltl    
FAGES Francois committed
255
256
257
258
259
260
261
262
263
264
265
266
267
268

:- initial('ltl_pattern reached(Molecule, Concentration) = F(Molecule >= Concentration)').
:- initial('ltl_pattern unreached(Molecule, Concentration) = F(Molecule <= Concentration)').
:- initial('ltl_pattern inf_amplitude(Molecule, Amplitude) = exists(Concentration, F(Molecule <= Concentration /\\ F(Molecule >= Concentration+Amplitude)))').
:- initial('ltl_pattern sup_amplitude(Molecule, Amplitude) = exists(Concentration, G(Molecule >= Concentration /\\ Molecule <= Concentration+Amplitude))').

:- initial('ltl_pattern first_value(Molecule, Concentration) = (Molecule=Concentration)').
:- initial('ltl_pattern last_value(Molecule, Concentration) = F(G(Molecule=Concentration))').

% non linear constraints
%:- initial('ltl_pattern value(Molecule, T, Concentration) = F(exists(c, exists(t1, exists(t2, Molecule=c /\\ Time=t1 /\\ t1<=T /\\ X(Time=t2 /\\ t2>T /\\ Concentration=c+(T-t1)*(Molecule-c)/(t2-t1))))))').
%:- initial('ltl_pattern differential(Molecule, T, Diff) = F(exists(Concentration, exists(t1, exists(t2, Molecule=Concentration /\\ Time=t1 /\\ t1<=T /\\ X(Time=t2 /\\ t2>T /\\ Diff=(Molecule-Concentration)/(t2-t1))))))').
%:- initial('ltl_pattern differential(Molecule, Diff) = exists(Concentration, exists(t1, exists(t2, Molecule=Concentration /\\ Time=t1 /\\ X(Time=t2 /\\ Diff=(Molecule-Concentration)/(t2-t1)))))').

FAGES Francois's avatar
FAGES Francois committed
269
270
271
272
273
%:- initial('ltl_pattern maximum(Molecule, Concentration) = (G(Molecule <= Concentration) /\\ F(Molecule=Concentration))').
:- initial('ltl_pattern maximum(Molecule, Concentration) = (G(Molecule <= Concentration) /\\ F(Molecule>=Concentration))').
:- initial('ltl_pattern minimum(Molecule, Concentration) = (G(Molecule >= Concentration) /\\ F(Molecule<=Concentration))').
:- initial('ltl_pattern maximum(Molecule, Concentration, T) = (G(Molecule <= Concentration) /\\ F(Molecule>=Concentration /\\ Time=T))').
:- initial('ltl_pattern minimum(Molecule, Concentration, T) = (G(Molecule >= Concentration) /\\ F(Molecule<=Concentration /\\ Time=T))').
FAGES Francois's avatar
foltl    
FAGES Francois committed
274
275

:- initial('ltl_pattern amplitude(Molecule, Amplitude) = exists(Minimum, exists(Maximum, maximum(Molecule,Maximum) /\\ minimum(Molecule, Minimum) /\\ Amplitude=Maximum-Minimum))').
FAGES Francois's avatar
FAGES Francois committed
276
277
%:- initial('ltl_pattern amplitude2(Molecule, Amplitude) = exists(Minimum, minimum(Molecule,Minimum) /\\ maximum(Molecule, Minimum+Amplitude))'). % infinite loop in PPL ? why ?
%:- initial('ltl_pattern amplitude3(Molecule, Amplitude) = exists(Minimum, (G(Minimum <= Molecule /\\ Molecule <= Minimum+Amplitude) /\\ F(Molecule=Minimum) /\\ F(Molecule=Minimum+Amplitude)))'). 
FAGES Francois's avatar
foltl    
FAGES Francois committed
278
279
280
281


:- doc('\\emph{Local extrema}').

FAGES Francois's avatar
FAGES Francois committed
282
283
284
%:- initial('ltl_pattern increase(Molecule, Concentration) = ((Molecule=Concentration) /\\ X(Molecule>=Concentration))').
:- initial('ltl_pattern increase(Molecule, Concentration) = ((Molecule<=Concentration) /\\ X(Molecule>=Concentration))').
:- initial('ltl_pattern decrease(Molecule, Concentration) = ((Molecule>=Concentration) /\\ X(Molecule<=Concentration))').
FAGES Francois's avatar
foltl    
FAGES Francois committed
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
:- initial('ltl_pattern increase(Molecule) = exists(Concentration, increase(Molecule, Concentration))').
:- initial('ltl_pattern decrease(Molecule) = exists(Concentration, decrease(Molecule, Concentration))').

:- initial('ltl_pattern local_maximum(Molecule, Concentration) = F(peak(Molecule,Concentration))').
:- initial('ltl_pattern local_maximum(Molecule, Concentration, T) = F(peak(Molecule,Concentration,T))').
:- initial('ltl_pattern peak(Molecule, Concentration) = (Molecule<Concentration /\\ X(decrease(Molecule, Concentration)))').
:- initial('ltl_pattern peak(Molecule, Concentration, T) = (Molecule<Concentration /\\ X(decrease(Molecule, Concentration) /\\ Time=T))').
:- initial('ltl_pattern first_peak(Molecule, Concentration, T) = U(decrease(Molecule), U(increase(Molecule), peak(Molecule, Concentration, T)))').
:- initial('ltl_pattern first_peak(Molecule, Concentration) = U(decrease(Molecule), U(increase(Molecule), peak(Molecule, Concentration)))').
:- initial('ltl_pattern last_peak(Molecule, Concentration, T) = F(peak(Molecule, Concentration, T) /\\ X(U(decrease(Molecule), U(increase(Molecule), exists(c, G(Molecule=c))))))').
:- initial('ltl_pattern last_peak(Molecule, Concentration) = F(peak(Molecule, Concentration /\\ X(U(decrease(Molecule), U(increase(Molecule), exists(c, G(Molecule=c)))))))').
:- initial('ltl_pattern successive_peaks(Molecule, C1, T1, C2, T2) = F(U(decrease(Molecule), U(increase(Molecule), peak(Molecule,C1, T1) /\\ X(first_peak(Molecule, C2, T2)))))').
:- initial('ltl_pattern successive_peaks(Molecule, C1, C2) = F(U(decrease(Molecule), U(increase(Molecule), peak(Molecule,C1) /\\ X(first_peak(Molecule, C2)))))').
:- initial('ltl_pattern first_successive_peaks(Molecule, C1, T1, C2, T2) = U(decrease(Molecule), U(increase(Molecule), peak(Molecule,C1, T1) /\\ X(first_peak(Molecule, C2, T2))))').
:- initial('ltl_pattern last_successive_peaks(Molecule, C1, T1, C2, T2) = F(peak(Molecule,C1, T1) /\\ X(U(decrease(Molecule), U(increase(Molecule), peak(Molecule,C2, T2) /\\ X(U(decrease(Molecule), U(increase(Molecule), exists(c, G(Molecule=c)))))))))').

:- initial('ltl_pattern local_minimum(Molecule, Concentration) = F(base(Molecule,Concentration))').
:- initial('ltl_pattern local_minimum(Molecule, Concentration, T) = F(base(Molecule,Concentration,T))').
:- initial('ltl_pattern base(Molecule, Concentration) = (Molecule>Concentration /\\ X(increase(Molecule, Concentration)))').
:- initial('ltl_pattern base(Molecule, Concentration, T) = (Molecule>Concentration /\\ X(increase(Molecule, Concentration) /\\ Time=T))').
:- initial('ltl_pattern first_base(Molecule, Concentration, T) = U(increase(Molecule), U(decrease(Molecule), base(Molecule, Concentration, T)))').
:- initial('ltl_pattern first_base(Molecule, Concentration) = U(increase(Molecule), U(decrease(Molecule), base(Molecule, Concentration)))').
:- initial('ltl_pattern last_base(Molecule, Concentration, T) = F(base(Molecule, Concentration, T) /\\ X(U(increase(Molecule), U(decrease(Molecule), exists(c, G(Molecule=c))))))').
:- initial('ltl_pattern last_base(Molecule, Concentration) = F(base(Molecule, Concentration /\\ X(U(increase(Molecule), U(decrease(Molecule), exists(c, G(Molecule=c)))))))').
:- initial('ltl_pattern successive_bases(Molecule, C1, T1, C2, T2) = F(U(increase(Molecule), U(decrease(Molecule), base(Molecule,C1, T1) /\\ X(first_base(Molecule, C2, T2)))))').
:- initial('ltl_pattern successive_bases(Molecule, C1, C2) = F(U(increase(Molecule), U(decrease(Molecule), base(Molecule,C1) /\\ X(first_base(Molecule, C2)))))').
:- initial('ltl_pattern first_successive_bases(Molecule, C1, T1, C2, T2) = U(increase(Molecule), U(decrease(Molecule), base(Molecule,C1, T1) /\\ X(first_base(Molecule, C2, T2))))').
:- initial('ltl_pattern last_successive_bases(Molecule, C1, T1, C2, T2) = F(base(Molecule,C1, T1) /\\ X(U(increase(Molecule), U(decrease(Molecule), base(Molecule,C2, T2) /\\ X(U(increase(Molecule), U(decrease(Molecule), exists(c, G(Molecule=c)))))))))').

:- doc('\\emph{Periods and delays.}').


:- initial('ltl_pattern period(Molecule, Period) = exists(T1, exists(T2, exists(C1, exists(C2, successive_peaks(Molecule, C1, T1, C2, T2) /\\ Period=T2-T1))))').
:- initial('ltl_pattern first_period(Molecule, Period) =  exists(T1, exists(T2, exists(C1, exists(C2, first_successive_peaks(Molecule, C1, T1, C2, T2) /\\ Period=T2-T1))))').
:- initial('ltl_pattern last_period(Molecule, Period) =  exists(T1, exists(T2, exists(C1, exists(C2, last_successive_peaks(Molecule, C1, T1, C2, T2) /\\ Period=T2-T1))))').
:- initial('ltl_pattern delay(Molecule1, Molecule2, Delay) = exists(T1, exists(T2, exists(C1, exists(C2, F(peak(Molecule1,C1,T1) /\\ first_peak(Molecule2,C2,T2) /\\ Delay=T2-T1)))))').
:- initial('ltl_pattern first_delay(Molecule1, Molecule2, Delay) = exists(T1, exists(T2, exists(C1, exists(C2, U(decrease(Molecule1), U(increase(Molecule1), peak(Molecule1, C1, T1) /\\ first_peak(Molecule2,C2,T2) /\\ Delay=T2-T1))))))').
% first_peak(Molecule1,C1,T1) /\\ first_peak(Molecule2,C2,T2) /\\ Delay=T2-T1))))').
:- initial('ltl_pattern last_delay(Molecule1, Molecule2, Delay) =  exists(T1, exists(T2, exists(C1, exists(C2, last_peak(Molecule1,C1,T1) /\\ last_peak(Molecule2,C2,T2) /\\ Delay=T2-T1))))').


:- initial('ltl_pattern base_period(Molecule, Period) = exists(T1, exists(T2, exists(C1, exists(C2, successive_bases(Molecule, C1, T1, C2, T2) /\\ Period=T2-T1))))').
:- initial('ltl_pattern first_base_period(Molecule, Period) =  exists(T1, exists(T2, exists(C1, exists(C2, first_successive_bases(Molecule, C1, T1, C2, T2) /\\ Period=T2-T1))))').
:- initial('ltl_pattern last_base_period(Molecule, Period) =  exists(T1, exists(T2, exists(C1, exists(C2, last_successive_bases(Molecule, C1, T1, C2, T2) /\\ Period=T2-T1))))').
:- initial('ltl_pattern base_delay(Molecule1, Molecule2, Delay) = exists(T1, exists(T2, exists(C1, exists(C2, F(base(Molecule1,C1,T1) /\\ first_base(Molecule2,C2,T2) /\\ Delay=T2-T1)))))').
:- initial('ltl_pattern first_base_delay(Molecule1, Molecule2, Delay) = exists(T1, exists(T2, exists(C1, exists(C2, U(increase(Molecule1), U(decrease(Molecule1), base(Molecule1, C1, T1) /\\ first_base(Molecule2,C2,T2) /\\ Delay=T2-T1))))))').
% first_base(Molecule1,C1,T1) /\\ first_base(Molecule2,C2,T2) /\\ Delay=T2-T1))))').
:- initial('ltl_pattern last_base_delay(Molecule1, Molecule2, Delay) =  exists(T1, exists(T2, exists(C1, exists(C2, last_base(Molecule1,C1,T1) /\\ last_base(Molecule2,C2,T2) /\\ Delay=T2-T1))))').



Thierry Martinez's avatar
Thierry Martinez committed
336

FAGES Francois's avatar
doc    
FAGES Francois committed
337
338
:- devdoc('\\section{Commands}').

FAGES Francois's avatar
FAGES Francois committed
339
:- doc('FO-LTL(Rlin) formulas are evaluated on traces, i.e. numerical data time series either generated by simulation or loaded from biological experiments. FO-LTL(Rlin) formulas may contain free variables, in which case they are called constraints. While a closed formula (i.e. without free variable) is either true or false on a trace, a formula with free variable is either satisfiable (i.e. true for some valuations of the variables) or unsatisfiable (i.e. false for any valuation).').
Thierry Martinez's avatar
Thierry Martinez committed
340

Thierry Martinez's avatar
FOLTL    
Thierry Martinez committed
341
342
343
344
validity_domain(Formula) :-
  biocham_command,
  type(Formula, foltl),
  doc('
FAGES Francois's avatar
FAGES Francois committed
345
    solves a FOLTL(Rlin) constraint on the current trace, i.e. computes
Thierry Martinez's avatar
FOLTL    
Thierry Martinez committed
346
347
    the validity domain for the free variables
    that make the formula true on the numerical trace.
348
    The formula is false if the validity domain of one of its free variables is empty, and satisfiable otherwise.
Thierry Martinez's avatar
Thierry Martinez committed
349
350
351
\\clearmodel
\\begin{example}
\\trace{
FAGES Francois's avatar
FAGES Francois committed
352
353
biocham: a => b.
biocham: present(a,10).
354
355
biocham: numerical_simulation. plot.
biocham: validity_domain(G(Time < T => a > b)).
Thierry Martinez's avatar
Thierry Martinez committed
356
357
358
}
\\end{example}
  '),
FAGES Francois's avatar
FAGES Francois committed
359
  option(foltl_magnitude, number, _Magnitude, 'order of magnitude for << and >> operators'),
360
361
362
  validity_domain(Formula, Domain),
  format('~w\n', [Domain]).

Thierry Martinez's avatar
Thierry Martinez committed
363

364
satisfaction_degree(Formula, Objective) :-
365
366
  biocham_command,
  type(Formula, foltl),
367
  type(Objective, [variable_objective]),
FAGES Francois's avatar
FAGES Francois committed
368
  option(foltl_magnitude, number, _Magnitude, 'order of magnitude for << and >> operators'),
369
  doc('
FAGES Francois's avatar
FAGES Francois committed
370
371
372
373
    computes a continuous satisfaction degree in the interval [0,+∞) for an
    FOLTL(Rlin) property on the current trace with respect to some objective values for the free variables of the formula. The degree is greater or
    equal than 1 if the formula is satisfied. The greater the degree the greater the margin in the satisfaction of the formula (i.e. formula satisfaction robustness).
    This satisfaction degree is computed by (an approximation of) the distance of the objective point to the validity domain of the formula (if less than 1), or by its penetration depth (if greater than 1).
374
  '),
375
376
377
  satisfaction_degree(Formula, Objective, Degree),
  format('~f\n', [Degree]).

378

Thierry Martinez's avatar
Thierry Martinez committed
379
380
ltl_pattern(PatternList) :-
  biocham_command(*),
381
  type(PatternList, '*'(function_prototype = foltl)),
Thierry Martinez's avatar
Thierry Martinez committed
382
383
384
385
  doc('sets the definition of patterns.'),
  \+ (
    member(Pattern = Value, PatternList),
    \+ (
386
      set_macro(ltl_pattern, Pattern, Value)
Thierry Martinez's avatar
Thierry Martinez committed
387
388
389
390
391
392
393
394
395
396
    )
  ).


list_ltl_patterns :-
  biocham_command,
  doc('lists all known LTL patterns.'),
  list_items([kind: ltl_pattern]).


FAGES Francois's avatar
FAGES Francois committed
397
398
399
400
401
402
403
expand_ltl(Formula) :-
  biocham_command,
  type(Formula, foltl),
  doc('shows the expansion in LTL of a formula with patterns.'),
  expand_formula(Formula,ExpandedFormula),
  format('~w\n', [ExpandedFormula]).

Thierry Martinez's avatar
Thierry Martinez committed
404
delete_ltl_pattern(FunctorSet) :-
405
406
407
408
  biocham_command(*),
  type(FunctorSet, '*'(functor)),
  doc('deletes some LTL patterns. Either arity is given, or all LTL patterns with
  the given functor are deleted.'),
Thierry Martinez's avatar
Thierry Martinez committed
409
410
411
  \+ (
    member(Functor, FunctorSet),
    \+ (
412
      delete_macro(ltl_pattern, Functor)
Thierry Martinez's avatar
Thierry Martinez committed
413
414
415
416
    )
  ).


Thierry Martinez's avatar
FOLTL    
Thierry Martinez committed
417
418
419
420
:- devdoc('\\section{Public API}').


validity_domain(Formula, Domain) :-
421
422
423
424
425
426
427
428
429
430
431
432
433
434
  compute_domain('ppl_validity_domain.cc', Formula, none, DomainRaw),
  reformat_domain(DomainRaw, Domain).


satisfaction_degree(Formula, Objective, SatisfactionDegree) :-
  compute_domain(
    'satisfaction_degree.cc', Formula, some(Objective), SatisfactionDegree
  ).


:- devdoc('\\section{Private predicates}').


compute_domain(CppProgram, Formula, Objective, Result) :-
435
436
  CFilename = 'check.inc',
  TableFilename = 'check.csv',
Thierry Martinez's avatar
FOLTL    
Thierry Martinez committed
437
  ExecutableFilename = 'check',
Thierry Martinez's avatar
Thierry Martinez committed
438
  expand_formula(Formula, ExpandedFormula),
Thierry Martinez's avatar
Thierry Martinez committed
439
440
441
442
  with_clean(
    [foltl:column/2],
    (
      populate_table_columns,
443
444
445
446
447
448
449
      with_output_to_file(
        CFilename,
        (
          generate_domain_cpp(ExpandedFormula),
          (
            Objective = some(Objective0)
          ->
450
            generate_objective([Objective0])
451
452
453
454
455
          ;
            true
          )
        )
      )
Thierry Martinez's avatar
Thierry Martinez committed
456
457
    )
  ),
458
  compile_domain_cpp_program(CppProgram, ExecutableFilename),
459
  export_table(TableFilename),
Thierry Martinez's avatar
FOLTL    
Thierry Martinez committed
460
  call_subprocess(
461
    ExecutableFilename, [TableFilename], [stdout(pipe(ResultStream))]
Thierry Martinez's avatar
FOLTL    
Thierry Martinez committed
462
463
464
  ),
  read_term(
    ResultStream,
465
    Result,
Thierry Martinez's avatar
FOLTL    
Thierry Martinez committed
466
467
468
    [variables(Variables), variable_names(VariableNames)]),
  name_variables_and_anonymous(Variables, VariableNames),
  !,
469
470
471
472
473
474
475
476
477
  (
    have_to_delete_temporary_files
  ->
    delete_file(CFilename),
    delete_file(ExecutableFilename),
    delete_file(TableFilename)
  ;
    true
  ).
Thierry Martinez's avatar
Thierry Martinez committed
478

Thierry Martinez's avatar
FOLTL    
Thierry Martinez committed
479

Thierry Martinez's avatar
Thierry Martinez committed
480
481
482
483
484
485
486
487
488
populate_table_columns :-
  get_current_table(Table),
  \+ (
    columns(Table, Index, X),
    \+ (
      assertz(column(X, Index))
    )
  ).

FAGES Francois's avatar
FAGES Francois committed
489
490


FAGES Francois's avatar
FAGES Francois committed
491
492
493
expand_formula(curve(Molecule, Time_Value_List), ExpandedFormula):-
  !,
  expand_curve(Time_Value_List, Molecule, ExpandedFormula).
Thierry Martinez's avatar
Thierry Martinez committed
494
495

expand_formula(F => G, ExpandedFormula) :-
FAGES Francois's avatar
FAGES Francois committed
496
  !,
Thierry Martinez's avatar
Thierry Martinez committed
497
  expand_formula(not(F) \/ G, ExpandedFormula).
FAGES Francois's avatar
FAGES Francois committed
498

Thierry Martinez's avatar
Thierry Martinez committed
499
expand_formula(not(Formula), ExpandedNegatedFormula) :-
Thierry Martinez's avatar
Thierry Martinez committed
500
  !,
Thierry Martinez's avatar
Thierry Martinez committed
501
502
503
504
505
506
507
508
  expand_formula(Formula, ExpandedFormula),
  (
    negate_formula(ExpandedFormula, NegatedFormula)
  ->
    expand_formula(NegatedFormula, ExpandedNegatedFormula)
  ;
    ExpandedNegatedFormula = not(ExpandedFormula)
  ).
Thierry Martinez's avatar
Thierry Martinez committed
509
510

expand_formula(Formula, ExpandedFormula) :-
Thierry Martinez's avatar
Thierry Martinez committed
511
  macro_apply(ltl_pattern, Formula, NewBody),
Thierry Martinez's avatar
Thierry Martinez committed
512
513
514
  !,
  expand_formula(NewBody, ExpandedFormula).

FAGES Francois's avatar
ppl    
FAGES Francois committed
515
516
517
518
519
520
521
522
523
524
525
%expand_formula((A < B), (A+P <= B)):- % let us believe in the precision option
%    !,
%    get_option(precision, FOLTLPrecision),
%    P is 10 ** (-FOLTLPrecision).
%
%expand_formula((A > B), (A >= B+P)):-
%    !,
%    get_option(precision, FOLTLPrecision),
%    P is 10 ** (-FOLTLPrecision).

expand_formula((A << B), R):- 
526
    !,
FAGES Francois's avatar
ppl    
FAGES Francois committed
527
528
    get_option(foltl_magnitude, M),
    expand_formula((M*A < B), R).
529

FAGES Francois's avatar
ppl    
FAGES Francois committed
530
expand_formula((A >> B), R):- 
FAGES Francois's avatar
FAGES Francois committed
531
    !,
FAGES Francois's avatar
ppl    
FAGES Francois committed
532
533
    get_option(foltl_magnitude, M),
    expand_formula((A > M*B), R).
FAGES Francois's avatar
FAGES Francois committed
534

FAGES Francois's avatar
FAGES Francois committed
535
536
537
538
539
540
541
expand_formula(true, ExpandedFormula):- % ...
    !,
    expand_formula(1<1000, ExpandedFormula).

expand_formula(false, ExpandedFormula):- 
    !,
    expand_formula(1>1000, ExpandedFormula).
FAGES Francois's avatar
FAGES Francois committed
542

Thierry Martinez's avatar
Thierry Martinez committed
543
expand_formula(Formula, ExpandedFormula) :-
544
545
  grammar_map(
    foltl,
Thierry Martinez's avatar
Thierry Martinez committed
546
547
    [foltl:foltl:expand_formula,
     foltl_expression:foltl:expand_expression],
548
549
550
    Formula,
    ExpandedFormula
  ).
Thierry Martinez's avatar
Thierry Martinez committed
551
552


553
expand_curve([Time:Value], Molecule, 'F'('Time'=Time /\ Molecule=Value)) :-
FAGES Francois's avatar
FAGES Francois committed
554
555
556
557
558
    !.

expand_curve([(Time:Value)|Tail], Molecule, 'Time'=Time /\ Molecule=Value /\ 'X'('F'(TailFormula))) :-
    expand_curve(Tail, Molecule, TailFormula).

559

Thierry Martinez's avatar
Thierry Martinez committed
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
expand_expression(Expression, ExpandedExpression) :-
  function_apply(Expression, NewBody),
  !,
  expand_expression(NewBody, ExpandedExpression).


expand_expression(Expression, ExpandedExpression) :-
  grammar_map(
    foltl_expression,
    [foltl_expression:foltl:expand_expression],
    Expression,
    ExpandedExpression
  ).


FAGES Francois's avatar
FAGES Francois committed
575
576
577
578
negate_formula(false, true).

negate_formula(true, false).

FAGES Francois's avatar
FAGES Francois committed
579
negate_formula(not(A), A).
Thierry Martinez's avatar
Thierry Martinez committed
580

FAGES Francois's avatar
FAGES Francois committed
581
582
583
negate_formula(A \/ B, NotA /\ NotB) :-
  negate_formula(A, NotA),
  negate_formula(B, NotB).
Thierry Martinez's avatar
Thierry Martinez committed
584

FAGES Francois's avatar
FAGES Francois committed
585
586
587
negate_formula(A /\ B, NotA \/ NotB) :-
  negate_formula(A, NotA),
  negate_formula(B, NotB).
Thierry Martinez's avatar
Thierry Martinez committed
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605

negate_formula('X'(A), 'X'(NotA)) :-
  negate_formula(A, NotA).

negate_formula('F'(A), 'G'(NotA)) :-
  negate_formula(A, NotA).

negate_formula('G'(A), 'F'(NotA)) :-
  negate_formula(A, NotA).

negate_formula('U'(A, B), 'W'(NotB, NotA)) :-
  negate_formula(A, NotA),
  negate_formula(B, NotB).

negate_formula('W'(A, B), 'U'(NotB, NotA)) :-
  negate_formula(A, NotA),
  negate_formula(B, NotB).

FAGES Francois's avatar
FAGES Francois committed
606
607
608
609
negate_formula('R'(A, B), 'U'(NotA, NotB)) :-
  negate_formula(A, NotA),
  negate_formula(B, NotB).

FAGES Francois's avatar
FAGES Francois committed
610
negate_formula(A <= B, A > B).
Thierry Martinez's avatar
Thierry Martinez committed
611

FAGES Francois's avatar
FAGES Francois committed
612
613
614
615
616
617
negate_formula(A > B, A <= B).

negate_formula(A >= B, A < B).

negate_formula(A < B, A >= B).

618
negate_formula(A = B, A <> B). % A < B \/ A > B).
FAGES Francois's avatar
FAGES Francois committed
619
620
621
622
623
624

negate_formula(A << B, M*A >= B):-
    get_option(foltl_magnitude, M).   

negate_formula(A >> B, A <= M*B):-
    get_option(foltl_magnitude, M).   
Thierry Martinez's avatar
Thierry Martinez committed
625
626


Thierry Martinez's avatar
Thierry Martinez committed
627
628
629
630
631
prolog:message(error(unknown_free_variable(Variable))) -->
  ['Unknown free variable: ~p'-[Variable]].


:- dynamic(free_variable_index/2).
Thierry Martinez's avatar
FOLTL    
Thierry Martinez committed
632
633
634
635
636


:- dynamic(sub_formula/2).


Thierry Martinez's avatar
Thierry Martinez committed
637
638
639
:- dynamic(column/2).


640
generate_objective(Objectives) :-
FAGES Francois's avatar
FAGES Francois committed
641
  get_option(precision, FOLTLPrecision),
642
643
  Precision is 10 ** FOLTLPrecision,
  format('#define FOLTL_PRECISION ~d\n', [Precision]),
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
  maplist(length, Objectives, Counts),
  write('static const int objective_count[] = {\n'),
  forall(
    member(Count, Counts),
    format('   ~d,~n', [Count])
  ),
  write('};\n'),
  forall(
    nth0(Index, Objectives, Objective),
    generate_objective(Index, Objective)
  ),
  length(Objectives, NObjectives),
  MaxObj is NObjectives - 1,
  write('static const struct variable_objective *var_obj[] = {\n'),
  forall(
    between(0, MaxObj, I),
    format('    variable_objectives~d,~n', [I])
  ),
  write('};\n').


generate_objective(Index, Objective) :-
666
  length(Objective, ObjectiveCount),
667
668
  write('static const struct variable_objective\n'),
  format('variable_objectives~d[~d] = {~n', [Index, ObjectiveCount]),
669
670
671
672
  \+ (
    member((Variable -> Value), Objective),
    \+ (
      (
673
674
675
676
677
        foltl:free_variable_index(Variable, VariableIndex)
      ->
        true
      ;
        search:free_variable_index(Index, Variable, VariableIndex)
678
679
680
681
682
683
684
685
686
687
688
      ->
        true
      ;
        throw(error(unknown_free_variable(Variable)))
      ),
      format('    {~d, ~f},\n', [VariableIndex, Value])
    )
  ),
  write('};\n').


Thierry Martinez's avatar
Thierry Martinez committed
689
generate_domain_cpp(Formula) :-
690
691
692
693
  generate_domain_cpp(Formula, '').


generate_domain_cpp(Formula, FuncSuffix) :-
Thierry Martinez's avatar
Thierry Martinez committed
694
  expand_formula(Formula, ExpandedFormula),
Thierry Martinez's avatar
FOLTL    
Thierry Martinez committed
695
  set_counter(free_variables, 0),
Thierry Martinez's avatar
Thierry Martinez committed
696
  retractall(free_variable_index(_, _)),
Thierry Martinez's avatar
FOLTL    
Thierry Martinez committed
697
698
  set_counter(sub_formulae, 0),
  retractall(sub_formula(_, _)),
699
  format(
Thierry Martinez's avatar
FOLTL    
Thierry Martinez committed
700
    '\c
Thierry Martinez's avatar
Thierry Martinez committed
701
static Domain
702
703
compute_domain~w(const Table &table) {
', [FuncSuffix]),
Thierry Martinez's avatar
Thierry Martinez committed
704
  declare_free_variables(ExpandedFormula),
Thierry Martinez's avatar
Thierry Martinez committed
705
  check_linearity(ExpandedFormula),
Thierry Martinez's avatar
Thierry Martinez committed
706
  declare_formula(ExpandedFormula, RootIndex),
Thierry Martinez's avatar
FOLTL    
Thierry Martinez committed
707
708
  write(
    '    \c
Thierry Martinez's avatar
Thierry Martinez committed
709
710
    for (Table::const_reverse_iterator i = table.rbegin(); i != table.rend();
            ++i) {
Thierry Martinez's avatar
FOLTL    
Thierry Martinez committed
711
712
713
714
715
716
717
718
719
720
721
722
723
'),
  \+ (
    sub_formula(SubFormula, Index),
    \+ (
      generate_sub_formula(SubFormula, Index)
    )
  ),
  \+ (
    sub_formula(_SubFormula, Index),
    \+ (
      format('        domain~d = next_domain~d;\n', [Index, Index])
    )
  ),
Thierry Martinez's avatar
Thierry Martinez committed
724
  format(
Thierry Martinez's avatar
FOLTL    
Thierry Martinez committed
725
726
    '    \c
    }
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
727
    domain~d.pairwise_reduce();
Thierry Martinez's avatar
Thierry Martinez committed
728
    return domain~d;
Thierry Martinez's avatar
FOLTL    
Thierry Martinez committed
729
}
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
730
', [RootIndex, RootIndex]).
Thierry Martinez's avatar
FOLTL    
Thierry Martinez committed
731
732
733


generate_sub_formula(Predicate, Index) :-
FAGES Francois's avatar
FAGES Francois committed
734
  get_option(precision, FOLTLPrecision),
735
  Precision is 10 ** FOLTLPrecision,
Thierry Martinez's avatar
Thierry Martinez committed
736
  binary_predicate(Predicate, A, Op, B),
Thierry Martinez's avatar
FOLTL    
Thierry Martinez committed
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
  !,
  with_output_to(
    atom(Left),
    generate_expression(A)
  ),
  with_output_to(
    atom(Right),
    generate_expression(B)
  ),
  peek_count(free_variables, FreeVariableCount),
  (
    (
      has_variable(A)
    ;
      has_variable(B)
    )
  ->
    format(
      '        \c
        Domain next_domain~d(~d, PPL::EMPTY);
Thierry Martinez's avatar
Thierry Martinez committed
757
758
        {
          PPL::Constraint_System cs;
Thierry Martinez's avatar
Thierry Martinez committed
759
          cs.set_space_dimension(~d);
760
761
          cs.insert((~a) * ~d ~a (~a) * ~d);
          next_domain~d.add_disjunct(PPL::NNC_Polyhedron(cs));
Thierry Martinez's avatar
Thierry Martinez committed
762
        }
Thierry Martinez's avatar
FOLTL    
Thierry Martinez committed
763
',
764
        [Index, FreeVariableCount, FreeVariableCount, Left, Precision,
FAGES Francois's avatar
FAGES Francois committed
765
         Op, Right, Precision, Index]
Thierry Martinez's avatar
FOLTL    
Thierry Martinez committed
766
767
768
769
    )
  ;
    format(
      '        \c
Thierry Martinez's avatar
Thierry Martinez committed
770
        Domain next_domain~d(~d, ~a ~a ~a ? PPL::UNIVERSE : PPL::EMPTY);
Thierry Martinez's avatar
FOLTL    
Thierry Martinez committed
771
',
Thierry Martinez's avatar
Thierry Martinez committed
772
        [Index, FreeVariableCount, Left, Op, Right]
Thierry Martinez's avatar
FOLTL    
Thierry Martinez committed
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
    )
  ).

generate_sub_formula('X'(A), Index) :-
  sub_formula(A, AIndex),
  format(
    '        \c
        Domain next_domain~d(domain~d);
',
    [Index, AIndex]
  ).

generate_sub_formula('F'(A), Index) :-
  sub_formula(A, AIndex),
  format(
    '        \c
        Domain next_domain~d(domain~d);
        for (
            Domain::iterator j = next_domain~d.begin();
            j != next_domain~d.end(); ++j
        ) {
            next_domain~d.add_disjunct(j->pointset());
        }
',
    [Index, Index, AIndex, AIndex, Index]
  ).

generate_sub_formula('G'(A), Index) :-
  sub_formula(A, AIndex),
  format(
    '        \c
        Domain next_domain~d(domain~d);
        next_domain~d.intersection_assign(next_domain~d);
',
Thierry Martinez's avatar
Thierry Martinez committed
807
    [Index, Index, Index, AIndex]
Thierry Martinez's avatar
FOLTL    
Thierry Martinez committed
808
809
810
811
812
813
814
815
  ).

generate_sub_formula('U'(A, B), Index) :-
  generate_sub_formula_u_or_w(A, B, Index).

generate_sub_formula('W'(A, B), Index) :-
  generate_sub_formula_u_or_w(A, B, Index).

816
generate_sub_formula('R'(A, B), Index) :-
FAGES Francois's avatar
FAGES Francois committed
817
818
  generate_sub_formula_u_or_w(A, B, Index).

Thierry Martinez's avatar
FOLTL    
Thierry Martinez committed
819
820
821
822
823
824
825
826
827
828
829
830
831
832
generate_sub_formula(A /\ B, Index) :-
  sub_formula(A, AIndex),
  sub_formula(B, BIndex),
  format(
    '        \c
        Domain next_domain~d(next_domain~d);
        next_domain~d.intersection_assign(next_domain~d);
',
    [Index, AIndex, Index, BIndex]
  ).

generate_sub_formula(A \/ B, Index) :-
  sub_formula(A, AIndex),
  sub_formula(B, BIndex),
Thierry Martinez's avatar
Thierry Martinez committed
833
  format('
Thierry Martinez's avatar
FOLTL    
Thierry Martinez committed
834
835
836
837
838
839
840
841
842
843
844
        Domain next_domain~d(next_domain~d);
        for (
            Domain::iterator j = next_domain~d.begin();
            j != next_domain~d.end(); ++j
        ) {
            next_domain~d.add_disjunct(j->pointset());
        }
',
    [Index, AIndex, BIndex, BIndex, Index]
  ).

Thierry Martinez's avatar
Thierry Martinez committed
845
846
847
848
849
850
851
852
853
854
generate_sub_formula(exists(X, A), Index) :-
  sub_formula(A, AIndex),
  free_variable_index(X, XIndex),
  format('
        Domain next_domain~d(next_domain~d);
        next_domain~d.unconstrain(x~d);
',
    [Index, AIndex, Index, XIndex]
  ).

Thierry Martinez's avatar
Thierry Martinez committed
855
generate_sub_formula(not(_A), _Index) :-
FAGES Francois's avatar
FAGES Francois committed
856
  throw(error('LTL \'not\' is not implemented yet')). % ???
Thierry Martinez's avatar
Thierry Martinez committed
857

Thierry Martinez's avatar
FOLTL    
Thierry Martinez committed
858
859
860
generate_sub_formula_u_or_w(A, B, Index) :-
  sub_formula(A, AIndex),
  sub_formula(B, BIndex),
Thierry Martinez's avatar
Thierry Martinez committed
861
  format('
Thierry Martinez's avatar
FOLTL    
Thierry Martinez committed
862
        Domain next_domain~d(domain~d);
863
        next_domain~d.intersection_assign(next_domain~d);
Thierry Martinez's avatar
FOLTL    
Thierry Martinez committed
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
        for (
            Domain::iterator j = next_domain~d.begin();
            j != next_domain~d.end(); ++j
        ) {
            next_domain~d.add_disjunct(j->pointset());
        }
',
    [Index, Index, Index, AIndex, BIndex, BIndex, Index]
  ).


generate_expression(Number) :-
  number(Number),
  !,
  format('~w', [Number]).

generate_expression(A) :-
Thierry Martinez's avatar
Thierry Martinez committed
881
  free_variable_index(A, Index),
Thierry Martinez's avatar
FOLTL    
Thierry Martinez committed
882
883
884
885
  !,
  format('x~d', [Index]).

generate_expression(A) :-
886
887
888
889
890
891
  (
    column(A, Index)
  ;
    A = [Object],
    column(Object, Index)
  ),
Thierry Martinez's avatar
FOLTL    
Thierry Martinez committed
892
893
894
  !,
  format('(*i)[~d]', [Index]).

895
896
897
898
899
900
901
902
generate_expression(A^B) :-
  !,
  write('pow('),
  generate_expression(A),
  write(', '),
  generate_expression(B),
  write(')').

Thierry Martinez's avatar
Thierry Martinez committed
903
904
905
906
907
generate_expression(Expression) :-
  binary_operator(Expression, A, Op, B),
  !,
  generate_expression_with_parentheses(A),
  write(Op),
908
  generate_expression_with_parentheses(B).
Thierry Martinez's avatar
Thierry Martinez committed
909
910


Thierry Martinez's avatar
Thierry Martinez committed
911
912
913
914
binary_predicate(A = B, A, '==', B).

binary_predicate(A <> B, A, '!=', B).

FAGES Francois's avatar
FAGES Francois committed
915
916
917
918
binary_predicate(A << B, A, '<<', B).

binary_predicate(A >> B, A, '>>', B).

Thierry Martinez's avatar
Thierry Martinez committed
919
920
921
922
923
924
925
926
927
binary_predicate(A < B, A, '<', B).

binary_predicate(A <= B, A, '<=', B).

binary_predicate(A > B, A, '>', B).

binary_predicate(A >= B, A, '>=', B).


Thierry Martinez's avatar
Thierry Martinez committed
928
929
930
931
932
933
934
935
binary_operator(A + B, A, '+', B).

binary_operator(A - B, A, '-', B).

binary_operator(A * B, A, '*', B).

binary_operator(A / B, A, '/', B).

Thierry Martinez's avatar
Thierry Martinez committed
936
937


Thierry Martinez's avatar
Thierry Martinez committed
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
check_linearity(Formula) :-
  grammar_iter(
    foltl,
    [foltl_expression: foltl:check_linearity_expression],
    Formula
  ).


check_linearity_expression(Expression) :-
  (
    Expression = A + B
  ;
    Expression = A - B
  ),
  !,
  check_linearity_expression(A),
  check_linearity_expression(B).

check_linearity_expression(Expression) :-
  Expression = A * B,
  !,
  (
    has_variable(A),
    has_variable(B)
  ->
    throw(error(not_linear(Expression)))
  ;
    true
  ).

968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
check_linearity_expression(Expression) :-
  Expression = A ^ B,
  !,
  (
    (
      has_variable(A)
    ;
      has_variable(B)
    )
  ->
    throw(error(not_linear(Expression)))
  ;
    true
  ).

Thierry Martinez's avatar
Thierry Martinez committed
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
check_linearity_expression(Expression) :-
  Expression = A / B,
  !,
  (
    has_variable(B)
  ->
    throw(error(not_linear(Expression)))
  ;
    true
  ),
  check_linearity_expression(A).

check_linearity_expression(_).


prolog:message(error(not_linear(Expression))) -->
  ['Not linear: ~p'-[Expression]].

Thierry Martinez's avatar
Thierry Martinez committed
1001

Thierry Martinez's avatar
Thierry Martinez committed
1002
1003
1004
1005
1006
generate_expression_with_parentheses(A) :-
  write('('),
  generate_expression(A),
  write(')').

Thierry Martinez's avatar
FOLTL    
Thierry Martinez committed
1007
1008

has_variable(A) :-
Thierry Martinez's avatar
Thierry Martinez committed
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
  binary_operator(A, Left, _Op, Right),
  !,
  (
    has_variable(Left)
  ;
    has_variable(Right)
  ).

has_variable(A) :-
  is_free_variable(A).


is_free_variable(A) :-
Thierry Martinez's avatar
Thierry Martinez committed
1022
  free_variable_index(A, _Index).
Thierry Martinez's avatar
FOLTL    
Thierry Martinez committed
1023
1024
1025


declare_free_variables(Formula) :-
Thierry Martinez's avatar
Thierry Martinez committed
1026
1027
1028
1029
1030
  grammar_iter(
    foltl,
    [name: foltl:declare_free_variable_name],
    Formula
  ).
Thierry Martinez's avatar
FOLTL    
Thierry Martinez committed
1031

Thierry Martinez's avatar
Thierry Martinez committed
1032
declare_free_variable_name(X) :-
Thierry Martinez's avatar
FOLTL    
Thierry Martinez committed
1033
  (
Thierry Martinez's avatar
Thierry Martinez committed
1034
    column(X, _)
Thierry Martinez's avatar
FOLTL    
Thierry Martinez committed
1035
1036
1037
  ->
    true
  ;
Thierry Martinez's avatar
Thierry Martinez committed
1038
    free_variable_index(X, _Index)
Thierry Martinez's avatar
FOLTL    
Thierry Martinez committed
1039
1040
1041
1042
  ->
    true
  ;
    count(free_variables, Index),
Thierry Martinez's avatar
Thierry Martinez committed
1043
    asserta(free_variable_index(X, Index)),
Thierry Martinez's avatar
FOLTL    
Thierry Martinez committed
1044
1045
1046
1047
1048
    format('    PPL::Variable x~d(~d);\n', [Index, Index])
  ).


declare_formula(Formula) :-
Thierry Martinez's avatar
Thierry Martinez committed
1049
1050
1051
1052
  declare_formula(Formula, _Index).


declare_formula(Formula, Index) :-
Thierry Martinez's avatar
FOLTL    
Thierry Martinez committed
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
  (
    (
      Formula = 'G'(_)
    ;
      Formula = 'W'(_, _)
    )
  ->
    Initial = 'PPL::UNIVERSE'
  ;
    Initial = 'PPL::EMPTY'
  ),
1064
  declare_sub_formulae(Formula),
Thierry Martinez's avatar
Thierry Martinez committed
1065
  index_formula(Formula, Initial, Index).
Thierry Martinez's avatar
FOLTL    
Thierry Martinez committed
1066
1067
1068


declare_sub_formulae(Formula) :-
Thierry Martinez's avatar
Thierry Martinez committed
1069
1070
1071
1072
1073
  grammar_iter(
    foltl,
    [foltl: foltl:declare_formula],
    Formula
  ).
Thierry Martinez's avatar
FOLTL    
Thierry Martinez committed
1074
1075


Thierry Martinez's avatar
Thierry Martinez committed
1076
index_formula(Formula, Initial, Index) :-
Thierry Martinez's avatar
FOLTL    
Thierry Martinez committed
1077
  (
Thierry Martinez's avatar
Thierry Martinez committed
1078
    sub_formula(Formula, Index)
Thierry Martinez's avatar
FOLTL    
Thierry Martinez committed
1079
1080
1081
1082
1083
  ->
    true
  ;
    peek_count(free_variables, FreeVariableCount),
    count(sub_formulae, Index),
1084
    assertz(sub_formula(Formula, Index)),
Thierry Martinez's avatar
FOLTL    
Thierry Martinez committed
1085
1086
1087
1088
1089
1090
1091
    format(
      '    Domain domain~d(~d, ~a);\n',
      [Index, FreeVariableCount, Initial]
    )
  ).


1092
compile_domain_cpp_program(CppProgram, ExecutableFilename) :-
Thierry Martinez's avatar
Thierry Martinez committed
1093
1094
  ppl_compile_options(PplCompileOptions),
  compile_cpp_program(
1095
    [CppProgram, 'csv_reader.o'],
Thierry Martinez's avatar
Thierry Martinez committed
1096
1097
1098
1099
1100
    PplCompileOptions,
    ExecutableFilename
  ).


Thierry Martinez's avatar
FOLTL    
Thierry Martinez committed
1101
1102
1103
reformat_domain({ Conjunction0 }, Conjunction1) :-
  reformat_domain_conjunction(Conjunction0, Conjunction1).

FAGES Francois's avatar
FAGES Francois committed
1104
reformat_domain((A0, B0), B1 \/ A1) :- % reversed order
Thierry Martinez's avatar
FOLTL    
Thierry Martinez committed
1105
1106
  reformat_domain(A0, A1),
  reformat_domain(B0, B1).
1107
1108
1109
1110
 
reformat_domain(false, false).

reformat_domain(true, true).
Thierry Martinez's avatar
FOLTL    
Thierry Martinez committed
1111

FAGES Francois's avatar
ppl    
FAGES Francois committed
1112
1113
1114
reformat_ppl(A, B, K, X) :-
  (number(B), (((A = -X), (K = -1)) ; (A=(K * X), number(K)))). % PPL variable domain normal form

Thierry Martinez's avatar
FOLTL    
Thierry Martinez committed
1115

1116
reformat_domain_conjunction(A0 = B0, R) :-
Thierry Martinez's avatar
FOLTL    
Thierry Martinez committed
1117
  reformat_expression(A0, A1),
1118
1119
  reformat_expression(B0, B1),
  (
FAGES Francois's avatar
ppl    
FAGES Francois committed
1120
      reformat_ppl(A1, B1, K, X)
1121
1122
1123
1124
1125
1126
1127
      ->
          Z is B1/K,
          R=(X = Z)
      ;
      R=(A1 = B1)
  ).

Thierry Martinez's avatar
FOLTL    
Thierry Martinez committed
1128
1129
1130
1131
1132
1133
1134
1135
1136

reformat_domain_conjunction(A0 < B0, A1 < B1) :-
  reformat_expression(A0, A1),
  reformat_expression(B0, B1).

reformat_domain_conjunction(A0 <= B0, A1 <= B1) :-
  reformat_expression(A0, A1),
  reformat_expression(B0, B1).

FAGES Francois's avatar
ppl    
FAGES Francois committed
1137
reformat_domain_conjunction(A0 > B0, R) :-
Thierry Martinez's avatar
FOLTL    
Thierry Martinez committed
1138
  reformat_expression(A0, A1),
FAGES Francois's avatar
ppl    
FAGES Francois committed
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
  reformat_expression(B0, B1),
  (
      reformat_ppl(A1, B1, K, X)
      ->
          Z is B1/K,
          (
              K<0
              ->
                  R=(X < Z)
              ;
              R=(X > Z)
          )
      ;
      R=(A1 > B1)
  ).
Thierry Martinez's avatar
FOLTL    
Thierry Martinez committed
1154

FAGES Francois's avatar
domains    
FAGES Francois committed
1155
reformat_domain_conjunction(A0 >= B0, R) :-
Thierry Martinez's avatar
FOLTL    
Thierry Martinez committed
1156
  reformat_expression(A0, A1),
FAGES Francois's avatar
domains    
FAGES Francois committed
1157
1158
  reformat_expression(B0, B1),
  (
FAGES Francois's avatar
ppl    
FAGES Francois committed
1159
      reformat_ppl(A1, B1, K, X)
FAGES Francois's avatar
domains    
FAGES Francois committed
1160
      ->
FAGES Francois's avatar
domains    
FAGES Francois committed
1161
          Z is B1/K,
FAGES Francois's avatar
domains    
FAGES Francois committed
1162
          (
FAGES Francois's avatar
domains    
FAGES Francois committed
1163
              K<0
FAGES Francois's avatar
domains    
FAGES Francois committed
1164
              ->
FAGES Francois's avatar
domains    
FAGES Francois committed
1165
                  R=(X <= Z)
FAGES Francois's avatar
domains    
FAGES Francois committed
1166
              ;
FAGES Francois's avatar
domains    
FAGES Francois committed
1167
              R=(X >= Z)
FAGES Francois's avatar
domains    
FAGES Francois committed
1168
1169
1170
1171
1172
          )
      ;
      R=(A1 >= B1)
  ).

FAGES Francois's avatar
FAGES Francois committed
1173
reformat_domain_conjunction((A0, B0), B1 /\ A1) :- % reversed order
Thierry Martinez's avatar
FOLTL    
Thierry Martinez committed
1174
1175
1176
1177
  reformat_domain_conjunction(A0, A1),
  reformat_domain_conjunction(B0, B1).


Thierry Martinez's avatar
Thierry Martinez committed
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
reformat_expression(-E0, -E1) :-
  !,
  reformat_expression(E0, E1).

reformat_expression(E0 + F0, E1 + F1) :-
  !,
  reformat_expression(E0, E1),
  reformat_expression(F0, F1).

reformat_expression(E0 - F0, E1 - F1) :-
  !,
  reformat_expression(E0, E1),
  reformat_expression(F0, F1).

reformat_expression(E0 * F0, E1 * F1) :-
  !,
  reformat_expression(E0, E1),
  reformat_expression(F0, F1).

reformat_expression(E0 / F0, E1 / F1) :-
  !,
  reformat_expression(E0, E1),
  reformat_expression(F0, F1).

Thierry Martinez's avatar
FOLTL    
Thierry Martinez committed
1202
1203
1204
1205
reformat_expression(Number, Number) :-
  number(Number),
  !.

Thierry Martinez's avatar
Thierry Martinez committed
1206
reformat_expression(DimensionName, X) :-
Thierry Martinez's avatar
FOLTL    
Thierry Martinez committed
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
  atom(DimensionName),
  !,
  atom_length(DimensionName, NameLength),
  (
    NameLength > 1
  ->
    sub_atom(DimensionName, 0, 1, _, Letter),
    sub_atom(DimensionName, 1, _, 0, Counter),
    letter_index(Letter, LetterIndex),
    Index is Counter * 26 + LetterIndex
  ;
    letter_index(DimensionName, Index)
  ),
Thierry Martinez's avatar
Thierry Martinez committed
1220
  once(free_variable_index(X, Index)).
Thierry Martinez's avatar
FOLTL    
Thierry Martinez committed
1221
1222
1223
1224


letter_index(Letter, Index) :-
  once(sub_atom('ABCDEFGHIJKLMNOPQRSTUVWXYZ', Index, 1, _, Letter)).
FAGES Francois's avatar
FAGES Francois committed
1225
1226