Debian Codegraph @

pretyping/arguments_renaming.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 (*i*)
12 open Names
13 open Globnames
14 open Constr
15 open Context
16 open Environ
17 open Util
18 open Libobject
19
20 (*i*)
21
22 module GlobRefMap = Environ.QGlobRef.Map
23
24 let name_table =
25 Summary.ref (GlobRefMap.empty : Name.t list GlobRefMap.t)
26 ~name:"rename-arguments"
27
28 type req =
29 | ReqLocal
30 | ReqGlobal
31
32 let load_rename_args _ (_, (r, names)) =
33 name_table := GlobRefMap.add (Global.env ()) r names !name_table
34
35 let cache_rename_args o = load_rename_args 1 o
36
37 let classify_rename_args = function
38 | ReqLocal, _ -> Dispose
39 | ReqGlobal, _ -> Substitute
40
41 let subst_rename_args (subst, (_, (r, names as orig))) =
42 ReqLocal,
43 let r' = fst (subst_global subst r) in
44 if r==r' then orig else (r', names)
45
46 let discharge_rename_args = function
47 | ReqGlobal, (c, names) as req when not (isVarRef c && Global.is_in_section c) ->
48 (try
49 let var_names = Array.map_to_list (fun c -> Name (destVar c)) (Global.section_instance c) in
50 let names = var_names @ names in
51 Some (ReqGlobal, (c, names))
52 with Not_found -> Some req)
53 | _ -> None
54
55 let inRenameArgs = declare_object { (default_object "RENAME-ARGUMENTS" ) with
56 load_function = load_rename_args;
57 cache_function = cache_rename_args;
58 classify_function = classify_rename_args;
59 subst_function = subst_rename_args;
60 discharge_function = discharge_rename_args;
61 }
62
63 let rename_arguments local r names =
64 let () = match r with
65 | GlobRef.VarRef id ->
66 CErrors.user_err
67 Pp.(str "Arguments of section variables such as "
68 ++ Id.print id
69 ++ str" may not be renamed.")
70 | _ -> ()
71 in
72 let req = if local then ReqLocal else ReqGlobal in
73 Lib.add_leaf (inRenameArgs (req, (r, names)))
74
75 let arguments_names env r = GlobRefMap.find env r !name_table
76
77 let rename_type env ty ref =
78 let name_override old_name override =
79 match override with
80 | Name _ as x -> {old_name with binder_name=x}
81 | Anonymous -> old_name in
82 let rec rename_type_aux c = function
83 | [] -> c
84 | rename :: rest as renamings ->
85 match Constr.kind c with
86 | Prod (old, s, t) ->
87 mkProd (name_override old rename, s, rename_type_aux t rest)
88 | LetIn (old, s, b, t) ->
89 mkLetIn (old ,s, b, rename_type_aux t renamings)
90 | Cast (t,_, _) -> rename_type_aux t renamings
91 | _ -> c
92 in
93 try rename_type_aux ty (arguments_names env ref)
94 with Not_found -> ty
95
96 let rename_typing env c =
97 let j = Typeops.infer env c in
98 let j' =
99 match kind c with
100 | Const (c,u) -> { j with uj_type = rename_type env j.uj_type (GlobRef.ConstRef c) }
101 | Ind (i,u) -> { j with uj_type = rename_type env j.uj_type (GlobRef.IndRef i) }
102 | Construct (k,u) -> { j with uj_type = rename_type env j.uj_type (GlobRef.ConstructRef k) }
103 | _ -> j
104 in j'
105