6. Unit = 1
Maybe a = 1 + a
Pair a b = a × b
Either a b = a + b
data () = ()
data Maybe a = Nothing | Just a
data a b = (a, b)
data Either a b = Left a | Right b
6 — jaiyalas @ funth - 2017 oct 05
7. Unit = 1
Maybe a = 1 + a
Pair a b = a × b
Either a b = a + b
data () = ()
data Maybe a = Nothing | Just a
data a b = (a, b)
data Either a b = Left a | Right b
7 — jaiyalas @ funth - 2017 oct 05
8. Unit = 1
Maybe a = 1 + a
Pair a b = a × b
Either a b = a + b
data () = ()
data Maybe a = Nothing | Just a
data a b = (a, b)
data Either a b = Left a | Right b
8 — jaiyalas @ funth - 2017 oct 05
9. Unit = 1
Maybe a = 1 + a
Pair a b = a × b
Either a b = a + b
data () = ()
data Maybe a = Nothing | Just a
data a b = (a, b)
data Either a b = Left a | Right b
9 — jaiyalas @ funth - 2017 oct 05
10. ListF a x = 1 + a × x
List a = μx . (1 + a × x)
BTreeF a x = 1 + a × x × x
BTree a = μx . (1 + a × x × x)
data ListF a x = [] | a : x
type List = Fix ListF
data BTreeF a x = Nil | Fork a x x
type BTree = Fix BTreeF
10 — jaiyalas @ funth - 2017 oct 05
11. ListF a x = 1 + a × x
List a = μx . (1 + a × x)
BTreeF a x = 1 + a × x × x
BTree a = μx . (1 + a × x × x)
data ListF a x = [] | a : x
type List = Fix ListF
data BTreeF a x = Nil | Fork a x x
type BTree = Fix BTreeF
11 — jaiyalas @ funth - 2017 oct 05
29. data BT a = Leaf a
| Fork a (BT a) (BT a)
data Rib a = LRib a (Bt a)
| RRib a (Bt a)
data Zipper a = Zipper [Rib a] (BT a)
29 — jaiyalas @ funth - 2017 oct 05
30. data BT a = Leaf a
| Fork a (BT a) (BT a)
data Rib a = LRib a (Bt a)
| RRib a (Bt a)
data Zipper a = Zipper [Rib a] (BT a)
30 — jaiyalas @ funth - 2017 oct 05
31. data BT a = Leaf a
| Fork a (BT a) (BT a)
data Rib a = LRib a (Bt a)
| RRib a (Bt a)
data Zipper a = Zipper [Rib a] (BT a)
31 — jaiyalas @ funth - 2017 oct 05
53. data Term a = Lit a
| Var Int
| Abs (Term a)
| App (Term a) (Term a)
-- F a x = a + Int + x + x × x
-- T a = μx.F a x
53 — jaiyalas @ funth - 2017 oct 05
58. data Rose a = Rose a [Rose a]
-- RoseF a x = a × (List x)
-- = a × (μy.ListF x y)
-- = a × (μy.1 + x × y)
-- Rose a = μx.RoseF a x
58 — jaiyalas @ funth - 2017 oct 05
61. type Size = Int
type Name = String
type Attack = Int
type HP = Int
data Player = Player Name Bag HP
data Bag = Bag [Item]
data Item = Weapon Name Attack
| Potion Name PotionEffect
| SmallBag Size Bag
data PotionEffect = Poison Int | Healing Int
61 — jaiyalas @ funth - 2017 oct 05
63. bless :: Player -> Player
bless (Player n bag ss) = Player n (updateBag bag) ss
updateBag :: Bag -> Bag
updateBag (Bag xs) = Bag $ map updatePotion xs
updatePotion :: Item -> Item
updatePotion (Potion n pe) = Potion n (updateHealing pe)
updatePotion (SmallBag s bag) = SmallBag s (updateBag bag)
updatePotion x = x
updateHealing :: PotionEffect -> PotionEffect
updateHealing (Healing i) = Healing (i + 10)
updateHealing x = x
63 — jaiyalas @ funth - 2017 oct 05
64. bless :: Player -> Player
bless (Player n bag ss) = Player n (updateBag bag) ss
updateBag :: Bag -> Bag
updateBag (Bag xs) = Bag $ map updatePotion xs
updatePotion :: Item -> Item
updatePotion (Potion n pe) = Potion n (updateHealing pe)
updatePotion (SmallBag s bag) = SmallBag s (updateBag bag)
updatePotion x = x
updateHealing :: PotionEffect -> PotionEffect
updateHealing (Healing i) = Healing (i + 10)
updateHealing x = x
64 — jaiyalas @ funth - 2017 oct 05
68. updateHealing :: PotionEffect -> PotionEffect
updateHealing (Healing i) = Healing (i + 10)
updateHealing x = x
bless' :: Player -> Player
bless' p = everywhere (mkT updateHealing) p
-- mkT = type extension
-- everywhere = general traversal
68 — jaiyalas @ funth - 2017 oct 05
70. {- Data.Typeable -}
-- abstract class
class Typeable a where
typeOf :: a -> TypeRep
-- type-safe cast operation
cast :: (Typeable a, Typeable b) => a -> Maybe b
cast = ... typeOf ...
70 — jaiyalas @ funth - 2017 oct 05
71. mkT :: (Typeable a, Typeable b)
=> (b -> b) -> a -> a
mkT f = case cast f of
Just g -> g
Nothing -> id
updateHealing' :: Typeable a => a -> a
updateHealing' = mkT updateHealing
71 — jaiyalas @ funth - 2017 oct 05
72. *Main> updateHealing' (HP 10)
HP 10
*Main> updateHealing' (Potion "A" (Healing 10))
Potion "A" (Healing 10)
*Main> updateHealing' (Healing 10)
Healing 20
72 — jaiyalas @ funth - 2017 oct 05
73. data Player = Player Name Bag HP
deriving (Typeable)
data Bag = Bag [Item]
deriving (Typeable)
data Item = Weapon Name Attack
| Potion Name PotionEffect
| SmallBag Size Bag
deriving (Typeable)
data PotionEffect = Poison Int
| Healing Int
deriving (Typeable)
73 — jaiyalas @ funth - 2017 oct 05
75. one-layertraversal
{- Data.Data -}
class Typeable a => Data a where
gmapT :: (forall b. Data b => b -> b) -> a -> a
instance Typeable a => Data [a] where
gmapT f [] = []
gmapT f (x : xs) = (f x) : (f xs)
instance Data Player where
gmapT f (Player name bag hp) =
Player (f name) (f bag) (f hp)
75 — jaiyalas @ funth - 2017 oct 05
76. recursivetraversal
-- bottom-up
everywhere :: Data a
=> (forall b. Data b => b -> b)
-> a -> a
everywhere f x = f (gmapT (everywhere f) x)
-- top-down
everywhere' :: Data a
=> (forall b. Data b => b -> b)
-> a -> a
everywhere' f x = gmapT (everywhere' f) (f x)
76 — jaiyalas @ funth - 2017 oct 05
77. data Player = Player Name Bag HP
deriving (Typeable, Data)
data Bag = Bag [Item]
deriving (Typeable, Data)
data Item = Weapon Name Attack
| Potion Name PotionEffect
| SmallBag Size Bag
deriving (Typeable, Data)
data PotionEffect = Poison Int
| Healing Int
deriving (Typeable, Data)
77 — jaiyalas @ funth - 2017 oct 05
78. bless' p = everywhere (mkT updateHealing) p
jack = Player
"Jack"
(Bag [ Potion "A" (Healing 1)
, Potion "B" (Poison 5)
, SmallBag 1 (Bag [Potion "C" (Healing 5)])
]) 10
*Main> bless' jack
Player
"Jack"
(Bag [ Potion "A" (Healing 11)
, Potion "B" (Poison 5)
, SmallBag 1 (Bag [Potion "C" (Healing 15)])
]) 10
78 — jaiyalas @ funth - 2017 oct 05