99{- - | GADT representing categorical expressions that instantiate type classes
1010 Closed, Cartesian, Category, and others. Serves as the compilation target for toCCC.
1111
12+ Booleans are Scott-encoded as selector morphisms (CatExpr (a,a) a):
13+ TRUE = Snd (selects second, like A combinator)
14+ FALSE = Fst (selects first, like K combinator)
15+ Conditionals are expressed as: Apply ∘ ⟨selector, ⟨thenVal, elseVal⟩⟩
16+
1217 > toCCC @CatExpr (\(x, y) -> x)
1318 Comp Fst Id
1419--}
1520
1621module CCC.CatExpr where
1722
18- import CCC.Cat (BoolCat (.. ), BoolLike (.. ), Cartesian (.. ), Cond (.. ),
19- Category (.. ), Closed (.. ), EqCat (.. ), EqLike (.. ),
20- FixCat (.. ), IfValCat (.. ), Monoidal (.. ), NumCat (.. ), fanC )
23+ import CCC.Cat (Cartesian (.. ), Category (.. ), Closed (.. ),
24+ EqCat (.. ), FixCat (.. ), Monoidal (.. ), NumCat (.. ), fanC )
2125import Prelude hiding (id , (.) )
2226
2327data CatExpr a b where
@@ -38,21 +42,15 @@ data CatExpr a b where
3842 Curry :: CatExpr (a , b ) c -> CatExpr a (CatExpr b c )
3943 Uncurry :: CatExpr a (CatExpr b c ) -> CatExpr (a , b ) c
4044 Lift :: (a -> b ) -> CatExpr a b
41- Eql :: (EqLike a b , BoolLike b ) => CatExpr (a , a ) b
42- Leq :: (Ord a , BoolLike b ) => CatExpr (a , a ) b
43- Geq :: (Ord a , BoolLike b ) => CatExpr (a , a ) b
44- Les :: (Ord a , BoolLike b ) => CatExpr (a , a ) b
45- Gre :: (Ord a , BoolLike b ) => CatExpr (a , a ) b
46- -- Boolean combinators
47- And :: (BoolLike a ) => CatExpr (a , a ) a
48- Or :: (BoolLike a ) => CatExpr (a , a ) a
49- Not :: (BoolLike a ) => CatExpr a a
50- T :: (BoolLike a ) => CatExpr b a
51- F :: (BoolLike a ) => CatExpr b a
52- -- Conditional branching: selects between morphisms based on a boolean
53- IfThenElse :: CatExpr (Bool , (CatExpr b c , CatExpr b c )) (CatExpr b c )
54- -- Value-level conditional: selects between values based on a boolean
55- IfVal :: CatExpr (Bool , (a , a )) a
45+ -- Comparison operators return Scott-encoded booleans (selector morphisms)
46+ -- A selector CatExpr (b,b) b picks one element from a pair:
47+ -- Snd = TRUE (selects second, like A: λt e. e)
48+ -- Fst = FALSE (selects first, like K: λt e. t)
49+ Eql :: (Eq a ) => CatExpr (a , a ) (CatExpr (b , b ) b )
50+ Leq :: (Ord a ) => CatExpr (a , a ) (CatExpr (b , b ) b )
51+ Geq :: (Ord a ) => CatExpr (a , a ) (CatExpr (b , b ) b )
52+ Les :: (Ord a ) => CatExpr (a , a ) (CatExpr (b , b ) b )
53+ Gre :: (Ord a ) => CatExpr (a , a ) (CatExpr (b , b ) b )
5654 -- Fixpoint combinator for recursive definitions
5755 Fix :: CatExpr (CatExpr a b , a ) b -> CatExpr a b
5856
@@ -100,53 +98,16 @@ instance (Num a) => Num (CatExpr z a) where
10098 signum = error " TODO sig"
10199 fromInteger i = FromInt . IntConst i
102100
103- instance BoolCat CatExpr where
104- andC = And
105- orC = Or
106- notC = Not
107-
108- ifTE = IfThenElse
109-
110- instance (BoolLike b ) => BoolLike (CatExpr a b ) where
111- f && g = And . fanC f g
112- f || g = Or . fanC f g
113- not f = Not . f
114- true = T
115- false = F
116-
117101instance EqCat CatExpr where
118102 eqlC = Eql
119103
120- instance IfValCat CatExpr where
121- ifValC = IfVal
122-
123104instance FixCat CatExpr where
124105 fixC = Fix
125106
126- -- Cond instance for CatExpr: combines condition and branches
127- instance Cond (CatExpr z Bool ) (CatExpr z a ) where
128- ite cond thenBranch elseBranch = IfVal . fanC cond (fanC thenBranch elseBranch)
129-
130107-- Apply a morphism-valued expression to an argument
131108applyF :: CatExpr z (CatExpr a b ) -> CatExpr z a -> CatExpr z b
132109applyF f x = Apply . fanC f x
133110
134- -- Equality comparison with fixed result type (avoids ambiguity)
135- eqF :: (EqLike a Bool ) => CatExpr z a -> CatExpr z a -> CatExpr z Bool
136- eqF f g = Eql . fanC f g
137-
138- -- Conditional with fixed condition type (avoids ambiguity)
139- iteF :: CatExpr z Bool -> CatExpr z a -> CatExpr z a -> CatExpr z a
140- iteF cond t e = IfVal . fanC cond (fanC t e)
141-
142- -- General instance for comparing categorical morphisms
143- instance {-# OVERLAPPABLE #-} (BoolLike b , EqLike a b ) => EqLike (CatExpr z a ) (CatExpr z b ) where
144- f == g = Eql . fanC f g
145-
146- -- Instance for comparing plain values to produce CatExpr (used by toCCC)
147- instance {-# OVERLAPPABLE #-} (BoolLike b , EqLike a b ) => EqLike a (CatExpr a b ) where
148- x == y = Eql . fanC (Lift (const x)) (Lift (const y))
149-
150111instance Eq (CatExpr a b ) where
151112 f == g = f Prelude. == g
152113
0 commit comments