Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Translate chapter 3.9 into Swift #51

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
149 changes: 149 additions & 0 deletions 3.9-algebras-for-monads.md
Original file line number Diff line number Diff line change
@@ -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<A>(_ f : @escaping (A, A) -> A, _ z : A, _ l : [A]) -> A
```
................
```Haskell
foldr f z [x] = x `f` z
```
```swift
func foldr<A>(_ 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<S, A> : Kind2<ForStore, S, A> {
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, S>(_ a : A, _ s : S) -> A
func get<A, S>(_ a : A) -> S
```
................
```Haskell
coalgs a = Store (set a) (get a)
```
```swift
func coalg<A, S>(_ a : A) -> Store<S, A> {
return Store<S, A>({ 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<S> : Comonad {
typealias F = Kind<ForStore, S>

func extract<A>(_ wa : Kind2<ForStore, S, A>) -> A {
let store = Store<S, A>.fix(wa)
return store.run(store.s)
}

func duplicate<A>(_ wa : Kind2<ForStore, S, A>) -> Kind2<ForStore, S, Kind2<ForStore, S, A>> {
let store = Store<S, A>.fix(wa)
return Store<S, Store<S, A>>({ 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
```