@@ -664,27 +664,33 @@ let append_changes = S.S.append_changes
664
664
module type Obj = sig
665
665
type const
666
666
667
- type const_s
667
+ type polarity
668
668
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
674
670
end
675
671
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
+
676
680
module Common (Obj : Obj ) = struct
677
681
open Obj
678
682
679
- type 'd t = (const_s , 'd ) S .mode
683
+ type 'd t = (const * polarity , 'd ) S .mode
680
684
681
685
type l = (allowed * disallowed ) t
682
686
683
687
type r = (disallowed * allowed ) t
684
688
685
689
type lr = (allowed * allowed ) t
686
690
687
- type error = unit
691
+ type nonrec error = const error
692
+
693
+ type equate_error = equate_step * error
688
694
689
695
let disallow_right m = S. disallow_right m
690
696
@@ -712,24 +718,19 @@ module Common (Obj : Obj) = struct
712
718
713
719
let submode_exn m0 m1 = assert (submode m0 m1 |> Result. is_ok)
714
720
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
720
722
721
723
let equate_exn m0 m1 = assert (equate m0 m1 |> Result. is_ok)
722
724
723
725
let print ?verbose ?axis () ppf m = S. print obj_s ?verbose ?axis ppf m
724
726
725
- let constrain_upper m = Obj. unpack ( S. constrain_upper obj_s m)
727
+ let constrain_upper m = S. constrain_upper obj_s m
726
728
727
- let constrain_lower m = Obj. unpack ( S. constrain_lower obj_s m)
729
+ let constrain_lower m = S. constrain_lower obj_s m
728
730
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
731
732
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
733
734
end
734
735
735
736
module Locality = struct
@@ -738,15 +739,11 @@ module Locality = struct
738
739
module Obj = struct
739
740
type const = Const .t
740
741
741
- type const_s = Const .t S .pos
742
+ type polarity = positive
742
743
743
744
let obj = C. Locality
744
745
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
750
747
end
751
748
752
749
include Common (Obj )
@@ -766,13 +763,9 @@ module Regionality = struct
766
763
module Obj = struct
767
764
type const = Const .t
768
765
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
774
767
775
- let pack a = S. Pos a
768
+ let obj_s : (const * polarity) S.obj = S. Positive C. Regionality
776
769
end
777
770
778
771
include Common (Obj )
@@ -794,15 +787,11 @@ module Linearity = struct
794
787
module Obj = struct
795
788
type const = Const .t
796
789
797
- type const_s = Const .t S .pos
790
+ type polarity = positive
798
791
799
792
let obj = C. Linearity
800
793
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
806
795
end
807
796
808
797
include Common (Obj )
@@ -823,15 +812,11 @@ module Uniqueness = struct
823
812
type const = Const .t
824
813
825
814
(* the negation of Uniqueness_op gives us the proper uniqueness *)
826
- type const_s = Const .t S .neg
815
+ type polarity = negative
827
816
828
817
let obj = C. Uniqueness_op
829
818
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
835
820
end
836
821
837
822
include Common (Obj )
@@ -870,22 +855,20 @@ module Comonadic_with_regionality = struct
870
855
module Obj = struct
871
856
type const = Const .t
872
857
873
- type const_s = Const .t S .pos
858
+ type polarity = positive
874
859
875
860
let obj : const C.obj = C. Comonadic_with_regionality
876
861
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
882
863
end
883
864
884
865
include Common (Obj )
885
866
886
867
type error =
887
- [ `Regionality
888
- | `Linearity ]
868
+ [ `Regionality of Regionality .error
869
+ | `Linearity of Linearity .error ]
870
+
871
+ type equate_error = equate_step * error
889
872
890
873
let regionality m =
891
874
S. apply Regionality.Obj. obj_s (S. Pos_Pos (C. Proj (Obj. obj, Axis0 ))) m
@@ -933,27 +916,16 @@ module Comonadic_with_regionality = struct
933
916
let submode m0 m1 =
934
917
match submode m0 m1 with
935
918
| 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 })
944
926
945
927
(* 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
957
929
958
930
(* * overriding to check per-axis *)
959
931
let check_const m =
@@ -970,22 +942,20 @@ module Comonadic_with_locality = struct
970
942
module Obj = struct
971
943
type const = Const .t
972
944
973
- type const_s = Const .t S .pos
945
+ type polarity = positive
974
946
975
947
let obj : const C.obj = C. Comonadic_with_locality
976
948
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
982
950
end
983
951
984
952
include Common (Obj )
985
953
986
954
type error =
987
- [ `Locality
988
- | `Linearity ]
955
+ [ `Locality of Locality .error
956
+ | `Linearity of Linearity .error ]
957
+
958
+ type equate_error = equate_step * error
989
959
990
960
let locality m =
991
961
S. apply Locality.Obj. obj_s (S. Pos_Pos (C. Proj (Obj. obj, Axis0 ))) m
@@ -1033,27 +1003,16 @@ module Comonadic_with_locality = struct
1033
1003
let submode m0 m1 =
1034
1004
match submode m0 m1 with
1035
1005
| 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 })
1044
1013
1045
1014
(* 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
1057
1016
1058
1017
(* * overriding to check per-axis *)
1059
1018
let check_const m =
@@ -1068,7 +1027,9 @@ module Monadic = struct
1068
1027
(* secretly just uniqueness *)
1069
1028
include Uniqueness
1070
1029
1071
- type error = [`Uniqueness ]
1030
+ type error = [`Uniqueness of Uniqueness .error ]
1031
+
1032
+ type equate_error = equate_step * error
1072
1033
1073
1034
let max_with_uniqueness m = S. disallow_left m
1074
1035
@@ -1079,10 +1040,9 @@ module Monadic = struct
1079
1040
let set_uniqueness_min _ = Uniqueness. min |> S. disallow_right |> S. allow_left
1080
1041
1081
1042
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)
1083
1044
1084
- let equate m0 m1 =
1085
- match equate m0 m1 with Ok () -> Ok () | Error () -> Error `Uniqueness
1045
+ let equate = equate_from_submode submode
1086
1046
end
1087
1047
1088
1048
type ('mo, 'como) monadic_comonadic =
@@ -1139,9 +1099,11 @@ module Value = struct
1139
1099
let regionality { comonadic; _ } = Comonadic. regionality comonadic
1140
1100
1141
1101
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
1145
1107
1146
1108
(* NB: state mutated when error *)
1147
1109
let submode { monadic = monadic0 ; comonadic = comonadic0 }
@@ -1155,8 +1117,7 @@ module Value = struct
1155
1117
| Error e -> Error e
1156
1118
| Ok () -> Ok () )
1157
1119
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
1160
1121
1161
1122
let submode_exn m0 m1 =
1162
1123
match submode m0 m1 with
@@ -1368,9 +1329,11 @@ module Alloc = struct
1368
1329
let locality { comonadic; _ } = Comonadic. locality comonadic
1369
1330
1370
1331
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
1374
1337
1375
1338
(* NB: state mutated when error - should be fine as this always indicates type
1376
1339
error in typecore.ml which triggers backtracking. *)
@@ -1383,8 +1346,7 @@ module Alloc = struct
1383
1346
| Error e -> Error e
1384
1347
| Ok () -> Ok () )
1385
1348
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
1388
1350
1389
1351
let submode_exn m0 m1 =
1390
1352
match submode m0 m1 with
0 commit comments