sexp-grammar-2.3.4.1: Invertible grammar combinators for S-expressions
Safe HaskellSafe-Inferred
LanguageHaskell2010

Language.SexpGrammar.TH

Synopsis

TemplateHaskell helpers

grammarFor :: Name -> ExpQ #

Build a prism and the corresponding grammar that will match on the given constructor and convert it to reverse sequence of :- stacks.

E.g. consider a data type:

data FooBar a b c = Foo a b c | Bar

For constructor Foo

fooGrammar = $(grammarFor 'Foo)

will expand into

fooGrammar = PartialIso
  (\(c :- b :- a :- t) -> Foo a b c :- t)
  (\case { Foo a b c :- t -> Just $ c :- b :- a :- t; _ -> Nothing })

Note the order of elements on the stack:

ghci> :t fooGrammar
fooGrammar :: Grammar p (c :- (b :- (a :- t))) (FooBar a b c :- t)

match :: Name -> ExpQ #

Build prisms and corresponding grammars for all data constructors of given type. Expects grammars to zip built ones with.

$(match ''Maybe)

Will expand into a lambda:

(\nothingG justG -> ($(grammarFor 'Nothing) . nothingG) <>
                    ($(grammarFor 'Just)    . justG))