Skip to content

Commit c2822d4

Browse files
author
Thomas Mahler
committed
Implement more simplifications
1 parent 35c78c8 commit c2822d4

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

src/CCC/Compiler.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ newtype Closed a = Closed (forall z. CatExpr z a)
4848
-- SFun is a Haskell-level function modelling an arrow without fixing a context type.
4949
data SVal
5050
= SInt (Closed Integer)
51-
| SSel (ClosedSel)
51+
| SSel ClosedSel
5252
| SFun (SVal -> Either String SVal)
5353

5454
-- A closed selector morphism: a Scott-encoded boolean valid in any context.

src/CCC/Rewrite.hs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,50 @@ ruleCurry :: Rule
5757
ruleCurry (Curry (Uncurry f)) = Just f
5858
ruleCurry _ = Nothing
5959

60+
-- Dual: uncurry (curry f) = f
61+
ruleUncurry :: Rule
62+
ruleUncurry (Uncurry (Curry f)) = Just f
63+
ruleUncurry _ = Nothing
64+
6065
ruleCurryApply :: Rule
6166
ruleCurryApply (Curry Apply) = Just Id
6267
ruleCurryApply _ = Nothing
6368

69+
-- CCC beta rule: apply ∘ (curry f × id) = f
70+
-- This eliminates redundant Apply ∘ Curry pairs from explicit abstraction.
71+
ruleApplyCurry :: Rule
72+
ruleApplyCurry (Comp Apply (Par (Curry f) Id)) = Just f
73+
ruleApplyCurry _ = Nothing
74+
75+
-- Projection through product: fst ∘ (f × g) = f ∘ fst
76+
-- Combined with fst ∘ dup = id, this reduces fst ∘ ⟨f, g⟩ → f.
77+
ruleFstPar :: Rule
78+
ruleFstPar (Comp Fst (Par f _)) = Just (Comp f Fst)
79+
ruleFstPar _ = Nothing
80+
81+
-- Projection through product: snd ∘ (f × g) = g ∘ snd
82+
-- Combined with snd ∘ dup = id, this reduces snd ∘ ⟨f, g⟩ → g.
83+
ruleSndPar :: Rule
84+
ruleSndPar (Comp Snd (Par _ g)) = Just (Comp g Snd)
85+
ruleSndPar _ = Nothing
86+
87+
-- Par-composition fusion: (f × g) ∘ (h × k) = (f ∘ h) × (g ∘ k)
88+
ruleParComp :: Rule
89+
ruleParComp (Comp (Par f g) (Par h k)) = Just (Par (Comp f h) (Comp g k))
90+
ruleParComp _ = Nothing
91+
92+
-- Fan-composition distribution: ⟨f, g⟩ ∘ h = ⟨f ∘ h, g ∘ h⟩
93+
-- Since fanC f g = (f × g) ∘ dup, and ruleParen normalizes to (f × g) ∘ (dup ∘ h),
94+
-- this rewrites to ((f ∘ h) × (g ∘ h)) ∘ dup, i.e. fanC (f ∘ h) (g ∘ h).
95+
ruleFanComp :: Rule
96+
ruleFanComp (Comp (Par f g) (Comp Dup h)) = Just (Comp (Par (Comp f h) (Comp g h)) Dup)
97+
ruleFanComp _ = Nothing
98+
99+
-- Trivial parallel identity: id × id = id
100+
ruleParId :: Rule
101+
ruleParId (Par Id Id) = Just Id
102+
ruleParId _ = Nothing
103+
64104
ruleIdLeft :: Rule
65105
ruleIdLeft (Comp Id f) = Just f
66106
ruleIdLeft _ = Nothing
@@ -76,11 +116,18 @@ allRules =
76116
ruleIdLeft,
77117
ruleFstDup,
78118
ruleSndDup,
119+
ruleFstPar,
120+
ruleSndPar,
79121
ruleParDup,
80122
ruleParDup',
81123
ruleParDup'',
124+
ruleParComp,
125+
ruleFanComp,
126+
ruleParId,
82127
ruleCurry,
128+
ruleUncurry,
83129
ruleCurryApply,
130+
ruleApplyCurry,
84131
ruleParen
85132
]
86133

0 commit comments

Comments
 (0)