Bidirectional serialization based on Lysxia’s post on Monadic profunctors for bidirectional programming.
Let’s assume we have a parser like the following
int :: Parser (ReaderT String Maybe) (Writer [Int]) Int Int
= parser (ReaderT readMaybe) (\x -> x <$ tell [show x]) int
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