Skip to content

Commit 152071c

Browse files
committed
- solver returns gap
- refactor polarized_solver (avoid wrapper around const)
1 parent 2b37068 commit 152071c

File tree

8 files changed

+281
-298
lines changed

8 files changed

+281
-298
lines changed

ocaml/typing/ctype.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4364,9 +4364,9 @@ let relevant_pairs pairs v =
43644364
let moregen_alloc_mode v a1 a2 =
43654365
match
43664366
match v with
4367-
| Invariant -> Alloc.equate a1 a2
4368-
| Covariant -> Alloc.submode a1 a2
4369-
| Contravariant -> Alloc.submode a2 a1
4367+
| Invariant -> Result.map_error ignore (Alloc.equate a1 a2)
4368+
| Covariant -> Result.map_error ignore (Alloc.submode a1 a2)
4369+
| Contravariant -> Result.map_error ignore (Alloc.submode a2 a1)
43704370
| Bivariant -> Ok ()
43714371
with
43724372
| Ok () -> ()

ocaml/typing/mode.ml

Lines changed: 72 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -664,27 +664,33 @@ let append_changes = S.S.append_changes
664664
module type Obj = sig
665665
type const
666666

667-
type const_s
667+
type polarity
668668

669-
val obj_s : const_s S.obj
670-
671-
val unpack : const_s -> const
672-
673-
val pack : const -> const_s
669+
val obj_s : (const * polarity) S.obj
674670
end
675671

672+
let equate_from_submode submode m0 m1 =
673+
match submode m0 m1 with
674+
| Error e -> Error (Left_le_right, e)
675+
| Ok () -> (
676+
match submode m1 m0 with
677+
| Error e -> Error (Right_le_left, e)
678+
| Ok () -> Ok ())
679+
676680
module Common (Obj : Obj) = struct
677681
open Obj
678682

679-
type 'd t = (const_s, 'd) S.mode
683+
type 'd t = (const * polarity, 'd) S.mode
680684

681685
type l = (allowed * disallowed) t
682686

683687
type r = (disallowed * allowed) t
684688

685689
type lr = (allowed * allowed) t
686690

687-
type error = unit
691+
type nonrec error = const error
692+
693+
type equate_error = equate_step * error
688694

689695
let disallow_right m = S.disallow_right m
690696

@@ -712,24 +718,19 @@ module Common (Obj : Obj) = struct
712718

713719
let submode_exn m0 m1 = assert (submode m0 m1 |> Result.is_ok)
714720

715-
let equate m0 m1 =
716-
match submode m0 m1 with
717-
| Error () -> Error ()
718-
| Ok () -> (
719-
match submode m1 m0 with Error () -> Error () | Ok () -> Ok ())
721+
let equate = equate_from_submode submode
720722

721723
let equate_exn m0 m1 = assert (equate m0 m1 |> Result.is_ok)
722724

723725
let print ?verbose ?axis () ppf m = S.print obj_s ?verbose ?axis ppf m
724726

725-
let constrain_upper m = Obj.unpack (S.constrain_upper obj_s m)
727+
let constrain_upper m = S.constrain_upper obj_s m
726728

727-
let constrain_lower m = Obj.unpack (S.constrain_lower obj_s m)
729+
let constrain_lower m = S.constrain_lower obj_s m
728730

729-
let of_const : type l r. const -> (l * r) t =
730-
fun a -> S.of_const obj_s (Obj.pack a)
731+
let of_const : type l r. const -> (l * r) t = fun a -> S.of_const obj_s a
731732

732-
let check_const m = Option.map Obj.unpack (S.check_const obj_s m)
733+
let check_const m = S.check_const obj_s m
733734
end
734735

735736
module Locality = struct
@@ -738,15 +739,11 @@ module Locality = struct
738739
module Obj = struct
739740
type const = Const.t
740741

741-
type const_s = Const.t S.pos
742+
type polarity = positive
742743

743744
let obj = C.Locality
744745

745-
let obj_s : const_s S.obj = S.Positive obj
746-
747-
let unpack (S.Pos a) = a
748-
749-
let pack a = S.Pos a
746+
let obj_s : (const * polarity) S.obj = S.Positive obj
750747
end
751748

752749
include Common (Obj)
@@ -766,13 +763,9 @@ module Regionality = struct
766763
module Obj = struct
767764
type const = Const.t
768765

769-
type const_s = Const.t S.pos
770-
771-
let obj_s : const_s S.obj = S.Positive C.Regionality
772-
773-
let unpack (S.Pos a) = a
766+
type polarity = positive
774767

775-
let pack a = S.Pos a
768+
let obj_s : (const * polarity) S.obj = S.Positive C.Regionality
776769
end
777770

778771
include Common (Obj)
@@ -794,15 +787,11 @@ module Linearity = struct
794787
module Obj = struct
795788
type const = Const.t
796789

797-
type const_s = Const.t S.pos
790+
type polarity = positive
798791

799792
let obj = C.Linearity
800793

801-
let obj_s : const_s S.obj = S.Positive obj
802-
803-
let unpack (S.Pos a) = a
804-
805-
let pack a = S.Pos a
794+
let obj_s : (const * polarity) S.obj = S.Positive obj
806795
end
807796

808797
include Common (Obj)
@@ -823,15 +812,11 @@ module Uniqueness = struct
823812
type const = Const.t
824813

825814
(* the negation of Uniqueness_op gives us the proper uniqueness *)
826-
type const_s = Const.t S.neg
815+
type polarity = negative
827816

828817
let obj = C.Uniqueness_op
829818

830-
let obj_s : const_s S.obj = S.Negative obj
831-
832-
let unpack (S.Neg a) = a
833-
834-
let pack a = S.Neg a
819+
let obj_s : (const * polarity) S.obj = S.Negative obj
835820
end
836821

837822
include Common (Obj)
@@ -870,22 +855,20 @@ module Comonadic_with_regionality = struct
870855
module Obj = struct
871856
type const = Const.t
872857

873-
type const_s = Const.t S.pos
858+
type polarity = positive
874859

875860
let obj : const C.obj = C.Comonadic_with_regionality
876861

877-
let obj_s : const_s S.obj = S.Positive obj
878-
879-
let unpack (S.Pos a) = a
880-
881-
let pack a = S.Pos a
862+
let obj_s : (const * polarity) S.obj = S.Positive obj
882863
end
883864

884865
include Common (Obj)
885866

886867
type error =
887-
[ `Regionality
888-
| `Linearity ]
868+
[ `Regionality of Regionality.error
869+
| `Linearity of Linearity.error ]
870+
871+
type equate_error = equate_step * error
889872

890873
let regionality m =
891874
S.apply Regionality.Obj.obj_s (S.Pos_Pos (C.Proj (Obj.obj, Axis0))) m
@@ -933,27 +916,16 @@ module Comonadic_with_regionality = struct
933916
let submode m0 m1 =
934917
match submode m0 m1 with
935918
| Ok () -> Ok ()
936-
| Error () -> (
937-
(* find out the offending axis *)
938-
match Regionality.submode (regionality m0) (regionality m1) with
939-
| Error () -> Error `Regionality
940-
| Ok () -> (
941-
match Linearity.submode (linearity m0) (linearity m1) with
942-
| Error () -> Error `Linearity
943-
| Ok () -> assert false (* sanity *)))
919+
| Error { left = reg0, lin0; right = reg1, lin1 } ->
920+
if Regionality.Const.le reg0 reg1
921+
then
922+
if Linearity.Const.le lin0 lin1
923+
then assert false
924+
else Error (`Linearity { left = lin0; right = lin1 })
925+
else Error (`Regionality { left = reg0; right = reg1 })
944926

945927
(* override to report the offending axis *)
946-
let equate m0 m1 =
947-
match equate m0 m1 with
948-
| Ok () -> Ok ()
949-
| Error () -> (
950-
(* find out the offending axis *)
951-
match Regionality.equate (regionality m0) (regionality m1) with
952-
| Error () -> Error `Regionality
953-
| Ok () -> (
954-
match Linearity.equate (linearity m0) (linearity m1) with
955-
| Ok () -> assert false (* sanity *)
956-
| Error () -> Error `Linearity))
928+
let equate = equate_from_submode submode
957929

958930
(** overriding to check per-axis *)
959931
let check_const m =
@@ -970,22 +942,20 @@ module Comonadic_with_locality = struct
970942
module Obj = struct
971943
type const = Const.t
972944

973-
type const_s = Const.t S.pos
945+
type polarity = positive
974946

975947
let obj : const C.obj = C.Comonadic_with_locality
976948

977-
let obj_s : const_s S.obj = S.Positive obj
978-
979-
let unpack (S.Pos a) = a
980-
981-
let pack a = S.Pos a
949+
let obj_s : (const * polarity) S.obj = S.Positive obj
982950
end
983951

984952
include Common (Obj)
985953

986954
type error =
987-
[ `Locality
988-
| `Linearity ]
955+
[ `Locality of Locality.error
956+
| `Linearity of Linearity.error ]
957+
958+
type equate_error = equate_step * error
989959

990960
let locality m =
991961
S.apply Locality.Obj.obj_s (S.Pos_Pos (C.Proj (Obj.obj, Axis0))) m
@@ -1033,27 +1003,16 @@ module Comonadic_with_locality = struct
10331003
let submode m0 m1 =
10341004
match submode m0 m1 with
10351005
| Ok () -> Ok ()
1036-
| Error () -> (
1037-
(* find out the offending axis *)
1038-
match Locality.submode (locality m0) (locality m1) with
1039-
| Error () -> Error `Locality
1040-
| Ok () -> (
1041-
match Linearity.submode (linearity m0) (linearity m1) with
1042-
| Ok () -> assert false (* sanity *)
1043-
| Error () -> Error `Linearity))
1006+
| Error { left = loc0, lin0; right = loc1, lin1 } ->
1007+
if Locality.Const.le loc0 loc1
1008+
then
1009+
if Linearity.Const.le lin0 lin1
1010+
then assert false
1011+
else Error (`Linearity { left = lin0; right = lin1 })
1012+
else Error (`Locality { left = loc0; right = loc1 })
10441013

10451014
(* override to report the offending axis *)
1046-
let equate m0 m1 =
1047-
match equate m0 m1 with
1048-
| Ok () -> Ok ()
1049-
| Error () -> (
1050-
(* find out the offending axis *)
1051-
match Locality.equate (locality m0) (locality m1) with
1052-
| Error () -> Error `Locality
1053-
| Ok () -> (
1054-
match Linearity.equate (linearity m0) (linearity m1) with
1055-
| Ok () -> assert false (* sanity *)
1056-
| Error () -> Error `Linearity))
1015+
let equate = equate_from_submode submode
10571016

10581017
(** overriding to check per-axis *)
10591018
let check_const m =
@@ -1068,7 +1027,9 @@ module Monadic = struct
10681027
(* secretly just uniqueness *)
10691028
include Uniqueness
10701029

1071-
type error = [`Uniqueness]
1030+
type error = [`Uniqueness of Uniqueness.error]
1031+
1032+
type equate_error = equate_step * error
10721033

10731034
let max_with_uniqueness m = S.disallow_left m
10741035

@@ -1079,10 +1040,9 @@ module Monadic = struct
10791040
let set_uniqueness_min _ = Uniqueness.min |> S.disallow_right |> S.allow_left
10801041

10811042
let submode m0 m1 =
1082-
match submode m0 m1 with Ok () -> Ok () | Error () -> Error `Uniqueness
1043+
match submode m0 m1 with Ok () -> Ok () | Error e -> Error (`Uniqueness e)
10831044

1084-
let equate m0 m1 =
1085-
match equate m0 m1 with Ok () -> Ok () | Error () -> Error `Uniqueness
1045+
let equate = equate_from_submode submode
10861046
end
10871047

10881048
type ('mo, 'como) monadic_comonadic =
@@ -1139,9 +1099,11 @@ module Value = struct
11391099
let regionality { comonadic; _ } = Comonadic.regionality comonadic
11401100

11411101
type error =
1142-
[ `Regionality
1143-
| `Uniqueness
1144-
| `Linearity ]
1102+
[ `Regionality of Regionality.error
1103+
| `Uniqueness of Uniqueness.error
1104+
| `Linearity of Linearity.error ]
1105+
1106+
type equate_error = equate_step * error
11451107

11461108
(* NB: state mutated when error *)
11471109
let submode { monadic = monadic0; comonadic = comonadic0 }
@@ -1155,8 +1117,7 @@ module Value = struct
11551117
| Error e -> Error e
11561118
| Ok () -> Ok ())
11571119

1158-
let equate m0 m1 =
1159-
match submode m0 m1 with Error e -> Error e | Ok () -> submode m1 m0
1120+
let equate = equate_from_submode submode
11601121

11611122
let submode_exn m0 m1 =
11621123
match submode m0 m1 with
@@ -1368,9 +1329,11 @@ module Alloc = struct
13681329
let locality { comonadic; _ } = Comonadic.locality comonadic
13691330

13701331
type error =
1371-
[ `Locality
1372-
| `Uniqueness
1373-
| `Linearity ]
1332+
[ `Locality of Locality.error
1333+
| `Uniqueness of Uniqueness.error
1334+
| `Linearity of Linearity.error ]
1335+
1336+
type equate_error = equate_step * error
13741337

13751338
(* NB: state mutated when error - should be fine as this always indicates type
13761339
error in typecore.ml which triggers backtracking. *)
@@ -1383,8 +1346,7 @@ module Alloc = struct
13831346
| Error e -> Error e
13841347
| Ok () -> Ok ())
13851348

1386-
let equate m0 m1 =
1387-
match submode m0 m1 with Error e -> Error e | Ok () -> submode m1 m0
1349+
let equate = equate_from_submode submode
13881350

13891351
let submode_exn m0 m1 =
13901352
match submode m0 m1 with

0 commit comments

Comments
 (0)