| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
open Univ |
| 12 |
open UnivSubst |
| 13 |
|
| 14 |
|
| 15 |
let { Goptions.get = get_set_minimization } = |
| 16 |
Goptions.declare_bool_option_and_ref |
| 17 |
~key:["Universe";"Minimization";"ToSet"] |
| 18 |
~value:true |
| 19 |
() |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
let add_list_map u t map = |
| 24 |
try |
| 25 |
let l = Level.Map.find u map in |
| 26 |
Level.Map.set u (t :: l) map |
| 27 |
with Not_found -> |
| 28 |
Level.Map.add u [t] map |
| 29 |
|
| 30 |
|
| 31 |
let choose_canonical ctx flexible algebraic s = |
| 32 |
let global = Level.Set.diff s ctx in |
| 33 |
let flexible, rigid = Level.Set.partition flexible (Level.Set.inter s ctx) in |
| 34 |
|
| 35 |
if not (Level.Set.is_empty global) then |
| 36 |
let canon = Level.Set.choose global in |
| 37 |
canon, (Level.Set.remove canon global, rigid, flexible) |
| 38 |
else |
| 39 |
if not (Level.Set.is_empty rigid) then |
| 40 |
let canon = Level.Set.choose rigid in |
| 41 |
canon, (global, Level.Set.remove canon rigid, flexible) |
| 42 |
else |
| 43 |
|
| 44 |
let algs, nonalgs = Level.Set.partition algebraic flexible in |
| 45 |
if not (Level.Set.is_empty nonalgs) then |
| 46 |
let canon = Level.Set.choose nonalgs in |
| 47 |
canon, (global, rigid, Level.Set.remove canon flexible) |
| 48 |
else |
| 49 |
let canon = Level.Set.choose algs in |
| 50 |
canon, (global, rigid, Level.Set.remove canon flexible) |
| 51 |
|
| 52 |
let compare_constraint_type d d' = |
| 53 |
let open UnivConstraint in |
| 54 |
match d, d' with |
| 55 |
| Eq, Eq -> 0 |
| 56 |
| Eq, _ -> -1 |
| 57 |
| _, Eq -> 1 |
| 58 |
| Le, Le -> 0 |
| 59 |
| Le, _ -> -1 |
| 60 |
| _, Le -> 1 |
| 61 |
| Lt, Lt -> 0 |
| 62 |
|
| 63 |
type lowermap = UnivConstraint.kind Level.Map.t |
| 64 |
|
| 65 |
let lower_union = |
| 66 |
let merge k a b = |
| 67 |
match a, b with |
| 68 |
| Some _, None -> a |
| 69 |
| None, Some _ -> b |
| 70 |
| None, None -> None |
| 71 |
| Some l, Some r -> |
| 72 |
if compare_constraint_type l r >= 0 then a |
| 73 |
else b |
| 74 |
in Level.Map.merge merge |
| 75 |
|
| 76 |
let lower_add l c m = |
| 77 |
try let c' = Level.Map.find l m in |
| 78 |
if compare_constraint_type c c' > 0 then |
| 79 |
Level.Map.add l c m |
| 80 |
else m |
| 81 |
with Not_found -> Level.Map.add l c m |
| 82 |
|
| 83 |
let lower_of_list l = |
| 84 |
List.fold_left (fun acc (d,l) -> Level.Map.add l d acc) Level.Map.empty l |
| 85 |
|
| 86 |
type lbound = { enforce : bool; alg : bool; lbound: Universe.t; lower : lowermap } |
| 87 |
|
| 88 |
module LBMap : |
| 89 |
sig |
| 90 |
type t = private { lbmap : lbound Level.Map.t; lbrev : (Level.t * lowermap) Universe.Map.t } |
| 91 |
val empty : t |
| 92 |
val add : Level.t -> lbound -> t -> t |
| 93 |
end = |
| 94 |
struct |
| 95 |
type t = { lbmap : lbound Level.Map.t; lbrev : (Level.t * lowermap) Universe.Map.t } |
| 96 |
|
| 97 |
let empty = { lbmap = Level.Map.empty; lbrev = Universe.Map.empty } |
| 98 |
let add u bnd m = |
| 99 |
let lbmap = Level.Map.add u bnd m.lbmap in |
| 100 |
let lbrev = |
| 101 |
if not bnd.alg && bnd.enforce then |
| 102 |
match Universe.Map.find bnd.lbound m.lbrev with |
| 103 |
| (v, _) -> |
| 104 |
if Level.compare u v <= 0 then |
| 105 |
Universe.Map.add bnd.lbound (u, bnd.lower) m.lbrev |
| 106 |
else m.lbrev |
| 107 |
| exception Not_found -> |
| 108 |
Universe.Map.add bnd.lbound (u, bnd.lower) m.lbrev |
| 109 |
else m.lbrev |
| 110 |
in |
| 111 |
{ lbmap; lbrev } |
| 112 |
end |
| 113 |
|
| 114 |
let find_inst insts v = Universe.Map.find v insts.LBMap.lbrev |
| 115 |
|
| 116 |
let compute_lbound left = |
| 117 |
|
| 118 |
|
| 119 |
let sup l lbound = |
| 120 |
match lbound with |
| 121 |
| None -> Some l |
| 122 |
| Some l' -> Some (Universe.sup l l') |
| 123 |
in |
| 124 |
List.fold_left (fun lbound (d, l) -> |
| 125 |
if d == UnivConstraint.Le then sup l lbound |
| 126 |
else |
| 127 |
(assert (d == Lt); |
| 128 |
if not (Universe.level l == None) then |
| 129 |
sup (Universe.super l) lbound |
| 130 |
else None)) |
| 131 |
None left |
| 132 |
|
| 133 |
let instantiate_with_lbound u lbound lower ~alg ~enforce (ctx, us, insts, cstrs) = |
| 134 |
if enforce then |
| 135 |
let inst = Universe.make u in |
| 136 |
let cstrs' = enforce_leq lbound inst cstrs in |
| 137 |
(ctx, UnivFlex.make_nonalgebraic_variable us u, |
| 138 |
LBMap.add u {enforce;alg;lbound;lower} insts, cstrs'), |
| 139 |
{enforce; alg; lbound=inst; lower} |
| 140 |
else |
| 141 |
(Level.Set.remove u ctx, UnivFlex.define u lbound us, |
| 142 |
LBMap.add u {enforce;alg;lbound;lower} insts, cstrs), |
| 143 |
{enforce; alg; lbound; lower} |
| 144 |
|
| 145 |
type constraints_map = (UnivConstraint.kind * Level.Map.key) list Level.Map.t |
| 146 |
|
| 147 |
let _pr_constraints_map (cmap:constraints_map) = |
| 148 |
let open Pp in |
| 149 |
Level.Map.fold (fun l cstrs acc -> |
| 150 |
Level.raw_pr l ++ str " => " ++ |
| 151 |
prlist_with_sep spc (fun (d,r) -> UnivConstraint.pr_kind d ++ Level.raw_pr r) cstrs ++ |
| 152 |
fnl () ++ acc) |
| 153 |
cmap (mt ()) |
| 154 |
|
| 155 |
let remove_alg l (ctx, us, insts, cstrs) = |
| 156 |
(ctx, UnivFlex.make_nonalgebraic_variable us l, insts, cstrs) |
| 157 |
|
| 158 |
let not_lower lower (d,l) = |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
Universe.exists |
| 165 |
(fun (l,i) -> |
| 166 |
let d = |
| 167 |
if i == 0 then d |
| 168 |
else match d with |
| 169 |
| UnivConstraint.Le -> UnivConstraint.Lt |
| 170 |
| d -> d |
| 171 |
in |
| 172 |
try let d' = Level.Map.find l lower in |
| 173 |
|
| 174 |
|
| 175 |
compare_constraint_type d d' > 0 |
| 176 |
with Not_found -> |
| 177 |
true) l |
| 178 |
|
| 179 |
exception UpperBoundedAlg |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
let enforce_uppers upper lbound cstrs = |
| 187 |
List.fold_left (fun cstrs (d, r) -> |
| 188 |
if d == UnivConstraint.Le then |
| 189 |
enforce_leq lbound (Universe.make r) cstrs |
| 190 |
else |
| 191 |
match Universe.level lbound with |
| 192 |
| Some lev -> UnivConstraints.add (lev, d, r) cstrs |
| 193 |
| None -> raise UpperBoundedAlg) |
| 194 |
cstrs upper |
| 195 |
|
| 196 |
let minimize_univ_variables ctx us left right cstrs = |
| 197 |
let left, lbounds = |
| 198 |
Level.Map.fold (fun r lower (left, lbounds as acc) -> |
| 199 |
if UnivFlex.mem r us || not (Level.Set.mem r ctx) then acc |
| 200 |
else |
| 201 |
let lbounds = |
| 202 |
match compute_lbound (List.map (fun (d,l) -> d, Universe.make l) lower) with |
| 203 |
| None -> lbounds |
| 204 |
| Some lbound -> LBMap.add r {enforce=true; alg=false; lbound; lower=lower_of_list lower} |
| 205 |
lbounds |
| 206 |
in (Level.Map.remove r left, lbounds)) |
| 207 |
left (left, LBMap.empty) |
| 208 |
in |
| 209 |
let rec instance (ctx, us, insts, cstrs as acc) u = |
| 210 |
let acc, left, lower = |
| 211 |
match Level.Map.find u left with |
| 212 |
| exception Not_found -> acc, [], Level.Map.empty |
| 213 |
| l -> |
| 214 |
let acc, left, newlow, lower = |
| 215 |
List.fold_left |
| 216 |
(fun (acc, left, newlow, lower') (d, l) -> |
| 217 |
let acc', {enforce=enf;alg;lbound=l';lower} = aux acc l in |
| 218 |
let l' = |
| 219 |
if enf then Universe.make l |
| 220 |
else l' |
| 221 |
in acc', (d, l') :: left, |
| 222 |
lower_add l d newlow, lower_union lower lower') |
| 223 |
(acc, [], Level.Map.empty, Level.Map.empty) l |
| 224 |
in |
| 225 |
let left = CList.uniquize (List.filter (not_lower lower) left) in |
| 226 |
(acc, left, Level.Map.lunion newlow lower) |
| 227 |
in |
| 228 |
let instantiate_lbound lbound = |
| 229 |
let alg = UnivFlex.is_algebraic u us in |
| 230 |
if Universe.is_type0 lbound && not (get_set_minimization()) then |
| 231 |
|
| 232 |
instantiate_with_lbound u lbound lower ~alg ~enforce:true acc |
| 233 |
else if alg then |
| 234 |
|
| 235 |
|
| 236 |
let lower = Level.Set.fold Level.Map.remove (Universe.levels lbound) lower in |
| 237 |
instantiate_with_lbound u lbound lower ~alg:true ~enforce:false acc |
| 238 |
else |
| 239 |
match Universe.level lbound with |
| 240 |
| Some l -> |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
let lower = Level.Map.remove l lower in |
| 245 |
if not (Level.equal l u) then |
| 246 |
|
| 247 |
|
| 248 |
let acc = remove_alg l acc in |
| 249 |
instantiate_with_lbound u lbound lower ~alg:false ~enforce:false acc |
| 250 |
else acc, {enforce=true; alg=false; lbound; lower} |
| 251 |
| None -> |
| 252 |
begin match find_inst insts lbound with |
| 253 |
| can, lower -> |
| 254 |
|
| 255 |
|
| 256 |
let lower = Level.Map.remove can lower in |
| 257 |
instantiate_with_lbound u (Universe.make can) lower ~alg:false ~enforce:false acc |
| 258 |
| exception Not_found -> |
| 259 |
|
| 260 |
instantiate_with_lbound u lbound lower ~alg:false ~enforce:true acc |
| 261 |
end |
| 262 |
in |
| 263 |
let enforce_uppers ((ctx,us,insts,cstrs), b as acc) = |
| 264 |
match Level.Map.find u right with |
| 265 |
| exception Not_found -> acc |
| 266 |
| upper -> |
| 267 |
let upper = List.filter (fun (d, r) -> not (UnivFlex.mem r us)) upper in |
| 268 |
let cstrs = enforce_uppers upper b.lbound cstrs in |
| 269 |
(ctx, us, insts, cstrs), b |
| 270 |
in |
| 271 |
if not (Level.Set.mem u ctx) |
| 272 |
then enforce_uppers (acc, {enforce=true; alg=false; lbound=Universe.make u; lower}) |
| 273 |
else |
| 274 |
let lbound = compute_lbound left in |
| 275 |
match lbound with |
| 276 |
| None -> |
| 277 |
enforce_uppers (acc, {enforce=true;alg=false;lbound=Universe.make u; lower}) |
| 278 |
| Some lbound -> |
| 279 |
try enforce_uppers (instantiate_lbound lbound) |
| 280 |
with UpperBoundedAlg -> |
| 281 |
enforce_uppers (acc, {enforce=true; alg=false; lbound=Universe.make u; lower}) |
| 282 |
and aux (ctx, us, seen, cstrs as acc) u = |
| 283 |
try acc, Level.Map.find u seen.LBMap.lbmap |
| 284 |
with Not_found -> instance acc u |
| 285 |
in |
| 286 |
UnivFlex.fold (fun u ~is_defined (ctx, us, seen, cstrs as acc) -> |
| 287 |
if not is_defined then fst (aux acc u) |
| 288 |
else Level.Set.remove u ctx, UnivFlex.make_nonalgebraic_variable us u, seen, cstrs) |
| 289 |
us (ctx, us, lbounds, cstrs) |
| 290 |
|
| 291 |
module UPairs = OrderedType.UnorderedPair(Level) |
| 292 |
module UPairSet = Set.Make (UPairs) |
| 293 |
|
| 294 |
type extra = { |
| 295 |
weak_constraints : UPairSet.t; |
| 296 |
above_prop : Level.Set.t; |
| 297 |
} |
| 298 |
|
| 299 |
let empty_extra = { |
| 300 |
weak_constraints = UPairSet.empty; |
| 301 |
above_prop = Level.Set.empty; |
| 302 |
} |
| 303 |
|
| 304 |
let extra_union a b = { |
| 305 |
weak_constraints = UPairSet.union a.weak_constraints b.weak_constraints; |
| 306 |
above_prop = Level.Set.union a.above_prop b.above_prop; |
| 307 |
} |
| 308 |
|
| 309 |
let normalize_context_set g ctx (us:UnivFlex.t) {weak_constraints=weak;above_prop} = |
| 310 |
let (ctx, csts) = ctx in |
| 311 |
|
| 312 |
let smallles, csts = |
| 313 |
UnivConstraints.partition (fun (l,d,r) -> d == Le && Level.is_set l) csts |
| 314 |
in |
| 315 |
|
| 316 |
|
| 317 |
let smallles, csts, g = UPairSet.fold (fun (u,v) (smallles, csts, g as acc) -> |
| 318 |
let norm = level_subst_of (UnivFlex.normalize_univ_variable us) in |
| 319 |
let u = norm u and v = norm v in |
| 320 |
if (Level.is_set u || Level.is_set v) then begin |
| 321 |
if get_set_minimization() then begin |
| 322 |
if Level.is_set u then (UnivConstraints.add (u,Le,v) smallles,csts,g) |
| 323 |
else (UnivConstraints.add (v,Le,u) smallles,csts,g) |
| 324 |
end else acc |
| 325 |
end else |
| 326 |
let set_to a b = |
| 327 |
(smallles, |
| 328 |
UnivConstraints.add (a,Eq,b) csts, |
| 329 |
UGraph.enforce_constraint (a,Eq,b) g) |
| 330 |
in |
| 331 |
let check_le a b = UGraph.check_constraint g (a,Le,b) in |
| 332 |
if check_le u v || check_le v u |
| 333 |
then acc |
| 334 |
else if UnivFlex.mem u us |
| 335 |
then set_to u v |
| 336 |
else if UnivFlex.mem v us |
| 337 |
then set_to v u |
| 338 |
else acc) |
| 339 |
weak (smallles, csts, g) |
| 340 |
in |
| 341 |
let smallles = if get_set_minimization () then |
| 342 |
UnivConstraints.filter (fun (l,d,r) -> UnivFlex.mem r us) smallles |
| 343 |
else UnivConstraints.empty |
| 344 |
in |
| 345 |
let smallles = if get_set_minimization() then |
| 346 |
let fold u accu = if UnivFlex.mem u us then UnivConstraints.add (Level.set, Le, u) accu else accu in |
| 347 |
Level.Set.fold fold above_prop smallles |
| 348 |
else smallles |
| 349 |
in |
| 350 |
let csts, partition = |
| 351 |
|
| 352 |
|
| 353 |
let g = UGraph.initial_universes_with g in |
| 354 |
|
| 355 |
let g = Level.Set.fold (fun v g -> UGraph.add_universe ~strict:false v g) |
| 356 |
ctx g |
| 357 |
in |
| 358 |
let add_soft u g = |
| 359 |
if not (Level.is_set u || Level.Set.mem u ctx) |
| 360 |
then try UGraph.add_universe ~strict:false u g with UGraph.AlreadyDeclared -> g |
| 361 |
else g |
| 362 |
in |
| 363 |
let g = UnivConstraints.fold |
| 364 |
(fun (l, d, r) g -> add_soft r (add_soft l g)) |
| 365 |
csts g |
| 366 |
in |
| 367 |
let g = UGraph.merge_constraints csts g in |
| 368 |
UGraph.constraints_of_universes g |
| 369 |
in |
| 370 |
|
| 371 |
let noneqs = |
| 372 |
UnivConstraints.filter |
| 373 |
(fun (l,d,r) -> not (d == Le && Level.is_set l)) |
| 374 |
csts |
| 375 |
in |
| 376 |
|
| 377 |
let noneqs = UnivConstraints.union noneqs smallles in |
| 378 |
let flex x = UnivFlex.mem x us in |
| 379 |
let algebraic x = UnivFlex.is_algebraic x us in |
| 380 |
let ctx, us, eqs = List.fold_left (fun (ctx, us, cstrs) s -> |
| 381 |
let canon, (global, rigid, flexible) = choose_canonical ctx flex algebraic s in |
| 382 |
|
| 383 |
let cstrs = Level.Set.fold (fun g cst -> |
| 384 |
UnivConstraints.add (canon, Eq, g) cst) global |
| 385 |
cstrs |
| 386 |
in |
| 387 |
|
| 388 |
let cstrs = Level.Set.fold (fun g cst -> |
| 389 |
UnivConstraints.add (canon, Eq, g) cst) rigid |
| 390 |
cstrs |
| 391 |
in |
| 392 |
let canonu = Universe.make canon in |
| 393 |
let us = Level.Set.fold (fun f -> UnivFlex.define f canonu) flexible us in |
| 394 |
(Level.Set.diff ctx flexible, us, cstrs)) |
| 395 |
(ctx, us, UnivConstraints.empty) partition |
| 396 |
in |
| 397 |
|
| 398 |
|
| 399 |
let noneqs = |
| 400 |
let norm = level_subst_of (UnivFlex.normalize_univ_variable us) in |
| 401 |
let fold (u,d,v) noneqs = |
| 402 |
let u = norm u and v = norm v in |
| 403 |
if d != UnivConstraint.Lt && Level.equal u v then noneqs |
| 404 |
else UnivConstraints.add (u,d,v) noneqs |
| 405 |
in |
| 406 |
UnivConstraints.fold fold noneqs UnivConstraints.empty |
| 407 |
in |
| 408 |
|
| 409 |
|
| 410 |
let noneqs, ucstrsl, ucstrsr = |
| 411 |
UnivConstraints.fold (fun (l,d,r as cstr) (noneq, ucstrsl, ucstrsr) -> |
| 412 |
let lus = UnivFlex.mem l us and rus = UnivFlex.mem r us in |
| 413 |
let ucstrsl' = |
| 414 |
if lus then add_list_map l (d, r) ucstrsl |
| 415 |
else ucstrsl |
| 416 |
and ucstrsr' = |
| 417 |
add_list_map r (d, l) ucstrsr |
| 418 |
in |
| 419 |
let noneqs = |
| 420 |
if lus || rus then noneq |
| 421 |
else UnivConstraints.add cstr noneq |
| 422 |
in (noneqs, ucstrsl', ucstrsr')) |
| 423 |
noneqs (UnivConstraints.empty, Level.Map.empty, Level.Map.empty) |
| 424 |
in |
| 425 |
|
| 426 |
let ctx', us, inst, noneqs = |
| 427 |
minimize_univ_variables ctx us ucstrsr ucstrsl noneqs |
| 428 |
in |
| 429 |
let us = UnivFlex.normalize us in |
| 430 |
us, (ctx', UnivConstraints.union noneqs eqs) |