SlideShare ist ein Scribd-Unternehmen logo
1 von 30
Downloaden Sie, um offline zu lesen
Comonads: what are they and what can you do with them?
Melbourne Haskell Users Group
David Overton
29 May 2014
Table of Contents
1 Motivation
2 Theory
3 Examples
4 Applications
5 Other Considerations
6 Further Reading
Motivation
• Monads are an abstract concept from category theory, have turned out to be
surprisingly useful in functional programming.
• Category theory also says that there exists a dual concept called comonads. Can
they be useful too?
• Intuition:
• Monads abstract the notion of effectful computation of a value.
• Comonads abstract the notion of a value in a context.
• “Whenever you see large datastructures pieced together from lots of small but
similar computations there’s a good chance that we’re dealing with a comonad.”
—Dan Piponi
Table of Contents
1 Motivation
2 Theory
3 Examples
4 Applications
5 Other Considerations
6 Further Reading
What is a Comonad?
• A comonad is just a comonoid in the category of endofunctors. . .
• A comonad is the category theoretic dual of a monad.
• A comonad is a monad with the “arrows” reversed.
What is a Comonad?
Both monads and comonads are functors. (Functor is its own dual.)
class Functor f where
fmap :: (a → b) → (f a → f b)
class Functor m ⇒ Monad m where class Functor w ⇒ Comonad w where
return :: a → m a extract :: w a → a
bind :: (a → m b) → (m a → m b) extend :: (w b → a) → (w b → w a)
join :: m (m a) → m a duplicate :: w a → w (w a)
join = bind id duplicate = extend id
bind f = fmap f ◦ join extend f = fmap f ◦ duplicate
(>>=) :: m a → (a → m b) → m b (=>>) :: w b → (w b → a) → w a
(>>=) = flip bind (=>>) = flip extend
Intuition
• Monadic values are typically produced in effectful computations:
a → m b
• Comonadic values are typically consumed in context-sensitive computations:
w a → b
Monad/comonad laws
Monad laws
Left identity return ◦ bind f = f
Right identify bind return = id
Associativity bind f ◦ bind g = bind (f ◦ bind g)
Comonad laws
Left identity extract ◦ extend f = f
Right identity extend extract = id
Associativity extend f ◦ extend g = extend (f ◦ extend g)
Table of Contents
1 Motivation
2 Theory
3 Examples
4 Applications
5 Other Considerations
6 Further Reading
Example: reader/writer duality
-- Reader monad
instance Monad ((→) e) where
return = const
bind f r = λc → f (r c) c
-- CoReader (a.k.a. Env) comonad
instance Comonad ((,) e) where
extract = snd
extend f w = (fst w, f w)
-- Writer monad
instance Monoid e ⇒ Monad ((,) e)
where
return = ((,) mempty)
bind f (c, a) = (c ♦ c’, a’)
where (c’, a’) = f a
-- CoWriter (a.k.a. Traced) comonad
instance Monoid e ⇒ Comonad ((→) e)
where
extract m = m mempty
extend f m = λc →
f (λc’ → m (c ♦ c’))
Example: state
newtype State s a = State { runState :: s → (a, s) }
instance Monad (State s) where
return a = State $ λs → (a, s)
bind f (State g) = State $ λs →
let (a, s’) = g s
in runState (f a) s’
data Store s a = Store (s → a) s -- a.k.a. ‘‘Costate’’
instance Comonad (Store s) where
extract (Store f s) = f s
extend f (Store g s) = Store (f ◦ Store g) s
One definition of Lens:
type Lens s a = a → Store s a
Hence the statement that lenses are “the coalgebras of the costate comonad”.
Example: stream comonad
data Stream a = Cons a (Stream a)
instance Functor Stream where
fmap f (Cons x xs) = Cons (f x) (fmap f xs)
instance Comonad Stream where
extract (Cons x ) = x
duplicate xs@(Cons xs’) = Cons xs (duplicate xs’)
extend f xs@(Cons xs’) = Cons (f xs) (extend f xs’)
• extract = head, duplicate = tails.
• extend extends the function f :: Stream a → b by applying it to all tails of
stream to get a new Stream b.
• extend is kind of like fmap, but instead of each call to f having access only to a
single element, it has access to that element and the whole tail of the list from
that element onwards, i.e. it has access to the element and a context.
Example: list zipper
data Z a = Z [a] a [a]
left, right :: Z a → Z a
left (Z (l:ls) a rs) = Z ls l (a:rs)
right (Z ls a (r:rs)) = Z (a:ls) r rs
instance Functor Z where
fmap f (Z l a r) = Z (fmap f l) (f a) (fmap f r)
iterate1 :: (a → a) → a → [a]
iterate1 f = tail ◦ iterate f
instance Comonad Z where
extract (Z a ) = a
duplicate z = Z (iterate1 left z) z (iterate1 right z)
extend f z = Z (fmap f $ iterate1 left z) (f z)
(fmap f $ iterate1 right z)
Example: list zipper (cont.)
• A zipper for a data structure is a transformed structure which gives you a focus
element and a means of stepping around the structure.
• extract returns the focused element.
• duplicate returns a zipper where each element is itself a zipper focused on the
corresponding element in the original zipper.
• extend is kind of like fmap, but instead of having access to just one element, each
call to f has access to the entire zipper focused at that element. I.e. it has the
whole zipper for context.
• Compare this to the Stream comonad where the context was not the whole
stream, but only the tail from the focused element onwards.
• It turns out that every zipper is a comonad.
Example: array with context
data CArray i a = CA (Array i a) i
instance Ix i ⇒ Functor (CArray i) where
fmap f (CA a i) = CA (fmap f a) i
instance Ix i ⇒ Comonad (CArray i) where
extract (CA a i) = a ! i
extend f (CA a i) =
let es’ = map (λj → (j, f (CA a j))) (indices a)
in CA (array (bounds a) es’) i
• CArray is basically a zipper for Arrays.
• extract returns the focused element.
• extend provides the entire array as a context.
Table of Contents
1 Motivation
2 Theory
3 Examples
4 Applications
5 Other Considerations
6 Further Reading
Application: 1-D cellular automata – Wolfram’s rules
rule :: Word8 → Z Bool → Bool
rule w (Z (a: ) b (c: )) = testBit w (sb 2 a .|. sb 1 b .|. sb 0 c) where
sb n b = if b then bit n else 0
move :: Int → Z a → Z a
move i u = iterate (if i < 0 then left else right) u !! abs i
toList :: Int → Int → Z a → [a]
toList i j u = take (j - i) $ half $ move i u where
half (Z b c) = b : c
testRule :: Word8 → IO ()
testRule w = let u = Z (repeat False) True (repeat False)
in putStr $ unlines $ take 20 $
map (map (λx → if x then ’#’ else ’ ’) ◦ toList (-20) 20) $
iterate (=>> rule w) u
Application: 2-D cellular automata – Conway’s Game of Life
data Z2 a = Z2 (Z (Z a))
instance Functor Z2 where
fmap f (Z2 z) = Z2 (fmap (fmap f) z)
instance Comonad Z2 where
extract (Z2 z) = extract (extract z)
duplicate (Z2 z) = fmap Z2 $ Z2 $ roll $ roll z where
roll a = Z (iterate1 (fmap left) a) a (iterate1 (fmap right) a)
Application: 2-D cellular automata – Conway’s Game of Life
countNeighbours :: Z2 Bool → Int
countNeighbours (Z2 (Z
(Z (n0: ) n1 (n2: ): )
(Z (n3: ) (n4: ))
(Z (n5: ) n6 (n7: ): ))) =
length $ filter id [n0, n1, n2, n3, n4, n5, n6, n7]
life :: Z2 Bool → Bool
life z = (a && (n == 2 | | n == 3))
| | (not a && n == 3) where
a = extract z
n = countNeighbours z
Application: image processing
laplace2D :: CArray (Int, Int) Float → Float
laplace2D a = a ? (-1, 0)
+ a ? (0, 1)
+ a ? (0, -1)
+ a ? (1, 0)
- 4 ∗ a ? (0, 0)
(?) :: (Ix i, Num a, Num i) ⇒ CArray i a → i → a
CA a i ? d = if inRange (bounds a) (i + d) then a ! (i + d) else 0
• laplace2D computes the Laplacian at a single context, using the focused element
and its four nearest neighbours.
• extend laplace2D computes the Laplacian for the entire array.
• Output of extend laplace2D can be passed to another operator for further
processing.
Application: Env (CoReader) for saving and reverting to an initial value
type Env e a = (e, a)
ask :: Env e a → e
ask = fst
local :: (e → e’) → Env e a → Env e’ a
local f (e, a) = (f e, a)
initial = (n, n) where n = 0
experiment = fmap (+ 10) initial
result = extract experiment
initialValue = extract (experiment =>> ask)
Other applications of comonads
• Signal processing: using a stream comonad.
• Functional reactive programming: it has been postulated (e.g. by Dan Piponi,
Conal Elliott) that some sort of “causal stream” comonad should work well for
FRP, but there don’t yet seem to be any actual implementations of this.
• Gabriel Gonzalez’s three examples of “OO” design patterns:
• The Builder pattern: using CoWriter / Traced to build an “object” step-by-step.
• The Iterator pattern: using Stream to keep a history of events in reverse
chronological order.
• The Command pattern: using Store to represent an “object” with internal state.
Table of Contents
1 Motivation
2 Theory
3 Examples
4 Applications
5 Other Considerations
6 Further Reading
Syntactic sugar
At least two different proposals for a comonadic equivalent of do notation for
comonads:
• Gonzalez’s method notation – “OOP-like” with this keyword representing the
argument of the function passed to extend.
• Orchard & Mycroft’s codo notation – resembles Paterson’s arrow notation.
Unsugared Gonzalez Orchard & Mycroft
λwa →
let wb = extend (λthis → expr1) wa
wc = extend (λthis → expr2) wb
in (λthis → expr3) wc
method
wa> expr1
wb> expr2
wc> expr3
codo wa ⇒
wb ← λthis → expr1
wc ← λthis → expr2
λthis → expr3
λwa →
let wb = extend func1 wa
wc = extend func2 wb
in func3 wc
method
wa> func1 this
wb> func2 this
wc> func3 this
codo wa ⇒
wb ← func1
wc ← func2
func3
Comonad transformers
-- Monad transformers
class MonadTrans t where
lift :: Monad m ⇒ m a → t m a
-- Comonad transformers
class ComonadTrans t where
lower :: Comonad w ⇒ t w a → w a
The comonad package provides a few standard transformers:
• EnvT – analogous to ReaderT
• StoreT – analogous to StateT
• TracedT – analogous to WriterT
Cofree comonads
-- Free monad
data Free f a = Pure a | Free (f (Free f a))
instance Functor f ⇒ Monad (Free f) where
return = Pure
bind f (Pure a) = f a
bind f (Free r) = Free (fmap (bind f) r)
-- Cofree comonad
data Cofree f a = Cofree a (f (Cofree f a))
instance Functor f ⇒ Comonad (Cofree f) where
extract (Cofree a ) = a
extend f w@(Cofree r) = Cofree (f w) (fmap (extend f) r)
• Cofree Identity is an infinite stream.
• Cofree Maybe is a non-empty list.
• Cofree [] is a rose tree.
A bit of category theory
-- Kleisli category identity and composition (monads)
return :: Monad m ⇒ a → m a
(>=>) :: Monad m ⇒ (a → m b) → (b → m c) → (a → m c)
f >=> g = λa → f a >>= g
-- Co-Kleisli category identity and composition (comonads)
extract :: Comonad w ⇒ w a → a
(=>=) :: Comonad w ⇒ (w a → b) → (w b → c) → (w a → c)
f =>= g = λw → f w =>> g
• Each monad has a corresponding Kleisli category with morphisms a → m b,
identity return and composition operator (>=>).
• Each comonad has a corresponding Co-Kleisli category with morphisms w a → b,
identity extract and composition operator (=>=).
Category laws
Monad laws
Left identity return >=> f = f
Right identify f >=> return = f
Associativity (f >=> g) >=> h = f >=> (g >=> h)
Comonad laws
Left identity extract =>= f = f
Right identify f =>= extract = f
Associativity (f =>= g) =>= h = f =>= (g =>= h)
Category laws
Left identity id ◦ f = f
Right identify f ◦ id = f
Associativity (f ◦ g) ◦ h = f ◦ (g ◦ h)
Table of Contents
1 Motivation
2 Theory
3 Examples
4 Applications
5 Other Considerations
6 Further Reading
Further reading
• http://blog.sigfpe.com/2006/12/evaluating-cellular-automata-is.html
• http://blog.sigfpe.com/2008/03/comonadic-arrays.html
• http://blog.sigfpe.com/2008/03/transforming-comonad-with-monad.html
• http://blog.sigfpe.com/2014/05/cofree-meets-free.html
• https://www.fpcomplete.com/user/edwardk/cellular-automata
• http://www.jucs.org/jucs_11_7/signals_and_comonads/jucs_11_7_1311_1327_
vene.pdf
• http://www.haskellforall.com/2013/02/you-could-have-invented-
comonads.html
• http://www.cl.cam.ac.uk/~dao29/publ/codo-notation-orchard-ifl12.pdf
• http://conal.net/blog/tag/comonad

Weitere ähnliche Inhalte

Was ist angesagt?

Introduction to Categorical Programming (Revised)
Introduction to Categorical Programming (Revised)Introduction to Categorical Programming (Revised)
Introduction to Categorical Programming (Revised)Masahiro Sakai
 
Scala 初心者が Hom 函手を Scala で考えてみた
Scala 初心者が Hom 函手を Scala で考えてみたScala 初心者が Hom 函手を Scala で考えてみた
Scala 初心者が Hom 函手を Scala で考えてみたKazuyuki TAKASE
 
トポロジーと圏論の夜明け
トポロジーと圏論の夜明けトポロジーと圏論の夜明け
トポロジーと圏論の夜明けTaketo Sano
 
Deno で始めるフロントエンド
Deno で始めるフロントエンドDeno で始めるフロントエンド
Deno で始めるフロントエンド虎の穴 開発室
 
たのしい高階関数
たのしい高階関数たのしい高階関数
たのしい高階関数Shinichi Kozake
 
関数型プログラミングのデザインパターンひとめぐり
関数型プログラミングのデザインパターンひとめぐり関数型プログラミングのデザインパターンひとめぐり
関数型プログラミングのデザインパターンひとめぐりKazuyuki TAKASE
 
Rust製の全文検索エンジンライブラリ(tantivy bayard)を試してみた
Rust製の全文検索エンジンライブラリ(tantivy bayard)を試してみたRust製の全文検索エンジンライブラリ(tantivy bayard)を試してみた
Rust製の全文検索エンジンライブラリ(tantivy bayard)を試してみた虎の穴 開発室
 
今から始める Lens/Prism
今から始める Lens/Prism今から始める Lens/Prism
今から始める Lens/PrismNaoki Aoyama
 
Re永続データ構造が分からない人のためのスライド
Re永続データ構造が分からない人のためのスライドRe永続データ構造が分からない人のためのスライド
Re永続データ構造が分からない人のためのスライドMasaki Hara
 
静的解析を使った開発ツールの開発
静的解析を使った開発ツールの開発静的解析を使った開発ツールの開発
静的解析を使った開発ツールの開発Takuya Ueda
 
Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...Mario Fusco
 
If You Think You Can Stay Away from Functional Programming, You Are Wrong
If You Think You Can Stay Away from Functional Programming, You Are WrongIf You Think You Can Stay Away from Functional Programming, You Are Wrong
If You Think You Can Stay Away from Functional Programming, You Are WrongMario Fusco
 
機械学習の力を引き出すための依存性管理
機械学習の力を引き出すための依存性管理機械学習の力を引き出すための依存性管理
機械学習の力を引き出すための依存性管理Takahiro Kubo
 
ゲーム開発者のための C++11/C++14
ゲーム開発者のための C++11/C++14ゲーム開発者のための C++11/C++14
ゲーム開発者のための C++11/C++14Ryo Suzuki
 
Freer Monads, More Extensible Effects
Freer Monads, More Extensible EffectsFreer Monads, More Extensible Effects
Freer Monads, More Extensible EffectsHiromi Ishii
 
グラフデータベース:Neo4j、そしてRDBからの移行手順の紹介
グラフデータベース:Neo4j、そしてRDBからの移行手順の紹介グラフデータベース:Neo4j、そしてRDBからの移行手順の紹介
グラフデータベース:Neo4j、そしてRDBからの移行手順の紹介ippei_suzuki
 
yieldとreturnの話
yieldとreturnの話yieldとreturnの話
yieldとreturnの話bleis tift
 
The Functional Programmer's Toolkit (NDC London 2019)
The Functional Programmer's Toolkit (NDC London 2019)The Functional Programmer's Toolkit (NDC London 2019)
The Functional Programmer's Toolkit (NDC London 2019)Scott Wlaschin
 

Was ist angesagt? (20)

Introduction to Categorical Programming (Revised)
Introduction to Categorical Programming (Revised)Introduction to Categorical Programming (Revised)
Introduction to Categorical Programming (Revised)
 
Scala 初心者が Hom 函手を Scala で考えてみた
Scala 初心者が Hom 函手を Scala で考えてみたScala 初心者が Hom 函手を Scala で考えてみた
Scala 初心者が Hom 函手を Scala で考えてみた
 
トポロジーと圏論の夜明け
トポロジーと圏論の夜明けトポロジーと圏論の夜明け
トポロジーと圏論の夜明け
 
Deno で始めるフロントエンド
Deno で始めるフロントエンドDeno で始めるフロントエンド
Deno で始めるフロントエンド
 
Monadic Java
Monadic JavaMonadic Java
Monadic Java
 
たのしい高階関数
たのしい高階関数たのしい高階関数
たのしい高階関数
 
関数型プログラミングのデザインパターンひとめぐり
関数型プログラミングのデザインパターンひとめぐり関数型プログラミングのデザインパターンひとめぐり
関数型プログラミングのデザインパターンひとめぐり
 
Rust製の全文検索エンジンライブラリ(tantivy bayard)を試してみた
Rust製の全文検索エンジンライブラリ(tantivy bayard)を試してみたRust製の全文検索エンジンライブラリ(tantivy bayard)を試してみた
Rust製の全文検索エンジンライブラリ(tantivy bayard)を試してみた
 
今から始める Lens/Prism
今から始める Lens/Prism今から始める Lens/Prism
今から始める Lens/Prism
 
Re永続データ構造が分からない人のためのスライド
Re永続データ構造が分からない人のためのスライドRe永続データ構造が分からない人のためのスライド
Re永続データ構造が分からない人のためのスライド
 
静的解析を使った開発ツールの開発
静的解析を使った開発ツールの開発静的解析を使った開発ツールの開発
静的解析を使った開発ツールの開発
 
Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...
 
If You Think You Can Stay Away from Functional Programming, You Are Wrong
If You Think You Can Stay Away from Functional Programming, You Are WrongIf You Think You Can Stay Away from Functional Programming, You Are Wrong
If You Think You Can Stay Away from Functional Programming, You Are Wrong
 
機械学習の力を引き出すための依存性管理
機械学習の力を引き出すための依存性管理機械学習の力を引き出すための依存性管理
機械学習の力を引き出すための依存性管理
 
ゲーム開発者のための C++11/C++14
ゲーム開発者のための C++11/C++14ゲーム開発者のための C++11/C++14
ゲーム開発者のための C++11/C++14
 
Map
MapMap
Map
 
Freer Monads, More Extensible Effects
Freer Monads, More Extensible EffectsFreer Monads, More Extensible Effects
Freer Monads, More Extensible Effects
 
グラフデータベース:Neo4j、そしてRDBからの移行手順の紹介
グラフデータベース:Neo4j、そしてRDBからの移行手順の紹介グラフデータベース:Neo4j、そしてRDBからの移行手順の紹介
グラフデータベース:Neo4j、そしてRDBからの移行手順の紹介
 
yieldとreturnの話
yieldとreturnの話yieldとreturnの話
yieldとreturnの話
 
The Functional Programmer's Toolkit (NDC London 2019)
The Functional Programmer's Toolkit (NDC London 2019)The Functional Programmer's Toolkit (NDC London 2019)
The Functional Programmer's Toolkit (NDC London 2019)
 

Andere mochten auch

Constraint Programming in Haskell
Constraint Programming in HaskellConstraint Programming in Haskell
Constraint Programming in HaskellDavid Overton
 
Just Enough Category Theory for Haskell, part 1
Just Enough Category Theory for Haskell, part 1Just Enough Category Theory for Haskell, part 1
Just Enough Category Theory for Haskell, part 1Heejong Ahn
 
Humble introduction to category theory in haskell
Humble introduction to category theory in haskellHumble introduction to category theory in haskell
Humble introduction to category theory in haskellJongsoo Lee
 
Streams for (Co)Free!
Streams for (Co)Free!Streams for (Co)Free!
Streams for (Co)Free!Denver FP
 
LISP: How I Learned To Stop Worrying And Love Parantheses
LISP: How I Learned To Stop Worrying And Love ParanthesesLISP: How I Learned To Stop Worrying And Love Parantheses
LISP: How I Learned To Stop Worrying And Love ParanthesesDominic Graefen
 
Yampa AFRP Introduction
Yampa AFRP IntroductionYampa AFRP Introduction
Yampa AFRP IntroductionChengHui Weng
 
Catch a spider monkey
Catch a spider monkeyCatch a spider monkey
Catch a spider monkeyChengHui Weng
 
Railway Oriented Programming
Railway Oriented ProgrammingRailway Oriented Programming
Railway Oriented ProgrammingScott Wlaschin
 
더 나은 팀을 위하여
더 나은 팀을 위하여더 나은 팀을 위하여
더 나은 팀을 위하여Heejong Ahn
 
Functional Programming Patterns (NDC London 2014)
Functional Programming Patterns (NDC London 2014)Functional Programming Patterns (NDC London 2014)
Functional Programming Patterns (NDC London 2014)Scott Wlaschin
 
How to Become a Thought Leader in Your Niche
How to Become a Thought Leader in Your NicheHow to Become a Thought Leader in Your Niche
How to Become a Thought Leader in Your NicheLeslie Samuel
 

Andere mochten auch (13)

Constraint Programming in Haskell
Constraint Programming in HaskellConstraint Programming in Haskell
Constraint Programming in Haskell
 
Just Enough Category Theory for Haskell, part 1
Just Enough Category Theory for Haskell, part 1Just Enough Category Theory for Haskell, part 1
Just Enough Category Theory for Haskell, part 1
 
Humble introduction to category theory in haskell
Humble introduction to category theory in haskellHumble introduction to category theory in haskell
Humble introduction to category theory in haskell
 
Streams for (Co)Free!
Streams for (Co)Free!Streams for (Co)Free!
Streams for (Co)Free!
 
LISP: How I Learned To Stop Worrying And Love Parantheses
LISP: How I Learned To Stop Worrying And Love ParanthesesLISP: How I Learned To Stop Worrying And Love Parantheses
LISP: How I Learned To Stop Worrying And Love Parantheses
 
Yampa AFRP Introduction
Yampa AFRP IntroductionYampa AFRP Introduction
Yampa AFRP Introduction
 
Zippers
ZippersZippers
Zippers
 
Catch a spider monkey
Catch a spider monkeyCatch a spider monkey
Catch a spider monkey
 
Railway Oriented Programming
Railway Oriented ProgrammingRailway Oriented Programming
Railway Oriented Programming
 
dmo-phd-thesis
dmo-phd-thesisdmo-phd-thesis
dmo-phd-thesis
 
더 나은 팀을 위하여
더 나은 팀을 위하여더 나은 팀을 위하여
더 나은 팀을 위하여
 
Functional Programming Patterns (NDC London 2014)
Functional Programming Patterns (NDC London 2014)Functional Programming Patterns (NDC London 2014)
Functional Programming Patterns (NDC London 2014)
 
How to Become a Thought Leader in Your Niche
How to Become a Thought Leader in Your NicheHow to Become a Thought Leader in Your Niche
How to Become a Thought Leader in Your Niche
 

Ähnlich wie Comonads in Haskell

Introduction to idris
Introduction to idrisIntroduction to idris
Introduction to idrisConor Farrell
 
Lambda? You Keep Using that Letter
Lambda? You Keep Using that LetterLambda? You Keep Using that Letter
Lambda? You Keep Using that LetterKevlin Henney
 
Haskell retrospective
Haskell retrospectiveHaskell retrospective
Haskell retrospectivechenge2k
 
Big Data Day LA 2016/ Hadoop/ Spark/ Kafka track - Iterative Spark Developmen...
Big Data Day LA 2016/ Hadoop/ Spark/ Kafka track - Iterative Spark Developmen...Big Data Day LA 2016/ Hadoop/ Spark/ Kafka track - Iterative Spark Developmen...
Big Data Day LA 2016/ Hadoop/ Spark/ Kafka track - Iterative Spark Developmen...Data Con LA
 
The Functional Programming Triad of Folding, Scanning and Iteration - a first...
The Functional Programming Triad of Folding, Scanning and Iteration - a first...The Functional Programming Triad of Folding, Scanning and Iteration - a first...
The Functional Programming Triad of Folding, Scanning and Iteration - a first...Philip Schwarz
 
[FT-11][ltchen] A Tale of Two Monads
[FT-11][ltchen] A Tale of Two Monads[FT-11][ltchen] A Tale of Two Monads
[FT-11][ltchen] A Tale of Two MonadsFunctional Thursday
 
3.1 derivative of a function
3.1 derivative of a function3.1 derivative of a function
3.1 derivative of a functionbtmathematics
 
Introduction to Scala for JCConf Taiwan
Introduction to Scala for JCConf TaiwanIntroduction to Scala for JCConf Taiwan
Introduction to Scala for JCConf TaiwanJimin Hsieh
 
ScalaDays 2013 Keynote Speech by Martin Odersky
ScalaDays 2013 Keynote Speech by Martin OderskyScalaDays 2013 Keynote Speech by Martin Odersky
ScalaDays 2013 Keynote Speech by Martin OderskyTypesafe
 
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 4
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 4Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 4
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 4Philip Schwarz
 
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part ...
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part ...Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part ...
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part ...Philip Schwarz
 
The Functional Programming Triad of Folding, Scanning and Iteration - a first...
The Functional Programming Triad of Folding, Scanning and Iteration - a first...The Functional Programming Triad of Folding, Scanning and Iteration - a first...
The Functional Programming Triad of Folding, Scanning and Iteration - a first...Philip Schwarz
 

Ähnlich wie Comonads in Haskell (20)

Bound
BoundBound
Bound
 
Introduction to idris
Introduction to idrisIntroduction to idris
Introduction to idris
 
Lambda? You Keep Using that Letter
Lambda? You Keep Using that LetterLambda? You Keep Using that Letter
Lambda? You Keep Using that Letter
 
Haskell retrospective
Haskell retrospectiveHaskell retrospective
Haskell retrospective
 
Lecture 3
Lecture 3Lecture 3
Lecture 3
 
Monads
MonadsMonads
Monads
 
Big Data Day LA 2016/ Hadoop/ Spark/ Kafka track - Iterative Spark Developmen...
Big Data Day LA 2016/ Hadoop/ Spark/ Kafka track - Iterative Spark Developmen...Big Data Day LA 2016/ Hadoop/ Spark/ Kafka track - Iterative Spark Developmen...
Big Data Day LA 2016/ Hadoop/ Spark/ Kafka track - Iterative Spark Developmen...
 
The Functional Programming Triad of Folding, Scanning and Iteration - a first...
The Functional Programming Triad of Folding, Scanning and Iteration - a first...The Functional Programming Triad of Folding, Scanning and Iteration - a first...
The Functional Programming Triad of Folding, Scanning and Iteration - a first...
 
[FT-11][ltchen] A Tale of Two Monads
[FT-11][ltchen] A Tale of Two Monads[FT-11][ltchen] A Tale of Two Monads
[FT-11][ltchen] A Tale of Two Monads
 
SacalaZa #1
SacalaZa #1SacalaZa #1
SacalaZa #1
 
Functional ruby
Functional rubyFunctional ruby
Functional ruby
 
3.1 derivative of a function
3.1 derivative of a function3.1 derivative of a function
3.1 derivative of a function
 
Introduction to Scala for JCConf Taiwan
Introduction to Scala for JCConf TaiwanIntroduction to Scala for JCConf Taiwan
Introduction to Scala for JCConf Taiwan
 
ScalaDays 2013 Keynote Speech by Martin Odersky
ScalaDays 2013 Keynote Speech by Martin OderskyScalaDays 2013 Keynote Speech by Martin Odersky
ScalaDays 2013 Keynote Speech by Martin Odersky
 
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 4
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 4Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 4
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 4
 
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part ...
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part ...Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part ...
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part ...
 
1519 differentiation-integration-02
1519 differentiation-integration-021519 differentiation-integration-02
1519 differentiation-integration-02
 
The Functional Programming Triad of Folding, Scanning and Iteration - a first...
The Functional Programming Triad of Folding, Scanning and Iteration - a first...The Functional Programming Triad of Folding, Scanning and Iteration - a first...
The Functional Programming Triad of Folding, Scanning and Iteration - a first...
 
Derivative rules.docx
Derivative rules.docxDerivative rules.docx
Derivative rules.docx
 
08. haskell Functions
08. haskell Functions08. haskell Functions
08. haskell Functions
 

Kürzlich hochgeladen

How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerThousandEyes
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionOnePlan Solutions
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension AidPhilip Schwarz
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech studentsHimanshiGarg82
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionSolGuruz
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplatePresentation.STUDIO
 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfproinshot.com
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024Mind IT Systems
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsAndolasoft Inc
 

Kürzlich hochgeladen (20)

How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdf
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 

Comonads in Haskell

  • 1. Comonads: what are they and what can you do with them? Melbourne Haskell Users Group David Overton 29 May 2014
  • 2. Table of Contents 1 Motivation 2 Theory 3 Examples 4 Applications 5 Other Considerations 6 Further Reading
  • 3. Motivation • Monads are an abstract concept from category theory, have turned out to be surprisingly useful in functional programming. • Category theory also says that there exists a dual concept called comonads. Can they be useful too? • Intuition: • Monads abstract the notion of effectful computation of a value. • Comonads abstract the notion of a value in a context. • “Whenever you see large datastructures pieced together from lots of small but similar computations there’s a good chance that we’re dealing with a comonad.” —Dan Piponi
  • 4. Table of Contents 1 Motivation 2 Theory 3 Examples 4 Applications 5 Other Considerations 6 Further Reading
  • 5. What is a Comonad? • A comonad is just a comonoid in the category of endofunctors. . . • A comonad is the category theoretic dual of a monad. • A comonad is a monad with the “arrows” reversed.
  • 6. What is a Comonad? Both monads and comonads are functors. (Functor is its own dual.) class Functor f where fmap :: (a → b) → (f a → f b) class Functor m ⇒ Monad m where class Functor w ⇒ Comonad w where return :: a → m a extract :: w a → a bind :: (a → m b) → (m a → m b) extend :: (w b → a) → (w b → w a) join :: m (m a) → m a duplicate :: w a → w (w a) join = bind id duplicate = extend id bind f = fmap f ◦ join extend f = fmap f ◦ duplicate (>>=) :: m a → (a → m b) → m b (=>>) :: w b → (w b → a) → w a (>>=) = flip bind (=>>) = flip extend
  • 7. Intuition • Monadic values are typically produced in effectful computations: a → m b • Comonadic values are typically consumed in context-sensitive computations: w a → b
  • 8. Monad/comonad laws Monad laws Left identity return ◦ bind f = f Right identify bind return = id Associativity bind f ◦ bind g = bind (f ◦ bind g) Comonad laws Left identity extract ◦ extend f = f Right identity extend extract = id Associativity extend f ◦ extend g = extend (f ◦ extend g)
  • 9. Table of Contents 1 Motivation 2 Theory 3 Examples 4 Applications 5 Other Considerations 6 Further Reading
  • 10. Example: reader/writer duality -- Reader monad instance Monad ((→) e) where return = const bind f r = λc → f (r c) c -- CoReader (a.k.a. Env) comonad instance Comonad ((,) e) where extract = snd extend f w = (fst w, f w) -- Writer monad instance Monoid e ⇒ Monad ((,) e) where return = ((,) mempty) bind f (c, a) = (c ♦ c’, a’) where (c’, a’) = f a -- CoWriter (a.k.a. Traced) comonad instance Monoid e ⇒ Comonad ((→) e) where extract m = m mempty extend f m = λc → f (λc’ → m (c ♦ c’))
  • 11. Example: state newtype State s a = State { runState :: s → (a, s) } instance Monad (State s) where return a = State $ λs → (a, s) bind f (State g) = State $ λs → let (a, s’) = g s in runState (f a) s’ data Store s a = Store (s → a) s -- a.k.a. ‘‘Costate’’ instance Comonad (Store s) where extract (Store f s) = f s extend f (Store g s) = Store (f ◦ Store g) s One definition of Lens: type Lens s a = a → Store s a Hence the statement that lenses are “the coalgebras of the costate comonad”.
  • 12. Example: stream comonad data Stream a = Cons a (Stream a) instance Functor Stream where fmap f (Cons x xs) = Cons (f x) (fmap f xs) instance Comonad Stream where extract (Cons x ) = x duplicate xs@(Cons xs’) = Cons xs (duplicate xs’) extend f xs@(Cons xs’) = Cons (f xs) (extend f xs’) • extract = head, duplicate = tails. • extend extends the function f :: Stream a → b by applying it to all tails of stream to get a new Stream b. • extend is kind of like fmap, but instead of each call to f having access only to a single element, it has access to that element and the whole tail of the list from that element onwards, i.e. it has access to the element and a context.
  • 13. Example: list zipper data Z a = Z [a] a [a] left, right :: Z a → Z a left (Z (l:ls) a rs) = Z ls l (a:rs) right (Z ls a (r:rs)) = Z (a:ls) r rs instance Functor Z where fmap f (Z l a r) = Z (fmap f l) (f a) (fmap f r) iterate1 :: (a → a) → a → [a] iterate1 f = tail ◦ iterate f instance Comonad Z where extract (Z a ) = a duplicate z = Z (iterate1 left z) z (iterate1 right z) extend f z = Z (fmap f $ iterate1 left z) (f z) (fmap f $ iterate1 right z)
  • 14. Example: list zipper (cont.) • A zipper for a data structure is a transformed structure which gives you a focus element and a means of stepping around the structure. • extract returns the focused element. • duplicate returns a zipper where each element is itself a zipper focused on the corresponding element in the original zipper. • extend is kind of like fmap, but instead of having access to just one element, each call to f has access to the entire zipper focused at that element. I.e. it has the whole zipper for context. • Compare this to the Stream comonad where the context was not the whole stream, but only the tail from the focused element onwards. • It turns out that every zipper is a comonad.
  • 15. Example: array with context data CArray i a = CA (Array i a) i instance Ix i ⇒ Functor (CArray i) where fmap f (CA a i) = CA (fmap f a) i instance Ix i ⇒ Comonad (CArray i) where extract (CA a i) = a ! i extend f (CA a i) = let es’ = map (λj → (j, f (CA a j))) (indices a) in CA (array (bounds a) es’) i • CArray is basically a zipper for Arrays. • extract returns the focused element. • extend provides the entire array as a context.
  • 16. Table of Contents 1 Motivation 2 Theory 3 Examples 4 Applications 5 Other Considerations 6 Further Reading
  • 17. Application: 1-D cellular automata – Wolfram’s rules rule :: Word8 → Z Bool → Bool rule w (Z (a: ) b (c: )) = testBit w (sb 2 a .|. sb 1 b .|. sb 0 c) where sb n b = if b then bit n else 0 move :: Int → Z a → Z a move i u = iterate (if i < 0 then left else right) u !! abs i toList :: Int → Int → Z a → [a] toList i j u = take (j - i) $ half $ move i u where half (Z b c) = b : c testRule :: Word8 → IO () testRule w = let u = Z (repeat False) True (repeat False) in putStr $ unlines $ take 20 $ map (map (λx → if x then ’#’ else ’ ’) ◦ toList (-20) 20) $ iterate (=>> rule w) u
  • 18. Application: 2-D cellular automata – Conway’s Game of Life data Z2 a = Z2 (Z (Z a)) instance Functor Z2 where fmap f (Z2 z) = Z2 (fmap (fmap f) z) instance Comonad Z2 where extract (Z2 z) = extract (extract z) duplicate (Z2 z) = fmap Z2 $ Z2 $ roll $ roll z where roll a = Z (iterate1 (fmap left) a) a (iterate1 (fmap right) a)
  • 19. Application: 2-D cellular automata – Conway’s Game of Life countNeighbours :: Z2 Bool → Int countNeighbours (Z2 (Z (Z (n0: ) n1 (n2: ): ) (Z (n3: ) (n4: )) (Z (n5: ) n6 (n7: ): ))) = length $ filter id [n0, n1, n2, n3, n4, n5, n6, n7] life :: Z2 Bool → Bool life z = (a && (n == 2 | | n == 3)) | | (not a && n == 3) where a = extract z n = countNeighbours z
  • 20. Application: image processing laplace2D :: CArray (Int, Int) Float → Float laplace2D a = a ? (-1, 0) + a ? (0, 1) + a ? (0, -1) + a ? (1, 0) - 4 ∗ a ? (0, 0) (?) :: (Ix i, Num a, Num i) ⇒ CArray i a → i → a CA a i ? d = if inRange (bounds a) (i + d) then a ! (i + d) else 0 • laplace2D computes the Laplacian at a single context, using the focused element and its four nearest neighbours. • extend laplace2D computes the Laplacian for the entire array. • Output of extend laplace2D can be passed to another operator for further processing.
  • 21. Application: Env (CoReader) for saving and reverting to an initial value type Env e a = (e, a) ask :: Env e a → e ask = fst local :: (e → e’) → Env e a → Env e’ a local f (e, a) = (f e, a) initial = (n, n) where n = 0 experiment = fmap (+ 10) initial result = extract experiment initialValue = extract (experiment =>> ask)
  • 22. Other applications of comonads • Signal processing: using a stream comonad. • Functional reactive programming: it has been postulated (e.g. by Dan Piponi, Conal Elliott) that some sort of “causal stream” comonad should work well for FRP, but there don’t yet seem to be any actual implementations of this. • Gabriel Gonzalez’s three examples of “OO” design patterns: • The Builder pattern: using CoWriter / Traced to build an “object” step-by-step. • The Iterator pattern: using Stream to keep a history of events in reverse chronological order. • The Command pattern: using Store to represent an “object” with internal state.
  • 23. Table of Contents 1 Motivation 2 Theory 3 Examples 4 Applications 5 Other Considerations 6 Further Reading
  • 24. Syntactic sugar At least two different proposals for a comonadic equivalent of do notation for comonads: • Gonzalez’s method notation – “OOP-like” with this keyword representing the argument of the function passed to extend. • Orchard & Mycroft’s codo notation – resembles Paterson’s arrow notation. Unsugared Gonzalez Orchard & Mycroft λwa → let wb = extend (λthis → expr1) wa wc = extend (λthis → expr2) wb in (λthis → expr3) wc method wa> expr1 wb> expr2 wc> expr3 codo wa ⇒ wb ← λthis → expr1 wc ← λthis → expr2 λthis → expr3 λwa → let wb = extend func1 wa wc = extend func2 wb in func3 wc method wa> func1 this wb> func2 this wc> func3 this codo wa ⇒ wb ← func1 wc ← func2 func3
  • 25. Comonad transformers -- Monad transformers class MonadTrans t where lift :: Monad m ⇒ m a → t m a -- Comonad transformers class ComonadTrans t where lower :: Comonad w ⇒ t w a → w a The comonad package provides a few standard transformers: • EnvT – analogous to ReaderT • StoreT – analogous to StateT • TracedT – analogous to WriterT
  • 26. Cofree comonads -- Free monad data Free f a = Pure a | Free (f (Free f a)) instance Functor f ⇒ Monad (Free f) where return = Pure bind f (Pure a) = f a bind f (Free r) = Free (fmap (bind f) r) -- Cofree comonad data Cofree f a = Cofree a (f (Cofree f a)) instance Functor f ⇒ Comonad (Cofree f) where extract (Cofree a ) = a extend f w@(Cofree r) = Cofree (f w) (fmap (extend f) r) • Cofree Identity is an infinite stream. • Cofree Maybe is a non-empty list. • Cofree [] is a rose tree.
  • 27. A bit of category theory -- Kleisli category identity and composition (monads) return :: Monad m ⇒ a → m a (>=>) :: Monad m ⇒ (a → m b) → (b → m c) → (a → m c) f >=> g = λa → f a >>= g -- Co-Kleisli category identity and composition (comonads) extract :: Comonad w ⇒ w a → a (=>=) :: Comonad w ⇒ (w a → b) → (w b → c) → (w a → c) f =>= g = λw → f w =>> g • Each monad has a corresponding Kleisli category with morphisms a → m b, identity return and composition operator (>=>). • Each comonad has a corresponding Co-Kleisli category with morphisms w a → b, identity extract and composition operator (=>=).
  • 28. Category laws Monad laws Left identity return >=> f = f Right identify f >=> return = f Associativity (f >=> g) >=> h = f >=> (g >=> h) Comonad laws Left identity extract =>= f = f Right identify f =>= extract = f Associativity (f =>= g) =>= h = f =>= (g =>= h) Category laws Left identity id ◦ f = f Right identify f ◦ id = f Associativity (f ◦ g) ◦ h = f ◦ (g ◦ h)
  • 29. Table of Contents 1 Motivation 2 Theory 3 Examples 4 Applications 5 Other Considerations 6 Further Reading
  • 30. Further reading • http://blog.sigfpe.com/2006/12/evaluating-cellular-automata-is.html • http://blog.sigfpe.com/2008/03/comonadic-arrays.html • http://blog.sigfpe.com/2008/03/transforming-comonad-with-monad.html • http://blog.sigfpe.com/2014/05/cofree-meets-free.html • https://www.fpcomplete.com/user/edwardk/cellular-automata • http://www.jucs.org/jucs_11_7/signals_and_comonads/jucs_11_7_1311_1327_ vene.pdf • http://www.haskellforall.com/2013/02/you-could-have-invented- comonads.html • http://www.cl.cam.ac.uk/~dao29/publ/codo-notation-orchard-ifl12.pdf • http://conal.net/blog/tag/comonad