-
Notifications
You must be signed in to change notification settings - Fork 0
/
stateful_repl.pl
300 lines (245 loc) · 9.34 KB
/
stateful_repl.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
%%% -*- Mode: Prolog; Module: stateful_repl; -*-.
:- module(stateful_repl, [
add/1,
asserted/1, formulas/1,
reset/0,
declarations/1,
model/1,
scopes/1,
decl/1,
implies/1,
implied/1,
consistent/1,
status/1,
show_status/0,
save_formulas/1,
add_formulas/1,
z3_help/0,
% can this repetition be avoided?
op(750, xfy, and), % =, >, etc. are 700
op(751, xfy, or),
op(740, xfy, <>), % different-than
op(739, xfy, <=>), % iff
op(299, xfy, :)
]).
/** <module> Stateful REPL for Z3
Remembers asserted Z3 formulas and declarations from one query to the next.
*/
:- use_module(z3_swi_foreign, [
z3_assert/2,
z3_declarations/2,
z3_free_handle/1,
z3_model_lists/2,
z3_new_handle/1,
z3_remove_declaration/3,
z3_check/2,
z3_solver_pop/3,
z3_solver_push/2,
z3_solver_scopes/2,
z3_model_assocs/2
]).
:- use_module(z3_utils, [
z3_declare_types_for_symbols/3,
ground_version/3,
remove_type_annotations/2,
z3_expand_term/2
]).
:- use_module(type_inference, [
typecheck/4
]).
:- use_module(library(assoc)).
% TODO: allow adding inconsistencies, add explain/relax?
%! z3_help
% Print help.
z3_help :- format(
"Z3 repl help:
add(F) Add formula F
consistent(F) Check if F is consistent with what has been added so far
implies(F) Check if F is implied by what has been added so far
formulas(L) Get list of formulas asserted so far
status(S) Get solver status (l_true, l_false, l_undef)
model(M) Get a model for formulas added so far, if possible
reset Reset all declarations"),
true.
% z3.pl shares no state between queries, except for enum declarations.
% this code, in contrast, shares everything: the solver, the type declarations, and the enums.
% can still pop the stack. Surgically remove assertions?
% also saves the session to disk to resume later
:- initialization(reset_globals).
% fails if the variable does not exist:
get_repl_handle(H) :- nb_current(repl_handle, H).
get_type_map(M) :- nb_current(typecheck_map, M).
set_type_map(M) :- nb_setval(typecheck_map, M).
get_recorded_formulas(L) :- nb_current(recorded_formulas, L).
set_recorded_formulas(L) :- nb_setval(recorded_formulas, L).
record_formula(F) :- get_recorded_formulas(L),
ord_add_element(L, F, New),
set_recorded_formulas(New).
get_asserted(M) :- nb_current(asserted_formulas, M).
% we should call reset_globals if there is a chance for the context to be invalidated
% before we do a reset. Otherwise, we'll have an old invalid pointer as the solver.
clear_globals :- (nb_current(repl_handle, H) -> (
z3_free_handle(H),
nb_delete(repl_handle)
)
; true),
set_type_map(t),
set_recorded_formulas([]).
reset_globals :- clear_globals,
z3_new_handle(H),
nb_setval(repl_handle, H),
true.
push_formula(Formula, NewMap, NewSymbols, Status) :-
%% must_be(ground, Formula),
get_type_map(OldAssoc),
z3_expand_term(Formula, FormulaTransformed),
ground_version(FormulaTransformed, FG, Symbols),
(typecheck(FG, bool, OldAssoc, NewMap) -> true ;
print_message(error, z3_message("Type error for %w", [FG])),
fail
),
!, %% commit to first solution
exclude(>>({OldAssoc}/[X], get_assoc(X, OldAssoc, _Y)), Symbols, NewSymbols),
get_repl_handle(Handle),
z3_declare_types_for_symbols(Handle, NewSymbols, NewMap),
z3_solver_push(Handle, _),
remove_type_annotations(FG, FG_notypes),
z3_assert(Handle, FG_notypes),
z3_check(Handle, Status).
remove_one(H, F/N) :- z3_remove_declaration(H, F, N).
remove_declarations(L) :-
get_repl_handle(H),
maplist(remove_one(H), L). % all declarations should have the form F/N
%! add_formula(+F)
% Adds Z3 formula F. Typechecks and adds resulting declarations as well.
% If the formula is inconsistent with other formulas so far, fails,
% and the context is unchanged.
add_formula(F) :- push_formula(F, NewMap, NewSymbols, Status),
(member(Status, [l_false, l_type_error]) -> (
get_repl_handle(Handle),
z3_solver_pop(Handle, 1, _),
remove_declarations(NewSymbols),
fail
)
; (
set_type_map(NewMap),
record_formula(F)
)).
%! decl(-M)
% Get all declarations, in both prolog and Z3 versions.
decl(M) :-
get_repl_handle(H),
z3_declarations(H, Z),
declarations(D),
M = {z3:Z, pl:D}.
%% user-visible:
%! add(+F)
% Shortcut for add_formula
add(F) :- add_formula(F).
%! asserted(-List)
% Get list of asserted formulas.
asserted(L) :- get_recorded_formulas(L).
%! formulas(-List)
% Get list of asserted formulas (alias for `asserted`).
formulas(L) :- get_recorded_formulas(L).
%! reset
% Clear all asserted formulas and declarations.
reset :- reset_globals.
%! declarations(-List)
% Get all Prolog declarations, constructed by the type checker, as a list of `term-type` pairs.
declarations(L) :- get_type_map(M),
assoc_to_list(M, L).
%! model(-Model)
% Get a Z3 model for the formulas asserted so far.
% Note that Z3 can return "uncertain" models if the status is `l_undef`.
model(Model) :- get_repl_handle(H),
status(_),
z3_model_lists(H, Model).
model_assoc(Model) :- get_repl_handle(H),
z3_model_assocs(H, Model).
%! scopes(-N)
% Get the number of scopes for the (implicit) solver.
% Should be generally equal to the number of formulas asserted so far.
scopes(N) :- get_repl_handle(H),
z3_solver_scopes(H, N).
push_check_and_pop(F, Status) :-
push_formula(F, _NewMap, NewSymbols, Status),
get_repl_handle(H),
z3_solver_pop(H, 1, _),
remove_declarations(NewSymbols).
%! implied(+Formula)
% Succeeds iff `Formula` is known to be implied by the current asserted formulas. Fails if this can't be proved.
implied(F) :- push_check_and_pop(not(F), Status),
Status == l_false.
%% todo: handle l_undef
%! consistent(+Formula)
% Succeeds iff `Formula` is known to be consistent with the current asserted formulas, that is, check status is `l_true`. Fails if this can't be proved.
consistent(F) :- push_check_and_pop(F, Status),
Status == l_true.
%! implies(+Formula)
% Alias for implied.
implies(X) :- implied(X).
%! status(-Status)
% Does a z3_check for the formulas asserted so far, returning the
% status, one of (`l_true`, `l_false`, `l_undef`).
status(Status) :- get_repl_handle(H),
z3_check(H, Status).
/****
%% give option to pop last asserted thing? But types remain...
%% Could include in the stack the new tupes introduced at each step, erase them on pop.
z3_pop(Formula) :-
must_be(var, Formula),
pop_recorded_formulas(Formula),
get_repl_handle(S),
z3_solver_pop(S).
****/
%! save_formulas(+Filename)
% Saves the set of added formulas to the given Filename (an atom or a string).
% Fails if the file exists.
save_formulas(Filename) :-
(exists_file(Filename) -> (write("File exists"), fail) ; true),
open(Filename, write, Output, [create([all])] ),
formulas(L),
write_canonical(Output, L), %% fast_write fails?
writeln(Output, "."),
close(Output).
%! add_formulas(+Filename)
% Reads formulas from the given filename, adds them to the current set.
add_formulas(Filename) :-
open(Filename, read, Input),
read(Input, L),
close(Input),
maplist(add, L),
show_status.
%! show_status
% Prints summary of current status.
show_status :-
formulas(L),
length(L, Len),
print_message(informational, z3_message("There are %w added formulas", [Len])),
status(Status),
print_message(informational, z3_message("Status is %w", [Status])),
get_type_map(M),
assoc_to_list(M, ML),
length(ML, NumDecl),
print_message(informational, z3_message("Type map has %w declarations", [NumDecl])),
true.
:- begin_tests(repl_tests,
[setup(reset), cleanup(clear_globals)]
).
test(instantiate_type) :-
reset,
add(a:bv(32) = b:bv(X)),
assertion(X == 32).
test(clear_types) :-
reset,
add(x:int = y:int),
(add((b:real = c:real) and (1 = 2)) -> true ;
add(b:int = c:int)).
test(implied_and_consistent) :-
reset,
add(a > 10),
implied(a > 1),
\+ consistent(a < 5),
\+ implied(a > 20).
:- end_tests(repl_tests).