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.10 into Swift #52

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
307 changes: 307 additions & 0 deletions 3.10-ends-and-coends.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,307 @@
```Haskell
class Profunctor p where
dimap :: (c -> a) -> (b -> d) -> p a b -> p c d
```
```swift
protocol Profunctor {
associatedtype P

func dimap<A, B, C, D>(_ pab : Kind2<P, A, B>, _ f : @escaping (C) -> A, _ g : @escaping (B) -> D) -> Kind2<P, C, D>
}
```
................
```Haskell
dimap f id pbb :: p a b
```
```swift
dimap(pbb, f, id) : Kind2<P, A, B>
```
................
```Haskell
dimap id f paa :: p a b
```
```swift
dimap(paa, id, f) : Kind2<P, A, B>
```
................
```Haskell
dimap id f . alpha = dimap f id . alpha
```
```swift
compose({ pab in dimap(pab, id, f)}, alpha) ==
{ pab in dimap(pab, f, compose(id, alpha)) }
```
................
```Haskell
forall a. p a a
```
```swift
// No Rank-N types in Swift
// We have to introduce polymorphic function
protocol PolyFunction1 {
associatedtype P

func apply<A>() -> Kind2<P, A, A>
}
```
................
```Haskell
dimap f id . pi = dimap id f . pi
```
```swift
compose({ pab in dimap(pab, f, id) }, pi.apply) ==
{ pab in dimap(pab, id, compose(f, pi.apply))
```
................
```Haskell
dimap f idb . pib = dimap ida f . pia
```
```swift
compose({ pab in dimap(pab, f, id) }, pi_b.apply) ==
compose({ pab in dimap(pab, id, )}, pi_a.apply)
```
................
```Haskell
Profunctor p => (forall c. p c c) -> p a b
```
```swift
func side<P, A, B, ProfunctorP, PolyFP>(_ f : PolyFP, _ profunctor : ProfunctorP) -> Kind<P, A, B> where
PolyFP : PolyFunction1, PolyFP.P == P,
ProfunctorP : Profunctor, ProfunctorP.P == P
```
................
```Haskell
pi :: Profunctor p => forall c. (forall a. p a a) -> p c c
pi e = e
```
```Swift
protocol PolyFunction2 {
associatedtype P

func apply<C, PolyFP>(_ in : PolyFP) -> Kind2<P, C, C> where PolyFP : PolyFunction1, PolyFP.P == P
}

class BasePolyFunction2<P> : PolyFunction2 {
func apply<C, PolyFP>(_ in : PolyFP) -> Kind2<P, C, C> where PolyFP : PolyFunction1, PolyFP.P == P {
return in.apply()
}
}

func pi<P, ProfunctorP>(_ profunctor : ProfunctorP) -> BasePolyFunction2 {
return BasePolyFunction2<P>()
}
```
................
```Haskell
lambda :: Profunctor p => p a a -> (a -> b) -> p a b
lambda paa f = dimap id f paa

rho :: Profunctor p => p b b -> (a -> b) -> p a b
rho pbb f = dimap f id pbb
```
```swift
func lambda<P, A, B, ProfunctorP>(_ paa : Kind2<P, A, A>, _ f : @escaping (A) -> B, _ profunctor : ProfunctorP) -> Kind2<P, A, B> where
ProfunctorP : Profunctor, ProfunctorP.P == P {
return profunctor.dimap(paa, id, f)
}

func lambda<P, A, B, ProfunctorP>(_ pbb : Kind2<P, B, B>, _ f : @escaping (A) -> B, _ profunctor : ProfunctorP) -> Kind2<P, A, B> where
ProfunctorP : Profunctor, ProfunctorP.P == P {
return profunctor.dimap(pbb, f, id)
}
```
................
```Haskell
type ProdP p = forall a b. (a -> b) -> p a b
```
```swift
protocol ProdP {
associatedtype P

func apply<A, B>(_ f : @escaping (A) -> B) -> Kind2<P, A, B>
}
```
................
```Haskell
newtype DiaProd p = DiaProd (forall a. p a a)
```
```swift
class DiaProd<PolyFP, P> where PolyFP : PolyFunction1, PolyFP.P == P {
let paa : PolyFP
}
```
................
```Haskell
lambdaP :: Profunctor p => DiaProd p -> ProdP p
lambdaP (DiaProd paa) = lambda paa

rhoP :: Profunctor p => DiaProd p -> ProdP p
rhoP (DiaProd paa) = rho paa
```
```swift
class LambdaProdP<P, ProfunctorP, PolyFP> : ProdP where
ProfunctorP : Profunctor, ProfunctorP.P == P,
PolyFP : PolyFunction1, PolyFP.P == P {

let profunctor : ProfunctorP
let dia : DiaProd<PolyFP, P>

init(_ profunctor : ProfunctorP, _ dia : DiaProd<PolyFP, P>) {
self.profunctor = profunctor
self.dia = dia
}

func apply<A, B>(_ f : @escaping (A) -> B) -> Kind2<P, A, B> {
return lambda(dia.paa, f, profunctor)
}
}

func lambdaP<P, ProfunctorP, PolyFP>(_ profunctor : ProfunctorP) -> (DiaProd<PolyFP, P>) -> LambdaProdP<P> {
return { dia in LambdaProdP(profunctor, dia)}
}

class RhoProdP<P, ProfunctorP, PolyFP> : ProdP where
ProfunctorP : Profunctor, ProfunctorP.P == P,
PolyFP : PolyFunction1, PolyFP.P == P {

let profunctor : ProfunctorP
let dia : DiaProd<PolyFP, P>

init(_ profunctor : ProfunctorP, _ dia : DiaProd<PolyFP, P>) {
self.profunctor = profunctor
self.dia = dia
}

func apply<A, B>(_ f : @escaping (A) -> B) -> Kind2<P, A, B> {
return rho(dia.paa, f, profunctor)
}
}
```
................
```Haskell
forall a. f a -> g a
```
```swift
protocol FunctionK {
associatedtype F
associatedtype G

func apply<A>(_ f : Kind<F, A>) -> Kind<G, A>
}
```
................
```Haskell
exists a. p a a
```
```swift
Kind2<P, A, A>
```
................
```Haskell
data Coend p = forall a. Coend (p a a)
```
```swift
protocol Coend {
associatedtype P

func paa<A>() -> Kind2<P, A, A>
}
```
................
```Haskell
data SumP p = forall a b. SumP (b -> a) (p a b)
```
```swift
protocol SumP {
associatedtype P

func f<A, B>(_ b : B) -> A
func pab<A, B>() -> Kind2<P, A, B>
}
```
................
```Haskell
data DiagSum p = forall a. DiagSum (p a a)
```
```swift
protocol DiagSum {
associatedtype P

func paa<A>() -> Kind2<P, A, A>
}
```
................
```Haskell
lambda, rho :: Profunctor p => SumP p -> DiagSum p
lambda (SumP f pab) = DiagSum (dimap f id pab)
rho (SumP f pab) = DiagSum (dimap id f pab)
```
```swift
class LambdaDiagSum<P, ProfunctorP, ASumP> : DiagSum where
ProfunctorP : Profunctor, ProfunctorP.P == P,
ASumP : SumP, ASumP.P == P {

let profunctor : ProfunctorP
let sump : ASumP

init(_ profunctor : ProfunctorP, _ sump : ASumP) {
self.profunctor = profunctor
self.sump = sump
}

func paa<A>() -> Kind2<P, A, A> {
return profunctor.dimap(sump.pab, sump.f, id)
}
}

func lambda<P, ProfunctorF, ASumP>(_ sum : ASumP, _ profunctor : ProfunctorP) -> LambdaDiagSum<P> {
return LambdaDiagSum(profunctor, sum)
}

class RhoDiagSum<P, ProfunctorP, ASumP> : DiagSum where
ProfunctorP : Profunctor, ProfunctorP.P == P,
ASumP : SumP, ASumP.P == P {

let profunctor : ProfunctorP
let sump : ASumP

init(_ profunctor : ProfunctorP, _ sump : ASumP) {
self.profunctor = profunctor
self.sump = sump
}

func paa<A>() -> Kind2<P, A, A> {
return profunctor.dimap(sump.pab, id, sump.f)
}
}

func rho<P, ProfunctorF, ASumP>(_ sum : ASumP, _ profunctor : ProfunctorP) -> RhoDiagSum<P> {
return RhoDiagSum(profunctor, sum)
}
```
................
```Haskell
(exists x. p x x) -> c ≅ forall x. p x x -> c
```
```swift
(Kind2<P, A, A>) -> C ≅ (BasePolyFunction1<P>) -> C
```
................
```Haskell
data Procompose q p a b where
Procompose :: q a c -> p c b -> Procompose q p a b
```
```swift
class Procompose<Q, P, A, B> {}

func apply<Q, P, A, B, C>(_ qac : Kind2<Q, A, C>, _ pcb : Kind2<Q, C, B>) -> Procompose<Q, P, A, B> {
...
}
```
................
```Haskell
exists c. (q a c, p c b)
```
```swift
(Kind2<Q, A, C>, Kind2<P, C, B>)
```