No matching definitions.

tur/comonad

stdlib/comonad.tur

the Comonad typeclass, the categorical dual of Monad.

A comonad is a value in a context you can always read from (extract) and that can compute a new context from every position of an old one (extend). A Monad builds context up; a Comonad consumes it -- the shape behind cellular automata, zippers and "value plus environment".

Ships two instances, both over a heap cell held as an :int carrier: the Identity comonad (identity) and the Pair/environment comonad (pair). Because both instances share that carrier, a call names its instance with a witness: (.extract @identity w).

Since: Phase CA0

defclass

Comonad

(defclass Comonad [^w])

a value in a context: read the focus, or recompute every position.

^wthe comonadic type constructor (kind * -> *)
(extract [wa] :int)

the value at the focus of wa. Never fails: a comonad always has a focused value, which is what makes it the dual of a Monad's pure.

(extend [wa f :fn] :int)

run f on wa as seen from every position and collect the results in a context of the same shape, focused where wa was. f receives the whole context, not just one value, so it can look at neighbours (a grid cell's surroundings, a zipper's left and right). f is a :fn carrier, so a capturing closure works.

(duplicate [wa] :int)

a context whose every position holds wa refocused there: (extend wa (fn [x] x)).

(extract (extend w f))         = (f w)
(extend w extract)             = w
(extend (extend w g) f)        = (extend w (fn [x] (f (extend x g))))
(extract (duplicate w))        = w
(.extract @identity (__identity_new 42))                         ; => 42
(.extract @pair (__pair_new 7 100))                              ; => 100
(.extract @identity (.extract @identity (.duplicate @identity (__identity_new 42))))  ; => 42

Every method is declared over the :int carrier, so a value built by one instance must be read back through the same one.

identity, pair

Since: Phase CA0

definstance

Comonad[identity]

(definstance Comonad [identity])

Comonad instance for the Identity comonad.

definstance

Comonad[pair]

(definstance Comonad [pair])

Comonad instance for the Pair (product) comonad.

Internal definitions
__identity_new-- allocate a new Identity comonad cell wrapping x.
__identity_get-- read the value stored in an Identity comonad cell.
__identity_extract-- extract the value from an Identity comonad cell.
__identity_extend-- apply fn to the Identity cell and wrap the result.
__identity_duplicate-- wrap an Identity cell inside another Identity cell.
__pair_new-- allocate a new Pair comonad cell with environment and value.
__pair_env-- read the environment component of a Pair comonad cell.
__pair_val-- read the value component of a Pair comonad cell.
__pair_extract-- extract the value from a Pair comonad cell.
__pair_extend-- extend over a Pair cell: (e, a) -> (e, f (e, a)).
__pair_duplicate-- duplicate a Pair cell: (e, a) -> (e, (e, a)).