Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Monad instance for Codec' #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Monad instance for Codec' #6

wants to merge 1 commit into from

Conversation

Lysxia
Copy link
Contributor

@Lysxia Lysxia commented Oct 13, 2016

This builds on top of PR #4 to use Codec' as a Monad.

This allows to parse formats that are prefixed with some metadata that determines the rest of the encoding. For instance, a bytestring can be encoded with its length first:

byteStringCodec
  :: Codec fr fw Int -- Length
  -> (Int -> Codec fr fw ByteString) -- A bytestring of fixed length
  -> Codec fr fw ByteString
byteStringCodec codecLength codecBS = do
  n <- BS.length =. codecLength
  codecBS n

I have put this in a separate PR because it is a breaking change.

  • Codec' has a slightly different definition;
  • some functions have more constrained types (a Functor fw constraint).

While #4 is not strictly required for the instance, the ability to (co)map over the w parameter is crucial to make that instance usable.

If you are interested in merging this, I can write some documentation about how to use write monadic Codecs before that happens.

@Lysxia Lysxia force-pushed the dev branch 6 times, most recently from 375d76b to f750a4b Compare October 13, 2016 16:05
@chpatrick
Copy link
Owner

This is very interesting! Are you sure that it follows the Monad laws though? Some examples would be great.

@Lysxia
Copy link
Contributor Author

Lysxia commented Oct 20, 2016

Oops. I didn't mean to close it.

@Lysxia Lysxia reopened this Oct 20, 2016
@Lysxia
Copy link
Contributor Author

Lysxia commented Oct 20, 2016

Codec' fr fw w r is isomorphic to Product get (ReaderT w put) r (Product from Data.Functor.Product) so I'm quite sure it is actually a monad. I'll think of more examples.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants