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
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
(** {1 Skolem symbols} *)
module T = TypedSTerm
module Stmt = Statement
module Fmt = CCFormat
type type_ = TypedSTerm.t
type term = TypedSTerm.t
type form = TypedSTerm.t
let section = Util.Section.(make "skolem")
type polarity =
[ `Pos
| `Neg
| `Both
]
let pp_polarity out = function
| `Pos -> CCFormat.string out "+"
| `Neg -> CCFormat.string out "-"
| `Both -> CCFormat.string out "+/-"
type form_definition = {
form: form;
proxy_id: ID.t;
proxy : term;
proxy_ty : type_;
rw_rules: bool;
polarity : polarity;
proof: Proof.step;
as_stmt: Statement.input_t list lazy_t;
}
type term_definition = {
td_id: ID.t;
td_ty: type_;
td_rules: (form, term, type_) Statement.def_rule list;
td_as_def: (form,term,type_) Statement.def;
td_proof: Proof.step;
td_stmt: Statement.input_t list lazy_t;
}
type definition =
| Def_form of form_definition
| Def_term of term_definition
type ctx = {
sc_prefix : string;
sc_prop_prefix : string;
mutable sc_counter: int;
mutable sc_gensym: (string,int) Hashtbl.t;
mutable sc_new_defs : definition list;
mutable sc_new_ids: (ID.t * type_) list;
sc_on_new : ID.t -> type_ -> unit;
}
let create
?(prefix="zip_sk_") ?(prop_prefix="zip_prop") ?(on_new=fun _ _->()) () =
let ctx = {
sc_prefix=prefix;
sc_prop_prefix=prop_prefix;
sc_counter=0;
sc_new_defs = [];
sc_gensym = Hashtbl.create 16;
sc_new_ids = [];
sc_on_new = on_new;
} in
ctx
let incr_counter ctx = ctx.sc_counter <- ctx.sc_counter + 1
let fresh_id ?(start0=false) ~ctx prefix =
let n = CCHashtbl.get_or ~default:0 ctx.sc_gensym prefix in
Hashtbl.replace ctx.sc_gensym prefix (n+1);
let name = if n=0 && not start0 then prefix else prefix ^ string_of_int n in
ID.make name
let fresh_skolem_prefix ~ctx ~ty prefix =
incr_counter ctx;
let s = fresh_id ~ctx prefix in
let kind =
if Ind_ty.is_inductive_simple_type ty then ID.K_ind else ID.K_normal
in
ID.set_payload s (ID.Attr_skolem kind);
ctx.sc_new_ids <- (s,ty) :: ctx.sc_new_ids;
ctx.sc_on_new s ty;
Util.debugf ~section 3 "@[<2>new skolem symbol `%a`@ with type `@[%a@]`@]"
(fun k->k ID.pp s T.pp ty);
s
let fresh_skolem ~ctx ~ty = fresh_skolem_prefix ~ctx ~ty ctx.sc_prefix
let collect_vars subst f =
let rec vars_seq t =
T.Seq.free_vars t
|> Iter.flat_map
(fun v -> match Var.Subst.find subst v with
| None -> Iter.return (Var.update_ty ~f:(T.Subst.eval subst) v)
| Some t' -> vars_seq t')
in
let is_ty_var v = T.Ty.is_tType (Var.ty v) in
vars_seq f
|> Var.Set.of_seq
|> Var.Set.to_list
|> List.partition is_ty_var
let ty_forall ?loc v ty =
if T.Ty.is_tType (Var.ty v) && T.Ty.returns_tType ty
then T.Ty.fun_ ?loc [T.Ty.tType] ty
else T.Ty.forall ?loc v ty
let ty_forall_l = List.fold_right ty_forall
let skolem_form ~ctx subst var form =
incr_counter ctx;
let tyvars, vars = collect_vars subst form in
Util.debugf ~section 5
"@[<2>creating skolem for@ `@[%a@]`@ with tyvars=[@[%a@]],@ vars=[@[%a@]],@ subst={@[%a@]}@]"
(fun k->k T.pp form (Util.pp_list Var.pp_full) tyvars
(Util.pp_list Var.pp_full) vars (Var.Subst.pp T.pp) subst);
let tyvars_t = List.map (fun v->T.Ty.var v) tyvars in
let vars_t = List.map (fun v->T.var v |> T.Subst.eval subst) vars in
let ty_var = T.Subst.eval subst (Var.ty var) in
let ty = ty_forall_l tyvars (T.Ty.fun_ (List.map Var.ty vars) ty_var) in
let prefix = "sk_" ^ Var.to_string var in
let f = fresh_skolem_prefix ~ctx ~ty prefix in
let skolem_t = T.app ~ty:T.Ty.prop (T.const ~ty f) (tyvars_t @ vars_t) in
T.Subst.eval subst skolem_t
let pop_new_skolem_symbols ~ctx =
let l = ctx.sc_new_ids in
ctx.sc_new_ids <- [];
l
let counter ctx = ctx.sc_counter
(** {2 Definitions} *)
let pp_form_definition out def =
Format.fprintf out "(@[<hv>def %a@ for: %a@ rw_rules: %B@ polarity: %a@])"
T.pp def.proxy T.pp def.form def.rw_rules pp_polarity def.polarity
let pp_term_definition out def =
let pp_rule out r = Stmt.pp_def_rule T.pp T.pp T.pp out r in
Format.fprintf out "(@[<hv>def_term `%a : %a`@ rules: (@[<hv>%a@])@])"
ID.pp def.td_id T.pp def.td_ty (Util.pp_list pp_rule) def.td_rules
let pp_definition out = function
| Def_form f -> pp_form_definition out f
| Def_term t -> pp_term_definition out t
let stmt_of_form rw_rules polarity proxy proxy_id proxy_ty form proof =
let module F = T.Form in
if rw_rules then (
let rule : _ Stmt.def_rule =
let vars = T.vars proxy in
let lhs, polarity, rhs = match polarity with
| `Neg -> SLiteral.atom_false proxy, `Imply, F.not_ form
| `Pos -> SLiteral.atom_true proxy, `Imply, form
| `Both -> SLiteral.atom_true proxy, `Equiv, form
in
Stmt.Def_form {vars;lhs;rhs=[rhs];polarity;as_form=[form]}
in
let proof = proof in
[Stmt.def ~proof [Stmt.mk_def ~rewrite:true proxy_id proxy_ty [rule]]]
) else (
let f' = match polarity with
| `Pos -> F.imply proxy form
| `Neg -> F.imply form proxy
| `Both -> F.equiv proxy form
in
let proof = proof in
[ Stmt.ty_decl ~proof proxy_id proxy_ty;
Stmt.assert_ ~proof f'
]
)
let find_def_in_ctx ~ctx form =
CCList.find_map (fun def ->
match def with
| Def_form def when not def.rw_rules ->
let def_form = def.form in
let df_vars, f_vars =
CCPair.map_same (fun x -> Var.Set.of_seq (T.Seq.vars x)) (def_form,form) in
if not (Var.Set.intersection_empty df_vars f_vars) then None
else CCOpt.map (fun subst -> def,subst) (TypedSTerm.try_alpha_renaming def_form form)
| _ -> None)
ctx.sc_new_defs
let define_form ?(pattern="zip_tseitin") ~ctx ~rw_rules ~polarity ~parents form =
let create_new ~ctx ~rw_rules ~polarity ~parents ~form =
incr_counter ctx;
let tyvars, vars = collect_vars Var.Subst.empty form in
let vars_t = List.map (fun v->T.var v) vars in
let tyvars_t = List.map (fun v->T.Ty.var v) tyvars in
let ty = ty_forall_l tyvars (T.Ty.fun_ (List.map Var.ty vars) T.Ty.prop) in
let f = fresh_id ~start0:true ~ctx pattern in
let proxy = T.app ~ty:T.Ty.prop (T.const ~ty f) (tyvars_t @ vars_t) in
let proof = Proof.Step.define_internal f parents in
let def = {
form;
proxy_id=f;
proxy_ty=ty;
rw_rules;
proxy;
polarity;
proof;
as_stmt=lazy (stmt_of_form rw_rules polarity proxy f ty form proof);
} in
ctx.sc_new_defs <- Def_form def :: ctx.sc_new_defs;
Util.debugf ~section 5 "@[<2>define_form@ %a@ :proof %a@]"
(fun k->k pp_form_definition def Proof.Step.pp proof);
def in
let res =
if not rw_rules then (
match find_def_in_ctx ~ctx form with
| Some (def, subst) ->
assert (T.equal form (T.Subst.eval ~rename_binders:false subst def.form));
assert(T.equal form (T.Subst.eval ~rename_binders:false subst form));
Util.debugf ~section 1 "@[Reusing definition %a. Old def: %a. New def: %a]"
(fun k -> k T.pp def.proxy T.pp def.form T.pp form);
let proxy = T.Subst.eval subst def.proxy in
let proof = Proof.Step.define_internal def.proxy_id parents in
let res = {
def with
form; proxy; proof; polarity;
as_stmt = lazy (stmt_of_form rw_rules polarity proxy
def.proxy_id def.proxy_ty form proof);
} in
if def.polarity != polarity then (
incr_counter ctx;
ctx.sc_new_defs <- Def_form res :: ctx.sc_new_defs
);
res
| None -> create_new ~ctx ~rw_rules ~polarity ~parents ~form
) else (create_new ~ctx ~rw_rules ~polarity ~parents ~form)
in
res
let pp_rules =
Fmt.(Util.pp_list Dump.(pair (list T.pp_inner |> hovbox) T.pp) |> hovbox)
let stmt_of_term id ty rules proof : Stmt.input_t list =
let module F = T.Form in
[Stmt.def ~proof [Stmt.mk_def ~rewrite:true id ty rules]]
let define_term ?(pattern="fun_") ~ctx ~parents rules : term_definition =
Util.debugf ~section 5
"(@[<hv2>define_term@ :rules (@[<hv>%a@])@])" (fun k->k pp_rules rules);
incr_counter ctx;
let some_args, ty_ret = match rules with
| [] -> assert false
| (args, rhs) :: _ -> args, T.ty_exn rhs
in
let ty_vars, ty_args =
CCList.partition_map
(fun t -> match T.view t with
| T.Var v when T.Ty.is_tType (Var.ty v) -> `Left v
| _ -> `Right (T.ty_exn t))
some_args
in
List.iter
(fun (args,_) ->
let args' = CCList.drop (List.length ty_vars) args in
assert (List.length args' = List.length ty_args);
assert (List.for_all2 (fun t ty -> T.Ty.equal ty (T.ty_exn t)) args' ty_args);
())
rules;
let ty = T.Ty.forall_l ty_vars (T.Ty.fun_ ty_args ty_ret) in
let is_prop = T.Ty.is_prop ty_ret in
let id = fresh_id ~start0:true ~ctx pattern in
let rules =
List.map
(fun (args,rhs) ->
let all_vars =
Iter.of_list (rhs::args)
|> Iter.flat_map T.Seq.free_vars
|> Var.Set.of_seq |> Var.Set.to_list
in
if is_prop
then (
let atom = T.app ~ty:ty_ret (T.const ~ty id) args in
Stmt.Def_form {
vars=all_vars; lhs=SLiteral.atom atom true;
rhs=[rhs]; polarity=`Equiv;
as_form=[T.Form.eq atom rhs |> T.Form.close_forall];
}
) else (
Stmt.Def_term {
vars=all_vars;id;ty;args;rhs;
as_form=
T.Form.eq (T.app (T.const ~ty id) ~ty:(T.ty_exn rhs) args) rhs
|> T.Form.close_forall;
}
))
rules
in
let td_as_def = Stmt.mk_def ~rewrite:true id ty rules in
let proof = Proof.Step.define_internal id parents in
let def = {
td_id=id;
td_ty=ty;
td_rules=rules;
td_as_def;
td_proof=proof;
td_stmt=lazy (stmt_of_term id ty rules proof);
} in
ctx.sc_new_defs <- Def_term def :: ctx.sc_new_defs;
Util.debugf ~section 4 "@[<2>define_term@ %a@ :proof %a@]"
(fun k->k pp_term_definition def Proof.Step.pp proof);
def
let new_definitions ~ctx = ctx.sc_new_defs
let pop_new_definitions ~ctx =
let l = ctx.sc_new_defs in
ctx.sc_new_defs <- [];
l
let rule_def = Proof.Rule.mk "define"
let def_as_stmt (d:definition): Stmt.input_t list = match d with
| Def_form d -> Lazy.force d.as_stmt
| Def_term d -> Lazy.force d.td_stmt