Debian Codegraph @

engine/univSubst.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 Sorts
12 open Util
13 open Constr
14 open Univ
15 open UVars
16
17 type 'a universe_map = 'a Level.Map.t
18 type universe_subst = Universe.t universe_map
19 type universe_subst_fn = Level.t -> Universe.t option
20 type universe_level_subst_fn = Level.t -> Level.t
21
22 type quality_subst = Quality.t QVar.Map.t
23 type quality_subst_fn = QVar.t -> Quality.t
24
25 let subst_univs_universe fn ul =
26 let addn n u = iterate Universe.super n u in
27 let subst, nosubst =
28 List.fold_right (fun (u, n) (subst,nosubst) ->
29 match fn u with
30 | Some u' ->
31 let a' = addn n u' in
32 (a' :: subst, nosubst)
33 | None -> (subst, (u, n) :: nosubst))
34 (Universe.repr ul) ([], [])
35 in
36 match subst with
37 | [] -> ul
38 | u :: ul ->
39 let substs = List.fold_left Universe.sup u subst in
40 List.fold_left (fun acc (u, n) -> Universe.sup acc (addn n (Universe.make u))) substs nosubst
41
42 let enforce_eq u v c =
43 if Universe.equal u v then c else match Universe.level u, Universe.level v with
44 | Some u, Some v -> enforce_eq_level u v c
45 | _ -> CErrors.anomaly (Pp.str "A universe comparison can only happen between variables.")
46
47 let constraint_add_leq v u c =
48 let eq (x, n) (y, m) = Int.equal m n && Level.equal x y in
49 (* We just discard trivial constraints like u<=u *)
50 if eq v u then c
51 else
52 match v, u with
53 | (x,n), (y,m) ->
54 let j = m - n in
55 if j = -1 (* n = m+1, v+1 <= u <-> v < u *) then
56 UnivConstraints.add (x,Lt,y) c
57 else if j <= -1 (* n = m+k, v+k <= u and k>0 *) then
58 if Level.equal x y then (* u+k <= u with k>0 *)
59 UnivConstraints.add (x,Lt,x) c
60 else CErrors.user_err (Pp.str"Unable to handle arbitrary u+k <= v constraints.")
61 else if j = 0 then
62 UnivConstraints.add (x,Le,y) c
63 else (* j >= 1 *) (* m = n + k, u <= v+k *)
64 if Level.equal x y then c (* u <= u+k, trivial *)
65 else if Level.is_set x then c (* Prop,Set <= u+S k, trivial *)
66 else UnivConstraints.add (x,Le,y) c (* u <= v implies u <= v+k *)
67
68 let check_univ_leq_one u v =
69 let leq (u,n) (v,n') =
70 let cmp = Level.compare u v in
71 if Int.equal cmp 0 then n <= n'
72 else false
73 in
74 Universe.exists (leq u) v
75
76 let check_univ_leq u v =
77 Universe.for_all (fun u -> check_univ_leq_one u v) u
78
79 let enforce_leq u v c =
80 List.fold_left (fun c v -> (List.fold_left (fun c u -> constraint_add_leq u v c) c u)) c v
81
82 let enforce_leq u v c =
83 if check_univ_leq u v then c
84 else enforce_leq (Universe.repr u) (Universe.repr v) c
85
86 let enforce_univ_constraint (u,d,v) =
87 match d with
88 | UnivConstraint.Eq -> enforce_eq u v
89 | UnivConstraint.Le -> enforce_leq u v
90 | UnivConstraint.Lt -> enforce_leq (Universe.super u) v
91
92 let subst_univs_constraint fn (u,d,v as c) cstrs =
93 let u' = fn u in
94 let v' = fn v in
95 match u', v' with
96 | None, None -> UnivConstraints.add c cstrs
97 | Some u, None -> enforce_univ_constraint (u,d,Universe.make v) cstrs
98 | None, Some v -> enforce_univ_constraint (Universe.make u,d,v) cstrs
99 | Some u, Some v -> enforce_univ_constraint (u,d,v) cstrs
100
101 let subst_univs_constraints subst csts =
102 UnivConstraints.fold
103 (fun c cstrs -> subst_univs_constraint subst c cstrs)
104 csts UnivConstraints.empty
105
106 let level_subst_of f =
107 fun l ->
108 match f l with
109 | None -> l
110 | Some u ->
111 match Universe.level u with
112 | None -> assert false
113 | Some l -> l
114
115 let subst_univs_fn_puniverses f (c, u as cu) =
116 let u' = Instance.subst_fn f u in
117 if u' == u then cu else (c, u')
118
119 let map_universes_opt_subst_with_binders next aux frel fqual funiv k c =
120 let flevel = fqual, level_subst_of funiv in
121 let aux_rec ((nas, tys, bds) as rc) =
122 let nas' = Array.Smart.map (Context.map_annot_relevance frel) nas in
123 let tys' = Array.Fun1.Smart.map aux k tys in
124 let k' = iterate next (Array.length tys') k in
125 let bds' = Array.Fun1.Smart.map aux k' bds in
126 if nas' == nas && tys' == tys && bds' == bds then rc
127 else (nas', tys', bds')
128 in
129 let aux_ctx ((nas, c) as p) =
130 let nas' = Array.Smart.map (Context.map_annot_relevance frel) nas in
131 let k' = iterate next (Array.length nas) k in
132 let c' = aux k' c in
133 if nas' == nas && c' == c then p
134 else (nas', c')
135 in
136 match kind c with
137 | Const pu ->
138 let pu' = subst_univs_fn_puniverses flevel pu in
139 if pu' == pu then c else mkConstU pu'
140 | Ind pu ->
141 let pu' = subst_univs_fn_puniverses flevel pu in
142 if pu' == pu then c else mkIndU pu'
143 | Construct pu ->
144 let pu' = subst_univs_fn_puniverses flevel pu in
145 if pu' == pu then c else mkConstructU pu'
146 | Sort s ->
147 let s' = Sorts.subst_fn (fqual, subst_univs_universe funiv) s in
148 if s' == s then c else mkSort s'
149 | Case (ci,u,pms,(p,rel),iv,t,br) ->
150 let u' = Instance.subst_fn flevel u in
151 let rel' = frel rel in
152 let pms' = Array.Fun1.Smart.map aux k pms in
153 let p' = aux_ctx p in
154 let iv' = map_invert (aux k) iv in
155 let t' = aux k t in
156 let br' = Array.Smart.map aux_ctx br in
157 if rel' == rel && u' == u && pms' == pms && p' == p && iv' == iv && t' == t && br' == br then c
158 else mkCase (ci, u', pms', (p',rel'), iv', t', br')
159 | Array (u,elems,def,ty) ->
160 let u' = Instance.subst_fn flevel u in
161 let elems' = CArray.Fun1.Smart.map aux k elems in
162 let def' = aux k def in
163 let ty' = aux k ty in
164 if u == u' && elems == elems' && def == def' && ty == ty' then c
165 else mkArray (u',elems',def',ty')
166 | Prod (na, t, u) ->
167 let na' = Context.map_annot_relevance frel na in
168 let t' = aux k t in
169 let u' = aux (next k) u in
170 if na' == na && t' == t && u' == u then c
171 else mkProd (na', t', u')
172 | Lambda (na, t, u) ->
173 let na' = Context.map_annot_relevance frel na in
174 let t' = aux k t in
175 let u' = aux (next k) u in
176 if na' == na && t' == t && u' == u then c
177 else mkLambda (na', t', u')
178 | LetIn (na, b, t, u) ->
179 let na' = Context.map_annot_relevance frel na in
180 let b' = aux k b in
181 let t' = aux k t in
182 let u' = aux (next k) u in
183 if na' == na && b' == b && t' == t && u' == u then c
184 else mkLetIn (na', b', t', u')
185 | Fix (i, rc) ->
186 let rc' = aux_rec rc in
187 if rc' == rc then c
188 else mkFix (i, rc')
189 | CoFix (i, rc) ->
190 let rc' = aux_rec rc in
191 if rc' == rc then c
192 else mkCoFix (i, rc')
193 | Proj (p, r, v) ->
194 let r' = frel r in
195 let v' = aux k v in
196 if r' == r && v' == v then c
197 else mkProj (p, r', v')
198 | _ -> Constr.map_with_binders next aux k c
199
200 let nf_evars_and_universes_opt_subst fevar frel fqual funiv c =
201 let rec self () c = match Constr.kind c with
202 | Evar (evk, args) ->
203 let args' = SList.Smart.map (self ()) args in
204 begin match try fevar (evk, args') with Not_found -> None with
205 | None -> if args == args' then c else mkEvar (evk, args')
206 | Some c -> self () c
207 end
208 | _ -> map_universes_opt_subst_with_binders ignore self frel fqual funiv () c
209 in
210 self () c
211
212 let pr_universe_subst prl =
213 let open Pp in
214 Level.Map.pr prl (fun u -> str" := " ++ Universe.pr prl u ++ spc ())