bidirectional-0.1.0.0: Simple bidirectional serialization and deserialization

Copyright(c) Mats Rauhala 2020
LicenseBSD-3
Maintainermats.rauhala@iki.fi
Stabilityexperimental
PortabilityPOSIX
Safe HaskellSafe
LanguageHaskell2010

Data.IParser

Description

Let's assume we have a parser like the following

int :: Parser (ReaderT String Maybe) (Writer [Int]) Int Int
int = parser (ReaderT readMaybe) (x -> x <$ tell [show x])

Then you can use the parser for parsing:

> runReaderT (decode int) "3"
Just 3

Or for encoding:

> execWriter (encode int 3)
["3"]

Or combine both of them

> runReaderT (decode int) $ head $ execWriter $ encode int 3
Just 3
Synopsis

Documentation

data IParser r w c a Source #

The core bidirectional parser type

See the module for usage example

Constructors

IParser 

Fields

  • decoder :: r a

    Return a value "a" in context "r".

  • encoder :: Star w c a

    Generate a value "a" from the initial object "c" in the context "w"

Instances
(Functor r, Functor w) => Profunctor (IParser r w) Source # 
Instance details

Defined in Data.IParser

Methods

dimap :: (a -> b) -> (c -> d) -> IParser r w b c -> IParser r w a d #

lmap :: (a -> b) -> IParser r w b c -> IParser r w a c #

rmap :: (b -> c) -> IParser r w a b -> IParser r w a c #

(#.) :: Coercible c b => q b c -> IParser r w a b -> IParser r w a c #

(.#) :: Coercible b a => IParser r w b c -> q a b -> IParser r w a c #

(Functor w, Functor r) => Functor (IParser r w c) Source # 
Instance details

Defined in Data.IParser

Methods

fmap :: (a -> b) -> IParser r w c a -> IParser r w c b #

(<$) :: a -> IParser r w c b -> IParser r w c a #

(Applicative w, Applicative r) => Applicative (IParser r w c) Source # 
Instance details

Defined in Data.IParser

Methods

pure :: a -> IParser r w c a #

(<*>) :: IParser r w c (a -> b) -> IParser r w c a -> IParser r w c b #

liftA2 :: (a -> b -> c0) -> IParser r w c a -> IParser r w c b -> IParser r w c c0 #

(*>) :: IParser r w c a -> IParser r w c b -> IParser r w c b #

(<*) :: IParser r w c a -> IParser r w c b -> IParser r w c a #

parser Source #

Arguments

:: r a

The parser

-> (c -> w a)

The encoder

-> IParser r w c a 

Smart constructor for the parser

decode :: IParser r w c a -> r a Source #

Extract the decoder from the parser

encode :: IParser r w c a -> c -> w a Source #

Extract the encoder from the parser

(.=) :: (Functor r, Functor w) => (c -> c') -> IParser r w c' a -> IParser r w c a Source #

Record accessor helper

Due to the nature of the parser, the encoder gets the full record type, when it should only focus on a specific part.

data Person = Person { name :: String, age :: Int }

person :: IParser r w Person Person
person =
 Person $ name .= string
        * age .= int