On Github dyokomizo / type-algebra-slides
a = (b × c) + d
data Either a b = Left a | Right b data Pair a b = Pair (a, b)
Faz sentido? Sim.
Como?
data () = () -- aka Unit
data Void -- requer EmptyDataDecls
|
(,)
→
data Either a b = Left a | Right b
c = a + b
data Pair a b = Pair (a, b)
c = a × b
data Fun a b = Fun (a → b)
c = ba
data Maybe a = Just a | Nothing -- () impĺícito
c = a + 1
data Bool = True | False
a = 1 + 1
a ≅ (a × 1) ≅ (1 × a)
String ≅ (String, ()) ≅ ((), String)
a ≅ (a + 0) ≅ (0 + a)
String ≅ (Either String Void) ≅ (Either Void String)
a × (b × c) ≅ (a × b) × c
(String,(Int,Bool)) ≅ ((String,Int),Bool)
a + (b + c) ≅ (a + b) + c
Either String (Either Int Bool) ≅ Either (Either String Int) Bool
a ≅ (a × 1) ≅ (1 × a)
a ≅ (a + 0) ≅ (0 + a)
a × (b × c) ≅ (a × b) × c
a + (b + c) ≅ (a + b) + c
a × (b + c) ≅ (a × b) + (a × c)
(String, Either Int Bool) ≅ Either (String, Int) (String, Bool)
data A a b = A1 a b | A2 b
type A a b = Either (a, b) b
c = (a × b) + b ≅ (a × b) + (1 × b) -- Elemento neutro ≅ (a + 1) × b -- Distributividade
type A a b = (Maybe a, b)
a → b ≍ ba
Bool → Int ≍ IntBool
Bool → a ≍ aBool
Bool ≍ 1 + 1
aBool≅ a1 + 1 ≅ a1× a1 ≅ a × a
Bool → a ≅ (a,a)
meaningOfLife :: Boolean -> Int meaningOfLife True = 42 meaningOfLife False = 37 Bool -> a ≅ (a, a) meaningOfLife' :: (Int, Int) meaningOfLife = (42,37)