什么是商店comonad? [英] What is the Store comonad?

查看:126
本文介绍了什么是商店comonad?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Haskell中的Comonad类型类型有一些想法 ,我听说过Store的comonad。但看看 Control.Comonad.Store.Lazy ,我真的不明白。这是什么意思?它是为了什么?我听说Store = CoState是State Monad的双重身份。这意味着什么?

解决方案

如果您看一下 StoreT本身



您可以将其视为更大结构中的地点。例如,镜头只是 a - >存储b a ;您将获得 b 字段的值,并且函数 b - >一个把一个新值放回到更大的上下文中。



考虑到它的简化,非变换形式:

data Store sa = Store(s - > a)s

实例Functor Store s)其中
fmap f(Store gs)= Store(f。g)s

实例Extend(Store s)其中
重复(Store fs)= Store(Store f)s

实例Comonad(商店)其中
提取(商店fs)= fs

重复更改 s - > a 转换为 s - >在替换值后存储sa ,它只返回更新的地方, extract 通过放置恢复原始的 a 价值回到更大的结构。

就它与国家的关系而言,你可以这样看待它:

  type State sa = s  - > (a,s)
类型商店s a =(s - > a,s)


Having some idea of what the Comonad typeclass is in Haskell, I've heard about the Store comonad. But looking at Control.Comonad.Store.Lazy, I don't really get it. What does it mean? What is it for? I've heard that Store = CoState, the dual of the State Monad. What does that mean?

解决方案

It's much easier if you look at the definition of StoreT itself.

You can think of it as a "place" in a larger structure. For instance, a lens is just a -> Store b a; you get the value of the b field, and a function b -> a to put a new value back into the larger context.

Considering it in its simplified, non-transformer form:

data Store s a = Store (s -> a) s

instance Functor (Store s) where
  fmap f (Store g s) = Store (f . g) s

instance Extend (Store s) where
  duplicate (Store f s) = Store (Store f) s

instance Comonad (Store s) where
  extract (Store f s) = f s

i.e. duplicate changes the s -> a into an s -> Store s a that just returns the "updated" place after replacing the value, and extract restores the original a by placing the value back into the larger structure.

As far as its relation to State goes, you could look at it like this:

type State s a = s -> (a, s)
type Store s a = (s -> a, s)

这篇关于什么是商店comonad?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆