From 448b32385bd0a1ba7310b8c83fae72963bbc3295 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Ruiz-L=C3=B3pez?= Date: Tue, 4 Dec 2018 10:48:58 +0100 Subject: [PATCH] Translate chapter 3.9 into Swift --- 3.9-algebras-for-monads.md | 149 +++++++++++++++++++++++++++++++++++++ 1 file changed, 149 insertions(+) create mode 100644 3.9-algebras-for-monads.md diff --git a/3.9-algebras-for-monads.md b/3.9-algebras-for-monads.md new file mode 100644 index 0000000..c69c850 --- /dev/null +++ b/3.9-algebras-for-monads.md @@ -0,0 +1,149 @@ +```Haskell +alg . return = id +alg . join = alg . fmap alg +``` +```swift +compose(alg, monad.pure) = id +compose(alg, monad.flatten) = compose(alg, monad.lift(alg)) +``` +................ +```Haskell +foldr :: (a -> a -> a) -> a -> [a] -> a +``` +```swift +func foldr(_ f : @escaping (A, A) -> A, _ z : A, _ l : [A]) -> A +``` +................ +```Haskell +foldr f z [x] = x `f` z +``` +```swift +func foldr(_ f : @escaping (A, A) -> A, _ z : A, _ l : [A]) -> A { + return f(l[0], z) +} +``` +................ +```Haskell +x `f` z = x +``` +```swift +f(x, z) == x +``` +................ +```Haskell +h = join . fmap g . f +``` +```swift +h == compose(monad.flatten, compose(monad.lift(g), f)) +``` +................ +```Haskell +return . f +``` +```swift +compose(pure, f) +``` +................ +```Haskell +data Store s a = Store (s -> a) s +``` +```swift +class Store : Kind2 { + let run : (S) -> A + let s : S + + init(_ run : @escaping (S) -> A, _ s : S) { + self.run = run + self.s = s + } +} +``` +................ +```Haskell +set :: a -> s -> a +get :: a -> s +``` +```swift +func set(_ a : A, _ s : S) -> A +func get(_ a : A) -> S +``` +................ +```Haskell +coalgs a = Store (set a) (get a) +``` +```swift +func coalg(_ a : A) -> Store { + return Store({ s in set(a, s) }, get(a)) +} +``` +................ +```Haskell +instance Comonad (Store s) where + extract (Store f s) = f s + duplicate (Store f s) = Store (Store f) s +``` +```swift +class StoreComonad : Comonad { + typealias F = Kind + + func extract(_ wa : Kind2) -> A { + let store = Store.fix(wa) + return store.run(store.s) + } + + func duplicate(_ wa : Kind2) -> Kind2> { + let store = Store.fix(wa) + return Store>({ s in Store(store.run, s) }, store.s) + } +} +``` +................ +```Haskell +fmap g (Store f s) = Store (g . f) s +``` +```swift +functor.map(Store(f, s), g) == Store(compose(g, f), s) +``` +................ +```Haskell +Store (coalg . set a) (get a) +``` +```swift +Store(compose(coalg, { s in set(a, s) }), get(a)) +``` +................ +```Haskell +Store (Store (set a)) (get a) +``` +```swift +Store(Store({ s in set(a, s)), get(a)) +``` +................ +```Haskell +coalg (set a s) = Store (set a) s +``` +```swift +coalg(set(a, s)) == Store({ s in set(a, s) }, s) +``` +................ +```Haskell +Store (set (set a s)) (get (set a s)) = Store (set a) s +``` +```scala +Store({ s1 in set(set(a, s), s1) }, get(set(a, s))) == + Store({ s in set(a, s) }, s) +``` +................ +```Haskell +set (set a s) = set a +``` +```swift +{ s1 in set(set(a, s), s1)} == { s1 in set(a, s1) } +``` +................ +```Haskell +get (set a s) = s +``` +```swift +get(set(a, s)) == s +```