Debian Codegraph @

plugins/ltac/comRewrite.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 Util
12 open Names
13 open Constr
14 open Constrexpr
15 open EConstr
16 open Libnames
17
18 let () = CErrors.register_handler begin function
19 | Rewrite.RewriteFailure (env, sigma, e) ->
20 let e = Himsg.explain_pretype_error env sigma e in
21 Some Pp.(str"setoid rewrite failed: " ++ e)
22 | _ -> None
23 end
24
25 module TC = Typeclasses
26
27 let classes_dirpath =
28 Names.DirPath.make (List.map Id.of_string ["Classes";"Corelib"])
29
30 let init_setoid () =
31 if is_dirpath_prefix_of classes_dirpath (Libnames.dirpath_of_path @@ Lib.cwd ()) then ()
32 else Rocqlib.check_required_library ["Corelib";"Setoids";"Setoid"]
33
34 type rewrite_attributes = {
35 poly : PolyFlags.t;
36 locality : Hints.hint_locality;
37 }
38
39 let rewrite_attributes =
40 let open Attributes.Notations in
41 Attributes.(poly PolyFlags.Definition ++ locality) >>=
42 fun (poly, locality) ->
43 let locality =
44 if Locality.make_section_locality locality then Hints.Local else SuperGlobal
45 in
46 Attributes.Notations.return { poly; locality }
47
48 (** Utility functions *)
49
50 module PropGlobal = struct
51
52 let respectful_ref () = Rocqlib.lib_ref "rewrite.prop.respectful"
53
54 let proper_class =
55 fun () -> Option.get (TC.class_info (Global.env ()) (Rocqlib.lib_ref "rewrite.prop.Proper"))
56
57 let proper_proj () = Rocqlib.lib_ref "rewrite.prop.proper_prf"
58
59 end
60
61 (* By default the strategy for "rewrite_db" is top-down *)
62
63 let mkappc s l = CAst.make @@ CAppExpl ((qualid_of_ident (Id.of_string s),None),l)
64
65 let declare_an_instance {CAst.v=n; loc} s args =
66 (((CAst.make ?loc @@ Name n),None),
67 CAst.make @@ CAppExpl ((qualid_of_string s,None), args))
68
69 let declare_instance a aeq n s = declare_an_instance n s [a;aeq]
70
71 let anew_instance atts binders (name,t) fields =
72 let _id = Classes.new_instance ~poly:atts.poly ~locality:atts.locality
73 name binders t (true, CAst.make @@ CRecord (fields))
74 Hints.empty_hint_info
75 in
76 ()
77
78 let add_suffix n suff = CAst.map (fun n -> Nameops.add_suffix n suff) n
79
80 let declare_instance_refl atts binders a aeq n lemma =
81 let instance = declare_instance a aeq (add_suffix n "_Reflexive") "Corelib.Classes.RelationClasses.Reflexive"
82 in anew_instance atts binders instance
83 [(qualid_of_ident (Id.of_string "reflexivity"),lemma)]
84
85 let declare_instance_sym atts binders a aeq n lemma =
86 let instance = declare_instance a aeq (add_suffix n "_Symmetric") "Corelib.Classes.RelationClasses.Symmetric"
87 in anew_instance atts binders instance
88 [(qualid_of_ident (Id.of_string "symmetry"),lemma)]
89
90 let declare_instance_trans atts binders a aeq n lemma =
91 let instance = declare_instance a aeq (add_suffix n "_Transitive") "Corelib.Classes.RelationClasses.Transitive"
92 in anew_instance atts binders instance
93 [(qualid_of_ident (Id.of_string "transitivity"),lemma)]
94
95 let declare_relation atts ?(binders=[]) a aeq n refl symm trans =
96 init_setoid ();
97 let instance = declare_instance a aeq (add_suffix n "_relation") "Corelib.Classes.RelationClasses.RewriteRelation" in
98 let () = anew_instance atts binders instance [] in
99 match (refl,symm,trans) with
100 (None, None, None) -> ()
101 | (Some lemma1, None, None) ->
102 declare_instance_refl atts binders a aeq n lemma1
103 | (None, Some lemma2, None) ->
104 declare_instance_sym atts binders a aeq n lemma2
105 | (None, None, Some lemma3) ->
106 declare_instance_trans atts binders a aeq n lemma3
107 | (Some lemma1, Some lemma2, None) ->
108 let () = declare_instance_refl atts binders a aeq n lemma1 in
109 declare_instance_sym atts binders a aeq n lemma2
110 | (Some lemma1, None, Some lemma3) ->
111 let () = declare_instance_refl atts binders a aeq n lemma1 in
112 let () = declare_instance_trans atts binders a aeq n lemma3 in
113 let instance = declare_instance a aeq n "Corelib.Classes.RelationClasses.PreOrder" in
114 anew_instance atts binders instance
115 [(qualid_of_ident (Id.of_string "PreOrder_Reflexive"), lemma1);
116 (qualid_of_ident (Id.of_string "PreOrder_Transitive"),lemma3)]
117 | (None, Some lemma2, Some lemma3) ->
118 let () = declare_instance_sym atts binders a aeq n lemma2 in
119 let () = declare_instance_trans atts binders a aeq n lemma3 in
120 let instance = declare_instance a aeq n "Corelib.Classes.RelationClasses.PER" in
121 anew_instance atts binders instance
122 [(qualid_of_ident (Id.of_string "PER_Symmetric"), lemma2);
123 (qualid_of_ident (Id.of_string "PER_Transitive"),lemma3)]
124 | (Some lemma1, Some lemma2, Some lemma3) ->
125 let () = declare_instance_refl atts binders a aeq n lemma1 in
126 let () = declare_instance_sym atts binders a aeq n lemma2 in
127 let () = declare_instance_trans atts binders a aeq n lemma3 in
128 let instance = declare_instance a aeq n "Corelib.Classes.RelationClasses.Equivalence" in
129 anew_instance atts binders instance
130 [(qualid_of_ident (Id.of_string "Equivalence_Reflexive"), lemma1);
131 (qualid_of_ident (Id.of_string "Equivalence_Symmetric"), lemma2);
132 (qualid_of_ident (Id.of_string "Equivalence_Transitive"), lemma3)]
133
134 let cHole = CAst.make @@ CHole (None)
135
136 let proper_projection env sigma r ty =
137 let rel_vect n m = Array.init m (fun i -> mkRel(n+m-i)) in
138 let ctx, inst = decompose_prod_decls sigma ty in
139 let mor, args = destApp sigma inst in
140 let instarg = mkApp (r, rel_vect 0 (List.length ctx)) in
141 let sigma, proj = Evd.fresh_global env sigma (PropGlobal.proper_proj ()) in
142 let app = mkApp (proj,
143 Array.append args [| instarg |]) in
144 sigma, it_mkLambda_or_LetIn app ctx
145
146 let declare_projection {CAst.v=name; loc} instance_id r =
147 let env = Global.env () in
148 let poly = Environ.is_polymorphic env r in
149 let poly = PolyFlags.of_univ_poly poly (* FIXME: cumulativity not handled *) in
150 let sigma = Evd.from_env env in
151 let sigma,c = Evd.fresh_global env sigma r in
152 let ty = Retyping.get_type_of env sigma c in
153 let sigma, body = proper_projection env sigma c ty in
154 let sigma, typ = Typing.type_of env sigma body in
155 let ctx, typ = decompose_prod_decls sigma typ in
156 let typ =
157 let n =
158 let rec aux t =
159 match EConstr.kind sigma t with
160 | App (f, [| a ; a' ; rel; rel' |])
161 when isRefX env sigma (PropGlobal.respectful_ref ()) f ->
162 succ (aux rel')
163 | _ -> 0
164 in
165 let init =
166 match EConstr.kind sigma typ with
167 App (f, args) when isRefX env sigma (PropGlobal.respectful_ref ()) f ->
168 mkApp (f, fst (Array.chop (Array.length args - 2) args))
169 | _ -> typ
170 in aux init
171 in
172 let ctx,ccl = Reductionops.whd_decompose_prod_n env sigma (3 * n) typ
173 in it_mkProd ccl ctx
174 in
175 let types = Some (it_mkProd_or_LetIn typ ctx) in
176 let kind = Decls.(IsDefinition Definition) in
177 let impargs, udecl = [], UState.default_univ_decl in
178 let cinfo = Declare.CInfo.make ?loc ~name ~impargs ~typ:types () in
179 let info = Declare.Info.make ~kind ~udecl ~poly () in
180 let _r : GlobRef.t =
181 Declare.declare_definition ~cinfo ~info ~opaque:false ~body sigma
182 in ()
183
184 let add_setoid atts binders a aeq t n =
185 init_setoid ();
186 let () = declare_instance_refl atts binders a aeq n (mkappc "Seq_refl" [a;aeq;t]) in
187 let () = declare_instance_sym atts binders a aeq n (mkappc "Seq_sym" [a;aeq;t]) in
188 let () = declare_instance_trans atts binders a aeq n (mkappc "Seq_trans" [a;aeq;t]) in
189 let instance = declare_instance a aeq n "Corelib.Classes.RelationClasses.Equivalence"
190 in
191 anew_instance atts binders instance
192 [(qualid_of_ident (Id.of_string "Equivalence_Reflexive"), mkappc "Seq_refl" [a;aeq;t]);
193 (qualid_of_ident (Id.of_string "Equivalence_Symmetric"), mkappc "Seq_sym" [a;aeq;t]);
194 (qualid_of_ident (Id.of_string "Equivalence_Transitive"), mkappc "Seq_trans" [a;aeq;t])]
195
196 let add_morphism_as_parameter atts m n : unit =
197 init_setoid ();
198 let instance_id = add_suffix n "_Proper" in
199 let env = Global.env () in
200 let evd = Evd.from_env env in
201 let kind = Decls.(IsAssumption Logical) in
202 let impargs, udecl = [], UState.default_univ_decl in
203 let evd, types = Rewrite.Internal.build_morphism_signature env evd m in
204 let evd, pe = Declare.prepare_parameter ~poly:atts.poly ~udecl ~types evd in
205 let cst = Declare.declare_constant ?loc:instance_id.loc ~name:instance_id.v ~kind (Declare.ParameterEntry pe) in
206 let cst = GlobRef.ConstRef cst in
207 Classes.Internal.add_instance
208 (PropGlobal.proper_class ()) Hints.empty_hint_info atts.locality cst;
209 declare_projection n instance_id cst
210
211 let add_morphism_interactive atts ~tactic m n : Declare.Proof.t =
212 init_setoid ();
213 let instance_id = add_suffix n "_Proper" in
214 let env = Global.env () in
215 let evd = Evd.from_env env in
216 let evd, morph = Rewrite.Internal.build_morphism_signature env evd m in
217 let kind = Decls.(IsDefinition Instance) in
218 let hook { Declare.Hook.S.dref; _ } = dref |> function
219 | GlobRef.ConstRef cst ->
220 Classes.Internal.add_instance (PropGlobal.proper_class ()) Hints.empty_hint_info
221 atts.locality (GlobRef.ConstRef cst);
222 declare_projection n instance_id (GlobRef.ConstRef cst)
223 | _ -> assert false
224 in
225 let hook = Declare.Hook.make hook in
226 Flags.silently
227 (fun () ->
228 let cinfo = Declare.CInfo.make ?loc:instance_id.loc ~name:instance_id.v ~typ:morph () in
229 let info = Declare.Info.make ~poly:atts.poly ~hook ~kind () in
230 let lemma = Declare.Proof.start ~cinfo ~info evd in
231 fst (Declare.Proof.by (Global.env ()) tactic lemma)) ()
232
233 let add_morphism atts ~tactic binders m s n =
234 init_setoid ();
235 let instance_id = add_suffix n "_Proper" in
236 let instance_name = (CAst.make ?loc:instance_id.loc @@ Name instance_id.v),None in
237 let instance_t =
238 CAst.make @@ CAppExpl
239 ((Libnames.qualid_of_string "Corelib.Classes.Morphisms.Proper",None),
240 [cHole; s; m])
241 in
242 let _id, lemma = Classes.new_instance_interactive
243 ~locality:atts.locality ~poly:atts.poly
244 instance_name binders instance_t
245 ~tac:tactic ~hook:(declare_projection n instance_id)
246 Hints.empty_hint_info None
247 in
248 lemma (* no instance body -> always open proof *)