Maybe String -> CoreExpr in a GHC plugin

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Maybe String -> CoreExpr in a GHC plugin

Joachim Breitner-2
Hi,

in a GHC plugin, I want to synthesize simple data structures, and
insert them into the code. What is the most idiomatic way of writing a
function, say,

  foo :: Maybe String -> CoreExpr

or

  foo :: Maybe String -> CoreM CoreExpr

so that the resulting CoreExpr describes the input. Abstractly
speaking, I could imagine creating the Core AST by hand (but I’d have
to figure out how to resolve the names of the constructors), or somehow
invoking the renamer, type-checker and desugarer from within CoreM.

Surely someone else has solved this problem before. How did you do it?

Thanks,
Joachim


--
Joachim “nomeata” Breitner
  [hidden email]
  https://www.joachim-breitner.de/

_______________________________________________
Glasgow-haskell-users mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/glasgow-haskell-users

signature.asc (849 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Maybe String -> CoreExpr in a GHC plugin

Joachim Breitner-2
Hi,

Am Dienstag, den 16.01.2018, 11:08 -0500 schrieb Joachim Breitner:

> in a GHC plugin, I want to synthesize simple data structures, and
> insert them into the code. What is the most idiomatic way of writing a
> function, say,
>
>   foo :: Maybe String -> CoreExpr
>
> or
>
>   foo :: Maybe String -> CoreM CoreExpr
>
> so that the resulting CoreExpr describes the input. Abstractly
> speaking, I could imagine creating the Core AST by hand (but I’d have
> to figure out how to resolve the names of the constructors), or somehow
> invoking the renamer, type-checker and desugarer from within CoreM.
I ended up writing this:

dcExpr :: TH.Name -> CoreM CoreExpr
dcExpr thn = do
    Just name <- thNameToGhcName thn
    dc <- lookupDataCon name
    pure $ Var (dataConWrapId dc)

resultToExpr :: Result -> CoreM CoreExpr
resultToExpr (Success s) = App <$> dcExpr 'Success <*> mkStringExpr s
resultToExpr (Failure s) = App <$> dcExpr 'Failure <*> mkStringExpr s

which seems to work fine.

Cheers,
Joachim
--
Joachim Breitner
  [hidden email]
  http://www.joachim-breitner.de/

_______________________________________________
Glasgow-haskell-users mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/glasgow-haskell-users

signature.asc (849 bytes) Download Attachment