Debian Codegraph @

tactics/btermdn.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 Util
12 open Constr
13 open Names
14 open Pattern
15
16 let debug = CDebug.create ~name:"dnet-decomp" ()
17
18 (* Discrimination nets with bounded depth.
19 See the module dn.ml for further explanations.
20 Eduardo (5/8/97). *)
21
22 let dnet_depth = ref 8
23
24 type term_label =
25 | GRLabel of GlobRef.t
26 | ProjLabel of Projection.Repr.t * int
27 (** [ProjLabel (p, n)] represents a possibly partially applied projection [p]
28 with [n] arguments missing to be fully applied. [n] is always zero for
29 labels derived from [Proj] terms but can be greater than zero for labels
30 derived from compatibility constants. *)
31 | ProdLabel
32 | SortLabel
33 | CaseLabel
34 | LamLabel
35
36 let compare_term_label t1 t2 = match t1, t2 with
37 | GRLabel gr1, GRLabel gr2 -> GlobRef.UserOrd.compare gr1 gr2
38 | ProjLabel (p1, n1), ProjLabel (p2, n2) ->
39 let c = Int.compare n1 n2 in
40 if c <> 0 then c else
41 (Projection.Repr.UserOrd.compare p1 p2)
42 | _ -> Stdlib.compare t1 t2 (** OK *)
43
44 let pr_term_label (l : term_label) =
45 let open Pp in
46 match l with
47 | GRLabel gr ->
48 str "GRLabel(" ++ GlobRef.print gr ++ str ")"
49 | ProjLabel (proj, i) ->
50 str "ProjLabel(" ++ Projection.Repr.print proj ++ str ", " ++ int i ++ str ")"
51 | ProdLabel -> str "ProdLabel"
52 | SortLabel -> str "SortLabel"
53 | CaseLabel -> str "CaseLabel"
54 | LamLabel -> str "LamLabel"
55
56 type 'res lookup_res = 'res Dn.lookup_res = Label of 'res | Nothing | Everything
57
58 let pr_lookup_res pr_res r =
59 let open Pp in
60 match r with
61 | Label lbl ->
62 str "Label(" ++ hv 2 (pr_res lbl) ++ str ")"
63 | Nothing -> str "Nothing"
64 | Everything -> str "Everything"
65
66
67 (* let eta_reduce = Reductionops.shrink_eta *)
68
69 let evaluable_constant c env ts =
70 (* This is a hack to work around a broken Print Module implementation, see
71 bug #2668. *)
72 (if Environ.mem_constant c env then Environ.evaluable_constant c env else true) &&
73 (match ts with None -> true | Some ts -> Structures.PrimitiveProjections.is_transparent_constant ts c)
74
75 let evaluable_named id env ts =
76 (try Environ.evaluable_named id env with Not_found -> true) &&
77 (match ts with None -> true | Some ts -> TransparentState.is_transparent_variable ts id)
78
79 let evaluable_projection p _env ts =
80 (match ts with None -> true | Some ts -> TransparentState.is_transparent_projection ts p)
81
82 let decomp_constant (c : Names.Constant.t) (args : 'a list) :
83 (Names.Projection.Repr.t * int) option * 'a list =
84 match Structures.PrimitiveProjections.find_opt c with
85 | None -> (None, args)
86 | Some p ->
87 let n_args_needed = Names.Projection.Repr.npars p + 1 in (* +1 for the record value itself *)
88 let n_args_given = List.length args in
89 let n_args_missing = max (n_args_needed - n_args_given) 0 in
90 let n_args_drop = min (n_args_needed - 1) n_args_given in (* we do not drop the record value from the stack *)
91 (Some (p, n_args_missing), List.skipn n_args_drop args)
92
93 let label_of_opaque_constant c stack =
94 match decomp_constant c stack with
95 | (None, stack) -> (GRLabel (ConstRef c), stack)
96 | (Some (p, i), stack) -> (ProjLabel (p,i), stack)
97
98 type constr_res = (term_label * partial_constr list) lookup_res
99 and partial_constr =
100 | Constr of EConstr.t
101 | PartialConstr of constr_res
102
103 let rec pr_constr_res pr_constr (cr : constr_res) =
104 let open Pp in
105 let pr_partial_constr (pc : partial_constr) =
106 match pc with
107 | Constr c ->
108 str "Constr(" ++ hv 2 (pr_constr c) ++ str ")"
109 | PartialConstr pc ->
110 str "PartialConstr([" ++ hv 2 (pr_constr_res pr_constr pc) ++ str "])"
111 in
112 let aux (lbl, pcs) =
113 pr_term_label lbl ++
114 str "," ++
115 prlist_with_sep
116 (fun () -> str ",")
117 pr_partial_constr
118 pcs
119 in
120 pr_lookup_res aux cr
121
122 let decomp_lambda_constr env sigma ts decomp : EConstr.t -> EConstr.t -> constr_res =
123 let res ds p =
124 match ds with
125 | [] -> assert false
126 | ty :: ds ->
127 let acc = Label (LamLabel, [Constr ty; p]) in
128 let fn acc ty = (Label (LamLabel, [Constr ty; PartialConstr acc])) in
129 let res = List.fold_left fn acc ds in
130 res
131 in
132 let rec decomp_app c stack =
133 let k = EConstr.kind sigma c in
134 match k with
135 | App (f, args) -> decomp_app f (Array.to_list args @ stack)
136 | Const (c, _) -> (k, decomp_constant c stack)
137 | Proj (p, b, c) -> (k, (Some (Projection.repr p, 0), c :: stack))
138 | _ -> (k, (None, stack))
139 in
140 let rec go numds ds p : constr_res =
141 match decomp_app p [] with
142 | (Lambda (_, ty, c), (None, [])) ->
143 let ds = ty :: ds in
144 go (numds + 1) ds c
145 | (_, (None, [])) -> (* this is neither a lambda nor an application *)
146 begin
147 match decomp [] p with
148 | _ when ds = [] -> assert false
149 | Label _ ->
150 (* there are left-over lambdas and the body is discriminating *)
151 res ds (Constr p)
152 | Nothing | Everything -> Everything
153 end
154 | (_ as f, (None, args)) -> (* this is an application whose head [f] is not a projection *)
155 let nargs = List.length args in
156 let n = min numds nargs in
157 let ds = List.skipn n ds in
158 let args = List.firstn (nargs - n) args in
159 let p = EConstr.mkApp (EConstr.of_kind f, Array.of_list args) in
160 let p = EConstr.Vars.lift (-n) p in
161 begin
162 match decomp [] p with
163 | _ as c when ds = [] ->
164 c (* no more remaining lambdas *)
165 | Label _ -> res ds (Constr p)
166 | Nothing | Everything -> Everything
167 end
168 | (_, (Some (p, _), _)) when evaluable_projection p env ts ->
169 Everything
170 | (_, (Some (p, args_missing), args)) ->
171 (* We have [num_params + |args| - args_missing] virtual arguments left. *)
172 let params = Projection.Repr.npars p in
173 let nargs = List.length args in
174 let total_nargs = params + nargs - args_missing in
175 let n = min numds total_nargs in
176 let ds = List.skipn n ds in
177 let args = List.firstn (max 0 (nargs - n)) args in
178 let args = List.map (fun c -> Constr c) args in
179 let args_missing = args_missing + (max 0 (n - nargs)) in
180 let p = Label (ProjLabel (p, args_missing), args) in
181 begin
182 match ds with
183 | [] -> p
184 | _ -> res ds (PartialConstr p)
185 end
186 in
187 fun ty -> go 1 [ty]
188
189 (* The pattern view functions below try to overapproximate βι-neutral terms up
190 to η-conversion. Some historical design choices are still incorrect w.r.t. to
191 this specification. TODO: try to make them follow the spec. *)
192 let constr_val_discr env sigma ts t : constr_res =
193 (* Should we perform weak βι here? *)
194 let open GlobRef in
195 let rec decomp (stack : partial_constr list) (t : EConstr.t) : constr_res =
196 debug Pp.(fun () -> str "constr_val_discr.decomp input: " ++ Printer.pr_leconstr_env env sigma t);
197 let out = match EConstr.kind sigma t with
198 | App (f,l) -> decomp (Array.fold_right (fun a l -> Constr a :: l) l stack) f
199 | Proj (p,_,c) when evaluable_projection (Projection.repr p) env ts -> Everything
200 | Proj (p,_,c) ->
201 let p = Environ.QProjection.canonize env p in
202 Label(ProjLabel (Projection.repr p, 0), Constr c :: stack)
203 | Cast (c,_,_) -> decomp stack c
204 | Const (c,_) when evaluable_constant c env ts -> Everything
205 | Const (c,_) ->
206 let c = Environ.QConstant.canonize env c in
207 Label (label_of_opaque_constant c stack)
208 | Ind (ind_sp,_) ->
209 let ind_sp = Environ.QInd.canonize env ind_sp in
210 Label(GRLabel (IndRef ind_sp), stack)
211 | Construct (cstr_sp,_) ->
212 let cstr_sp = Environ.QConstruct.canonize env cstr_sp in
213 Label(GRLabel (ConstructRef cstr_sp), stack)
214 | Var id when evaluable_named id env ts -> Everything
215 | Var id -> Label(GRLabel (VarRef id), stack)
216 | Prod (n,d,c) -> Label(ProdLabel, [Constr d; Constr c])
217 | Lambda (_,d,c) when List.is_empty stack ->
218 decomp_lambda_constr env sigma ts decomp d c
219 | Lambda _ -> Everything
220 | Sort _ -> Label(SortLabel, [])
221 | Evar _ -> Everything
222 | Case (_, _, _, _, _, c, _) ->
223 begin
224 match decomp stack c with
225 | Label (GRLabel (ConstructRef _), _) -> Everything (* over-approximating w.r.t. [fMATCH] *)
226 | (Label _ | Nothing) as res -> Label(CaseLabel, PartialConstr res :: stack)
227 | Everything -> Everything
228 end
229 | Rel _ | Meta _ | LetIn _ | Fix _ | CoFix _
230 | Int _ | Float _ | String _ | Array _ -> Nothing
231 in
232 debug Pp.(fun () -> str "constr_val_discr.decomp output: " ++ pr_constr_res (Printer.pr_leconstr_env env sigma) out);
233 out
234 and decomp_partial (stack : partial_constr list) (t : partial_constr) : constr_res =
235 match t with
236 | Constr t -> decomp stack t
237 | PartialConstr res -> res
238 in
239 decomp_partial [] t
240
241 type pat_res = (term_label * partial_pat list) option
242 and partial_pat =
243 | Pattern of constr_pattern
244 | PartialPat of pat_res
245
246 let rec pr_pat_res pr_pat (cr : pat_res) =
247 let open Pp in
248 let pr_partial_pat (pc : partial_pat) =
249 match pc with
250 | Pattern c ->
251 str "Pattern(" ++ hv 2 (pr_pat c) ++ str ")"
252 | PartialPat pc ->
253 str "PartialPat([" ++ hv 2 (pr_pat_res pr_pat pc) ++ str "])"
254 in
255 let aux (lbl, pcs) =
256 pr_term_label lbl ++
257 str "," ++
258 prlist_with_sep
259 (fun () -> str ",")
260 pr_partial_pat
261 pcs
262 in
263 match cr with
264 | Some x -> str "Some(" ++ hv 2 (aux x) ++ str ")"
265 | None -> str "None"
266
267 let decomp_lambda_pat env ts decomp : constr_pattern -> constr_pattern -> pat_res =
268 let res ds p =
269 match ds with
270 | [] -> assert false
271 | ty :: ds ->
272 let acc = Some (LamLabel, [Pattern ty; p]) in
273 let fn acc ty = Some (LamLabel, [Pattern ty; PartialPat acc]) in
274 let res = List.fold_left fn acc ds in
275 res
276 in
277 let rec decomp_app p stack =
278 match p with
279 | PApp (f, args) -> decomp_app f (Array.to_list args @ stack)
280 | PRef (ConstRef c) -> (p, decomp_constant c stack)
281 | PProj (pr, c) -> (p, (Some (Projection.repr pr, 0), c :: stack))
282 | _ -> (p, (None, stack))
283 in
284 let rec go numds ds p : pat_res =
285 match decomp_app p [] with
286 | (PLambda (_, ty, c), (None, [])) ->
287 let ds = ty :: ds in
288 go (numds + 1) ds c
289 | (_, (None, [])) -> (* this is neither a lambda nor an application *)
290 begin
291 match decomp [] p with
292 | _ when ds = [] -> assert false
293 | Some _ ->
294 (* there are left-over lambdas and the body is discriminating *)
295 res ds (Pattern p)
296 | None -> None
297 end
298 | (_ as f, (None, args)) -> (* this is an application whose head [f] is not a projection *)
299 let nargs = List.length args in
300 let n = min numds nargs in
301 let ds = List.skipn n ds in
302 let args = List.firstn (nargs - n) args in
303 let p = PApp (f, Array.of_list args) in
304 let p = Patternops.lift_pattern (-n) p in
305 begin
306 match decomp [] p with
307 | _ as c when ds = [] ->
308 c (* no more remaining lambdas *)
309 | Some _ ->
310 (* there are left-over lambdas and the body is discriminating *)
311 res ds (Pattern p)
312 | None -> None
313 end
314 | (_, (Some (p, _), _)) when evaluable_projection p env ts ->
315 None
316 | (_, (Some (p, args_missing), args)) ->
317 (* We have [num_params + |args| - args_missing] virtual arguments left. *)
318 let params = Projection.Repr.npars p in
319 let nargs = List.length args in
320 let total_nargs = params + nargs - args_missing in
321 let n = min numds total_nargs in
322 let ds = List.skipn n ds in
323 let args = List.firstn (max 0 (nargs - n)) args in
324 let args = List.map (fun c -> Pattern c) args in
325 let args_missing = args_missing + (max 0 (n - nargs)) in
326 let p = (Some (ProjLabel (p, args_missing), args)) in
327 begin
328 match ds with
329 | [] -> p
330 | _ -> res ds (PartialPat p)
331 end
332 in
333 fun ty -> go 1 [ty]
334
335
336 let constr_pat_discr env ts p : pat_res =
337 let open GlobRef in
338 let rec decomp (stack : partial_pat list) (p : constr_pattern) : pat_res =
339 debug Pp.(fun () -> str "constr_pat_discr.decomp input: " ++ Printer.pr_lconstr_pattern_env env Evd.empty p);
340 let out = match p with
341 | PApp (f,args) -> decomp ((Array.map_to_list (fun p -> Pattern p) args) @ stack) f
342 | PProj (p,c) when evaluable_projection (Projection.repr p) env ts -> None
343 | PProj (p,c) ->
344 let p = Environ.QProjection.canonize env p in
345 Some (ProjLabel (Projection.repr p, 0), Pattern c :: stack)
346 | PRef ((IndRef _) as ref)
347 | PRef ((ConstructRef _ ) as ref) ->
348 let ref = Environ.QGlobRef.canonize env ref in
349 Some (GRLabel ref, stack)
350 | PRef (VarRef v) when evaluable_named v env ts -> None
351 | PRef ((VarRef _) as ref) -> Some (GRLabel ref, stack)
352 | PRef (ConstRef c) when evaluable_constant c env ts -> None
353 | PRef (ConstRef c) ->
354 let c = Environ.QConstant.canonize env c in
355 Some (label_of_opaque_constant c stack)
356 | PVar v when evaluable_named v env ts -> None
357 | PVar v -> Some (GRLabel (VarRef v), stack)
358 | PProd (_,d,c) when stack = [] -> Some (ProdLabel, [Pattern d ; Pattern c])
359 | PLambda (_,d,c) when List.is_empty stack ->
360 decomp_lambda_pat env ts decomp d c
361 | PSort s when stack = [] -> Some (SortLabel, [])
362 | PCase(_,_,p,_) | PIf(p,_,_) ->
363 begin
364 match decomp stack p with
365 | Some (GRLabel (ConstructRef _), _) -> None (* over-approximating w.r.t. [fMATCH] *)
366 | Some _ as res -> Some (CaseLabel, PartialPat res :: stack)
367 | None -> None
368 end
369 | _ -> None
370 in
371 debug Pp.(fun () -> str "constr_pat_discr.decomp output: " ++ pr_pat_res (Printer.pr_lconstr_pattern_env env Evd.empty) out);
372 out
373 and decomp_partial (stack : partial_pat list) (t : partial_pat) : pat_res =
374 match t with
375 | Pattern p ->
376 decomp stack p
377 | PartialPat res -> res
378 in
379 decomp_partial [] p
380
381 let constr_pat_discr_syntactic env p =
382 let open GlobRef in
383 let rec decomp (stack : partial_pat list) (p : constr_pattern) : pat_res =
384 debug Pp.(fun () -> str "constr_pat_discr_syntactic.decomp input: " ++ Printer.pr_lconstr_pattern_env env Evd.empty p);
385 let out = match p with
386 | PApp (f,args) -> decomp ((Array.map_to_list (fun p -> Pattern p) args) @ stack) f
387 | PProj (p,c) ->
388 let p = Environ.QProjection.canonize env p in
389 Some (ProjLabel (Names.Projection.repr p, 0), Pattern c :: stack)
390 | PRef ((IndRef _) as ref)
391 | PRef ((ConstructRef _ ) as ref) ->
392 let ref = Environ.QGlobRef.canonize env ref in
393 Some (GRLabel ref, stack)
394 | PRef ((VarRef _) as ref) -> Some (GRLabel ref, stack)
395 | PRef (ConstRef c) ->
396 let c = Environ.QConstant.canonize env c in
397 Some (label_of_opaque_constant c stack)
398 | PVar v -> Some (GRLabel (VarRef v), stack)
399 | PProd (_,d,c) when stack = [] -> Some (ProdLabel, [Pattern d ; Pattern c])
400 | PLambda (_,d,c) when List.is_empty stack ->
401 decomp_lambda_pat env (Some TransparentState.full) decomp d c
402 | PSort s when stack = [] -> Some (SortLabel, [])
403 | PCase(_,_,p,_) | PIf(p,_,_) -> Some (CaseLabel, Pattern p :: stack)
404 | _ -> None
405 in
406 debug Pp.(fun () -> str "constr_pat_discr_syntactic.decomp output: " ++ pr_pat_res (Printer.pr_lconstr_pattern_env env Evd.empty) out);
407 out
408 and decomp_partial (stack : partial_pat list) (t : partial_pat) : pat_res =
409 match t with
410 | Pattern p -> decomp stack p
411 | PartialPat res -> res
412 in
413 decomp_partial [] p
414
415 let bounded_constr_pat_discr env st (t,depth) =
416 if Int.equal depth 0 then None
417 else match constr_pat_discr env st t with
418 | None -> None
419 | Some (c,l) -> Some(c,List.map (fun c -> (c,depth-1)) l)
420
421 let bounded_constr_pat_discr_syntactic env (t,depth) =
422 if Int.equal depth 0 then None
423 else match constr_pat_discr_syntactic env t with
424 | None -> None
425 | Some (c,l) -> Some(c,List.map (fun c -> (c,depth-1)) l)
426
427 let bounded_constr_val_discr env st sigma (t,depth) =
428 if Int.equal depth 0 then
429 Nothing
430 else match constr_val_discr env sigma st t with
431 | Label (c,l) -> Label(c,List.map (fun c -> (c,depth-1)) l)
432 | Nothing -> Nothing
433 | Everything -> Everything
434
435 module Make =
436 functor (Z : Map.OrderedType) ->
437 struct
438
439 module Y = struct
440 type t = term_label
441 let compare = compare_term_label
442 end
443
444 module Dn = Dn.Make(Y)(Z)
445
446 type t = Dn.t
447
448 type pattern = Dn.pattern
449
450 let pattern env st pat =
451 Dn.pattern (bounded_constr_pat_discr env st) (Pattern pat, !dnet_depth)
452
453 let pattern_syntactic env pat =
454 Dn.pattern (bounded_constr_pat_discr_syntactic env) (Pattern pat, !dnet_depth)
455
456 let constr_pattern env sigma st pat =
457 let mk p = match bounded_constr_val_discr env st sigma p with
458 | Label l -> Some l
459 | Everything | Nothing -> None
460 in
461 Dn.pattern mk (Constr pat, !dnet_depth)
462
463 let empty = Dn.empty
464 let add = Dn.add
465 let rmv = Dn.rmv
466
467 let lookup env sigma st dn t =
468 Dn.lookup dn (bounded_constr_val_discr env st sigma) (Constr t,!dnet_depth)
469
470 end