Debian Codegraph @

plugins/derive/derive.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 Context.Named.Declaration
12 open Vernacentries.DefAttributes
13 open Constrexpr
14
15 let check_allowed_implicit ?loc = function
16 | Generalized _ | Default (MaxImplicit | NonMaxImplicit) ->
17 CErrors.user_err ?loc (Pp.str "Unexpected implicit argument annotation.")
18 | Default Explicit -> ()
19
20 let check_allowed_binders = function
21 | CLocalAssum (n::_, _, impl, _) -> check_allowed_implicit ?loc:n.CAst.loc impl
22 | CLocalPattern _ -> () (* a priori acceptable, but interp_named_context_evars_as_arguments does not support it *)
23 | CLocalDef (n, _, _, _) -> ()
24 | CLocalAssum ([], _, _, _) -> assert false
25
26 let rec fill_assumptions env sigma = function
27 | [] -> sigma, env, []
28 | LocalAssum (na,t) :: ctx ->
29 let sigma, ev = Evarutil.new_evar env sigma ~src:(Loc.tag @@ Evar_kinds.GoalEvar)
30 ~naming:(IntroIdentifier na.binder_name)
31 ~typeclass_candidate:false t
32 in
33 let decl = LocalDef (na,ev,t) in
34 let sigma, env, ctx = fill_assumptions (EConstr.push_named decl env) sigma ctx in
35 sigma, env, decl :: ctx
36 | LocalDef _ as decl :: ctx ->
37 let sigma, env, ctx = fill_assumptions (EConstr.push_named decl env) sigma ctx in
38 sigma, env, decl :: ctx
39
40 (** [start_deriving f suchthat lemma] starts a proof of [suchthat]
41 (which can contain references to [f]) in the context extended by
42 [f:=?x]. When the proof ends, [f] is defined as the value of [?x]
43 and [lemma] as the proof. *)
44 let start_deriving ~atts bl suchthat name : Declare.Proof.t =
45
46 let scope, _local, poly, program_mode, user_warns, typing_flags, using, clearbody =
47 atts.scope, atts.locality, atts.poly, atts.program, atts.user_warns, atts.typing_flags, atts.using, atts.clearbody in
48 if program_mode then CErrors.user_err (Pp.str "Program mode not supported.");
49
50 let env = Global.env () in
51 let sigma = Evd.from_env env in
52 let () = List.iter check_allowed_binders bl in
53 let sigma, (impls_env, ((env', ctx'), _, locs)) = Constrintern.interp_named_context_evars env sigma bl in
54 (* ctx' is in reverse order (last binder first), so reverse it before processing *)
55 let ctx' = List.rev ctx' in
56 let locs = List.rev locs in
57 let sigma, env', ctx' = fill_assumptions env sigma ctx' in
58 let sigma = Evd.shelve sigma (List.map fst (Evar.Map.bindings (Evd.undefined_map sigma))) in
59 let flags = { Pretyping.all_no_fail_flags with poly } in
60 let sigma, (suchthat, impargs) = Constrintern.interp_type_evars_impls ~flags env' sigma ~impls:impls_env suchthat in
61 (* create the initial goals for the proof: |- Type ; |- ?1 ; f:=?2 |- suchthat *)
62 let goals =
63 let open Proofview in
64 let rec aux env sigma = function
65 | [] -> TCons ( env , sigma , suchthat , (fun sigma _ -> TNil sigma))
66 | LocalAssum (id, t) :: _ -> assert false
67 | LocalDef (id, c, t) as d :: ctx ->
68 TCons ( env , sigma , t , (fun sigma ef' ->
69 let sigma = Evd.define (fst (EConstr.destEvar sigma ef')) c sigma in
70 aux (EConstr.push_named d env) sigma ctx)) in
71 aux env sigma ctx' in
72 let kind = Decls.(IsDefinition Definition) in
73 let info = Declare.Info.make ~poly ~kind () in
74 let extract_manual = function Some Impargs.{ impl_pos = (na,_,_); impl_expl = Manual; impl_max } -> Some (na, impl_max) | _ -> None in
75 let cinfo =
76 let open Declare.CInfo in
77 List.map2 (fun d loc ->
78 let name = get_id d in
79 let impargs = Constrintern.implicits_of_decl_in_internalization_env name impls_env in
80 let impargs = List.map CAst.make (List.map extract_manual impargs) in
81 make ?loc ~name ~typ:() ~impargs ()) ctx' locs @
82 [make ?loc:name.CAst.loc ~name:name.CAst.v ~typ:() ~impargs ()] in
83 let lemma = Declare.Proof.start_derive ~name:name.v ~info ~cinfo goals in
84 Declare.Proof.map lemma ~f:(fun p ->
85 Util.pi1 @@ Proof.run_tactic env Proofview.(tclFOCUS 1 1 shelve) p)