Debian Codegraph @

engine/univFlex.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 Univ
12
13 type t = {
14 subst: Universe.t option Level.Map.t;
15 algs : Level.Set.t;
16 }
17
18 let empty = {
19 subst = Level.Map.empty;
20 algs = Level.Set.empty;
21 }
22
23 let is_empty {subst} = Level.Map.is_empty subst
24
25 let domain {subst} = Level.Map.domain subst
26
27 let mem l {subst} = Level.Map.mem l subst
28
29 let fold f {subst} acc =
30 Level.Map.fold (fun l v acc -> f l ~is_defined:(Option.has_some v) acc)
31 subst acc
32
33 let is_algebraic l {algs} = Level.Set.mem l algs
34
35 let make_nonalgebraic_variable {subst;algs} l =
36 { subst; algs = Level.Set.remove l algs }
37
38 let make_all_undefined_nonalgebraic {subst;algs=_} =
39 { subst; algs = Level.Set.empty }
40
41 let fix_undefined_variables us =
42 Level.Map.fold (fun u v ({subst; algs} as acc) ->
43 match v with
44 | None -> { subst = Level.Map.remove u subst; algs = Level.Set.remove u algs }
45 | Some _ -> acc)
46 us.subst us
47
48 let add l ~algebraic {subst; algs} =
49 let subst = Level.Map.update l
50 (function
51 | None -> Some None
52 | Some _ -> assert false)
53 subst
54 in
55 let algs = if algebraic then Level.Set.add l algs else algs in
56 { subst; algs }
57
58 let remove l { subst; algs } =
59 let subst = match Level.Map.find_opt l subst with
60 | None -> subst
61 | Some None -> Level.Map.remove l subst
62 | Some (Some _) ->
63 (* removing [l := v] is unsound as it loses a constraint *)
64 assert false
65 in
66 { subst = subst; algs = Level.Set.remove l algs }
67
68 let add_levels levels ~algebraic subst =
69 Level.Set.fold (fun l subst -> add l ~algebraic subst) levels subst
70
71 let define l v {subst;algs} =
72 (* XXX update algs? *)
73 let subst =
74 try Level.Map.modify l (fun _ old -> assert (Option.is_empty old); Some v) subst
75 with Not_found -> assert false
76 in
77 { subst; algs }
78
79 let constrain_variables diff us ctx =
80 (* XXX update algs? *)
81 Level.Set.fold (fun l ((univs,cstrs),{subst;algs} as acc) ->
82 match Level.Map.find_opt l subst with
83 | None | Some None -> acc
84 | Some (Some u) -> match Universe.level u with
85 | None -> acc
86 | Some u ->
87 ((Level.Set.add l univs, Univ.UnivConstraints.add (l, UnivConstraint.Eq, u) cstrs),
88 {subst = Level.Map.remove l subst; algs}))
89 diff
90 (ctx,us)
91
92 let biased_union {subst=lsubst;algs=lalgs} {subst=rsubst; algs=ralgs} =
93 let subst =
94 Level.Map.union (fun _k l r ->
95 match l, r with
96 | Some _, _ -> Some l
97 | None, None -> Some l
98 | _, _ -> Some r) lsubst rsubst
99 in
100 { subst; algs = Level.Set.union lalgs ralgs }
101
102 let normalize_univ_variable ~find =
103 let rec aux cur =
104 find cur |>
105 Option.map (fun b ->
106 let b' = UnivSubst.subst_univs_universe aux b in
107 if Universe.equal b' b then b
108 else b')
109 in aux
110
111 let normalize_univ_variable ectx =
112 let find l = Option.flatten (Univ.Level.Map.find_opt l ectx.subst) in
113 normalize_univ_variable ~find
114
115 let normalize_universe subst =
116 let normlevel = normalize_univ_variable subst in
117 UnivSubst.subst_univs_universe normlevel
118
119 let normalize ctx =
120 let normalize = normalize_universe ctx in
121 let subst =
122 Univ.Level.Map.mapi (fun u -> function
123 | None -> None
124 | Some v -> Some (normalize v)) ctx.subst
125 in
126 {subst; algs = ctx.algs}
127
128 let normalize_univ_variables ctx =
129 let ctx = normalize ctx in
130 let def, subst =
131 Univ.Level.Map.fold (fun u v (def, subst) ->
132 match v with
133 | None -> (def, subst)
134 | Some b -> (Univ.Level.Set.add u def, Univ.Level.Map.add u b subst))
135 ctx.subst (Univ.Level.Set.empty, Univ.Level.Map.empty)
136 in
137 let subst l = Level.Map.find_opt l subst in
138 ctx, def, subst
139
140 let pr prl {subst; algs} =
141 let open Pp in
142 let ppsubst = Level.Map.pr prl (function
143 | None -> mt()
144 | Some x -> str " := " ++ Universe.pr prl x)
145 subst
146 in
147 str"ALGEBRAIC UNIVERSES:"++brk(0,1)++
148 h (Level.Set.pr prl algs) ++ fnl() ++
149 str"FLEXIBLE UNIVERSES:"++brk(0,1)++
150 h ppsubst