Debian Codegraph @

vernac/comHints.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
14 (** (Partial) implementation of the [Hint] command; some more
15 functionality still lives in tactics/hints.ml *)
16
17 let project_hint ~poly pri l2r r =
18 let open EConstr in
19 let open Rocqlib in
20 let gr = Smartlocate.global_with_alias r in
21 let env = Global.env () in
22 let sigma = Evd.from_env env in
23 let sigma, c = Evd.fresh_global env sigma gr in
24 let t = Retyping.get_type_of env sigma c in
25 let t =
26 Tacred.reduce_to_quantified_ref env sigma (lib_ref "core.iff.type") t
27 in
28 let sign, ccl = decompose_prod_decls sigma t in
29 let a, b =
30 match snd (decompose_app sigma ccl) with
31 | [|a; b|] -> (a, b)
32 | _ -> assert false
33 in
34 let p = if l2r then lib_ref "core.iff.proj1" else lib_ref "core.iff.proj2" in
35 let sigma, p = Evd.fresh_global env sigma p in
36 let c =
37 Reductionops.whd_beta env sigma
38 (mkApp (c, Context.Rel.instance mkRel 0 sign))
39 in
40 let c =
41 it_mkLambda_or_LetIn
42 (mkApp
43 ( p
44 , [| mkArrow a ERelevance.relevant (Vars.lift 1 b)
45 ; mkArrow b ERelevance.relevant (Vars.lift 1 a)
46 ; c |] ))
47 sign
48 in
49 let name =
50 Nameops.add_suffix
51 (Nametab.basename_of_global gr)
52 ("_proj_" ^ if l2r then "l2r" else "r2l")
53 in
54 let ctx = Evd.univ_entry ~poly sigma in
55 let c = EConstr.to_constr sigma c in
56 let cb =
57 Declare.(DefinitionEntry (definition_entry ~univs:ctx ~opaque:false c))
58 in
59 let c =
60 Declare.declare_constant ~local:Locality.ImportDefaultBehavior ~name
61 ~kind:Decls.(IsDefinition Definition)
62 cb
63 in
64 let info = {Typeclasses.hint_priority = pri; hint_pattern = None} in
65 (info, true, GlobRef.ConstRef c)
66
67 (* Only error when we have to (axioms may be instantiated if from functors)
68 XXX maybe error if not from a functor argument?
69 *)
70 let soft_evaluable = Tacred.soft_evaluable_of_global_reference
71
72 (* Slightly more lenient global hint syntax for backwards compatibility *)
73 let rectify_hint_constr h = match h with
74 | Vernacexpr.HintsReference qid -> Some qid
75 | Vernacexpr.HintsConstr c ->
76 let open Constrexpr in
77 match c.CAst.v with
78 | CAppExpl ((qid, None), []) -> Some qid
79 | _ -> None
80
81 let interp_hints ~poly h =
82 let env = Global.env () in
83 let sigma = Evd.from_env env in
84 let fref r =
85 let gr = Smartlocate.global_with_alias r in
86 Dumpglob.add_glob ?loc:r.CAst.loc gr;
87 gr
88 in
89 let fr r = soft_evaluable ?loc:r.CAst.loc (fref r) in
90 let fi c =
91 match rectify_hint_constr c with
92 | Some c ->
93 Smartlocate.global_with_alias c
94 | None ->
95 CErrors.user_err (Pp.strbrk
96 "Declaring arbitrary terms as hints is forbidden. You must declare a \
97 toplevel constant instead.")
98 in
99 let fp = Constrintern.interp_constr_pattern env sigma in
100 let fres (info, b, r) =
101 let gr = fi r in
102 let info =
103 { info with
104 Typeclasses.hint_pattern = Option.map fp info.Typeclasses.hint_pattern
105 }
106 in
107 (info, b, gr)
108 in
109 let open Hints in
110 let open Vernacexpr in
111 let ft = function
112 | HintsVariables -> HintsVariables
113 | HintsConstants -> HintsConstants
114 | HintsProjections -> HintsProjections
115 | HintsReferences lhints -> HintsReferences (List.map fr lhints)
116 in
117 let fp = Constrintern.interp_constr_pattern (Global.env ()) in
118 match h with
119 | HintsResolve lhints -> HintsResolveEntry (List.map fres lhints)
120 | HintsResolveIFF (l2r, lc, n) ->
121 HintsResolveEntry (List.map (project_hint ~poly n l2r) lc)
122 | HintsImmediate lhints -> HintsImmediateEntry (List.map fi lhints)
123 | HintsUnfold lhints -> HintsUnfoldEntry (List.map fr lhints)
124 | HintsTransparency (t, b) -> HintsTransparencyEntry (ft t, b)
125 | HintsMode (r, l) -> HintsModeEntry (fref r, l)
126 | HintsConstructors lqid ->
127 let constr_hints_of_ind qid =
128 let ind = Smartlocate.global_inductive_with_alias qid in
129 Dumpglob.dump_reference ?loc:qid.CAst.loc "<>"
130 (Libnames.string_of_qualid qid)
131 "ind";
132 List.init (Inductiveops.nconstructors env ind) (fun i ->
133 let c = (ind, i + 1) in
134 let gr = GlobRef.ConstructRef c in
135 ( empty_hint_info
136 , true
137 , gr ))
138 in
139 HintsResolveEntry (List.flatten (List.map constr_hints_of_ind lqid))
140 | HintsExtern (pri, patcom, tacexp) ->
141 let pat = Option.map (fp sigma) patcom in
142 let ltacvars = match pat with None -> Id.Set.empty | Some (l, _) -> l in
143 let tacexp = Gentactic.intern ~ltacvars env tacexp in
144 HintsExternEntry
145 ({Typeclasses.hint_priority = Some pri; hint_pattern = pat}, tacexp)