Debian Codegraph @

pretyping/keys.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 (** Keys for unification and indexing *)
12
13 open Names
14 open Constr
15 open Libobject
16 open Globnames
17
18 type key =
19 | KGlob of GlobRef.t
20 | KLam
21 | KLet
22 | KProd
23 | KSort
24 | KCase
25 | KFix
26 | KCoFix
27 | KRel
28 | KInt
29 | KFloat
30 | KString
31 | KArray
32
33 module KeyOrdered = struct
34 type t = key
35
36 let hash gr =
37 match gr with
38 | KGlob gr -> 9 + GlobRef.UserOrd.hash gr
39 | KLam -> 0
40 | KLet -> 1
41 | KProd -> 2
42 | KSort -> 3
43 | KCase -> 4
44 | KFix -> 5
45 | KCoFix -> 6
46 | KRel -> 7
47 | KInt -> 8
48 | KFloat -> 9
49 | KString -> 10
50 | KArray -> 11
51
52 let compare gr1 gr2 =
53 match gr1, gr2 with
54 | KGlob gr1, KGlob gr2 -> GlobRef.UserOrd.compare gr1 gr2
55 | _, KGlob _ -> -1
56 | KGlob _, _ -> 1
57 | k, k' -> Int.compare (hash k) (hash k')
58
59 let equal k1 k2 =
60 match k1, k2 with
61 | KGlob gr1, KGlob gr2 -> GlobRef.UserOrd.equal gr1 gr2
62 | _, KGlob _ -> false
63 | KGlob _, _ -> false
64 | k, k' -> k == k'
65 end
66
67 module Keymap = HMap.Make(KeyOrdered)
68 module Keyset = Keymap.Set
69
70 (* Mapping structure for references to be considered equivalent *)
71
72 let keys = Summary.ref Keymap.empty ~name:"Keys_decl"
73
74 let add_kv k v m =
75 try Keymap.modify k (fun k' vs -> Keyset.add v vs) m
76 with Not_found -> Keymap.add k (Keyset.singleton v) m
77
78 let add_keys k v =
79 keys := add_kv k v (add_kv v k !keys)
80
81 let equiv_keys k k' =
82 k == k' || KeyOrdered.equal k k' ||
83 try Keyset.mem k' (Keymap.find k !keys)
84 with Not_found -> false
85
86 let mkKGlob env gr = KGlob (Environ.QGlobRef.canonize env gr)
87
88 (** Registration of keys as an object *)
89
90 let load_keys _ (ref,ref') =
91 add_keys ref ref'
92
93 let cache_keys o =
94 load_keys 1 o
95
96 let subst_key subst k =
97 match k with
98 | KGlob gr -> mkKGlob (Global.env ()) (subst_global_reference subst gr)
99 | _ -> k
100
101 let subst_keys (subst,(k,k')) =
102 (subst_key subst k, subst_key subst k')
103
104 let discharge_key = function
105 | KGlob (GlobRef.VarRef _ as g) when Global.is_in_section g -> None
106 | x -> Some x
107
108 let discharge_keys (k,k') =
109 match discharge_key k, discharge_key k' with
110 | Some x, Some y -> Some (x, y)
111 | _ -> None
112
113 type key_obj = key * key
114
115 let inKeys : key_obj -> obj =
116 declare_object @@ superglobal_object "KEYS"
117 ~cache:cache_keys
118 ~subst:(Some subst_keys)
119 ~discharge:discharge_keys
120
121 let declare_equiv_keys ref ref' =
122 Lib.add_leaf (inKeys (ref,ref'))
123
124 let constr_key env kind c =
125 try
126 let rec aux k =
127 match kind k with
128 | Const (c, _) -> mkKGlob env (GlobRef.ConstRef c)
129 | Ind (i, u) -> mkKGlob env (GlobRef.IndRef i)
130 | Construct (c,u) -> mkKGlob env (GlobRef.ConstructRef c)
131 | Var id -> mkKGlob env (GlobRef.VarRef id)
132 | App (f, _) -> aux f
133 | Proj (p, _, _) -> mkKGlob env (GlobRef.ConstRef (Projection.constant p))
134 | Cast (p, _, _) -> aux p
135 | Lambda _ -> KLam
136 | Prod _ -> KProd
137 | Case _ -> KCase
138 | Fix _ -> KFix
139 | CoFix _ -> KCoFix
140 | Rel _ -> KRel
141 | Meta _ -> raise Not_found
142 | Evar _ -> raise Not_found
143 | Sort _ -> KSort
144 | LetIn _ -> KLet
145 | Int _ -> KInt
146 | Float _ -> KFloat
147 | String _ -> KString
148 | Array _ -> KArray
149 in Some (aux c)
150 with Not_found -> None
151
152 open Pp
153
154 let pr_key pr_global = function
155 | KGlob gr -> pr_global gr
156 | KLam -> str"Lambda"
157 | KLet -> str"Let"
158 | KProd -> str"Product"
159 | KSort -> str"Sort"
160 | KCase -> str"Case"
161 | KFix -> str"Fix"
162 | KCoFix -> str"CoFix"
163 | KRel -> str"Rel"
164 | KInt -> str"Int"
165 | KFloat -> str"Float"
166 | KString -> str"String"
167 | KArray -> str"Array"
168
169 let pr_keyset pr_global v =
170 prlist_with_sep spc (pr_key pr_global) (Keyset.elements v)
171
172 let pr_mapping pr_global k v =
173 pr_key pr_global k ++ str" <-> " ++ pr_keyset pr_global v
174
175 let pr_keys pr_global =
176 Keymap.fold (fun k v acc -> pr_mapping pr_global k v ++ fnl () ++ acc) !keys (mt())