Created
November 22, 2012 20:08
-
-
Save dorchard/4132742 to your computer and use it in GitHub Desktop.
Comonad for protected operations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> {-# 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