Skip to content

Instantly share code, notes, and snippets.

@dorchard
Created November 22, 2012 20:08
Show Gist options
  • Save dorchard/4132742 to your computer and use it in GitHub Desktop.
Save dorchard/4132742 to your computer and use it in GitHub Desktop.
Comonad for protected operations
> {-# LANGUAGE QuasiQuotes #-}
> {-# LANGUAGE EmptyDataDecls #-}
> import Control.Comonad
> import Language.Haskell.Codo
> data Input a
> data Output a
> data InChannel a = InChannel {
> invalue :: Input a,
> inenable :: Input a,
> inready :: Output a
> }
> data OutChannel a
> data FIFO a = FIFO {
> enq :: InChannel a,
> deq :: OutChannel a
> }
> data C a = C a
> (=:) :: Output a -> C a -> ()
> (=:) = undefined
Define the C comonad - really this just provides a wrapper to
stop you using things like =: "out of context"
> instance Functor C where
> fmap f (C x) = C (f x)
> instance Comonad C where
> extract (C x) = x
> extend f (C x) = C (f (C x))
Use experimental codo notation:
> mkFIFO :: C (FIFO a) -> a
> mkFIFO = [codo| y => let (FIFO enq deq) = extract y
> full <- undefined
> (inready enq) =: full
> undefined |]
> mkComponent :: b -> C b
> mkComponent = C
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment