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
GHC
module documentation in the ghc docs:I found this by looking through the list of
GHC
modules, noticing theDesugar
module, noticingModGuts
in 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
CoreModule
containing a list ofCoreBind
s. We can't dump the AST directly because there aren'tShow
instances for the AST described inCoreSyn
, however theOutputable
instance forCoreModule
will 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 noimport
s and noTemplateHaskell
. We completely turn off the linker withNoLink
and tell the compiler to produce nothing withHscNothing
.Compiling a module to core consists of setting the target with
guessTarget
andaddTarget
, optionally loading dependencies withload
, building the module graph withdepanel
,find
ing the correctModSummary
in the module graph, parsing the module withparseModule
, type checking it withtypecheckModule
, desugarring it withdesugarModule
, converting it toModGuts
withcoreModule
from theDesugaredMod
instance 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 ghc
flag 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.