[Haskell-cafe] reasons why Template Haskell does not propose something similar to Python exec() or eval()

TP paratribulations at free.fr
Sat Aug 24 23:45:38 CEST 2013


Tobias Dammers wrote:

> IIRC you can use haskell-src-exts to parse a string into TH AST
> structures. There might even be a quasi-quoter for that; I don't have a
> real computer at hand right more, so you'll need to do some research of
> your own.

Thanks Tobias, it led me to the right path. There is indeed a solution in 
Language.Haskell.Meta:

$ ghci
> :m Language.Haskell.Meta
> parseDecs "s=s+1"
Right [ValD (VarP s) (NormalB (UInfixE (VarE s) (VarE +) (LitE (IntegerL 
1)))) []]
> parseExp "x+1"
Right (UInfixE (VarE x) (VarE +) (LitE (IntegerL 1)))
> :i parseDecs
parseDecs ::
  String -> Either String [Language.Haskell.TH.Syntax.Dec]
        -- Defined in `Language.Haskell.Meta.Parse'
> :i parseExp 
parseExp :: String -> Either String Language.Haskell.TH.Syntax.Exp
        -- Defined in `Language.Haskell.Meta.Parse'

Thanks,

TP





More information about the Haskell-Cafe mailing list