Running GHC in GHCi

Ben Gamari ben at
Wed Oct 27 20:49:14 UTC 2021

Hi all,

Today I verified that with Luite's recent work on the interpreter it is
now possible to run GHC entirely within GHCi (when bootstrapping from
GHC 9.2). Behold:

$ # First we must remove -fno-code from hadrian/ghci
$ git apply <<EOF
diff --git a/hadrian/ b/hadrian/
index 2a97b371802..f96919d91e1 100755
--- a/hadrian/
+++ b/hadrian/
@@ -6,4 +6,4 @@ set -e
 # Replace newlines with spaces, as these otherwise break the ghci invocation on windows.
 GHC_FLAGS="$GHC_FLAGS $(TERM=dumb CABFLAGS=-v0 "hadrian/build-cabal" tool:ghc/Main.hs -q --build-root=.hadrian_ghci --flavour=ghc-in-ghci $HADRIAN_ARGS | tr '\n\r' ' ')"
- at WithGhc@ --interactive $GHC_FLAGS $@ -fno-code -fwrite-interface -hidir=.hadrian_ghci/interface -O0 +RTS -A128m
+ at WithGhc@ --interactive $GHC_FLAGS $@ -fwrite-interface -hidir=.hadrian_ghci/interface -O0 +RTS -A128m

$ ./boot
$ ./configure GHC=ghc-9.2
$ hadrian/ghci
GHCi, version 9.2.1:  :? for help

[  1 of 668] Compiling GHC.Iface.Type[boot] ( /opt/exp/ghc/ghc-landing/compiler/GHC/Iface/Type.hs-boot, interpreted )
[  2 of 668] Compiling GHC.Platform.Constants ( /opt/exp/ghc/ghc-landing/.hadrian_ghci/stage0/compiler/build/GHC/Platform/Constants.hs, interpreted )
[668 of 668] Compiling Main             ( ghc/Main.hs, interpreted )
Ok, 668 modules loaded.
λ> import Main
λ> :set args -B/opt/exp/ghc/ghc-landing/_build/stage1/lib --version
λ> :trace main
The Glorious Glasgow Haskell Compilation System, version 9.3.20211027
λ> :set args -B/opt/exp/ghc/ghc-landing/_build/stage1/lib HelloWorld.hs
λ> main
[1 of 1] Compiling Main             ( HelloWorld.hs, HelloWorld.o )
Linking HelloWorld ...
λ> :! ./HelloWorld
"Hello World!"

Surprisingly, performance isn't even horrible, taking only a few seconds
to compile "hello world".

Sadly, `:trace` doesn't quite work yet on `main`:
λ> :trace main
*** Exception: ModBreaks.modBreaks_array not initialised
CallStack (from HasCallStack):
  error, called at compiler/GHC/ByteCode/Types.hs:231:24 in ghc:GHC.ByteCode.Types

I've opened #20570 to track this issue. Fixing it would allow
breakpoints and the like to be used on GHC.

Anyways, I found this quite exciting. Perhaps this will spur more
contributors to take an interest in GHCi.


- Ben
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 905 bytes
Desc: not available
URL: <>

More information about the ghc-devs mailing list