Debian Codegraph @

pretyping/typeclasses.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 Constr
14 open Evd
15 open Util
16 open Typeclasses_errors
17
18 (*i*)
19
20 (* Core typeclasses hints *)
21 type 'a hint_info_gen =
22 { hint_priority : int option;
23 hint_pattern : 'a option }
24
25 type hint_info = (Id.Set.t * Pattern.constr_pattern) hint_info_gen
26
27 let { Goptions.get = get_typeclasses_unique_solutions } =
28 Goptions.declare_bool_option_and_ref
29 ~key:["Typeclasses";"Unique";"Solutions"]
30 ~value:false
31 ()
32
33 type class_method = {
34 meth_name : Name.t;
35 meth_const : Constant.t option;
36 }
37
38 (* This module defines type-classes *)
39 type typeclass = {
40 (* Universe quantification *)
41 cl_univs : UVars.AbstractContext.t;
42
43 (* The class implementation *)
44 cl_impl : GlobRef.t;
45
46 cl_context : Constr.rel_context;
47
48 cl_trivial : bool;
49
50 cl_props : Constr.rel_context;
51
52 cl_projs : class_method list;
53
54 cl_strict : bool;
55
56 cl_unique : bool;
57 }
58
59 module GlobRefMap = Environ.QGlobRef.Map
60
61 type typeclasses = typeclass GlobRefMap.t
62 (* Invariant: for any pair (gr, tc) in the map, gr and tc.cl_impl are equal *)
63
64 type instance = {
65 is_class: GlobRef.t;
66 is_info: hint_info;
67 is_impl: GlobRef.t;
68 }
69
70 type instances = (instance GlobRefMap.t) GlobRefMap.t
71
72 let instance_impl is = is.is_impl
73
74 let hint_priority is = is.is_info.hint_priority
75
76 (*
77 * states management
78 *)
79
80 let classes : typeclasses ref = Summary.ref GlobRefMap.empty ~name:"classes"
81 let instances : instances ref = Summary.ref GlobRefMap.empty ~name:"instances"
82
83 let class_info env c = GlobRefMap.find_opt env c !classes
84
85 let class_info_exn env sigma r =
86 match class_info env r with
87 | Some v -> v
88 | None ->
89 let sigma, c = Evd.fresh_global env sigma r in
90 not_a_class env sigma c
91
92 let global_class_of_constr env sigma c =
93 try let gr, u = EConstr.destRef sigma c in
94 GlobRefMap.find env gr !classes, u
95 with DestKO | Not_found -> not_a_class env sigma c
96
97 let decompose_class_app env sigma c =
98 let hd, args = EConstr.decompose_app_list sigma c in
99 match EConstr.kind sigma hd with
100 | Proj (p, _, c) ->
101 let expp = Retyping.expand_projection env sigma p c args in
102 EConstr.decompose_app_list sigma expp
103 | _ -> hd, args
104
105 let dest_class_app env sigma c =
106 let cl, args = decompose_class_app env sigma c in
107 global_class_of_constr env sigma cl, (List.map EConstr.Unsafe.to_constr args)
108
109 let dest_class_arity env sigma c =
110 let open EConstr in
111 let rels, c = decompose_prod_decls sigma c in
112 rels, dest_class_app (push_rel_context rels env) sigma c
113
114 let class_of_constr env sigma c =
115 try Some (dest_class_arity env sigma c)
116 with e when CErrors.noncritical e -> None
117
118 let is_class_constr env sigma c =
119 try let gr, u = EConstr.destRef sigma c in
120 GlobRefMap.mem env gr !classes
121 with DestKO | Not_found -> false
122
123 let rec is_class_type env evd c =
124 let c, _ = EConstr.decompose_app evd c in
125 match EConstr.kind evd c with
126 | Prod (_, _, t) -> is_class_type env evd t
127 | Cast (t, _, _) -> is_class_type env evd t
128 | Proj (p, _, c) -> GlobRefMap.mem env (GlobRef.ConstRef (Projection.constant p)) !classes
129 | _ -> is_class_constr env evd c
130
131 let is_class_evar env evd evi =
132 is_class_type env evd (Evd.evar_concl evi)
133
134 let rec is_maybe_class_type env evd c =
135 let c, _ = EConstr.decompose_app evd c in
136 match EConstr.kind evd c with
137 | Prod (_, _, t) -> is_maybe_class_type env evd t
138 | Cast (t, _, _) -> is_maybe_class_type env evd t
139 | Evar _ -> true
140 | Proj (p, _, c) -> GlobRefMap.mem env (GlobRef.ConstRef (Projection.constant p)) !classes
141 | _ -> is_class_constr env evd c
142
143 let load_class env cl =
144 classes := GlobRefMap.add env cl.cl_impl cl !classes
145
146 (** Build the subinstances hints. *)
147
148 (*
149 * interface functions
150 *)
151
152 let load_instance env inst =
153 let insts =
154 try GlobRefMap.find env inst.is_class !instances
155 with Not_found -> GlobRefMap.empty in
156 let insts = GlobRefMap.add env inst.is_impl inst insts in
157 instances := GlobRefMap.add env inst.is_class insts !instances
158
159 let remove_instance env inst =
160 let insts =
161 try GlobRefMap.find env inst.is_class !instances
162 with Not_found -> assert false in
163 let insts = GlobRefMap.remove env inst.is_impl insts in
164 instances := GlobRefMap.add env inst.is_class insts !instances
165
166 let typeclasses () = GlobRefMap.fold (fun _ l c -> l :: c) !classes []
167
168 let cmap_elements c = GlobRefMap.fold (fun k v acc -> v :: acc) c []
169
170 let instances_of env c =
171 try cmap_elements (GlobRefMap.find env c.cl_impl !instances) with Not_found -> []
172
173 let all_instances () =
174 GlobRefMap.fold (fun k v acc ->
175 GlobRefMap.fold (fun k v acc -> v :: acc) v acc)
176 !instances []
177
178 let instances env r =
179 Option.map (fun m -> instances_of env m) (class_info env r)
180
181 let instances_exn env sigma r =
182 match instances env r with
183 | Some v -> v
184 | None ->
185 let sigma, c = Evd.fresh_global env sigma r in
186 not_a_class env sigma c
187
188 let is_class env gr =
189 GlobRefMap.mem env gr !classes
190
191 open Evar_kinds
192 type evar_filter = Evar.t -> Evar_kinds.t Lazy.t -> bool
193
194 let make_unresolvables filter evd =
195 let tcs = Evd.get_typeclass_evars evd in
196 Evd.set_typeclass_evars evd (Evar.Set.filter (fun x -> not (filter x)) tcs)
197
198 let all_evars _ _ = true
199 let all_goals _ source =
200 match Lazy.force source with
201 | VarInstance _ | GoalEvar -> true
202 | _ -> false
203
204 let no_goals ev evi = not (all_goals ev evi)
205 let no_goals_or_obligations _ source =
206 match Lazy.force source with
207 | VarInstance _ | GoalEvar | QuestionMark _ -> false
208 | _ -> true
209
210 let has_typeclasses filter evd =
211 let tcs = get_typeclass_evars evd in
212 let check ev = filter ev (lazy (snd (Evd.evar_source (Evd.find_undefined evd ev)))) in
213 Evar.Set.exists check tcs
214
215 let get_filtered_typeclass_evars filter evd =
216 let tcs = get_typeclass_evars evd in
217 let check ev = filter ev (lazy (snd (Evd.evar_source (Evd.find_undefined evd ev)))) in
218 Evar.Set.filter check tcs
219
220 let solve_all_instances_hook = ref (fun env evd filter unique fail -> assert false)
221
222 let solve_all_instances env evd filter unique fail =
223 !solve_all_instances_hook env evd filter unique fail
224
225 let set_solve_all_instances f = solve_all_instances_hook := f
226
227 let resolve_typeclasses ?(filter=no_goals) ?(unique=get_typeclasses_unique_solutions ())
228 ?(fail=true) env evd =
229 if not (has_typeclasses filter evd) then evd
230 else solve_all_instances env evd filter unique fail
231
232 (** In case of unsatisfiable constraints, build a nice error message *)
233
234 let error_unresolvable env evd comp =
235 let exception MultipleFound in
236 let fold ev accu =
237 match Evd.find_undefined evd ev with
238 | exception Not_found -> None
239 | evi ->
240 let ev_class = class_of_constr env evd (Evd.evar_concl evi) in
241 if Option.is_empty ev_class then accu
242 else (* focus on one instance if only one was searched for *)
243 if Option.has_some accu then raise MultipleFound
244 else (Some ev)
245 in
246 let ev = try Evar.Set.fold fold comp None with MultipleFound -> None in
247 Pretype_errors.unsatisfiable_constraints env evd ev comp
248
249 (** Deprecated *)
250
251 let solve_one_instance = ref (fun env evm t -> assert false)
252
253 let resolve_one_typeclass ?unique:_ env evm t =
254 !solve_one_instance env evm t
255
256 let set_solve_one_instance f = solve_one_instance := f