Debian Codegraph @

pretyping/find_subterm.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 Pp
12 open CErrors
13 open Names
14 open Locus
15 open EConstr
16 open Termops
17 open Pretype_errors
18
19 module NamedDecl = Context.Named.Declaration
20
21 (** Processing occurrences *)
22
23 let explain_incorrect_in_value_occurrence id =
24 Id.print id ++ str " has no value."
25
26 let proceed_with_occurrences f occs x =
27 match occs with
28 | NoOccurrences -> x
29 | occs ->
30 let (occs,x) = f (Locusops.initialize_occurrence_counter occs) x in
31 Locusops.check_used_occurrences occs;
32 x
33
34 (** Applying a function over a named_declaration with an hypothesis
35 location request *)
36
37 let map_named_declaration_with_hyploc f hyploc acc decl =
38 let open Context.Named.Declaration in
39 let f acc typ =
40 let acc, typ = f (Some (NamedDecl.get_id decl, hyploc)) acc typ in
41 acc, typ
42 in
43 match decl,hyploc with
44 | LocalAssum (id,_), InHypValueOnly ->
45 user_err (explain_incorrect_in_value_occurrence id.Context.binder_name)
46 | LocalAssum (id,typ), _ ->
47 let acc,typ = f acc typ in acc, LocalAssum (id,typ)
48 | LocalDef (id,body,typ), InHypTypeOnly ->
49 let acc,typ = f acc typ in acc, LocalDef (id,body,typ)
50 | LocalDef (id,body,typ), InHypValueOnly ->
51 let acc,body = f acc body in acc, LocalDef (id,body,typ)
52 | LocalDef (id,body,typ), InHyp ->
53 let acc,body = f acc body in
54 let acc,typ = f acc typ in
55 acc, LocalDef (id,body,typ)
56
57 (** Finding a subterm up to some testing function *)
58
59 exception SubtermUnificationError of subterm_unification_error
60
61 type ('a, 'b) testing_function = {
62 match_fun : int -> 'a -> EConstr.constr -> ('b, unit) Result.t;
63 merge_fun : 'b -> 'a -> ('a, unit) Result.t;
64 mutable testing_state : 'a;
65 mutable last_found : position_reporting option
66 }
67
68 (* Find subterms using a testing function, but only at a list of
69 locations or excluding a list of locations; in the occurrences list
70 (b,l), b=true means no occurrence except the ones in l and b=false,
71 means all occurrences except the ones in l *)
72
73 let replace_term_occ_gen_modulo env sigma like_first test bywhat cl count t =
74 let count = ref count in
75 let rec substrec (nested, k) t =
76 if Locusops.occurrences_done !count then t else
77 match test.match_fun k test.testing_state t with
78 | Result.Ok subst ->
79 let selected, count' = Locusops.update_occurrence_counter !count in
80 let () = count := count' in
81 if selected then
82 let pos = Locusops.current_occurrence !count in
83 if nested then
84 (* in case it is nested but not later detected as unconvertible,
85 as when matching "id _" in "id (id 0)" *)
86 let lastpos = Option.get test.last_found in
87 raise (SubtermUnificationError (nested, ((cl, pos), t), lastpos))
88 else match test.merge_fun subst test.testing_state with
89 | Result.Ok state ->
90 let () = test.testing_state <- state in
91 let () = test.last_found <- Some ((cl, pos), t) in
92 (* Check nested matching subterms *)
93 let () =
94 if Locusops.more_specific_occurrences !count then
95 ignore (subst_below (true, k) t)
96 in
97 Vars.lift k (bywhat ())
98 | Result.Error () ->
99 if like_first then subst_below (nested, k) t
100 else
101 let lastpos = Option.get test.last_found in
102 raise (SubtermUnificationError (nested, ((cl, pos), t), lastpos))
103 else
104 subst_below (nested, k) t
105 | Result.Error () ->
106 subst_below (nested, k) t
107 and subst_below k t =
108 map_constr_with_binders_left_to_right env sigma (fun d (nested, k) -> (nested, k + 1)) substrec k t
109 in
110 let t' = substrec (false, 0) t in
111 (!count, t')
112
113 let replace_term_occ_modulo env evd occs test bywhat t =
114 let occs',like_first =
115 match occs with AtOccs occs -> occs,false | LikeFirst -> AllOccurrences,true in
116 proceed_with_occurrences
117 (replace_term_occ_gen_modulo env evd like_first test bywhat None) occs' t
118
119 let replace_term_occ_decl_modulo env evd occs test bywhat d =
120 let (plocs,hyploc),like_first =
121 match occs with AtOccs occs -> occs,false | LikeFirst -> (AllOccurrences,InHyp),true in
122 proceed_with_occurrences
123 (map_named_declaration_with_hyploc
124 (replace_term_occ_gen_modulo env evd like_first test bywhat)
125 hyploc)
126 plocs d
127
128 (** Finding an exact subterm *)
129
130 let make_eq_univs_test env evd c =
131 { match_fun = (fun k evd c' ->
132 match EConstr.eq_constr_universes_proj env evd c c' with
133 | None -> Result.Error ()
134 | Some cst ->
135 try Result.Ok (Evd.add_constraints evd cst)
136 with Evd.UniversesDiffer | UGraph.UniverseInconsistency _ -> Result.Error ()
137 );
138 merge_fun = (fun evd _ -> Result.Ok evd);
139 testing_state = evd;
140 last_found = None
141 }
142
143 let subst_closed_term_occ env evd occs c t =
144 let test = make_eq_univs_test env evd c in
145 let bywhat () = mkRel 1 in
146 let t' = replace_term_occ_modulo env evd occs test bywhat t in
147 t', test.testing_state
148
149 let subst_closed_term_occ_decl env evd occs c d =
150 let test = make_eq_univs_test env evd c in
151 let (plocs,hyploc),like_first =
152 match occs with AtOccs occs -> occs,false | LikeFirst -> (AllOccurrences,InHyp),true in
153 let bywhat () = mkRel 1 in
154 proceed_with_occurrences
155 (map_named_declaration_with_hyploc
156 (fun _ -> replace_term_occ_gen_modulo env evd like_first test bywhat None)
157 hyploc) plocs d,
158 test.testing_state