Skip to content

Commit 978b51a

Browse files
authored
flambda-backend: Add missing morphism reductions (#2374)
* add missing reduction * address comments; more improvement
1 parent 39e2b1e commit 978b51a

File tree

1 file changed

+45
-8
lines changed

1 file changed

+45
-8
lines changed

typing/mode.ml

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,9 @@ module Lattices = struct
407407
| Linearity, (area, _) -> area, r
408408
| Uniqueness, (_, ()) -> r, ()
409409

410+
let set_areality : type a0 a1. a1 -> a0 comonadic_with -> a1 comonadic_with =
411+
fun r (_, lin) -> r, lin
412+
410413
let proj_obj : type t r. (t, r) axis -> t obj -> r obj =
411414
fun ax obj ->
412415
match ax, obj with
@@ -885,13 +888,33 @@ module Lattices_mono = struct
885888
type a b c d.
886889
c obj -> (b, c, d) morph -> (a, b, d) morph -> (a, c, d) morph option =
887890
fun dst m0 m1 ->
891+
let is_max c = le dst (max dst) c in
892+
let is_min c = le dst c (min dst) in
893+
let is_mid_max c =
894+
let mid = src dst m0 in
895+
le mid (max mid) c
896+
in
897+
let is_mid_min c =
898+
let mid = src dst m0 in
899+
le mid c (min mid)
900+
in
888901
match m0, m1 with
889902
| Id, m -> Some m
890903
| m, Id -> Some m
891904
| Meet_with c0, Meet_with c1 -> Some (Meet_with (meet dst c0 c1))
892905
| Join_with c0, Join_with c1 -> Some (Join_with (join dst c0 c1))
893-
| Meet_with c0, m1 when le dst (max dst) c0 -> Some m1
894-
| Join_with c0, m1 when le dst c0 (min dst) -> Some m1
906+
| Imply c0, Imply c1 -> Some (Imply (meet dst c0 c1))
907+
| Subtract c0, Subtract c1 -> Some (Subtract (join dst c0 c1))
908+
| Imply c0, Join_with c1 when le dst c0 c1 -> Some (Join_with (max dst))
909+
| Subtract c0, Meet_with c1 when le dst c1 c0 -> Some (Meet_with (min dst))
910+
| Meet_with c0, m1 when is_max c0 -> Some m1
911+
| Join_with c0, m1 when is_min c0 -> Some m1
912+
| Imply c0, m1 when is_max c0 -> Some m1
913+
| Subtract c0, m1 when is_min c0 -> Some m1
914+
| m1, Meet_with c0 when is_mid_max c0 -> Some m1
915+
| m1, Join_with c0 when is_mid_min c0 -> Some m1
916+
| m1, Imply c0 when is_mid_max c0 -> Some m1
917+
| m1, Subtract c0 when is_mid_min c0 -> Some m1
895918
| Compose (f0, f1), g -> (
896919
let mid = src dst f0 in
897920
match maybe_compose mid f1 g with
@@ -903,9 +926,9 @@ module Lattices_mono = struct
903926
| Some m -> Some (compose dst m g1)
904927
| None -> None)
905928
| Proj (mid, ax), Meet_with c ->
906-
Some (Compose (Meet_with (proj ax c), Proj (mid, ax)))
929+
Some (compose dst (Meet_with (proj ax c)) (Proj (mid, ax)))
907930
| Proj (mid, ax), Join_with c ->
908-
Some (Compose (Join_with (proj ax c), Proj (mid, ax)))
931+
Some (compose dst (Join_with (proj ax c)) (Proj (mid, ax)))
909932
| Proj (_, ax0), Max_with ax1 -> (
910933
match eq_axis ax0 ax1 with None -> None | Some Refl -> Some Id)
911934
| Proj (_, ax0), Min_with ax1 -> (
@@ -952,18 +975,32 @@ module Lattices_mono = struct
952975
Locality_as_regionality)
953976
| Map_comonadic f, Join_with c ->
954977
let dst0 = proj_obj Areality dst in
955-
let areality, linearity = c in
978+
let areality = proj Areality c in
956979
Some
957980
(compose dst
958-
(Join_with (min_with dst Linearity linearity))
981+
(Join_with (set_areality (min dst0) c))
959982
(Map_comonadic (compose dst0 f (Join_with areality))))
960983
| Map_comonadic f, Meet_with c ->
961984
let dst0 = proj_obj Areality dst in
962-
let areality, linearity = c in
985+
let areality = proj Areality c in
963986
Some
964987
(compose dst
965-
(Meet_with (max_with dst Linearity linearity))
988+
(Meet_with (set_areality (max dst0) c))
966989
(Map_comonadic (compose dst0 f (Meet_with areality))))
990+
| Map_comonadic f, Imply c ->
991+
let dst0 = proj_obj Areality dst in
992+
let areality = proj Areality c in
993+
Some
994+
(compose dst
995+
(Imply (set_areality (max dst0) c))
996+
(Map_comonadic (compose dst0 f (Imply areality))))
997+
| Map_comonadic f, Subtract c ->
998+
let dst0 = proj_obj Areality dst in
999+
let areality = proj Areality c in
1000+
Some
1001+
(compose dst
1002+
(Subtract (set_areality (min dst0) c))
1003+
(Map_comonadic (compose dst0 f (Subtract areality))))
9671004
| Regional_to_global, Locality_as_regionality -> Some Id
9681005
| Regional_to_global, Local_to_regional -> Some (Meet_with Locality.Global)
9691006
| Local_to_regional, Regional_to_local -> None

0 commit comments

Comments
 (0)