怎么就自然转变了呢?
How to it is a natural transformation?
我将使用很棒的库 https://tpolecat.github.io/doobie/,它功能齐全。
我正在经历 first example 我已经认识到:
A Transactor is a data type that knows how to connect to a database,
hand out connections, and clean them up; and with this knowledge it
can transform ConnectionIO ~> IO
, which gives us a program we can run.
ConnectionIO ~> IO
是范畴论中的自然变换,但一直没有完全理解,究竟什么是自然变换。
但是,我知道这是从一个类别到另一个类别的转换。例如:
F[A] ~> G[A]
是从类别 F
到 G
的自然转换,无需更改内容。
不是所有东西都可以自然转换,问题是,图书馆 doobie 的作者怎么知道,他可以从 ConnectionIO ~> IO
进行自然转换?
I know it [natural transformation] is transformation from one category into other category.
实际上没有。从类别到类别的转换是函子(它将对象映射到对象,将态射映射到态射)。自然变换是从函子到函子的变换(即它是函子范畴中的态射)。
Scala 中的类型类别是 category. Its objects are types, its morphisms 函数(不是函数类型)。
例如List
和Option
是functors。它们将对象映射到对象(类型 A
到类型 List[A]
,类型 A
到类型 Option[A]
)和态射到态射(函数 f: A => B
到函数 _.map(f) : List[A] => List[B]
, 函数 f: A => B
到函数 _.map(f) : Option[A] => Option[B]
).
例如 headOption
是 natural transformation (List ~> Option
)
val headOption: (List ~> Option) = new (List ~> Option) {
def apply[A](as: List[A]): Option[A] = as.headOption
}
或多蒂
val headOption: [A] => List[A] => Option[A] =
[A] => (as: List[A]) => as.headOption
存在不断发展的抽象序列:
- 类别(及其对象和态射),
- 态射范畴(它的对象是态射,它的态射是交换平方),
- 类别的类别(它的对象是类别,它的态射是函子),
- 函子的范畴(它的对象是函子,它的态射是自然变换),
- ...
https://github.com/hmemcpy/milewski-ctfp-pdf/releases/tag/v1.3.0
https://www.youtube.com/playlist?list=PLbgaMIhjbmEnaH_LTkxLI7FMa2HsnawM_
Not everything can be naturally transformed and the question is, how
do the author of library doobie know, that he can do the natural
transformation from ConnectionIO ~> IO
?
实际上,如果您有映射家族 ConnectionIO[A] => IO[A]
(A
遍历所有类型)并且这个家族 是使用参数多态性 定义的(而不是广告-hoc 多态性,即类型 类,即在类型 A
) = parametricity 上没有额外假设的情况下定义,然后自然性来自参数 "for free"。这是 "theorems for free"
之一
https://bartoszmilewski.com/2014/09/22/parametricity-money-for-nothing-and-theorems-for-free/
https://www.reddit.com/r/haskellquestions/comments/6fkufo/free_theorems/
https://ttic.uchicago.edu/~dreyer/course/papers/wadler.pdf
Good introduction to free theorems
我将使用很棒的库 https://tpolecat.github.io/doobie/,它功能齐全。
我正在经历 first example 我已经认识到:
A Transactor is a data type that knows how to connect to a database, hand out connections, and clean them up; and with this knowledge it can transform
ConnectionIO ~> IO
, which gives us a program we can run.
ConnectionIO ~> IO
是范畴论中的自然变换,但一直没有完全理解,究竟什么是自然变换。
但是,我知道这是从一个类别到另一个类别的转换。例如:
F[A] ~> G[A]
是从类别 F
到 G
的自然转换,无需更改内容。
不是所有东西都可以自然转换,问题是,图书馆 doobie 的作者怎么知道,他可以从 ConnectionIO ~> IO
进行自然转换?
I know it [natural transformation] is transformation from one category into other category.
实际上没有。从类别到类别的转换是函子(它将对象映射到对象,将态射映射到态射)。自然变换是从函子到函子的变换(即它是函子范畴中的态射)。
Scala 中的类型类别是 category. Its objects are types, its morphisms 函数(不是函数类型)。
例如List
和Option
是functors。它们将对象映射到对象(类型 A
到类型 List[A]
,类型 A
到类型 Option[A]
)和态射到态射(函数 f: A => B
到函数 _.map(f) : List[A] => List[B]
, 函数 f: A => B
到函数 _.map(f) : Option[A] => Option[B]
).
例如 headOption
是 natural transformation (List ~> Option
)
val headOption: (List ~> Option) = new (List ~> Option) {
def apply[A](as: List[A]): Option[A] = as.headOption
}
或多蒂
val headOption: [A] => List[A] => Option[A] =
[A] => (as: List[A]) => as.headOption
存在不断发展的抽象序列:
- 类别(及其对象和态射),
- 态射范畴(它的对象是态射,它的态射是交换平方),
- 类别的类别(它的对象是类别,它的态射是函子),
- 函子的范畴(它的对象是函子,它的态射是自然变换),
- ...
https://github.com/hmemcpy/milewski-ctfp-pdf/releases/tag/v1.3.0
https://www.youtube.com/playlist?list=PLbgaMIhjbmEnaH_LTkxLI7FMa2HsnawM_
Not everything can be naturally transformed and the question is, how do the author of library doobie know, that he can do the natural transformation from
ConnectionIO ~> IO
?
实际上,如果您有映射家族 ConnectionIO[A] => IO[A]
(A
遍历所有类型)并且这个家族 是使用参数多态性 定义的(而不是广告-hoc 多态性,即类型 类,即在类型 A
) = parametricity 上没有额外假设的情况下定义,然后自然性来自参数 "for free"。这是 "theorems for free"
https://bartoszmilewski.com/2014/09/22/parametricity-money-for-nothing-and-theorems-for-free/
https://www.reddit.com/r/haskellquestions/comments/6fkufo/free_theorems/
https://ttic.uchicago.edu/~dreyer/course/papers/wadler.pdf
Good introduction to free theorems