Source file enumTypes.ml
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
(** {1 Inference and simplification rules for Algebraic types} *)
open Logtk
open Libzipperposition
module T = Term
module S = Subst
module Lit = Literal
module Lits = Literals
module Stmt = Statement
type term = T.t
let prof_detect = Util.mk_profiler "enum_types.detect"
let prof_instantiate = Util.mk_profiler "enum_types.instantiate_vars"
let stat_declare = Util.mk_stat "enum_types.declare"
let stat_simplify = Util.mk_stat "enum_types.simplify"
let stat_instantiate = Util.mk_stat "enum_types.instantiate_axiom"
let section = Util.Section.make ~parent:Const.section "enum_ty"
let flag_enumeration_clause = SClause.new_flag ()
exception Error of string
let () = Printexc.register_printer
(function Error s -> Some ("error in enum_types: " ^s)
| _ -> None)
let error_ s = raise (Error s)
let errorf_ msg = CCFormat.ksprintf msg ~f:error_
type id_or_ty_builtin =
| I of ID.t
| B of Type.builtin
let pp_id_or_builtin out = function
| I id -> ID.pp out id
| B b -> Type.pp_builtin out b
(** {2 Inference rules} *)
module type S = sig
module Env : Env.S
module C : module type of Env.C
type decl
val pp_decl : decl CCFormat.printer
type declare_result =
| New of decl
| AlreadyDeclared of decl
val declare_ty :
proof:Proof.t ->
ty_id:ID.t ->
ty_vars:Type.t HVar.t list ->
var:Type.t HVar.t ->
term list ->
declare_result
(** Declare that the domain of the type [ty_id] is restricted to
given list of [cases], in the form [forall var. Or_{c in cases} var = c].
The type of [var] must be [ty_id ty_vars].
Will be ignored if the type already has a enum declaration, and the old
declaration will be returned instead.
@return either the new declaration, or the already existing one for
this type if any
@raise Error if some of the preconditions is not filled *)
val instantiate_vars : Env.multi_simpl_rule
(** Instantiate variables whose type is a known enumerated type,
with all cases of this type. *)
(** {6 Registration} *)
val setup : unit -> unit
(** Register rules in the environment *)
end
let _enable = ref false
let _instantiate_shielded = ref false
let _accept_unary_types = ref true
let _instantiate_projector_axiom = ref false
let is_projector_ id ~of_ = match Ind_ty.as_projector id with
| Some p -> ID.equal (Ind_ty.projector_id p) of_
| None -> false
module Make(E : Env.S) : S with module Env = E = struct
module Env = E
module C = Env.C
module PS = Env.ProofState
module Ctx = Env.Ctx
type decl = {
decl_ty_id : id_or_ty_builtin;
decl_ty_vars : Type.t HVar.t list;
decl_ty : Type.t;
decl_var : Type.t HVar.t;
decl_cases : term list;
decl_proof :
[ `Data of Proof.t * Type.t Statement.data
| `Clause of Proof.t
];
mutable decl_symbols : ID.Set.t;
}
let pp_decl out d =
Format.fprintf out "@[<1>{enum_ty=@[%a@],@ cases=@[%a@]}@]"
Type.pp d.decl_ty (Util.pp_list T.pp) d.decl_cases
let decls_by_id = ID.Tbl.create 16
let decls_builtin = ref []
let find_decl_ = function
| I i -> ID.Tbl.find decls_by_id i
| B b -> List.assoc b !decls_builtin
let add_decl_ id decl = match id with
| I id -> ID.Tbl.add decls_by_id id decl
| B b -> decls_builtin := (b,decl) :: !decls_builtin
let on_new_decl = Signal.create ()
let check_uniq_var_is_ ~var cases =
cases
|> Iter.of_list
|> Iter.flat_map T.Seq.vars
|> Iter.filter (fun v -> not (Type.equal Type.tType (HVar.ty v)))
|> Iter.for_all (HVar.equal Type.equal var)
let rec check_all_distinct_ acc l = match l with
| [] -> true
| v :: l' ->
not (CCList.mem ~eq:(HVar.equal Type.equal) v acc)
&& check_all_distinct_ (v :: acc) l'
type declare_result =
| New of decl
| AlreadyDeclared of decl
let declare_ ~proof ~ty_id:id ~ty_vars ~var cases =
if not (check_all_distinct_ [] ty_vars)
then errorf_ "invalid declaration %a: duplicate type variable" pp_id_or_builtin id;
if not (check_uniq_var_is_ ~var cases)
then errorf_ "invalid declaration %a: %a is not the only variable in @[%a@]"
pp_id_or_builtin id (Util.pp_list T.pp) cases HVar.pp var;
try
let decl = find_decl_ id in
Util.debugf ~section 3 "@[an enum is already declared for type %a@]"
(fun k->k pp_id_or_builtin id);
AlreadyDeclared decl
with Not_found ->
let ty = match id with
| I id -> Type.app id (List.map Type.var ty_vars)
| B b -> assert (ty_vars=[]); Type.builtin b
in
Util.debugf ~section 1
"@[<2>declare new enum type `@[%a@]`@ (@[cases %a ∈ {@[<hv>%a@]}@])@]"
(fun k->k Type.pp ty HVar.pp var (Util.pp_list ~sep:", " T.pp) cases);
Util.incr_stat stat_declare;
let decl_symbols =
List.fold_left
(fun set t -> match T.head t with
| None -> errorf_ "non-symbolic case @[%a@]" T.pp t
| Some s -> ID.Set.add s set)
ID.Set.empty cases
in
let decl = {
decl_ty_id=id;
decl_ty_vars=ty_vars;
decl_ty=ty;
decl_var=var;
decl_cases=cases;
decl_symbols;
decl_proof=proof;
} in
add_decl_ id decl;
Signal.send on_new_decl decl;
New decl
let declare_ty ~proof ~ty_id ~ty_vars ~var cases =
declare_ ~proof:(`Clause proof) ~var ~ty_id:(I ty_id) ~ty_vars cases
let as_simple_ty ty = match Type.view ty with
| Type.App (id, []) -> Some (I id)
| Type.Builtin b -> Some (B b)
| _ -> None
let is_simple_ty ty = as_simple_ty ty <> None
let detect_decl_ c =
let eq_var_ ~var t = match T.view t with
| T.Var v' -> HVar.equal Type.equal var v'
| _ -> false
and get_var_ t = match T.view t with
| T.Var v -> v
| _ -> assert false
and is_const t = match T.view t with
| T.Const _ -> true
| _ -> false
in
let rec _check_all_vars ~ty ~var acc lits = match lits with
| [] ->
if check_uniq_var_is_ ~var acc
&& (!_accept_unary_types || List.length acc >= 2)
then Some (var, acc)
else None
| Lit.Equation (l, r, true) :: lits' when eq_var_ ~var l && is_const r ->
_check_all_vars ~ty ~var (r::acc) lits'
| Lit.Equation (l, r, true) :: lits' when eq_var_ ~var r && is_const l ->
_check_all_vars ~ty ~var (l::acc) lits'
| _ -> None
in
let lits = C.lits c in
if CCArray.exists (fun l -> not (Lit.is_eq l)) lits then None
else match Array.to_list lits with
| Lit.Equation (l,r,true) :: lits when T.is_var l && is_simple_ty (T.ty l) && is_const r ->
let var = get_var_ l in
_check_all_vars ~ty:(T.ty l) ~var [r] lits
| Lit.Equation (l,r,true) :: lits when T.is_var r && is_simple_ty (T.ty r) && is_const l ->
let var = get_var_ r in
_check_all_vars ~ty:(T.ty r) ~var [l] lits
| _ -> None
let detect_declaration c = Util.with_prof prof_detect detect_decl_ c
let vars_under_eq_ lits =
Iter.of_array lits
|> Iter.filter Lit.is_eq
|> Iter.flat_map Lit.Seq.terms
|> Iter.filter T.is_var
let _shielded_vars lits =
Iter.of_array lits
|> Iter.flat_map Lit.Seq.terms
|> Iter.flat_map T.Seq.subterms_depth
|> Iter.filter_map
(fun (v,depth) -> if depth>0 && T.is_var v then Some v else None)
|> T.Seq.add_set T.Set.empty
let naked_vars_ lits =
let v =
vars_under_eq_ lits
|> T.Seq.add_set T.Set.empty
in
T.Set.diff v (_shielded_vars lits)
|> T.Set.elements
let bind_vars_ (d,sc_decl) (args,sc_args) =
List.fold_left2
(fun subst v arg ->
let v = (v : Type.t HVar.t :> InnerTerm.t HVar.t) in
Subst.Ty.bind subst (v,sc_decl) (arg,sc_args))
Subst.empty d.decl_ty_vars args
let find_ty_ sc_decl ty sc_ty =
let find_aux i l =
try
let d = find_decl_ i in
if List.length l = List.length d.decl_ty_vars
then
let subst = bind_vars_ (d,sc_decl) (l,sc_ty) in
Some (d, subst)
else None
with Not_found -> None
in
match Type.view ty with
| Type.Builtin b -> find_aux (B b) []
| Type.App (id, l) -> find_aux (I id) l
| _ -> None
let instantiate_vars_ c =
let vars =
if !_instantiate_shielded
then vars_under_eq_ (C.lits c) |> Iter.to_rev_list
else naked_vars_ (C.lits c)
in
let s_c = 0 and s_decl = 1 in
CCList.find_map
(fun v ->
match find_ty_ s_decl (T.ty v) s_c with
| None -> None
| Some (decl, subst) ->
Util.incr_stat stat_simplify;
let subst = Unif_subst.of_subst subst in
let l =
List.map
(fun case ->
let subst = Unif.FO.unify_full ~subst (v,s_c) (case,s_decl) in
let renaming = Subst.Renaming.create () in
let c_guard = Literals.of_unif_subst renaming subst
and subst = Unif_subst.subst subst in
let lits' = Lits.apply_subst renaming subst (C.lits c,s_c) in
let proof =
Proof.Step.inference [Proof.Parent.from @@ C.proof c]
~rule:(Proof.Rule.mk"enum_type_case_switch")
in
let trail = C.trail c and penalty = C.penalty c in
let c' =
C.create_a ~trail ~penalty
(CCArray.append c_guard lits') proof
in
Util.debugf ~section 3
"@[<2>deduce @[%a@]@ from @[%a@]@ @[(enum_type switch on %a)@]@]"
(fun k->k C.pp c' C.pp c Type.pp decl.decl_ty);
c')
decl.decl_cases
in
Some l)
vars
let instantiate_vars c = Util.with_prof prof_instantiate instantiate_vars_ c
let instantiate_axiom_ ~ty_s s poly_args decl =
if ID.Set.mem s decl.decl_symbols
then None
else (
let ty_args, _ = Type.open_fun ty_s in
decl.decl_symbols <- ID.Set.add s decl.decl_symbols;
let vars = List.mapi (fun i ty -> HVar.make ~ty i |> T.var) ty_args in
let t = T.app (T.const ~ty:ty_s s) vars in
Util.debugf ~section 5 "@[<2>instantiate enum type `%a`@ on `@[%a@]`@]"
(fun k->k pp_id_or_builtin decl.decl_ty_id T.pp t);
let us = bind_vars_ (decl,0) (poly_args,1) |> Unif_subst.of_subst in
let us = Unif.FO.unify_full ~subst:us (T.var decl.decl_var,0) (t,1) in
let renaming = Subst.Renaming.create () in
let subst = Unif_subst.subst us
and c_guard = Literal.of_unif_subst renaming us in
let lits =
List.map
(fun case ->
Lit.mk_eq
(S.FO.apply renaming subst (t,1))
(S.FO.apply renaming subst (case,0)))
decl.decl_cases
in
let proof =
let parent = match decl.decl_proof with
| `Data (src,_) -> src
| `Clause src -> src
in
Proof.Step.inference
~rule:(Proof.Rule.mk "axiom_enum_types")
[Proof.Parent.from parent]
in
let trail = Trail.empty in
let penalty = 4 in
let c' = C.create ~trail ~penalty (c_guard@lits) proof in
Util.debugf ~section 3 "@[<2>instantiate axiom of enum type `%a` \
on @[%a@]:@ clause @[%a@]@]"
(fun k->k pp_id_or_builtin decl.decl_ty_id ID.pp s C.pp c');
Util.incr_stat stat_instantiate;
Some c'
)
let instantiate_axiom ~ty_s s poly_args decl =
if !_instantiate_projector_axiom
then instantiate_axiom_ ~ty_s s poly_args decl
else None
let check_decl_ ~ty s decl =
let _, ty_ret = Type.open_fun ty in
match Type.view ty_ret, decl.decl_ty_id with
| Type.Builtin b, B b' when b=b' ->
instantiate_axiom ~ty_s:ty s [] decl
| Type.App (c, args), I i
when ID.equal c i
&& not (is_projector_ s ~of_:i)
&& List.length args = List.length decl.decl_ty_vars->
instantiate_axiom ~ty_s:ty s args decl
| _ -> None
let _on_new_symbol s ~ty =
let aux i =
try
let decl = find_decl_ i in
check_decl_ ~ty s decl |> CCOpt.to_list
with Not_found -> []
in
let clauses =
let _, ty_ret = Type.open_fun ty in
match Type.view ty_ret with
| Type.Builtin b -> aux (B b)
| Type.App (id, _) -> aux (I id)
| _ -> []
in
PS.ActiveSet.add (Iter.of_list clauses)
let _on_new_decl decl =
let clauses =
Signature.fold (Ctx.signature ()) []
(fun acc s (ty,_) ->
match check_decl_ s ~ty decl with
| None -> acc
| Some c -> c::acc)
in
PS.PassiveSet.add (Iter.of_list clauses)
let is_trivial c =
C.get_flag flag_enumeration_clause c
let _detect_and_declare c =
Util.debugf ~section 5 "@[<2>examine clause@ `@[%a@]`@]" (fun k->k C.pp c);
match detect_declaration c with
| None -> ()
| Some (var,cases) ->
let ty_id = CCOpt.get_exn (as_simple_ty (HVar.ty var)) in
let is_new = declare_ ~ty_id ~ty_vars:[] ~var ~proof:(`Clause (C.proof c)) cases in
match is_new with
| New _ -> C.set_flag flag_enumeration_clause c true
| AlreadyDeclared _ -> ()
let _declare_inductive ~proof d =
Util.debugf ~section 5 "@[<2>examine data `%a`@]" (fun k->k ID.pp d.Stmt.data_id);
let ty_vars = List.mapi (fun i _ -> HVar.make ~ty:Type.tType i) d.Stmt.data_args in
let ty_vars_t = List.map T.var ty_vars in
let ty_of_var =
Type.app d.Stmt.data_id (List.map Type.var ty_vars) in
let v = HVar.make ~ty:ty_of_var (List.length d.Stmt.data_args+1) in
let v_t = T.var v in
let cases =
List.map
(fun (c_id, c_ty, c_args) ->
let num_ty_vars, _, _ty_ret = Type.open_poly_fun c_ty in
assert (num_ty_vars = List.length ty_vars);
let projs_of_v =
List.map
(fun (_ty_arg,(proj, ty_proj)) ->
T.app (T.const ~ty:ty_proj proj) (ty_vars_t @ [v_t]))
c_args
in
T.app (T.const ~ty:c_ty c_id) (ty_vars_t @ projs_of_v)
)
d.Stmt.data_cstors
in
let _ =
declare_ ~proof:(`Data (proof,d))
~var:v ~ty_id:(I d.Stmt.data_id) ~ty_vars cases
in
()
let _detect_stmt stmt =
match Stmt.view stmt with
| Stmt.Assert c ->
let proof = Stmt.proof_step stmt in
let c = C.of_forms ~trail:Trail.empty c proof in
_detect_and_declare c
| Stmt.Data l ->
let proof = Stmt.as_proof_c stmt in
List.iter (_declare_inductive ~proof) l
| Stmt.TyDecl _
| Stmt.Def _
| Stmt.Rewrite _
| Stmt.Lemma _
| Stmt.NegatedGoal _
| Stmt.Goal _ -> ()
let setup () =
if !_enable then (
Util.debug ~section 1 "register handling of enumerated types";
Env.add_multi_simpl_rule instantiate_vars;
Env.add_is_trivial is_trivial;
Signal.on_every Env.on_input_statement _detect_stmt;
Signal.on_every Ctx.on_new_symbol
(fun (s, ty) -> _on_new_symbol s ~ty);
Signal.on_every on_new_decl
(fun decl ->
_on_new_decl decl;
Env.simplify_active_with instantiate_vars);
Signature.iter (Ctx.signature ()) (fun s (ty,_) -> _on_new_symbol s ~ty);
)
end
(** {2 As Extension} *)
let extension =
let register env =
let module E = (val env : Env.S) in
let module ET = Make(E) in
ET.setup ()
in
{ Extensions.default with Extensions.
name = "enum_types";
env_actions=[register];
}
let () =
Extensions.register extension;
Params.add_opts
[ "--enum-types"
, Options.switch_set true _enable
, " enable inferences for enumerated/inductive types"
; "--no-enum-types"
, Options.switch_set false _enable
, " disable inferences for enumerated/inductive types"
; "--projector-axioms"
, Options.switch_set true _instantiate_projector_axiom
, " enable exhaustiveness axioms for inductive types (with projectors)"
; "--no-projector-axioms"
, Options.switch_set false _instantiate_projector_axiom
, " disable exhaustiveness axioms for inductive types (with projectors)"
; "--enum-shielded"
, Options.switch_set true _instantiate_shielded
, " enable/disable instantiation of shielded variables of enum type"
; "--no-enum-shielded"
, Options.switch_set false _instantiate_shielded
, " enable/disable instantiation of shielded variables of enum type"
; "--enum-unary"
, Options.switch_set true _accept_unary_types
, " enable support for unary enum types (one case)"
; "--no-enum-unary"
, Options.switch_set false _accept_unary_types
, " disable support for unary enum types (one case)"
]