Skip to content

Commit 47edce8

Browse files
authored
Mode system exposes bounds (#2449)
1 parent bd8e0e6 commit 47edce8

File tree

9 files changed

+140
-103
lines changed

9 files changed

+140
-103
lines changed

ocaml/testsuite/tests/formatting/test_locations.dlocations.ocamlc.reference

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,14 @@ let rec fib = function | 0 | 1 -> 1 | n -> (fib (n - 1)) + (fib (n - 2))
8888
<def>
8989
pattern (test_locations.ml[17,534+8]..test_locations.ml[17,534+11])
9090
Tpat_var "fib"
91-
value_mode meet_local,once(0[global,many,global,many]),join_shared(1[shared,shared])
91+
value_mode global,many,shared
9292
expression (test_locations.ml[17,534+14]..test_locations.ml[19,572+34])
9393
Texp_function
9494
region true
95-
alloc_mode map_comonadic(regional_to_global)(6[global,many,global,many]),id(7[shared,shared])
95+
alloc_mode global,many,shared
9696
[]
9797
Tfunction_cases (test_locations.ml[17,534+14]..test_locations.ml[19,572+34])
98-
alloc_mode id(2[global,many,global,many]),id(3[shared,shared])
98+
alloc_mode global,many,shared
9999
value
100100
[
101101
<case>
@@ -110,11 +110,11 @@ let rec fib = function | 0 | 1 -> 1 | n -> (fib (n - 1)) + (fib (n - 2))
110110
<case>
111111
pattern (test_locations.ml[19,572+4]..test_locations.ml[19,572+5])
112112
Tpat_var "n"
113-
value_mode meet_global,many ∘ map_comonadic(local_to_regional)(2[global,many,global,many]),join_unique(3[shared,shared])
113+
value_mode global,many,unique
114114
expression (test_locations.ml[19,572+9]..test_locations.ml[19,572+34])
115115
Texp_apply
116116
apply_mode Tail
117-
locality_mode proj_areality(10[global,many,global,many])
117+
locality_mode global
118118
expression (test_locations.ml[19,572+21]..test_locations.ml[19,572+22])
119119
Texp_ident "Stdlib!.+"
120120
[
@@ -123,7 +123,7 @@ let rec fib = function | 0 | 1 -> 1 | n -> (fib (n - 1)) + (fib (n - 2))
123123
expression (test_locations.ml[19,572+9]..test_locations.ml[19,572+20])
124124
Texp_apply
125125
apply_mode Default
126-
locality_mode proj_areality(4[global,many,global,many])
126+
locality_mode global
127127
expression (test_locations.ml[19,572+9]..test_locations.ml[19,572+12])
128128
Texp_ident "fib"
129129
[
@@ -132,7 +132,7 @@ let rec fib = function | 0 | 1 -> 1 | n -> (fib (n - 1)) + (fib (n - 2))
132132
expression (test_locations.ml[19,572+13]..test_locations.ml[19,572+20])
133133
Texp_apply
134134
apply_mode Default
135-
locality_mode proj_areality(20[global,many,global,many])
135+
locality_mode global
136136
expression (test_locations.ml[19,572+16]..test_locations.ml[19,572+17])
137137
Texp_ident "Stdlib!.-"
138138
[
@@ -151,7 +151,7 @@ let rec fib = function | 0 | 1 -> 1 | n -> (fib (n - 1)) + (fib (n - 2))
151151
expression (test_locations.ml[19,572+23]..test_locations.ml[19,572+34])
152152
Texp_apply
153153
apply_mode Default
154-
locality_mode proj_areality(4[global,many,global,many])
154+
locality_mode global
155155
expression (test_locations.ml[19,572+23]..test_locations.ml[19,572+26])
156156
Texp_ident "fib"
157157
[
@@ -160,7 +160,7 @@ let rec fib = function | 0 | 1 -> 1 | n -> (fib (n - 1)) + (fib (n - 2))
160160
expression (test_locations.ml[19,572+27]..test_locations.ml[19,572+34])
161161
Texp_apply
162162
apply_mode Default
163-
locality_mode proj_areality(34[global,many,global,many])
163+
locality_mode global
164164
expression (test_locations.ml[19,572+30]..test_locations.ml[19,572+31])
165165
Texp_ident "Stdlib!.-"
166166
[

ocaml/testsuite/tests/formatting/test_locations.dno-locations.ocamlc.reference

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,14 @@ let rec fib = function | 0 | 1 -> 1 | n -> (fib (n - 1)) + (fib (n - 2))
8888
<def>
8989
pattern
9090
Tpat_var "fib"
91-
value_mode meet_local,once(0[global,many,global,many]),join_shared(1[shared,shared])
91+
value_mode global,many,shared
9292
expression
9393
Texp_function
9494
region true
95-
alloc_mode map_comonadic(regional_to_global)(6[global,many,global,many]),id(7[shared,shared])
95+
alloc_mode global,many,shared
9696
[]
9797
Tfunction_cases
98-
alloc_mode id(2[global,many,global,many]),id(3[shared,shared])
98+
alloc_mode global,many,shared
9999
value
100100
[
101101
<case>
@@ -110,11 +110,11 @@ let rec fib = function | 0 | 1 -> 1 | n -> (fib (n - 1)) + (fib (n - 2))
110110
<case>
111111
pattern
112112
Tpat_var "n"
113-
value_mode meet_global,many ∘ map_comonadic(local_to_regional)(2[global,many,global,many]),join_unique(3[shared,shared])
113+
value_mode global,many,unique
114114
expression
115115
Texp_apply
116116
apply_mode Tail
117-
locality_mode proj_areality(10[global,many,global,many])
117+
locality_mode global
118118
expression
119119
Texp_ident "Stdlib!.+"
120120
[
@@ -123,7 +123,7 @@ let rec fib = function | 0 | 1 -> 1 | n -> (fib (n - 1)) + (fib (n - 2))
123123
expression
124124
Texp_apply
125125
apply_mode Default
126-
locality_mode proj_areality(4[global,many,global,many])
126+
locality_mode global
127127
expression
128128
Texp_ident "fib"
129129
[
@@ -132,7 +132,7 @@ let rec fib = function | 0 | 1 -> 1 | n -> (fib (n - 1)) + (fib (n - 2))
132132
expression
133133
Texp_apply
134134
apply_mode Default
135-
locality_mode proj_areality(20[global,many,global,many])
135+
locality_mode global
136136
expression
137137
Texp_ident "Stdlib!.-"
138138
[
@@ -151,7 +151,7 @@ let rec fib = function | 0 | 1 -> 1 | n -> (fib (n - 1)) + (fib (n - 2))
151151
expression
152152
Texp_apply
153153
apply_mode Default
154-
locality_mode proj_areality(4[global,many,global,many])
154+
locality_mode global
155155
expression
156156
Texp_ident "fib"
157157
[
@@ -160,7 +160,7 @@ let rec fib = function | 0 | 1 -> 1 | n -> (fib (n - 1)) + (fib (n - 2))
160160
expression
161161
Texp_apply
162162
apply_mode Default
163-
locality_mode proj_areality(34[global,many,global,many])
163+
locality_mode global
164164
expression
165165
Texp_ident "Stdlib!.-"
166166
[

ocaml/typing/mode.ml

Lines changed: 27 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1195,15 +1195,21 @@ module Common (Obj : Obj) = struct
11951195

11961196
let print ?verbose () ppf m = Solver.print ?verbose obj ppf m
11971197

1198-
let print_raw ?verbose () ppf m = Solver.print_raw ?verbose obj ppf m
1199-
12001198
let zap_to_ceil m = with_log (Solver.zap_to_ceil obj m)
12011199

12021200
let zap_to_floor m = with_log (Solver.zap_to_floor obj m)
12031201

12041202
let of_const : type l r. const -> (l * r) t = fun a -> Solver.of_const obj a
12051203

1206-
let check_const m = Solver.check_const obj m
1204+
module Guts = struct
1205+
let get_floor m = Solver.get_floor obj m
1206+
1207+
let get_ceil m = Solver.get_ceil obj m
1208+
1209+
let get_conservative_floor m = Solver.get_conservative_floor obj m
1210+
1211+
let get_conservative_ceil m = Solver.get_conservative_ceil obj m
1212+
end
12071213
end
12081214
[@@inline]
12091215

@@ -1227,6 +1233,18 @@ module Locality = struct
12271233
let legacy = of_const Const.legacy
12281234

12291235
let zap_to_legacy = zap_to_floor
1236+
1237+
module Guts = struct
1238+
let check_const m =
1239+
let floor = Guts.get_floor m in
1240+
let ceil = Guts.get_ceil m in
1241+
if Const.le ceil floor then Some ceil else None
1242+
1243+
let check_const_conservative m =
1244+
let floor = Guts.get_conservative_floor m in
1245+
let ceil = Guts.get_conservative_ceil m in
1246+
if Const.le ceil floor then Some ceil else None
1247+
end
12301248
end
12311249

12321250
module Regionality = struct
@@ -1430,12 +1448,6 @@ module Comonadic_with_locality = struct
14301448

14311449
(* override to report the offending axis *)
14321450
let equate a b = try_with_log (equate_from_submode submode_log a b)
1433-
1434-
(** overriding to check per-axis *)
1435-
let check_const m =
1436-
let locality = Locality.check_const (proj Areality m) in
1437-
let linearity = Linearity.check_const (proj Linearity m) in
1438-
locality, linearity
14391451
end
14401452

14411453
module Monadic = struct
@@ -1502,11 +1514,6 @@ module Monadic = struct
15021514

15031515
(* override to report the offending axis *)
15041516
let equate a b = try_with_log (equate_from_submode submode_log a b)
1505-
1506-
(** overriding to check per-axis *)
1507-
let check_const m =
1508-
let uniqueness = Uniqueness.check_const (proj Uniqueness m) in
1509-
uniqueness, ()
15101517
end
15111518

15121519
type ('mo, 'como) monadic_comonadic =
@@ -1550,13 +1557,6 @@ module Value = struct
15501557
let uniqueness, () = monadic in
15511558
{ regionality; linearity; uniqueness }
15521559

1553-
let print_raw ?verbose () ppf { monadic; comonadic } =
1554-
Format.fprintf ppf "%a,%a"
1555-
(Comonadic.print_raw ?verbose ())
1556-
comonadic
1557-
(Monadic.print_raw ?verbose ())
1558-
monadic
1559-
15601560
let print ?verbose () ppf { monadic; comonadic } =
15611561
Format.fprintf ppf "%a,%a"
15621562
(Comonadic.print ?verbose ())
@@ -1585,7 +1585,9 @@ module Value = struct
15851585
let m1 = split m1 in
15861586
Comonadic.le m0.comonadic m1.comonadic && Monadic.le m0.monadic m1.monadic
15871587

1588-
let print ppf m = print_raw () ppf (of_const m)
1588+
let print ppf m =
1589+
let { monadic; comonadic } = split m in
1590+
Format.fprintf ppf "%a,%a" Comonadic.print comonadic Monadic.print monadic
15891591

15901592
let legacy =
15911593
merge { comonadic = Comonadic.legacy; monadic = Monadic.legacy }
@@ -1919,13 +1921,6 @@ module Alloc = struct
19191921
let equate_exn m0 m1 =
19201922
match equate m0 m1 with Ok () -> () | Error _ -> invalid_arg "equate_exn"
19211923

1922-
let print_raw ?verbose () ppf { monadic; comonadic } =
1923-
Format.fprintf ppf "%a,%a"
1924-
(Comonadic.print_raw ?verbose ())
1925-
comonadic
1926-
(Monadic.print_raw ?verbose ())
1927-
monadic
1928-
19291924
let print ?verbose () ppf { monadic; comonadic } =
19301925
Format.fprintf ppf "%a,%a"
19311926
(Comonadic.print ?verbose ())
@@ -2069,7 +2064,9 @@ module Alloc = struct
20692064
let m1 = split m1 in
20702065
Comonadic.le m0.comonadic m1.comonadic && Monadic.le m0.monadic m1.monadic
20712066

2072-
let print ppf m = print_raw () ppf (of_const m)
2067+
let print ppf m =
2068+
let { monadic; comonadic } = split m in
2069+
Format.fprintf ppf "%a,%a" Comonadic.print comonadic Monadic.print monadic
20732070

20742071
let legacy =
20752072
merge { comonadic = Comonadic.legacy; monadic = Monadic.legacy }
@@ -2158,11 +2155,6 @@ module Alloc = struct
21582155
let comonadic = Comonadic.zap_to_legacy comonadic in
21592156
merge { monadic; comonadic }
21602157

2161-
let check_const { comonadic; monadic } =
2162-
let comonadic = Comonadic.check_const comonadic in
2163-
let monadic = Monadic.check_const monadic in
2164-
merge { monadic; comonadic }
2165-
21662158
(** This is about partially applying [A -> B -> C] to [A] and getting [B ->
21672159
C]. [comonadic] and [monadic] constutute the mode of [A], and we need to
21682160
give the lower bound mode of [B -> C]. *)

ocaml/typing/mode_intf.mli

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,7 @@ module type Common = sig
8383

8484
val newvar_below : ('l * allowed) t -> ('l_ * 'r) t * bool
8585

86-
val print_raw :
87-
?verbose:bool -> unit -> Format.formatter -> ('l * 'r) t -> unit
88-
89-
val print :
90-
?verbose:bool -> unit -> Format.formatter -> (allowed * allowed) t -> unit
86+
val print : ?verbose:bool -> unit -> Format.formatter -> ('l * 'r) t -> unit
9187

9288
val of_const : Const.t -> ('l * 'r) t
9389
end
@@ -147,7 +143,21 @@ module type S = sig
147143

148144
val zap_to_ceil : ('l * allowed) t -> Const.t
149145

150-
val check_const : (allowed * allowed) t -> Const.t option
146+
module Guts : sig
147+
(** This module exposes some functions that allow callers to inspect modes
148+
directly, which could be useful for error printing and dev tools (such as
149+
merlin). Any usage of this in type checking should be pondered. *)
150+
151+
(** Returns [Some c] if the given mode has been constrained to constant
152+
[c]. see notes on [get_floor] in [solver_intf.mli] for cautions. *)
153+
val check_const : (allowed * allowed) t -> Const.t option
154+
155+
(** Similar to [check_const] but doesn't run the further constraining
156+
needed for precise bounds. As a result, it is inexpensive and returns
157+
a conservative result. I.e., it might return [None] for
158+
fully-constrained modes. *)
159+
val check_const_conservative : (l * 'r) t -> Const.t option
160+
end
151161
end
152162

153163
module Regionality : sig
@@ -389,8 +399,6 @@ module type S = sig
389399
and type error := error
390400
and type 'd t := 'd t
391401

392-
val check_const : (allowed * allowed) t -> Const.Option.t
393-
394402
val proj : ('m, 'a, 'l * 'r) axis -> ('l * 'r) t -> 'm
395403

396404
val max_with : ('m, 'a, 'l * 'r) axis -> 'm -> (disallowed * 'r) t

ocaml/typing/printtyped.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -404,16 +404,16 @@ and expression_extra i ppf x attrs =
404404
attributes i ppf attrs;
405405

406406
and alloc_mode: type l r. _ -> _ -> (l * r) Mode.Alloc.t -> _
407-
= fun i ppf m -> line i ppf "alloc_mode %a\n" (Mode.Alloc.print_raw ()) m
407+
= fun i ppf m -> line i ppf "alloc_mode %a\n" (Mode.Alloc.print ()) m
408408

409409
and alloc_mode_option i ppf m = Option.iter (alloc_mode i ppf) m
410410

411411
and locality_mode i ppf m =
412412
line i ppf "locality_mode %a\n"
413-
(Mode.Locality.print_raw ()) m
413+
(Mode.Locality.print ()) m
414414

415415
and value_mode i ppf m =
416-
line i ppf "value_mode %a\n" (Mode.Value.print_raw ()) m
416+
line i ppf "value_mode %a\n" (Mode.Value.print ()) m
417417

418418
and expression_alloc_mode i ppf (expr, am) =
419419
alloc_mode i ppf am;

0 commit comments

Comments
 (0)