I'm looking for ways how to convert a simple Haskell program (no imported libraries, just data types and pure functions) into a term of the untyped lambda calculus. A promising approach seems to be to use GHC API to compile a program into GHC core, which can be then converted into the untyped lambda calculus.
How to use GHC API to load a Haskell program and compile it into Core?
From the
GHCmodule documentation in the ghc docs:I found this by looking through the list of
GHCmodules, noticing theDesugarmodule, noticingModGutsin the result ofdeSugar, downloading all of the documentation, and searching the text forModGuts.Minimal Example
Our example will compile a simple module so we can see what the core looks like. It uses ghc-paths to provide the location of the ghc libs directory. The core will be represented in memory by a
CoreModulecontaining a list ofCoreBinds. We can't dump the AST directly because there aren'tShowinstances for the AST described inCoreSyn, however theOutputableinstance forCoreModulewill pretty-print the core so we can see that we compiled to core.runGhc'takes care of all the setup needed for compiling to core a module with noimports and noTemplateHaskell. We completely turn off the linker withNoLinkand tell the compiler to produce nothing withHscNothing.Compiling a module to core consists of setting the target with
guessTargetandaddTarget, optionally loading dependencies withload, building the module graph withdepanel,finding the correctModSummaryin the module graph, parsing the module withparseModule, type checking it withtypecheckModule, desugarring it withdesugarModule, converting it toModGutswithcoreModulefrom theDesugaredModinstance for the result of desugarring, and extracting the core from theModGuts. All of this is wrapped up in a nice package bycompileToCoreModule.Our whole example program will output the core with
showPpr.Compiling the above example requires the
-package ghcflag to expose the normally hidden ghc api package.The example module we'll compile to core,
"prettyPrint2dList.hs", contains a data type and a bit of code that uses functions from the prelude.Which produces a slew of pretty-printed core.