Skip to content

Commit 11d9fec

Browse files
author
Thomas Mahler
committed
use scott encoded booleans as in the Combinator based implementations
1 parent 0eb706a commit 11d9fec

6 files changed

Lines changed: 132 additions & 205 deletions

File tree

src/CCC/Cat.hs

Lines changed: 13 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
{-# LANGUAGE FlexibleInstances #-}
2-
{-# LANGUAGE MultiParamTypeClasses #-}
3-
41
{-- This module contains definition of categories that are required for
52
modelling Closed Cartesian Categories.
63
It re-exports Category from Control.Category and defines Monoidal, Cartesian and Closed.
4+
5+
Booleans are Scott-encoded as selector morphisms:
6+
TRUE = sndC (selects second from a pair, like A combinator: λt e. e)
7+
FALSE = fstC (selects first from a pair, like K combinator: λt e. t)
8+
Comparison operators return selector morphisms k (b,b) b instead of Bool values.
9+
Conditionals are just application of the selector to a pair of alternatives.
710
--}
811

912
module CCC.Cat
@@ -14,13 +17,8 @@ module CCC.Cat
1417
, fanC
1518
, idC
1619
, NumCat (..)
17-
, BoolCat (..)
18-
, BoolLike (..)
19-
, EqLike (..)
2020
, EqCat (..)
21-
, IfValCat (..)
2221
, FixCat (..)
23-
, Cond (..)
2422
) where
2523

2624
import Control.Category (Category (..))
@@ -51,64 +49,17 @@ class Cartesian k => NumCat k where
5149
addC :: Num a => k (a, a) a
5250
subC :: Num a => k (a, a) a
5351
absC :: Num a => k a a
52+
-- Comparison operators return Scott-encoded booleans (selector morphisms)
53+
leqC :: Ord a => k (a, a) (k (b, b) b)
54+
geqC :: Ord a => k (a, a) (k (b, b) b)
55+
lesC :: Ord a => k (a, a) (k (b, b) b)
56+
greC :: Ord a => k (a, a) (k (b, b) b)
5457

55-
-- eqlC :: (Eq a, BoolLike b) => k (a,a) b
56-
leqC :: (Ord a, BoolLike b) => k (a, a) b
57-
geqC :: (Ord a, BoolLike b) => k (a, a) b
58-
lesC :: (Ord a, BoolLike b) => k (a, a) b
59-
greC :: (Ord a, BoolLike b) => k (a, a) b
60-
61-
class Cartesian k => BoolCat k where
62-
andC :: BoolLike a => k (a, a) a
63-
orC :: BoolLike a => k (a, a) a
64-
notC :: BoolLike a => k a a
65-
ifTE :: k (Bool, (k b d, k b d)) (k b d)
66-
67-
class BoolLike a where
68-
(&&) :: a -> a -> a
69-
(||) :: a -> a -> a
70-
not :: a -> a
71-
true :: a
72-
false :: a
73-
74-
instance BoolLike Bool where
75-
(&&) = (Prelude.&&)
76-
(||) = (Prelude.||)
77-
not = Prelude.not
78-
true = True
79-
false = False
80-
81-
class (BoolLike b) => EqLike a b where
82-
(==) :: a -> a -> b
83-
84-
instance EqLike Integer Bool where
85-
(==) = (Prelude.==)
86-
87-
instance EqLike Bool Bool where
88-
(==) = (Prelude.==)
89-
58+
-- Equality comparison returns a Scott-encoded boolean (selector morphism)
9059
class Cartesian k => EqCat k where
91-
eqlC :: (EqLike a b, BoolLike b) => k (a, a) b
92-
93-
-- Value-level conditional: selects between two values based on boolean
94-
class Cartesian k => IfValCat k where
95-
ifValC :: k (Bool, (a, a)) a
60+
eqlC :: Eq a => k (a, a) (k (b, b) b)
9661

9762
-- Fixpoint combinator for recursive definitions
9863
-- Takes a step function (rec, input) -> output and produces the fixed point
9964
class Closed k => FixCat k where
10065
fixC :: k (k a b, a) b -> k a b
101-
102-
-- Conditional type class for source-level if-then-else
103-
-- Two-parameter class: c is the condition type, a is the branch type
104-
-- This allows CatExpr z Bool as condition for CatExpr z a branches
105-
class Cond c a where
106-
ite :: c -> a -> a -> a
107-
108-
instance Cond Bool Bool where
109-
ite True t _ = t
110-
ite False _ e = e
111-
112-
instance Cond Bool Integer where
113-
ite True t _ = t
114-
ite False _ e = e

src/CCC/CatExpr.hs

Lines changed: 16 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,19 @@
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

1621
module 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)
2125
import Prelude hiding (id, (.))
2226

2327
data 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-
117101
instance EqCat CatExpr where
118102
eqlC = Eql
119103

120-
instance IfValCat CatExpr where
121-
ifValC = IfVal
122-
123104
instance 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
131108
applyF :: CatExpr z (CatExpr a b) -> CatExpr z a -> CatExpr z b
132109
applyF 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-
150111
instance Eq (CatExpr a b) where
151112
f == g = f Prelude.== g
152113

0 commit comments

Comments
 (0)