6
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Lensの-erは-ingの一般化である(Getter,Setter)

6
Last updated at Posted at 2017-08-13

 最近lensの各型を調べていて、その理解のうちの1つの関門となったのが、
Lens型のうちGettingとGetter、SettingとSetterの関係だった。

定義をHackageで眺めていたらふと、その2つの**-erが-ing**の一般化になっていることに気づいた。

なぜ一般化されているのかはまだわかってない :confused:

このあたりはどこかの論文を読めば書いてあることなのかもしれないけど、僕は論文を読んでいない……。

GetterはGettingの一般化

 まずは定義をば。

type Getting r s a = (a -> Const r a) -> s -> Const r s
type Getter s a = forall f. (Contravariant f, Functor f) => (a -> f a) -> s -> f s

 GettingがGetterのfを(rを外に逃して)Const rに固定した型になっている。

(簡略) Getting :: (a -> Const r a) -> s -> Const r s
(簡略) Getter  :: (a -> f       a) -> s -> f       s

 Const rはContravariantFunctor Functorなので、fに当てはまる。

instance Contravariant (Const a)
instance Functor (Const a)

ということはGetterはGettingの一般化である。

SetterはSettingの一般化

 定義はこのようになっている。

type Setting p s t a b = p a (Identity b) -> s -> Identity t
type Setter s t a b = forall f. Settable f => (a -> f b) -> s -> f t

SettingはGettingと1つだけ違って、(->)がpに一般化されている。
pを(->)に特殊化すると、GettingのConst rをIdentityに置き換えた型になる。

type Setting p s t a b = p    a (Identity b) -> s -> Identity t
(特殊化)             ==> (->) a (Identity b) -> s -> Identity t
(簡約)               ==> (a ->   Identity b) -> s -> Identity t

 Settableはこのようになっているので

class (Applicative f, Distributive f, Traversable f) => Settable f where
  untainted :: f a -> a
  untaintedDot :: Profunctor p => p a (f b) -> p a b
  taintedDot :: Profunctor p => p a b -> p a (f b)

SettableはApplicative Distributive Traversable (functor) だと解釈できる。

type Setter s t a b = forall f. (Applicative f, Distributive f, Traversable f) => (a -> f b) -> s -> f t

 (->)はpで、

data (->) :: * -> * -> *

IdentityはApplicative Distributive Traversableなので

instance Applicative Identity
instance Distributive Identity
instance Traversable Identity

SetterはSettingの一般化である。

追伸

 ここのドキュメントコメントが、なぜ**-ingを-er**に一般化するのかのヒントになるかもしれない。

Anything Settable must be isomorphic to the Identity Functor.

訳) あらゆるSettableはIdentity Functorと同型でなければいけない。

この「同型」は圏論的意味だと思う。

6
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?