TMPA-2017: Tools and Methods of Program Analysis
3-4 March, 2017, Hotel Holiday Inn Moscow Vinogradovo, Moscow
Functional Parser of Markdown Language Based on Monad Combining and Monoidal Source Stream Representation
Georgy Lukyanov, Artem Pelenitsyn, Southern Federal University
For video follow the link: https://youtu.be/fDGG46gnOi0
Would like to know more?
Visit our website:
www.tmpaconf.org
www.exactprosystems.com/events/tmpa
Follow us:
https://www.linkedin.com/company/exactpro-systems-llc?trk=biz-companies-cym
https://twitter.com/exactpro
TMPA-2017: Functional Parser of Markdown Language Based on Monad Combining and Monoidal Source Stream Representation
1. functional parser of markdown
language based on monad combining
and monoidal source stream
representation
.
Georgiy Lukjanov (glukyanov@sfedu.ru)
Assistant professor Artem Pelenitsin (apel@sfedu.ru)
TMPA conference, 3 Feb 2017
Southern Federal University, Rostov-on-Don, Russia
2. Goals
.
∙ Explore approaches to structuring of computation with
multiple side-effects provided by modern Haskell
libraries
2 / 22
3. Goals
.
∙ Explore approaches to structuring of computation with
multiple side-effects provided by modern Haskell
libraries
∙ Monad Transformers
2 / 22
4. Goals
.
∙ Explore approaches to structuring of computation with
multiple side-effects provided by modern Haskell
libraries
∙ Monad Transformers
∙ Algebraic effects and effects handlers (specifically
Extensible Effects)
2 / 22
5. Goals
.
∙ Explore approaches to structuring of computation with
multiple side-effects provided by modern Haskell
libraries
∙ Monad Transformers
∙ Algebraic effects and effects handlers (specifically
Extensible Effects)
∙ As a case study, build parsers combinators libraries
and restricted Markdown parsers using these
approaches
2 / 22
6. Goals
.
∙ Explore approaches to structuring of computation with
multiple side-effects provided by modern Haskell
libraries
∙ Monad Transformers
∙ Algebraic effects and effects handlers (specifically
Extensible Effects)
∙ As a case study, build parsers combinators libraries
and restricted Markdown parsers using these
approaches
∙ Compare these approaches in terms of expressibility
and performance
2 / 22
8. Parsers
.
Informal definition
A parser is a program that converts text into some kind of
AST (abstract syntax tree)
∙ Auto-generated, by bottom-up parser generators (YACC)
3 / 22
9. Parsers
.
Informal definition
A parser is a program that converts text into some kind of
AST (abstract syntax tree)
∙ Auto-generated, by bottom-up parser generators (YACC)
∙ Manually-written (e.g. top-down recursive descent)
3 / 22
10. Parser Combinators
.
∙ Model parsers as higher-order functions
map :: (a → b) → [a] → [b]
map [ ] = [ ]
map f (x : xs) = f x : map f xs
4 / 22
11. Parser Combinators
.
∙ Model parsers as higher-order functions
map :: (a → b) → [a] → [b]
map [ ] = [ ]
map f (x : xs) = f x : map f xs
∙ Construct complex parsers from small set of basic ones
alphanum :: Parser r Char
alphanum = letter <|> digit
4 / 22
12. Parser Combinators
.
∙ Model parsers as higher-order functions
map :: (a → b) → [a] → [b]
map [ ] = [ ]
map f (x : xs) = f x : map f xs
∙ Construct complex parsers from small set of basic ones
alphanum :: Parser r Char
alphanum = letter <|> digit
∙ Mirror grammar rules in source code of the parser
4 / 22
13. Parser as a Monad
.
Parser data type
newtype Parser a = Parser {
parse :: String → Maybe (a, String)}
5 / 22
14. Parser as a Monad
.
Parser data type
newtype Parser a = Parser {
parse :: String → Maybe (a, String)}
Monad instance for Parser
instance Monad Parser where
return t = Parser $ λs → Just (t, s)
m >>= k = Parser $ λs →
do (u, v) ← parse m s
(x, y) ← parse (k u) v
return (x, y)
5 / 22
15. Types and Effects: main notions
.
∙ Pure/Impure functions and referential transparency
6 / 22
16. Types and Effects: main notions
.
∙ Pure/Impure functions and referential transparency
Pure function
show :: (Show a) ⇒ a → String
6 / 22
17. Types and Effects: main notions
.
∙ Pure/Impure functions and referential transparency
Pure function
show :: (Show a) ⇒ a → String
IO action
putStrLn :: String → IO ()
6 / 22
18. Types and Effects: main notions
.
∙ Static guarantees on computations permissions
7 / 22
19. Types and Effects: main notions
.
∙ Static guarantees on computations permissions
Computation with static environment
readUser :: (MonadReader Database m) ⇒
UserID → m UserData
7 / 22
20. Types and Effects: main notions
.
∙ Static guarantees on computations permissions
Computation with static environment
readUser :: (MonadReader Database m) ⇒
UserID → m UserData
∙ Combining multiple effects
7 / 22
21. Types and Effects: main notions
.
∙ Static guarantees on computations permissions
Computation with static environment
readUser :: (MonadReader Database m) ⇒
UserID → m UserData
∙ Combining multiple effects
Static environment and ‘mutable’ state
handler :: (MonadReader Config m
, MonadState Database m) ⇒ m a
7 / 22
22. Haskell frameworks for effects typing
.
Monad Transformers — type class based
class (Monad m) ⇒ MonadState m where
get :: m (StateType m)
put :: StateType m → m ()
newtype StateT s m a = StateT {runStateT :: s → m (a, s)}
instance (Monad m) ⇒ MonadState (StateT s m) where
get = StateT $ λs → return (s, s)
put s = StateT $ _ → return ((), s)
instance MonadTrans (StateT s) where ...
instance (MonadIO m) ⇒ MonadIO (StateT s m) where ...
8 / 22
23. Haskell frameworks for effects typing
.
Extensible Effects — free monad based
data Free f a where
Pure :: a → Free f a
Impure :: f (Free f a) → Free f a
data State s v where
Get :: State s s
Put :: !s → State s ()
instance Functor (State s)
type FState s = Free (State s)
9 / 22
24. Parser as a monadic stack
.
Parser
newtype Parser a = Parser (StateT String Maybe a)
10 / 22
25. Parser as a monadic stack
.
Parser
newtype Parser a = Parser (StateT String Maybe a)
Running a parser
parse :: Parser a → String → Maybe (a, String t)
parse (Parser p) s = runStateT p s
10 / 22
26. Parser as a Union of Effects
.
type Parsable r = (Member Fail r, Member (State String) r)
type Parser r a = Parsable r ⇒ Eff r a
11 / 22
27. Parser as a Union of Effects
.
type Parsable r = (Member Fail r, Member (State String) r)
type Parser r a = Parsable r ⇒ Eff r a
Running a parser (handling effects)
parse :: Eff (Fail ’: State String ’ : [ ]) a →
String → (Either () a, String)
parse p inp = run $ runState (runError p) inp
11 / 22
29. Basic parsers
.
Unconditional consumer
item :: Parser r Char
item = do s ← get
case s of [ ] → put s >> die
(x : xs) → put xs >> pure x
Conditional consumer
sat :: (Char → Bool) → Parser r Char
sat p = do x ← item
if p x then return x else die
12 / 22
31. Parsers combinators
.
Determenistic alternative combinator
alt :: Parser r a → Parser r a → Parser r a
alt ma mb = do s ← get
catchExc ma $ λea →
put s >> catchExc mb $ λeb → die
Repetition combinator
many :: Parser r a → Parser r [a]
many v = many_v
where many_v = some_v ‘alt‘ (pure [ ])
some_v = (fmap (:) v) < ∗ > many_v
13 / 22
35. Performance benchmarks. Freer EE
.
Estimate Confidence interval
Mean time 7.53 ms [7.44 ms, 7.66 ms]
σ 289 μs [194 μs, 436 μs]
17 / 22
36. String-like types in Haskell
.
String
Essentially a [Char] — poor performance
ByteString
High performance, but low-level interface
Text
Unicode-oriented high performance type
18 / 22
37. Text-oriented Monoids
.
Input-polymorphic base parser
item :: TextualMonoid t ⇒ Parser t Char
item = do
s ← splitCharacterPrefix ◦ remainder < $ > get
case s of Nothing → throwError
Just (c, rest) → put rest ∗ > pure c
‘uncons’-like function
splitCharacterPrefix :: TextualMonoid t ⇒
t → Maybe (Char, t)
19 / 22
38. Conclusion and results
.
1. Parsers combinators library and Markdown parser
based on Monad Transformers.
∙ https://github.com/geo2a/markdown_monparsing
2. Parsers combinators library and Markdown parser
based on Extensible Effects.
∙ https://github.com/geo2a/ext-effects-parsers
∙ https://github.com/geo2a/ext-effects-markdown
3. Performance comparison of Monad Transformers and
Extensible Effects based libraries.
20 / 22
39. References
.
∙ Monadic Parser Combinators // Graham Hutton, Erik
Meijer – Department of Computer Science, University of
Nottingham, 1996
∙ Adding Structure to Monoids // Mario Blaževic – Stilo
International plc
∙ Extensible Effects An Alternative to Monad
Transformers // Oleg Kiselyov, Amr Sabry, Cameron
Swords – Indiana University, USA
∙ Freer monads and more extensible effects // Oleg
Kiselyov, Hiromi Ishii
21 / 22
40. Questions?
.
Results outline
1. Parsers combinators library and Markdown parser
based on Monad Transformers.
2. Parsers combinators library and Markdown parser
based on Extensible Effects.
3. Performance comparison of Monad Transformers and
Extensible Effects based libraries.
Georgiy Lukyanov glukyanov@sfedu.ru
Artem Pelenitsin apel@sfedu.ru
22 / 22