Debian Codegraph @

vernac/comDefinition.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 Pp
12 open Util
13 open Redexpr
14 open Constrintern
15
16 (* Commands of the interface: Constant definitions *)
17
18 let red_constant_body red_opt env sigma body = match red_opt with
19 | None -> sigma, body
20 | Some red ->
21 let red, _ = reduction_of_red_expr env red in
22 red env sigma body
23
24 let warn_implicits_in_term =
25 CWarnings.create ~name:"implicits-in-term" ~category:CWarnings.CoreCategories.implicits
26 (fun () ->
27 strbrk "Implicit arguments declaration relies on type." ++ spc () ++
28 strbrk "Discarding incompatible declaration in term.")
29
30 let check_imps ~impsty ~impsbody =
31 let rec aux impsty impsbody =
32 match impsty, impsbody with
33 | a1 :: impsty, a2 :: impsbody ->
34 let () = match a1.CAst.v, a2.CAst.v with
35 | None , None | Some _, None -> ()
36 | Some (_,b1) , Some (_,b2) ->
37 if not ((b1:bool) = b2) then warn_implicits_in_term ?loc:a2.CAst.loc ()
38 | None, Some _ -> warn_implicits_in_term ?loc:a2.CAst.loc ()
39 in
40 aux impsty impsbody
41 | _ :: _, [] | [], _ :: _ -> (* Information only on one side *) ()
42 | [], [] -> () in
43 aux impsty impsbody
44
45 let protect_pattern_in_binder bl c ctypopt =
46 (* We turn "Definition d binders := body : typ" into *)
47 (* "Definition d := fun binders => body:type" *)
48 (* This is a hack while waiting for LocalPattern in regular environments *)
49 if List.exists (function Constrexpr.CLocalPattern _ -> true | _ -> false) bl
50 then
51 let t = match ctypopt with
52 | None -> CAst.make ?loc:c.CAst.loc (Constrexpr.CHole (None))
53 | Some t -> t in
54 let loc = Loc.merge_opt c.CAst.loc t.CAst.loc in
55 let c = CAst.make ?loc @@ Constrexpr.CCast (c, Some Constr.DEFAULTcast, t) in
56 let loc = match List.hd bl with
57 | Constrexpr.CLocalAssum (a::_,_,_,_) | Constrexpr.CLocalDef (a,_,_,_) -> a.CAst.loc
58 | Constrexpr.CLocalPattern {CAst.loc} -> loc
59 | Constrexpr.CLocalAssum ([],_,_,_) -> assert false in
60 let apply_under_binders f env evd c =
61 let rec aux env evd c =
62 let open Constr in
63 let open EConstr in
64 let open Context.Rel.Declaration in
65 match kind evd c with
66 | Lambda (x,t,c) ->
67 let evd,c = aux (push_rel (LocalAssum (x,t)) env) evd c in
68 evd, mkLambda (x,t,c)
69 | LetIn (x,b,t,c) ->
70 let evd,c = aux (push_rel (LocalDef (x,b,t)) env) evd c in
71 evd, mkLetIn (x,b,t,c)
72 | Case (ci,u,pms,p,iv,a,bl) ->
73 let (ci, p, iv, a, bl) = EConstr.expand_case env evd (ci, u, pms, p, iv, a, bl) in
74 let evd,bl = Array.fold_left_map (aux env) evd bl in
75 evd, mkCase (EConstr.contract_case env evd (ci, p, iv, a, bl))
76 | Cast (c,_,_) -> f env evd c (* we remove the cast we had set *)
77 (* This last case may happen when reaching the proof of an
78 impossible case, as when pattern-matching on a vector of length 1 *)
79 | _ -> (evd,c) in
80 aux env evd c in
81 ([], Constrexpr_ops.mkLambdaCN ?loc:(Loc.merge_opt loc c.CAst.loc) bl c, None, apply_under_binders)
82 else
83 (bl, c, ctypopt, fun f env evd c -> f env evd c)
84
85 let interp_definition ~program_mode ~poly env evd impl_env bl red_option c ctypopt =
86 let flags = Pretyping.{ all_no_fail_flags with program_mode; poly } in
87 let (bl, c, ctypopt, apply_under_binders) = protect_pattern_in_binder bl c ctypopt in
88 (* Build the parameters *)
89 let evd, (impls, ((env_bl, ctx), imps1, _locs)) = interp_context_evars ~program_mode ~impl_env env evd bl in
90 (* Build the type *)
91 let evd, tyopt = Option.fold_left_map
92 (interp_type_evars_impls ~flags ~impls env_bl)
93 evd ctypopt
94 in
95 (* Build the body, and merge implicits from parameters and from type/body *)
96 let evd, c, imps, tyopt =
97 match tyopt with
98 | None ->
99 let evd, (c, impsbody) = interp_constr_evars_impls ~program_mode ~poly ~impls env_bl evd c in
100 evd, c, imps1@impsbody, None
101 | Some (ty, impsty) ->
102 let evd, (c, impsbody) = interp_casted_constr_evars_impls ~program_mode ~poly ~impls env_bl evd c ty in
103 check_imps ~impsty ~impsbody;
104 evd, c, imps1@impsty, Some ty
105 in
106 (* Do the reduction *)
107 let evd, c = apply_under_binders (red_constant_body red_option) env_bl evd c in
108
109 (* Declare the definition *)
110 let c = EConstr.it_mkLambda_or_LetIn c ctx in
111 let tyopt = Option.map (fun ty -> EConstr.it_mkProd_or_LetIn ty ctx) tyopt in
112 evd, (c, tyopt), imps
113
114 let interp_statement ~program_mode env evd ~(flags : Pretyping.inference_flags) ~scope name bl typ =
115 let poly = Pretyping.(flags.poly) in
116 let evd, (impls, ((env, ctx), imps, _locs)) = Constrintern.interp_context_evars ~poly ~program_mode env evd bl in
117 let evd, (t', imps') = Constrintern.interp_type_evars_impls ~flags ~impls env evd typ in
118 let ids = List.map Context.Rel.Declaration.get_name ctx in
119 evd, ids, EConstr.it_mkProd_or_LetIn t' ctx, imps @ imps'
120
121 let do_definition ?loc ?hook ~name ?scope ?clearbody ~poly ?typing_flags ~kind ?using ?user_warns udecl bl red_option c ctypopt =
122 let program_mode = false in
123 let env = Global.env() in
124 let env = Environ.update_typing_flags ?typing_flags env in
125 (* Explicitly bound universes and constraints *)
126 let evd, udecl = interp_univ_decl_opt env udecl in
127 let evd, (body, types), impargs =
128 interp_definition ~program_mode ~poly env evd empty_internalization_env bl red_option c ctypopt
129 in
130 let kind = Decls.IsDefinition kind in
131 let cinfo = Declare.CInfo.make ?loc ~name ~impargs ~typ:types () in
132 let info = Declare.Info.make ?scope ?clearbody ~kind ?hook ~udecl ~poly ?typing_flags ?user_warns () in
133 let _ : Names.GlobRef.t =
134 Declare.declare_definition ~info ~cinfo ~opaque:false ~body ?using evd
135 in ()
136
137 let do_definition_program ?loc ?hook ~pm ~name ~scope ?clearbody ~poly ?typing_flags ~kind ?using ?user_warns udecl bl red_option c ctypopt =
138 let env = Global.env() in
139 let env = Environ.update_typing_flags ?typing_flags env in
140 (* Explicitly bound universes and constraints *)
141 let evd, udecl = interp_univ_decl_opt env udecl in
142 let evd, (body, types), impargs =
143 interp_definition ~program_mode:true ~poly env evd empty_internalization_env bl red_option c ctypopt
144 in
145 let body, typ, uctx, _, obls = Declare.Obls.prepare_obligations ~name poly ~body ?types env evd in
146 Evd.check_univ_decl_early ~poly ~with_obls:true (Evd.from_ctx uctx) udecl [body; typ];
147 let cinfo = Declare.CInfo.make ?loc ~name ~typ ~impargs () in
148 let info = Declare.Info.make ~udecl ~scope ?clearbody ~poly ~kind ?hook ?typing_flags ?user_warns () in
149 Declare.Obls.add_definition ~pm ~info ~cinfo ~opaque:false ~body ~uctx ?using obls
150
151 let do_definition_interactive ?loc ~program_mode ?hook ~name ~scope ?clearbody ~poly ~typing_flags ~kind ?using ?user_warns udecl bl t =
152 let env = Global.env () in
153 let env = Environ.update_typing_flags ?typing_flags env in
154 let flags = Pretyping.{ all_no_fail_flags with program_mode; poly } in
155 let evd, udecl = Constrintern.interp_univ_decl_opt env udecl in
156 let evd, args, typ,impargs = interp_statement ~program_mode ~flags ~scope env evd name bl t in
157 let evd =
158 let inference_hook = if program_mode then Some Declare.Obls.program_inference_hook else None in
159 Pretyping.solve_remaining_evars ?hook:inference_hook flags env evd in
160 let evd = Evd.minimize_universes evd in
161 Pretyping.check_evars_are_solved ~program_mode env evd;
162 let typ = EConstr.to_constr evd typ in
163 Evd.check_univ_decl_early ~poly ~with_obls:false evd udecl [typ];
164 let typ = EConstr.of_constr typ in
165 let info = Declare.Info.make ?hook ~poly ~scope ?clearbody ~kind ~udecl ?typing_flags ?user_warns () in
166 let cinfo = Declare.CInfo.make ?loc ~name ~typ ~args ~impargs () in
167 let evd = if PolyFlags.univ_poly poly then evd else Evd.fix_undefined_variables evd in
168 Declare.Proof.start_definition ~info ~cinfo ?using evd
169
170 let do_definition_refine ?loc ?hook ~name ~scope ?clearbody ~poly ~typing_flags ~kind ?using ?user_warns udecl bl c ctypopt =
171 let env = Global.env() in
172 let env = Environ.update_typing_flags ?typing_flags env in
173 (* Explicitly bound universes and constraints *)
174 let evd, udecl = interp_univ_decl_opt env udecl in
175 let evd, (body, typ), impargs =
176 interp_definition ~program_mode:false ~poly env evd empty_internalization_env bl None c ctypopt
177 in
178 let typ = match typ with Some typ -> typ | None -> Retyping.get_type_of env evd body in
179
180 let info = Declare.Info.make ?hook ~poly ~scope ?clearbody ~kind ~udecl ?typing_flags ?user_warns () in
181 let cinfo = Declare.CInfo.make ?loc ~name ~typ ~impargs () in
182 let evd = if PolyFlags.univ_poly poly then evd else Evd.fix_undefined_variables evd in
183
184 let future_goals, evd = Evd.pop_future_goals evd in
185 let gls = List.rev (Evd.FutureGoals.comb future_goals) in
186 let evd = Evd.push_future_goals evd in
187
188 let lemma = Declare.Proof.start_definition ~cinfo ~info ?using evd in
189 let init_refine =
190 Tacticals.tclTHENLIST [
191 Refine.refine ~typecheck:false (fun evd -> evd, body);
192 Proofview.Unsafe.tclNEWGOALS (CList.map Proofview.with_empty_state gls);
193 Tactics.reduce_after_refine;
194 ]
195 in
196 let lemma, _ = Declare.Proof.by (Global.env ()) init_refine lemma in
197 lemma