Debian Codegraph @

plugins/micromega/zify.ml

raw symbols SCIP
1 (************************************************************************)
2 (* * The Rocq Prover / The Rocq Development Team *)
3 (* v * Copyright INRIA, CNRS and contributors *)
4 (* <O___,, * (see version control and CREDITS file for authors & dates) *)
5 (* \VV/ **************************************************************)
6 (* // * This file is distributed under the terms of the *)
7 (* * GNU Lesser General Public License Version 2.1 *)
8 (* * (see LICENSE file for the text of the license) *)
9 (************************************************************************)
10
11 open Constr
12 open Names
13 open Pp
14 open Lazy
15 module NamedDecl = Context.Named.Declaration
16
17 module ERelevance = EConstr.ERelevance
18
19 let debug_zify = CDebug.create ~name:"zify" ()
20
21 (* The following [constr] are necessary for constructing the proof terms *)
22
23 let zify str =
24 EConstr.of_constr
25 (UnivGen.constr_of_monomorphic_global (Global.env ())
26 (Rocqlib.lib_ref ("ZifyClasses." ^ str)))
27
28 (** classes *)
29 let rocq_InjTyp = lazy (Rocqlib.lib_ref "ZifyClasses.InjTyp")
30
31 let rocq_BinOp = lazy (Rocqlib.lib_ref "ZifyClasses.BinOp")
32 let rocq_UnOp = lazy (Rocqlib.lib_ref "ZifyClasses.UnOp")
33 let rocq_CstOp = lazy (Rocqlib.lib_ref "ZifyClasses.CstOp")
34 let rocq_BinRel = lazy (Rocqlib.lib_ref "ZifyClasses.BinRel")
35 let rocq_PropBinOp = lazy (Rocqlib.lib_ref "ZifyClasses.PropBinOp")
36 let rocq_PropUOp = lazy (Rocqlib.lib_ref "ZifyClasses.PropUOp")
37 let rocq_BinOpSpec = lazy (Rocqlib.lib_ref "ZifyClasses.BinOpSpec")
38 let rocq_UnOpSpec = lazy (Rocqlib.lib_ref "ZifyClasses.UnOpSpec")
39 let rocq_Saturate = lazy (Rocqlib.lib_ref "ZifyClasses.Saturate")
40
41 (* morphism like lemma *)
42
43 let mkapp2 = lazy (zify "mkapp2")
44 let mkapp = lazy (zify "mkapp")
45 let eq_refl = lazy (zify "eq_refl")
46 let eq = lazy (zify "eq")
47 let mkrel = lazy (zify "mkrel")
48 let iff_refl = lazy (zify "iff_refl")
49 let eq_iff = lazy (zify "eq_iff")
50 let rew_iff = lazy (zify "rew_iff")
51 let rew_iff_rev = lazy (zify "rew_iff_rev")
52
53 (* propositional logic *)
54
55 let op_and = lazy (zify "and")
56 let op_and_morph = lazy (zify "and_morph")
57 let op_or = lazy (zify "or")
58 let op_or_morph = lazy (zify "or_morph")
59 let op_impl_morph = lazy (zify "impl_morph")
60 let op_iff = lazy (zify "iff")
61 let op_iff_morph = lazy (zify "iff_morph")
62 let op_not = lazy (zify "not")
63 let op_not_morph = lazy (zify "not_morph")
64 let op_True = lazy (zify "True")
65 let op_I = lazy (zify "I")
66
67 (** [unsafe_to_constr c] returns a [Constr.t] without considering an evar_map.
68 This is useful for calling Constr.hash *)
69 let unsafe_to_constr = EConstr.Unsafe.to_constr
70
71 let pr_constr env evd e = Printer.pr_econstr_env env evd e
72
73 let gl_pr_constr e =
74 let genv = Global.env () in
75 let evd = Evd.from_env genv in
76 pr_constr genv evd e
77
78 let whd = Reductionops.clos_whd_flags RedFlags.all
79 let is_convertible env evd t1 t2 = Reductionops.(is_conv env evd t1 t2)
80
81 (** [get_type_of] performs beta reduction ;
82 Is it ok for Retyping.get_type_of (Zpower_nat n q) to return (fun _ : nat => Z) q ? *)
83 let get_type_of env evd e =
84 Tacred.cbv_beta env evd (Retyping.get_type_of env evd e)
85
86 (* arguments are dealt with in a second step *)
87
88 let rec find_option pred l =
89 match l with
90 | [] -> raise Not_found
91 | e :: l -> ( match pred e with Some r -> r | None -> find_option pred l )
92
93 module ConstrMap = struct
94 open Names.GlobRef
95
96 type 'a t = 'a list Map.t
97
98 let add gr e m =
99 Map.update gr (function None -> Some [e] | Some l -> Some (e :: l)) m
100
101 let empty = Map.empty
102
103 let find evd h m =
104 match Map.find (fst (EConstr.destRef evd h)) m with
105 | e :: _ -> e
106 | [] -> assert false
107
108 let find_all evd h m = Map.find (fst (EConstr.destRef evd h)) m
109
110 let fold f m acc =
111 Map.fold
112 (fun k l acc -> List.fold_left (fun acc e -> f k e acc) acc l)
113 m acc
114 end
115
116 module HConstr = struct
117 module M = Map.Make (struct
118 type t = EConstr.t
119
120 let compare c c' = Constr.compare (unsafe_to_constr c) (unsafe_to_constr c')
121 end)
122
123 type 'a t = 'a M.t
124
125 let add h e m = M.add h e m
126 let empty = M.empty
127 let find = M.find
128 end
129
130 (** [get_projections_from_constant (evd,c) ]
131 returns an array of constr [| a1,.. an|] such that [c] is defined as
132 Definition c := mk a1 .. an with mk a constructor.
133 ai is therefore either a type parameter or a projection.
134 *)
135
136 let get_projections_from_constant (evd, i) =
137 match EConstr.kind evd (whd (Global.env ()) evd i) with
138 | App (c, a) -> a
139 | _ ->
140 raise
141 (CErrors.user_err
142 Pp.(
143 str "The hnf of term "
144 ++ pr_constr (Global.env ()) evd i
145 ++ str " should be an application i.e. (c a1 ... an)"))
146
147 (** An instance of type, say T, is registered into a hashtable, say TableT. *)
148
149 type 'a decl =
150 { decl : EConstr.t
151 ; (* Registered type instance *)
152 deriv : 'a (* Projections of insterest *) }
153
154 module EInjT = struct
155 type t =
156 { isid : bool
157 ; (* S = T -> inj = fun x -> x*)
158 source : EConstr.t
159 ; (* S *)
160 target : EConstr.t
161 ; (* T *)
162 (* projections *)
163 inj : EConstr.t
164 ; (* S -> T *)
165 pred : EConstr.t
166 ; (* T -> Prop *)
167 cstr : EConstr.t option (* forall x, pred (inj x) *) }
168
169 let eq_inj evd i1 i2 =
170 i1.isid = i2.isid && EConstr.eq_constr evd i1.inj i2.inj
171
172
173 end
174
175 (** [classify_op] classify injected operators and detect special cases. *)
176
177 type classify_op =
178 | OpInj (* e.g. Z.of_nat -> \x.x *)
179 | OpSame (* e.g. Z.add -> Z.add *)
180 | OpConv (* e.g. Pos.ge == \x.y. Z.ge (Z.pos x) (Z.pos y)
181 \x.y. Z.pos (Pos.add x y) == \x.y. Z.add (Z.pos x) (Z.pos y)
182 Z.succ == (\x.x + 1)
183 *)
184 | OpOther
185
186 (*let pp_classify_op = function
187 | OpInj -> Pp.str "Identity"
188 | OpSame -> Pp.str "Same"
189 | OpConv -> Pp.str "Conv"
190 | OpOther -> Pp.str "Other"
191 *)
192
193 let name x =
194 Context.make_annot (Name.mk_name (Names.Id.of_string x)) ERelevance.relevant
195
196 let mkconvert_unop i1 i2 op top =
197 (* fun x => inj (op x) *)
198 let op =
199 EConstr.mkLambda
200 ( name "x"
201 , i1.EInjT.source
202 , EConstr.mkApp (i2.EInjT.inj, [|EConstr.mkApp (op, [|EConstr.mkRel 1|])|])
203 )
204 in
205 (* fun x => top (inj x) *)
206 let top =
207 EConstr.mkLambda
208 ( name "x"
209 , i1.EInjT.source
210 , EConstr.mkApp
211 (top, [|EConstr.mkApp (i1.EInjT.inj, [|EConstr.mkRel 1|])|]) )
212 in
213 (op, top)
214
215 let mkconvert_binop i1 i2 i3 op top =
216 (* fun x y => inj (op x y) *)
217 let op =
218 EConstr.mkLambda
219 ( name "x"
220 , i1.EInjT.source
221 , EConstr.mkLambda
222 ( name "y"
223 , i1.EInjT.source
224 , EConstr.mkApp
225 ( i3.EInjT.inj
226 , [|EConstr.mkApp (op, [|EConstr.mkRel 2; EConstr.mkRel 1|])|] )
227 ) )
228 in
229 (* fun x y => top (inj x) (inj y) *)
230 let top =
231 EConstr.mkLambda
232 ( name "x"
233 , i1.EInjT.source
234 , EConstr.mkLambda
235 ( name "y"
236 , i2.EInjT.source
237 , EConstr.mkApp
238 ( top
239 , [| EConstr.mkApp (i1.EInjT.inj, [|EConstr.mkRel 2|])
240 ; EConstr.mkApp (i2.EInjT.inj, [|EConstr.mkRel 1|]) |] ) ) )
241 in
242 (op, top)
243
244 let mkconvert_rel i r tr =
245 let tr =
246 EConstr.mkLambda
247 ( name "x"
248 , i.EInjT.source
249 , EConstr.mkLambda
250 ( name "y"
251 , i.EInjT.source
252 , EConstr.mkApp
253 ( tr
254 , [| EConstr.mkApp (i.EInjT.inj, [|EConstr.mkRel 2|])
255 ; EConstr.mkApp (i.EInjT.inj, [|EConstr.mkRel 1|]) |] ) ) )
256 in
257 (r, tr)
258
259 (** [classify_op mkconvert op top] takes the injection [inj] for the origin operator [op]
260 and the destination operator [top] -- both [op] and [top] are closed terms *)
261 let classify_op mkconvert inj op top =
262 let env = Global.env () in
263 let evd = Evd.from_env env in
264 if is_convertible env evd inj op then OpInj
265 else if EConstr.eq_constr evd op top then OpSame
266 else
267 let op, top = mkconvert op top in
268 if is_convertible env evd op top then OpConv else OpOther
269
270 (*let classify_op mkconvert tysrc op top =
271 let res = classify_op mkconvert tysrc op top in
272 Feedback.msg_debug
273 Pp.(
274 str "classify_op:" ++ gl_pr_constr op ++ str " " ++ gl_pr_constr top
275 ++ str " " ++ pp_classify_op res ++ fnl ());
276 res
277 *)
278 module EBinOpT = struct
279 type t =
280 { (* Op : source1 -> source2 -> source3 *)
281 source1 : EConstr.t
282 ; source2 : EConstr.t
283 ; source3 : EConstr.t
284 ; target1 : EConstr.t
285 ; target2 : EConstr.t
286 ; target3 : EConstr.t
287 ; inj1 : EInjT.t (* InjTyp source1 target1 *)
288 ; inj2 : EInjT.t (* InjTyp source2 target2 *)
289 ; inj3 : EInjT.t (* InjTyp source3 target3 *)
290 ; bop : EConstr.t (* BOP *)
291 ; tbop : EConstr.t (* TBOP *)
292 ; tbopinj : EConstr.t (* TBOpInj *)
293 ; classify_binop : classify_op }
294 end
295
296 module ECstOpT = struct
297 type t =
298 { source : EConstr.t
299 ; target : EConstr.t
300 ; inj : EInjT.t
301 ; cst : EConstr.t
302 ; cstinj : EConstr.t
303 ; is_construct : bool }
304 end
305
306 module EUnOpT = struct
307 type t =
308 { source1 : EConstr.t
309 ; source2 : EConstr.t
310 ; target1 : EConstr.t
311 ; target2 : EConstr.t
312 ; uop : EConstr.t
313 ; inj1_t : EInjT.t
314 ; inj2_t : EInjT.t
315 ; tuop : EConstr.t
316 ; tuopinj : EConstr.t
317 ; classify_unop : classify_op
318 ; is_construct : bool }
319 end
320
321 module EBinRelT = struct
322 type t =
323 { source : EConstr.t
324 ; target : EConstr.t
325 ; inj : EInjT.t
326 ; brel : EConstr.t
327 ; tbrel : EConstr.t
328 ; brelinj : EConstr.t
329 ; classify_rel : classify_op }
330 end
331
332 module EPropBinOpT = struct
333 type t = {op : EConstr.t; op_iff : EConstr.t}
334 end
335
336 module EPropUnOpT = struct
337 type t = {op : EConstr.t; op_iff : EConstr.t}
338 end
339
340 module ESatT = struct
341 type t = {parg1 : EConstr.t; parg2 : EConstr.t; satOK : EConstr.t}
342 end
343
344 module ESpecT = struct
345 type t = {spec : EConstr.t}
346 end
347
348 (* Different type of declarations *)
349 type decl_kind =
350 | PropOp of EPropBinOpT.t decl
351 | PropUnOp of EPropUnOpT.t decl
352 | InjTyp of EInjT.t decl
353 | BinRel of EBinRelT.t decl
354 | BinOp of EBinOpT.t decl
355 | UnOp of EUnOpT.t decl
356 | CstOp of ECstOpT.t decl
357 | Saturate of ESatT.t decl
358 | UnOpSpec of ESpecT.t decl
359 | BinOpSpec of ESpecT.t decl
360
361 let target_inj d =
362 match d with
363 | BinOp d -> Some d.deriv.EBinOpT.inj3
364 | UnOp d -> Some d.deriv.EUnOpT.inj2_t
365 | CstOp d -> Some d.deriv.inj
366 | _ -> None
367
368 type term_kind = Application of EConstr.constr | OtherTerm of EConstr.constr
369
370 module type Elt = sig
371 type elt
372
373 (** name *)
374 val name : string
375
376 val gref : GlobRef.t Lazy.t
377 val table : (term_kind * decl_kind) ConstrMap.t ref
378 val cast : elt decl -> decl_kind
379 val dest : decl_kind -> elt decl option
380
381 (** [get_key] is the type-index used as key for the instance *)
382 val get_key : int
383
384 (** [mk_elt evd i [a0,..,an]] returns the element of the table
385 built from the type-instance i and the arguments (type indexes and projections)
386 of the type-class constructor. *)
387 val mk_elt : Evd.evar_map -> EConstr.t -> EConstr.t array -> elt
388
389 (* val arity : int*)
390 end
391
392 let table = Summary.ref ~name:"zify_table" ConstrMap.empty
393 let saturate = Summary.ref ~name:"zify_saturate" ConstrMap.empty
394 let specs = Summary.ref ~name:"zify_specs" ConstrMap.empty
395 let table_cache = ref ConstrMap.empty
396 let saturate_cache = ref ConstrMap.empty
397 let specs_cache = ref ConstrMap.empty
398
399 (** Each type-class gives rise to a different table.
400 They only differ on how projections are extracted. *)
401
402 module EInj = struct
403 open EInjT
404
405 type elt = EInjT.t
406
407 let name = "EInj"
408 let gref = rocq_InjTyp
409 let table = table
410 let cast x = InjTyp x
411 let dest = function InjTyp x -> Some x | _ -> None
412
413 let is_cstr_true evd c =
414 match EConstr.kind evd c with
415 | Lambda (_, _, c) -> EConstr.eq_constr_nounivs evd c (Lazy.force op_True)
416 | _ -> false
417
418 let mk_elt evd i (a : EConstr.t array) =
419 let isid = EConstr.eq_constr_nounivs evd a.(0) a.(1) in
420 { isid
421 ; source = a.(0)
422 ; target = a.(1)
423 ; inj = a.(2)
424 ; pred = a.(3)
425 ; cstr = (if is_cstr_true evd a.(3) then None else Some a.(4)) }
426
427 let get_key = 0
428 end
429
430 let get_inj evd c =
431 let a = get_projections_from_constant (evd, c) in
432 EInj.mk_elt evd c a
433
434 let rec decomp_type evd ty =
435 match EConstr.kind_of_type evd ty with
436 | EConstr.ProdType (_, t1, rst) -> t1 :: decomp_type evd rst
437 | _ -> [ty]
438
439 let pp_type env evd l =
440 Pp.prlist_with_sep (fun _ -> Pp.str " -> ") (pr_constr env evd) l
441
442 let check_typ evd expty op =
443 let env = Global.env () in
444 let ty = Retyping.get_type_of env evd op in
445 let ty = decomp_type evd ty in
446 if List.for_all2 (EConstr.eq_constr_nounivs evd) ty expty then ()
447 else
448 raise
449 (CErrors.user_err
450 Pp.(
451 str ": Cannot register operator "
452 ++ pr_constr env evd op ++ str ". It has type " ++ pp_type env evd ty
453 ++ str " instead of expected type "
454 ++ pp_type env evd expty))
455
456 module EBinOp = struct
457 type elt = EBinOpT.t
458
459 open EBinOpT
460
461 let name = "BinOp"
462 let gref = rocq_BinOp
463 let table = table
464
465 let mk_elt evd i a =
466 let i1 = get_inj evd a.(7) in
467 let i2 = get_inj evd a.(8) in
468 let i3 = get_inj evd a.(9) in
469 let bop = a.(6) in
470 let tbop = a.(10) in
471 check_typ evd EInjT.[i1.source; i2.source; i3.source] bop;
472 { source1 = a.(0)
473 ; source2 = a.(1)
474 ; source3 = a.(2)
475 ; target1 = a.(3)
476 ; target2 = a.(4)
477 ; target3 = a.(5)
478 ; inj1 = i1
479 ; inj2 = i2
480 ; inj3 = i3
481 ; bop
482 ; tbop
483 ; tbopinj = a.(11)
484 ; classify_binop =
485 classify_op (mkconvert_binop i1 i2 i3) i3.EInjT.inj a.(6) tbop }
486
487 let get_key = 6
488 let cast x = BinOp x
489 let dest = function BinOp x -> Some x | _ -> None
490 end
491
492 (*let debug_term msg c =
493 let genv = Global.env () in
494 Feedback.msg_debug
495 Pp.(str msg ++ str " " ++ pr_constr genv (Evd.from_env genv) c);
496 c
497 *)
498 module ECstOp = struct
499 type elt = ECstOpT.t
500
501 open ECstOpT
502
503 let name = "CstOp"
504 let gref = rocq_CstOp
505 let table = table
506 let cast x = CstOp x
507 let dest = function CstOp x -> Some x | _ -> None
508
509 let isConstruct evd c =
510 match EConstr.kind evd c with
511 | Construct _ | Int _ | Float _ -> true
512 | _ -> false
513
514 let mk_elt evd i a =
515 { source = a.(0)
516 ; target = a.(1)
517 ; inj = get_inj evd a.(3)
518 ; cst = a.(4)
519 ; cstinj = a.(5)
520 ; is_construct = isConstruct evd a.(2) }
521
522 let get_key = 2
523 end
524
525 module EUnOp = struct
526 type elt = EUnOpT.t
527
528 open EUnOpT
529
530 let name = "UnOp"
531 let gref = rocq_UnOp
532 let table = table
533 let cast x = UnOp x
534 let dest = function UnOp x -> Some x | _ -> None
535
536 let mk_elt evd i a =
537 let i1 = get_inj evd a.(5) in
538 let i2 = get_inj evd a.(6) in
539 let uop = a.(4) in
540 check_typ evd EInjT.[i1.source; i2.source] uop;
541 let tuop = a.(7) in
542 { source1 = a.(0)
543 ; source2 = a.(1)
544 ; target1 = a.(2)
545 ; target2 = a.(3)
546 ; uop
547 ; inj1_t = i1
548 ; inj2_t = i2
549 ; tuop
550 ; tuopinj = a.(8)
551 ; is_construct = EConstr.isConstruct evd uop
552 ; classify_unop = classify_op (mkconvert_unop i1 i2) i2.EInjT.inj uop tuop
553 }
554
555 let get_key = 4
556 end
557
558 module EBinRel = struct
559 type elt = EBinRelT.t
560
561 open EBinRelT
562
563 let name = "BinRel"
564 let gref = rocq_BinRel
565 let table = table
566 let cast x = BinRel x
567 let dest = function BinRel x -> Some x | _ -> None
568
569 let mk_elt evd i a =
570 let i = get_inj evd a.(3) in
571 let brel = a.(2) in
572 let tbrel = a.(4) in
573 check_typ evd EInjT.[i.source; i.source; EConstr.mkProp] brel;
574 { source = a.(0)
575 ; target = a.(1)
576 ; inj = get_inj evd a.(3)
577 ; brel
578 ; tbrel
579 ; brelinj = a.(5)
580 ; classify_rel = classify_op (mkconvert_rel i) i.EInjT.inj brel tbrel }
581
582 let get_key = 2
583 end
584
585 module EPropBinOp = struct
586 type elt = EPropBinOpT.t
587
588 open EPropBinOpT
589
590 let name = "PropBinOp"
591 let gref = rocq_PropBinOp
592 let table = table
593 let cast x = PropOp x
594 let dest = function PropOp x -> Some x | _ -> None
595 let mk_elt evd i a = {op = a.(0); op_iff = a.(1)}
596 let get_key = 0
597 end
598
599 module EPropUnOp = struct
600 type elt = EPropUnOpT.t
601
602 open EPropUnOpT
603
604 let name = "PropUOp"
605 let gref = rocq_PropUOp
606 let table = table
607 let cast x = PropUnOp x
608 let dest = function PropUnOp x -> Some x | _ -> None
609 let mk_elt evd i a = {op = a.(0); op_iff = a.(1)}
610 let get_key = 0
611 end
612
613 let constr_of_term_kind = function Application c -> c | OtherTerm c -> c
614
615 let zify_register_locality =
616 let open Attributes.Notations in
617 Attributes.explicit_hint_locality >>= function
618 | Some v ->
619 let () = Locality.check_locality_nodischarge v in
620 return v
621 | None -> return (if Lib.sections_are_opened () then Hints.Local else Hints.SuperGlobal)
622
623 module type S = sig
624 val register : Hints.hint_locality -> Libnames.qualid -> unit
625 val print : unit -> unit
626 end
627
628 module MakeTable (E : Elt) : S = struct
629 (** Given a term [c] and its arguments ai,
630 we construct a HConstr.t table that is
631 indexed by ai for i = E.get_key.
632 The elements of the table are built using E.mk_elt c [|a0,..,an|]
633 *)
634
635 let make_elt (evd, i) =
636 let a = get_projections_from_constant (evd, i) in
637 E.mk_elt evd i a
638
639 let safe_ref evd c =
640 try
641 fst (EConstr.destRef evd c)
642 with DestKO -> CErrors.user_err Pp.(str "Add Zify "++str E.name ++ str ": the term "++
643 gl_pr_constr c ++ str " should be a global reference")
644
645 let register_hint evd t elt =
646 match EConstr.kind evd t with
647 | App (c, _) ->
648 let gr = safe_ref evd c in
649 E.table := ConstrMap.add gr (Application t, E.cast elt) !E.table
650 | _ ->
651 let gr = safe_ref evd t in
652 E.table := ConstrMap.add gr (OtherTerm t, E.cast elt) !E.table
653
654 let register_constr env evd c =
655 let c = EConstr.of_constr c in
656 let t = get_type_of env evd c in
657 match EConstr.kind evd t with
658 | App (intyp, args) when EConstr.isRefX env evd (Lazy.force E.gref) intyp ->
659 let styp = args.(E.get_key) in
660 let elt = {decl = c; deriv = make_elt (evd, c)} in
661 register_hint evd styp elt
662 | _ ->
663 let env = Global.env () in
664 raise
665 (CErrors.user_err
666 Pp.(
667 str "Cannot register " ++ pr_constr env evd c
668 ++ str ". It has type " ++ pr_constr env evd t
669 ++ str " instead of type "
670 ++ Printer.pr_global (Lazy.force E.gref)
671 ++ str " X1 ... Xn"))
672
673 let register_obj : Libobject.locality * Constr.constr -> Libobject.obj =
674 let cache_constr c =
675 let env = Global.env () in
676 let evd = Evd.from_env env in
677 register_constr env evd c
678 in
679 let subst_constr (subst, c) = Mod_subst.subst_mps subst c in
680 Libobject.declare_object
681 @@ Libobject.object_with_locality
682 ("register-zify-" ^ E.name)
683 ~cache:cache_constr ~subst:(Some subst_constr)
684 ~discharge:(fun _ -> assert false)
685
686 (** [register c] is called from the VERNACULAR ADD [name] reference(t).
687 The term [c] is interpreted and
688 registered as a [superglobal_object_nodischarge].
689 TODO: pre-compute [get_type_of] - [cache_constr] is using another environment.
690 *)
691 let register local c =
692 try
693 let c = UnivGen.constr_of_monomorphic_global (Global.env ()) (Nametab.locate c) in
694 let _ = Lib.add_leaf (register_obj (local,c)) in
695 ()
696 with Not_found ->
697 raise
698 (CErrors.user_err Pp.(Libnames.pr_qualid c ++ str " does not exist."))
699
700 let pp_keys () =
701 let env = Global.env () in
702 let evd = Evd.from_env env in
703 ConstrMap.fold
704 (fun _ (k, d) acc ->
705 match E.dest d with
706 | None -> acc
707 | Some _ ->
708 Pp.(pr_constr env evd (constr_of_term_kind k) ++ str " " ++ acc))
709 !E.table (Pp.str "")
710
711 let print () = Feedback.msg_info (pp_keys ())
712 end
713
714 module InjTable = MakeTable (EInj)
715
716 module ESat = struct
717 type elt = ESatT.t
718
719 open ESatT
720
721 let name = "Saturate"
722 let gref = rocq_Saturate
723 let table = saturate
724 let cast x = Saturate x
725 let dest = function Saturate x -> Some x | _ -> None
726 let mk_elt evd i a = {parg1 = a.(2); parg2 = a.(3); satOK = a.(5)}
727 let get_key = 1
728 end
729
730 module EUnopSpec = struct
731 open ESpecT
732
733 type elt = ESpecT.t
734
735 let name = "UnopSpec"
736 let gref = rocq_UnOpSpec
737 let table = specs
738 let cast x = UnOpSpec x
739 let dest = function UnOpSpec x -> Some x | _ -> None
740 let mk_elt evd i a = {spec = a.(4)}
741 let get_key = 2
742 end
743
744 module EBinOpSpec = struct
745 open ESpecT
746
747 type elt = ESpecT.t
748
749 let name = "BinOpSpec"
750 let gref = rocq_BinOpSpec
751 let table = specs
752 let cast x = BinOpSpec x
753 let dest = function BinOpSpec x -> Some x | _ -> None
754 let mk_elt evd i a = {spec = a.(5)}
755 let get_key = 3
756 end
757
758 module BinOp = MakeTable (EBinOp)
759 module UnOp = MakeTable (EUnOp)
760 module CstOp = MakeTable (ECstOp)
761 module BinRel = MakeTable (EBinRel)
762 module PropBinOp = MakeTable (EPropBinOp)
763 module PropUnOp = MakeTable (EPropUnOp)
764 module Saturate = MakeTable (ESat)
765 module UnOpSpec = MakeTable (EUnopSpec)
766 module BinOpSpec = MakeTable (EBinOpSpec)
767
768 let init_cache () =
769 table_cache := !table;
770 saturate_cache := !saturate;
771 specs_cache := !specs
772
773 open EInjT
774
775 (** Get constr of lemma and projections in ZifyClasses. *)
776
777 (** Module [CstrTable] records terms [x] injected into [inj x]
778 together with the corresponding type constraint.
779 The terms are stored by side-effect during the traversal
780 of the goal. It must therefore be cleared before calling
781 the main tactic.
782 *)
783
784 module CstrTable = struct
785 module HConstr = Hashtbl.Make (struct
786 type t = EConstr.t
787
788 let hash c = Constr.hash (unsafe_to_constr c)
789 let equal c c' = Constr.equal (unsafe_to_constr c) (unsafe_to_constr c')
790 end)
791
792 let table : EConstr.t HConstr.t = HConstr.create 10
793 let register evd t (i : EConstr.t) = HConstr.add table t i
794
795 let get () =
796 let l = HConstr.fold (fun k i acc -> (k, i) :: acc) table [] in
797 HConstr.clear table; l
798
799 (** [gen_cstr table] asserts (cstr k) for each element of the table (k,cstr).
800 NB: the constraint is only asserted if it does not already exist in the context.
801 *)
802 let gen_cstr table =
803 Proofview.Goal.enter (fun gl ->
804 let env = Proofview.Goal.env gl in
805 let evd = Proofview.Goal.sigma gl in
806 (* Build the table of existing hypotheses *)
807 let has_hyp =
808 let hyps_table = HConstr.create 20 in
809 let () = List.iter
810 (fun decl -> HConstr.add hyps_table (NamedDecl.get_type decl) ())
811 (EConstr.named_context env)
812 in
813 fun c ->
814 let m = HConstr.mem hyps_table c in
815 if not m then HConstr.add hyps_table c ();
816 m
817 in
818 (* Add the constraint (cstr k) if it is not already present *)
819 let gen k cstr =
820 Proofview.Goal.enter (fun gl ->
821 let env = Proofview.Goal.env gl in
822 let term = EConstr.mkApp (cstr, [|k|]) in
823 let types = get_type_of env evd term in
824 if has_hyp types then Tacticals.tclIDTAC
825 else
826 let n =
827 Tactics.fresh_id_in_env Id.Set.empty
828 (Names.Id.of_string "cstr")
829 env
830 in
831 Tactics.pose_proof (Names.Name n) term)
832 in
833 List.fold_left
834 (fun acc (k, i) -> Tacticals.tclTHEN (gen k i) acc)
835 Tacticals.tclIDTAC table)
836 end
837
838 type prf =
839 | Term (* source is built from constructors.
840 target = compute(inj source)
841 inj source == target *)
842 | Same (* target = source
843 inj source == inj target *)
844 | Conv of EConstr.t (* inj source == target *)
845 | Prf of EConstr.t * EConstr.t
846
847 (** [eq_proof typ source target] returns (target = target : source = target) *)
848 let eq_proof typ source target =
849 EConstr.mkCast
850 ( EConstr.mkApp (force eq_refl, [|typ; target|])
851 , DEFAULTcast
852 , EConstr.mkApp (force eq, [|typ; source; target|]) )
853
854 let interp_prf evd inj source prf =
855 let inj_source =
856 if inj.EInjT.isid then source else EConstr.mkApp (inj.EInjT.inj, [|source|])
857 in
858 match prf with
859 | Term ->
860 let target = Tacred.compute (Global.env ()) evd inj_source in
861 (target, EConstr.mkApp (force eq_refl, [|inj.target; target|]))
862 | Same ->
863 (inj_source, EConstr.mkApp (force eq_refl, [|inj.target; inj_source|]))
864 | Conv trm -> (trm, eq_proof inj.target inj_source trm)
865 | Prf (target, prf) -> (target, prf)
866
867 let pp_prf prf =
868 match prf with
869 | Term -> Pp.str "Term"
870 | Same -> Pp.str "Same"
871 | Conv t -> Pp.(str "Conv " ++ gl_pr_constr t)
872 | Prf (_, _) -> Pp.str "Prf "
873
874 let interp_prf evd inj source prf =
875 let t, prf' = interp_prf evd inj source prf in
876 debug_zify (fun () ->
877 Pp.(
878 str "interp_prf " ++ gl_pr_constr inj.EInjT.inj ++ str " "
879 ++ gl_pr_constr source ++ str " = " ++ gl_pr_constr t ++ str " by "
880 ++ gl_pr_constr prf' ++ str " from " ++ pp_prf prf ++ fnl ()));
881 (t, prf')
882
883 let mkvar evd inj e =
884 (match inj.cstr with None -> () | Some ctr -> CstrTable.register evd e ctr);
885 Same
886
887 let pp_prf evd inj src prf =
888 let t, prf' = interp_prf evd inj src prf in
889 Pp.(
890 gl_pr_constr inj.EInjT.inj ++ str " " ++ gl_pr_constr src ++ str " = "
891 ++ gl_pr_constr t ++ str " by "
892 ++
893 match prf with
894 | Term -> Pp.str "Term"
895 | Same -> Pp.str "Same"
896 | Conv t -> Pp.str "Conv"
897 | Prf (_, p) -> Pp.str "Prf " ++ gl_pr_constr p)
898
899 let conv_of_term evd op isid arg =
900 Tacred.compute (Global.env ()) evd
901 (if isid then arg else EConstr.mkApp (op, [|arg|]))
902
903 let app_unop env evd src unop arg prf =
904 let cunop = unop.EUnOpT.classify_unop in
905 let default a' prf' =
906 let target = EConstr.mkApp (unop.EUnOpT.tuop, [|a'|]) in
907 let evd, h = Typing.checked_appvect env evd (force mkapp)
908 [| unop.source1
909 ; unop.source2
910 ; unop.target1
911 ; unop.target2 |]
912 in
913 evd,
914 EUnOpT.(
915 Prf
916 ( target
917 , EConstr.mkApp
918 ( h
919 , [| unop.uop
920 ; unop.inj1_t.EInjT.inj
921 ; unop.inj2_t.EInjT.inj
922 ; unop.tuop
923 ; unop.tuopinj
924 ; arg
925 ; a'
926 ; prf' |] ) ))
927 in
928 match prf with
929 | Term -> (
930 if unop.EUnOpT.is_construct then evd, Term (* Keep rebuilding *)
931 else
932 match cunop with
933 | OpInj -> evd, Conv (conv_of_term evd unop.EUnOpT.uop false arg)
934 | OpSame -> evd, Same
935 | _ ->
936 let a', prf = interp_prf evd unop.EUnOpT.inj1_t arg prf in
937 default a' prf )
938 | Same -> (
939 match cunop with
940 | OpSame -> evd, Same
941 | OpInj -> evd, Same
942 | OpConv ->
943 evd,
944 Conv
945 (EConstr.mkApp
946 ( unop.EUnOpT.tuop
947 , [|EConstr.mkApp (unop.EUnOpT.inj1_t.EInjT.inj, [|arg|])|] ))
948 | OpOther ->
949 let a', prf' = interp_prf evd unop.EUnOpT.inj1_t arg prf in
950 default a' prf' )
951 | Conv a' -> (
952 match cunop with
953 | OpSame | OpConv -> evd, Conv (EConstr.mkApp (unop.EUnOpT.tuop, [|a'|]))
954 | OpInj -> evd, Conv a'
955 | _ ->
956 let a', prf = interp_prf evd unop.EUnOpT.inj1_t arg prf in
957 default a' prf )
958 | Prf (a', prf') -> default a' prf'
959
960 let app_unop env evd src unop arg prf =
961 let (evd', r) as res = app_unop env evd src unop arg prf in
962 debug_zify (fun () ->
963 Pp.(
964 str "\napp_unop "
965 ++ pp_prf evd unop.EUnOpT.inj1_t arg prf
966 ++ str " => "
967 ++ pp_prf evd' unop.EUnOpT.inj2_t src r));
968 res
969
970 let app_binop env evd src binop arg1 prf1 arg2 prf2 =
971 EBinOpT.(
972 let mkApp a1 a2 = EConstr.mkApp (binop.tbop, [|a1; a2|]) in
973 let to_conv inj arg = function
974 | Term -> conv_of_term evd inj.EInjT.inj inj.EInjT.isid arg
975 | Same ->
976 if inj.EInjT.isid then arg else EConstr.mkApp (inj.EInjT.inj, [|arg|])
977 | Conv t -> t
978 | Prf _ -> failwith "Prf is not convertible"
979 in
980 let default a1 prf1 a2 prf2 =
981 let res = mkApp a1 a2 in
982 let evd, head = Typing.checked_appvect env evd (force mkapp2)
983 [| binop.source1
984 ; binop.source2
985 ; binop.source3
986 ; binop.target1
987 ; binop.target2
988 ; binop.target3 |]
989 in
990 let prf =
991 EBinOpT.(
992 EConstr.mkApp
993 ( head
994 , [| binop.bop
995 ; binop.inj1.EInjT.inj
996 ; binop.inj2.EInjT.inj
997 ; binop.inj3.EInjT.inj
998 ; binop.tbop
999 ; binop.tbopinj
1000 ; arg1
1001 ; a1
1002 ; prf1
1003 ; arg2
1004 ; a2
1005 ; prf2 |] ))
1006 in
1007 evd, Prf (res, prf)
1008 in
1009 match (binop.EBinOpT.classify_binop, prf1, prf2) with
1010 | OpSame, Same, Same -> evd, Same
1011 | OpSame, Term, Same | OpSame, Same, Term -> evd, Same
1012 | OpSame, (Term | Same | Conv _), (Term | Same | Conv _) ->
1013 let t1 = to_conv binop.EBinOpT.inj1 arg1 prf1 in
1014 let t2 = to_conv binop.EBinOpT.inj1 arg2 prf2 in
1015 evd, Conv (mkApp t1 t2)
1016 | _, _, _ ->
1017 let a1, prf1 = interp_prf evd binop.inj1 arg1 prf1 in
1018 let a2, prf2 = interp_prf evd binop.inj2 arg2 prf2 in
1019 default a1 prf1 a2 prf2)
1020
1021 type typed_constr = {constr : EConstr.t; typ : EConstr.t; inj : EInjT.t}
1022
1023
1024 (* [arrow] is the term (fun (x:Prop) (y : Prop) => x -> y) *)
1025 let arrow =
1026 let name x =
1027 Context.make_annot (Name.mk_name (Names.Id.of_string x)) ERelevance.relevant
1028 in
1029 EConstr.mkLambda
1030 ( name "x"
1031 , EConstr.mkProp
1032 , EConstr.mkLambda
1033 ( name "y"
1034 , EConstr.mkProp
1035 , EConstr.mkProd
1036 ( Context.make_annot Names.Anonymous ERelevance.relevant
1037 , EConstr.mkRel 2
1038 , EConstr.mkRel 2 ) ) )
1039
1040 let is_prop env sigma term =
1041 let sort = Retyping.get_sort_of env sigma term in
1042 EConstr.ESorts.is_prop sigma sort
1043
1044 let is_arrow env evd a p1 p2 =
1045 is_prop env evd p1
1046 && is_prop
1047 (EConstr.push_rel (Context.Rel.Declaration.LocalAssum (a, p1)) env)
1048 evd p2
1049 && (a.Context.binder_name = Names.Anonymous || EConstr.Vars.noccurn evd 1 p2)
1050
1051 (** [get_operator env evd e] expresses [e] as an application (c a)
1052 where c is the head symbol and [a] is the array of arguments.
1053 The function also transforms (x -> y) as (arrow x y) *)
1054 let get_operator barrow env evd e =
1055 let e' = EConstr.whd_evar evd e in
1056 match EConstr.kind evd e' with
1057 | Prod (a, p1, p2) ->
1058 if barrow && is_arrow env evd a p1 p2 then (arrow, [|p1; p2|], false)
1059 else raise Not_found
1060 | App (c, a) -> (
1061 let c' = EConstr.whd_evar evd c in
1062 match EConstr.kind evd c' with
1063 | Construct _ (* e.g. Z0 , Z.pos *) | Const _ (* e.g. Z.max *) | Proj _
1064 |Lambda _ (* e.g projections *) | Ind _ (* e.g. eq *) ->
1065 (c', a, false)
1066 | _ -> raise Not_found )
1067 | Const _ -> (e', [||], false)
1068 | Construct _ -> (e', [||], true)
1069 | Int _ | Float _ -> (e', [||], true)
1070 | _ -> raise Not_found
1071
1072 let decompose_app env evd e =
1073 match EConstr.kind evd e with
1074 | Prod (a, p1, p2) when is_arrow env evd a p1 p2 -> (arrow, [|p1; p2|])
1075 | App (c, a) -> (c, a)
1076 | _ -> (EConstr.whd_evar evd e, [||])
1077
1078 type 'op propop = {op : 'op; op_constr : EConstr.t; op_iff : EConstr.t}
1079
1080 let mk_propop op c1 c2 = {op; op_constr = c1; op_iff = c2}
1081
1082 type prop_binop = AND | OR | IFF | IMPL
1083 type prop_unop = NOT
1084
1085 type prop_op =
1086 | BINOP of prop_binop propop * EConstr.t * EConstr.t
1087 | UNOP of prop_unop propop * EConstr.t
1088 | OTHEROP of EConstr.t * EConstr.t array
1089
1090 let classify_prop env evd e =
1091 match EConstr.kind evd e with
1092 | Prod (a, p1, p2) when is_arrow env evd a p1 p2 ->
1093 BINOP (mk_propop IMPL arrow (force op_impl_morph), p1, p2)
1094 | App (c, a) -> (
1095 match Array.length a with
1096 | 1 ->
1097 if EConstr.eq_constr_nounivs evd (force op_not) c then
1098 UNOP (mk_propop NOT c (force op_not_morph), a.(0))
1099 else OTHEROP (c, a)
1100 | 2 ->
1101 if EConstr.eq_constr_nounivs evd (force op_and) c then
1102 BINOP (mk_propop AND c (force op_and_morph), a.(0), a.(1))
1103 else if EConstr.eq_constr_nounivs evd (force op_or) c then
1104 BINOP (mk_propop OR c (force op_or_morph), a.(0), a.(1))
1105 else if EConstr.eq_constr_nounivs evd (force op_iff) c then
1106 BINOP (mk_propop IFF c (force op_iff_morph), a.(0), a.(1))
1107 else OTHEROP (c, a)
1108 | _ -> OTHEROP (c, a) )
1109 | _ -> OTHEROP (e, [||])
1110
1111
1112 let check_target_inj evd inj d =
1113 match inj , target_inj d with
1114 | None , _ -> true
1115 | Some _ , None -> false
1116 | Some i1 , Some i2 -> EInjT.eq_inj evd i1 i2
1117
1118
1119 (** [match_operator env evd hd arg inj (t,d)]
1120 - hd is head operator of t
1121 - If t = OtherTerm _, then t = hd
1122 - If t = Application _, then
1123 we extract the relevant number of arguments from arg
1124 and check for convertibility *)
1125 let match_operator env evd hd args inj (t, d) =
1126 let decomp t i =
1127 let n = Array.length args in
1128 let t' = EConstr.mkApp (hd, Array.sub args 0 (n - i)) in
1129 if is_convertible env evd t' t then Some (d, t) else None
1130 in
1131
1132 if check_target_inj evd inj d
1133 then
1134 match t with
1135 | OtherTerm t -> Some (d, t)
1136 | Application t -> (
1137 match d with
1138 | CstOp _ -> decomp t 0
1139 | UnOp _ -> decomp t 1
1140 | BinOp _ -> decomp t 2
1141 | BinRel _ -> decomp t 2
1142 | PropOp _ -> decomp t 2
1143 | PropUnOp _ -> decomp t 1
1144 | _ -> None )
1145 else None
1146
1147 let pp_trans_expr env evd e res =
1148 debug_zify (fun () -> Pp.(str "\ntrans_expr " ++ pp_prf evd e.inj e.constr res));
1149 res
1150
1151 let rec trans_expr env evd e =
1152 let inj = e.inj in
1153 let e = e.constr in
1154 try
1155 let c, a, is_constant = get_operator false env evd e in
1156 if is_constant then evd, Term
1157 else
1158 let k, t =
1159 find_option
1160 (match_operator env evd c a (Some inj))
1161 (ConstrMap.find_all evd c !table_cache)
1162 in
1163 let n = Array.length a in
1164 match k with
1165 | CstOp {deriv = c'} ->
1166 ECstOpT.(if c'.is_construct then evd, Term else evd, Prf (c'.cst, c'.cstinj))
1167 | UnOp {deriv = unop} ->
1168 let evd, prf =
1169 trans_expr env evd
1170 { constr = a.(n - 1)
1171 ; typ = unop.EUnOpT.source1
1172 ; inj = unop.EUnOpT.inj1_t }
1173 in
1174 app_unop env evd e unop a.(n - 1) prf
1175 | BinOp {deriv = binop} ->
1176 let evd, prf1 =
1177 trans_expr env evd
1178 { constr = a.(n - 2)
1179 ; typ = binop.EBinOpT.source1
1180 ; inj = binop.EBinOpT.inj1 }
1181 in
1182 let evd, prf2 =
1183 trans_expr env evd
1184 { constr = a.(n - 1)
1185 ; typ = binop.EBinOpT.source2
1186 ; inj = binop.EBinOpT.inj2 }
1187 in
1188 app_binop env evd e binop a.(n - 2) prf1 a.(n - 1) prf2
1189 | d -> evd, mkvar evd inj e
1190 with Not_found | DestKO -> evd, mkvar evd inj e
1191
1192 let trans_expr env evd e =
1193 try
1194 let evd, prf = trans_expr env evd e in
1195 evd, pp_trans_expr env evd e prf
1196 with Not_found ->
1197 raise
1198 (CErrors.user_err
1199 ( Pp.str "Missing injection for type "
1200 ++ Printer.pr_leconstr_env env evd e.typ ))
1201
1202 type prfp =
1203 | TProof of EConstr.t * EConstr.t (** Proof of tranformed proposition *)
1204 | CProof of EConstr.t (** Transformed proposition is convertible *)
1205 | IProof (** Transformed proposition is identical *)
1206
1207 let pp_prfp = function
1208 | TProof (t, prf) ->
1209 Pp.str "TProof " ++ gl_pr_constr t ++ Pp.str " by " ++ gl_pr_constr prf
1210 | CProof t -> Pp.str "CProof " ++ gl_pr_constr t
1211 | IProof -> Pp.str "IProof"
1212
1213 let trans_binrel env evd src rop a1 prf1 a2 prf2 =
1214 EBinRelT.(
1215 match (rop.classify_rel, prf1, prf2) with
1216 | OpSame, Same, Same -> evd, IProof
1217 | (OpSame | OpConv), Conv t1, Conv t2 ->
1218 evd, CProof (EConstr.mkApp (rop.tbrel, [|t1; t2|]))
1219 | (OpSame | OpConv), (Same | Term | Conv _), (Same | Term | Conv _) ->
1220 let a1', _ = interp_prf evd rop.inj a1 prf1 in
1221 let a2', _ = interp_prf evd rop.inj a2 prf2 in
1222 evd, CProof (EConstr.mkApp (rop.tbrel, [|a1'; a2'|]))
1223 | _, _, _ ->
1224 let a1', prf1 = interp_prf evd rop.inj a1 prf1 in
1225 let a2', prf2 = interp_prf evd rop.inj a2 prf2 in
1226 (* XXX do we need to check more of this application or check other applications?
1227 This one found necessary in #16803 *)
1228 let evd, h = Typing.checked_appvect env evd (force mkrel) [| rop.source; rop.target |] in
1229 evd, TProof
1230 ( EConstr.mkApp (rop.EBinRelT.tbrel, [|a1'; a2'|])
1231 , EConstr.mkApp
1232 ( h
1233 , [| rop.brel
1234 ; rop.EBinRelT.inj.EInjT.inj
1235 ; rop.EBinRelT.tbrel
1236 ; rop.EBinRelT.brelinj
1237 ; a1
1238 ; a1'
1239 ; prf1
1240 ; a2
1241 ; a2'
1242 ; prf2 |] ) ))
1243
1244 let trans_binrel env evd src rop a1 prf1 a2 prf2 =
1245 let evd, res = trans_binrel env evd src rop a1 prf1 a2 prf2 in
1246 debug_zify (fun () -> Pp.(str "\ntrans_binrel " ++ pp_prfp res));
1247 evd, res
1248
1249 let mkprf t p =
1250 EConstr.(
1251 match p with
1252 | IProof -> (t, mkApp (force iff_refl, [|t|]))
1253 | CProof t' -> (t', mkApp (force eq_iff, [|t; t'; eq_proof mkProp t t'|]))
1254 | TProof (t', p) -> (t', p))
1255
1256 let mkprf t p =
1257 let t', p = mkprf t p in
1258 debug_zify (fun () ->
1259 Pp.(
1260 str "mkprf " ++ gl_pr_constr t ++ str " <-> " ++ gl_pr_constr t'
1261 ++ str " by " ++ gl_pr_constr p));
1262 (t', p)
1263
1264 let trans_bin_prop op_constr op_iff t1 p1 t2 p2 =
1265 match (p1, p2) with
1266 | IProof, IProof -> IProof
1267 | CProof t1', IProof -> CProof (EConstr.mkApp (op_constr, [|t1'; t2|]))
1268 | IProof, CProof t2' -> CProof (EConstr.mkApp (op_constr, [|t1; t2'|]))
1269 | CProof t1', CProof t2' -> CProof (EConstr.mkApp (op_constr, [|t1'; t2'|]))
1270 | _, _ ->
1271 let t1', p1 = mkprf t1 p1 in
1272 let t2', p2 = mkprf t2 p2 in
1273 TProof
1274 ( EConstr.mkApp (op_constr, [|t1'; t2'|])
1275 , EConstr.mkApp (op_iff, [|t1; t2; t1'; t2'; p1; p2|]) )
1276
1277 let trans_bin_prop op_constr op_iff t1 p1 t2 p2 =
1278 let prf = trans_bin_prop op_constr op_iff t1 p1 t2 p2 in
1279 debug_zify (fun () -> pp_prfp prf);
1280 prf
1281
1282 let trans_un_prop op_constr op_iff p1 prf1 =
1283 match prf1 with
1284 | IProof -> IProof
1285 | CProof p1' -> CProof (EConstr.mkApp (op_constr, [|p1'|]))
1286 | TProof (p1', prf) ->
1287 TProof
1288 ( EConstr.mkApp (op_constr, [|p1'|])
1289 , EConstr.mkApp (op_iff, [|p1; p1'; prf|]) )
1290
1291 let rec trans_prop env evd e =
1292 match classify_prop env evd e with
1293 | BINOP ({op_constr; op_iff}, p1, p2) ->
1294 let evd, prf1 = trans_prop env evd p1 in
1295 let evd, prf2 = trans_prop env evd p2 in
1296 evd, trans_bin_prop op_constr op_iff p1 prf1 p2 prf2
1297 | UNOP ({op_constr; op_iff}, p1) ->
1298 let evd, prf1 = trans_prop env evd p1 in
1299 evd, trans_un_prop op_constr op_iff p1 prf1
1300 | OTHEROP (c, a) -> (
1301 try
1302 let k, t =
1303 find_option
1304 (match_operator env evd c a None)
1305 (ConstrMap.find_all evd c !table_cache)
1306 in
1307 let n = Array.length a in
1308 match k with
1309 | BinRel {decl = br; deriv = rop} ->
1310 let evd, a1 =
1311 trans_expr env evd
1312 { constr = a.(n - 2)
1313 ; typ = rop.EBinRelT.source
1314 ; inj = rop.EBinRelT.inj }
1315 in
1316 let evd, a2 =
1317 trans_expr env evd
1318 { constr = a.(n - 1)
1319 ; typ = rop.EBinRelT.source
1320 ; inj = rop.EBinRelT.inj }
1321 in
1322 trans_binrel env evd e rop a.(n - 2) a1 a.(n - 1) a2
1323 | _ -> evd, IProof
1324 with Not_found | DestKO -> evd, IProof )
1325
1326 let trans_check_prop env evd t =
1327 if is_prop env evd t then
1328 let evd, p = trans_prop env evd t in
1329 evd, Some p
1330 else evd, None
1331
1332 let get_hyp_typ = function
1333 | NamedDecl.LocalDef (h, _, ty) | NamedDecl.LocalAssum (h, ty) ->
1334 (h.Context.binder_name, EConstr.of_constr ty)
1335
1336 let trans_hyps env evd l =
1337 List.fold_left
1338 (fun (evd, acc) decl ->
1339 let h, ty = get_hyp_typ decl in
1340 match trans_check_prop env evd ty with
1341 | evd, None -> evd, acc
1342 | evd, Some p' -> evd, (h, ty, p') :: acc)
1343 (evd, []) l
1344
1345 let trans_hyp h t0 prfp =
1346 debug_zify (fun () -> Pp.(str "trans_hyp: " ++ pp_prfp prfp ++ fnl ()));
1347 match prfp with
1348 | IProof -> Tacticals.tclIDTAC (* Should detect before *)
1349 | CProof t' ->
1350 Proofview.Goal.enter (fun gl ->
1351 let env = Proofview.Goal.env gl in
1352 let evd = Proofview.Goal.sigma gl in
1353 let t' = Reductionops.nf_betaiota env evd t' in
1354 Tactics.change_in_hyp ~check:true None
1355 (Tactics.make_change_arg t')
1356 (h, Locus.InHypTypeOnly))
1357 | TProof (t', prf) ->
1358 Tacticals.(
1359 Proofview.Goal.enter (fun gl ->
1360 let env = Proofview.Goal.env gl in
1361 let evd = Proofview.Goal.sigma gl in
1362 let target = Reductionops.nf_betaiota env evd t' in
1363 let h' = Tactics.fresh_id_in_env Id.Set.empty h env in
1364 let prf =
1365 EConstr.mkApp (force rew_iff, [|t0; target; prf; EConstr.mkVar h|])
1366 in
1367 tclTHEN
1368 (Tactics.pose_proof (Name.Name h') prf)
1369 (tclTRY
1370 (tclTHEN (Tactics.clear [h]) (Tactics.rename_hyp [(h', h)])))))
1371
1372 let trans_concl prfp =
1373 debug_zify (fun () -> Pp.(str "trans_concl: " ++ pp_prfp prfp ++ fnl ()));
1374 match prfp with
1375 | IProof -> Tacticals.tclIDTAC
1376 | CProof t ->
1377 Proofview.Goal.enter (fun gl ->
1378 let env = Proofview.Goal.env gl in
1379 let evd = Proofview.Goal.sigma gl in
1380 let t' = Reductionops.nf_betaiota env evd t in
1381 Tactics.change_concl t')
1382 | TProof (t, prf) ->
1383 Proofview.Goal.enter (fun gl ->
1384 let env = Proofview.Goal.env gl in
1385 let evd = Proofview.Goal.sigma gl in
1386 let typ = get_type_of env evd prf in
1387 match EConstr.kind evd typ with
1388 | App (c, a) when Array.length a = 2 ->
1389 Tactics.apply (EConstr.mkApp (Lazy.force rew_iff_rev, [|a.(0); a.(1); prf|]))
1390 | _ ->
1391 raise (CErrors.anomaly Pp.(str "zify cannot transform conclusion")))
1392
1393 let tclTHENOpt e tac tac' =
1394 match e with None -> tac' | Some e' -> Tacticals.tclTHEN (tac e') tac'
1395
1396
1397 let elim_binding x t ty =
1398 Proofview.Goal.enter (fun gl ->
1399 let env = Proofview.Goal.env gl in
1400 let h =
1401 Tactics.fresh_id_in_env Id.Set.empty (Nameops.add_prefix "heq_" x) env
1402 in
1403 Tacticals.tclTHEN
1404 (Tactics.pose_proof (Name h) (eq_proof ty (EConstr.mkVar x) t))
1405 (Tacticals.tclTRY (Tactics.clear_body [x])))
1406
1407 let do_let tac (h : Constr.named_declaration) =
1408 match h with
1409 | Context.Named.Declaration.LocalAssum _ -> Tacticals.tclIDTAC
1410 | Context.Named.Declaration.LocalDef (id, t, ty) ->
1411 Proofview.Goal.enter (fun gl ->
1412 let env = Proofview.Goal.env gl in
1413 let evd = Proofview.Goal.sigma gl in
1414 try
1415 let x = id.Context.binder_name in
1416 ignore
1417 (let eq = Lazy.force eq in
1418 find_option
1419 (match_operator env evd eq
1420 [|EConstr.of_constr ty; EConstr.mkVar x; EConstr.of_constr t|] None)
1421 (ConstrMap.find_all evd eq !table_cache));
1422 tac x (EConstr.of_constr t) (EConstr.of_constr ty)
1423 with Not_found -> Tacticals.tclIDTAC)
1424
1425 let iter_let_aux tac =
1426 Proofview.Goal.enter (fun gl ->
1427 let env = Proofview.Goal.env gl in
1428 let sign = Environ.named_context env in
1429 init_cache ();
1430 Tacticals.tclMAP (do_let tac) sign)
1431
1432 let iter_let (tac : Ltac_plugin.Tacinterp.Value.t) =
1433 iter_let_aux (fun (id : Names.Id.t) t ty ->
1434 Ltac_plugin.Tacinterp.Value.apply tac
1435 [ Ltac_plugin.Tacinterp.Value.of_constr (EConstr.mkVar id)
1436 ; Ltac_plugin.Tacinterp.Value.of_constr t
1437 ; Ltac_plugin.Tacinterp.Value.of_constr ty ])
1438
1439 let elim_let = iter_let_aux elim_binding
1440
1441 let zify_tac =
1442 Proofview.Goal.enter (fun gl ->
1443 Rocqlib.check_required_library ["Stdlib"; "micromega"; "ZifyClasses"];
1444 Rocqlib.check_required_library ["Stdlib"; "micromega"; "ZifyInst"];
1445 init_cache ();
1446 let evd = Proofview.Goal.sigma gl in
1447 let env = Proofview.Goal.env gl in
1448 let sign = Environ.named_context env in
1449 let concl = Proofview.Goal.concl gl in
1450 let evd, concl = trans_check_prop env evd concl in
1451 let evd, hyps = trans_hyps env evd sign in
1452 let l = CstrTable.get () in
1453 Proofview.tclTHEN (Proofview.Unsafe.tclEVARS evd)
1454 (tclTHENOpt concl trans_concl
1455 (Tacticals.tclTHEN
1456 (Tacticals.tclTHENLIST
1457 (List.rev_map (fun (h, p, t) -> trans_hyp h p t) hyps))
1458 (CstrTable.gen_cstr l))))
1459
1460 type pscript = Set of Names.Id.t * EConstr.t | Pose of Names.Id.t * EConstr.t
1461
1462 type spec_env =
1463 { map : Names.Id.t HConstr.t
1464 ; spec_name : Names.Id.t
1465 ; term_name : Names.Id.t
1466 ; fresh : Nameops.Subscript.t
1467 ; proofs : pscript list }
1468
1469 let register_constr {map; spec_name; term_name; fresh; proofs} c thm =
1470 let tname = Nameops.add_subscript term_name fresh in
1471 let sname = Nameops.add_subscript spec_name fresh in
1472 ( EConstr.mkVar tname
1473 , { map = HConstr.add c tname map
1474 ; spec_name
1475 ; term_name
1476 ; fresh = Nameops.Subscript.succ fresh
1477 ; proofs = Set (tname, c) :: Pose (sname, thm) :: proofs } )
1478
1479 let fresh_subscript env =
1480 let ctx = (Environ.named_context_val env).Environ.env_named_map in
1481 Nameops.Subscript.succ
1482 (Names.Id.Map.fold
1483 (fun id _ s ->
1484 let _, s' = Nameops.get_subscript id in
1485 let cmp = Nameops.Subscript.compare s s' in
1486 if cmp = 0 then s else if cmp < 0 then s' else s)
1487 ctx Nameops.Subscript.zero)
1488
1489 let init_env sname tname s =
1490 { map = HConstr.empty
1491 ; spec_name = sname
1492 ; term_name = tname
1493 ; fresh = s
1494 ; proofs = [] }
1495
1496 let rec spec_of_term env evd (senv : spec_env) t =
1497 let get_name t env =
1498 try EConstr.mkVar (HConstr.find t senv.map) with Not_found -> t
1499 in
1500 let c, a = decompose_app env evd t in
1501 if a = [||] then (* The term cannot be decomposed. *)
1502 (get_name t senv, senv)
1503 else
1504 (* recursively analyse the sub-terms *)
1505 let a', senv' =
1506 Array.fold_right
1507 (fun e (l, senv) ->
1508 let r, senv = spec_of_term env evd senv e in
1509 (r :: l, senv))
1510 a ([], senv)
1511 in
1512 let a' = Array.of_list a' in
1513 let t' = EConstr.mkApp (c, a') in
1514 try (EConstr.mkVar (HConstr.find t' senv'.map), senv')
1515 with Not_found -> (
1516 try
1517 match snd (ConstrMap.find evd c !specs_cache) with
1518 | UnOpSpec s | BinOpSpec s ->
1519 let thm = EConstr.mkApp (s.deriv.ESpecT.spec, a') in
1520 register_constr senv' t' thm
1521 | _ -> (get_name t' senv', senv')
1522 with Not_found | DestKO -> (t', senv') )
1523
1524 let interp_pscript s =
1525 match s with
1526 | Set (id, c) ->
1527 Tacticals.tclTHEN
1528 (Tactics.letin_tac None (Names.Name id) c None
1529 {Locus.onhyps = None; Locus.concl_occs = Locus.AllOccurrences})
1530 (Tacticals.tclTRY (Tactics.clear_body [id]))
1531 | Pose (id, c) -> Tactics.pose_proof (Names.Name id) c
1532
1533 let rec interp_pscripts l =
1534 match l with
1535 | [] -> Tacticals.tclIDTAC
1536 | s :: l -> Tacticals.tclTHEN (interp_pscript s) (interp_pscripts l)
1537
1538 let spec_of_hyps =
1539 Proofview.Goal.enter (fun gl ->
1540 let env = Proofview.Goal.env gl in
1541 let concl = Proofview.Goal.concl gl in
1542 let evd = Proofview.Goal.sigma gl in
1543 let terms = concl :: List.map NamedDecl.get_type (EConstr.named_context env) in
1544 let s = fresh_subscript env in
1545 let env =
1546 List.fold_left
1547 (fun acc t -> snd (spec_of_term env evd acc t))
1548 (init_env (Names.Id.of_string "H") (Names.Id.of_string "z") s)
1549 terms
1550 in
1551 interp_pscripts (List.rev env.proofs))
1552
1553 let iter_specs = spec_of_hyps
1554
1555 let find_hyp evd t l =
1556 try
1557 Some
1558 (EConstr.mkVar
1559 (fst (List.find (fun (h, t') -> EConstr.eq_constr evd t t') l)))
1560 with Not_found -> None
1561
1562 let find_proof evd t l =
1563 if EConstr.eq_constr evd t (Lazy.force op_True) then Some (Lazy.force op_I)
1564 else
1565 let l = List.map (fun decl -> NamedDecl.get_id decl, NamedDecl.get_type decl) l in
1566 find_hyp evd t l
1567
1568 (** [sat_constr env evd sub taclist hyps c d]= (sub',taclist',hyps') where
1569 - sub' is a fresh subscript obtained from sub
1570 - taclist' is obtained from taclist by posing the lemma 'd' applied to 'c'
1571 - hyps' is obtained from hyps'
1572 taclist and hyps are threaded to avoid adding duplicates
1573 *)
1574 let sat_constr env evd hyps (sub, taclist, prfs) c d =
1575 match EConstr.kind evd c with
1576 | App (c, args) ->
1577 if Array.length args = 2 then
1578 let h1 =
1579 Tacred.cbv_beta env evd
1580 (EConstr.mkApp (d.ESatT.parg1, [|args.(0)|]))
1581 in
1582 let h2 =
1583 Tacred.cbv_beta env evd
1584 (EConstr.mkApp (d.ESatT.parg2, [|args.(1)|]))
1585 in
1586 let n = Nameops.add_subscript (Names.Id.of_string "__sat") sub in
1587 let trm =
1588 match (find_proof evd h1 hyps, find_proof evd h2 hyps) with
1589 | Some h1, Some h2 ->
1590 (EConstr.mkApp (d.ESatT.satOK, [|args.(0); args.(1); h1; h2|]))
1591 | Some h1, _ ->
1592 EConstr.mkApp (d.ESatT.satOK, [|args.(0); args.(1); h1|])
1593 | _, _ -> EConstr.mkApp (d.ESatT.satOK, [|args.(0); args.(1)|])
1594 in
1595 let rtrm = Tacred.cbv_beta env evd trm in
1596 let typ = Retyping.get_type_of env evd rtrm in
1597 match find_hyp evd typ prfs with
1598 | None -> (Nameops.Subscript.succ sub, (Tactics.pose_proof (Names.Name n) rtrm :: taclist) , (n,typ)::prfs)
1599 | Some _ -> (sub, taclist, prfs)
1600 else (sub,taclist,prfs)
1601 | _ -> (sub,taclist,prfs)
1602
1603
1604 let get_all_sat env evd c =
1605 List.fold_left
1606 (fun acc e -> match e with _, Saturate s -> s :: acc | _ -> acc)
1607 []
1608 ( try ConstrMap.find_all evd c !saturate_cache
1609 with DestKO | Not_found -> [] )
1610
1611 let saturate =
1612 Proofview.Goal.enter (fun gl ->
1613 init_cache ();
1614 let table = CstrTable.HConstr.create 20 in
1615 let concl = Proofview.Goal.concl gl in
1616 let env = Proofview.Goal.env gl in
1617 let hyps = EConstr.named_context env in
1618 let evd = Proofview.Goal.sigma gl in
1619 let rec sat t =
1620 match EConstr.kind evd t with
1621 | App (c, args) ->
1622 sat c;
1623 Array.iter sat args;
1624 if Array.length args = 2 then
1625 let ds = get_all_sat env evd c in
1626 if ds = [] || CstrTable.HConstr.mem table t
1627 then ()
1628 else List.iter (fun x ->
1629 CstrTable.HConstr.add table t x.deriv) ds
1630 else ()
1631 | Prod (a, t1, t2) when a.Context.binder_name = Names.Anonymous ->
1632 sat t1; sat t2
1633 | _ -> ()
1634 in
1635 (* Collect all the potential saturation lemma *)
1636 sat concl;
1637 let () = List.iter (fun decl -> sat (NamedDecl.get_type decl)) hyps in
1638 let s0 = fresh_subscript env in
1639 let (_,tacs,_) = CstrTable.HConstr.fold (fun c d acc -> sat_constr env evd hyps acc c d) table (s0,[],[]) in
1640 Tacticals.tclTHENLIST tacs)