• Haskell语言学习笔记(51)Comonad


    Comonad

    class Functor w => Comonad w where
      extract :: w a -> a
    
      duplicate :: w a -> w (w a)
      duplicate = extend id
    
      extend :: (w a -> b) -> w a -> w b
      extend f = fmap f . duplicate
    

    Comonad 是个类型类。

    比较 Monad 和 Comonad

    class Functor m => Monad m where
      return :: a -> m a
      bind :: (a -> m b) -> (m a -> m b)
      join :: m (m a) -> m a
    
      join = bind id
      bind f = fmap f . join
      
      (>>=) :: m a -> (a -> m b) -> m b
      (>>=) = flip bind
    
    
    class Functor w => Comonad w where
      extract :: w a -> a
      extend :: (w a -> b) -> w a -> w b
      duplicate :: w a -> w (w a)
    
      duplicate = extend id
      extend f = fmap f . duplicate
      
      (=>>) :: w b -> (w b -> a) -> a
      (=>>) = flip extend
    

    ((,) e) 是个 Comonad

    也称作 CoReader(Env) Comonad。

    instance Comonad ((,)e) where
      duplicate p = (fst p, p)
      extract = snd
    
    Prelude Control.Comonad> duplicate (3,4)
    (3,(3,4))
    Prelude Control.Comonad> extract (3,4)
    4
    Prelude Control.Comonad> extend fst (3,4)
    (3,3)
    Prelude Control.Comonad> extend snd (3,4)
    (3,4)
    

    ((->)m) 是个 Comonad

    也称作 CoWriter(Traced) Comonad。

    instance Monoid m => Comonad ((->)m) where
      duplicate f m = f . mappend m
      extract f = f mempty
    

    参考链接

    Haskell for all: Comonads are objects
    Comonads in Haskell

  • 相关阅读:
    pat 1034 Head of a Gang (30分)
    pta坑点
    Devc++ 编译 c++11
    Invitation Cards dijkstra法
    Cube Stacking
    畅通工程
    蚂蚁上树
    洛谷 P1439 【模板】最长公共子序列
    Recursive sequence
    A Simple Math Problem
  • 原文地址:https://www.cnblogs.com/zwvista/p/7887111.html
Copyright © 2020-2023  润新知