# 2022 edition

After taking a break for some years I revisited this problem.  I built
GHC 4.08.2 with GCC 2.95 which is from the same era and has no problem
with the odd C code.  I submitted a preliminary
patch to add a ghc-4 package to
Guix, but I was suspicious: this was too easy.  And I was right: the
resulting package didn't come with a standard
library.
I missed this the first time, because this old build system just keeps
on going when it encounters actually fatal errors.

At least the RTS was built successfully, so I decided to resume the
original plan: combine the GHC RTS with the Hugs interpreter, and then
attempt to interpret the compiler to compile itself.

There were a couple of boring problems like linker
problems,
but I eventually managed to build a broken version of STGhugs.  It
failed to load the Prelude, because the glibc package I used was built
without support for shared libraries.  Once I had rebuilt glibc 2.2.5
with shared library support I was able to build STGhugs and have it
load the Prelude (and pretty much any other included library) without
crashing.  (This hugs doesn't come with runhugs, which is a little
inconvenient, but in the larger scheme of things it's probably not a
real obstacle.)

$ ./hugs
__   __ __  __  ____   ___      _________________________________________
||   || ||  || ||  || ||__      STGHugs: Based on the Haskell 98 standard
||___|| ||__|| ||__||  __||     Copyright (c) 1994-2000
||---||         ___||           World Wide Web: http://haskell.org/hugs
||   ||                         Report bugs to: hugs-bugs@haskell.org
||   || Version: STGHugs-000425 _________________________________________

Haskell 98 mode: Restart with command line option -98 to enable extensions
Standalone mode: Restart with command line +c for combined mode

Reading source file "/tmp/guix-build-ghc-4.08.2.drv-0/ghc-4.08.2/ghc/interpreter/lib/Prelude.hs"
Reading source file "/tmp/guix-build-ghc-4.08.2.drv-0/ghc-4.08.2/ghc/interpreter/lib/PrelPrim.hs"
Hugs session for:
  PrelPrim
  Prelude
Type :? for help
Prelude>

Yay!

Loading the compiler sources fails because they include preprocessor
instructions.  So we need to set up a preprocessor first.  Luckily,
we've already built hscpp.  We configure the preprocessor and its
include path:

Prelude> :set +F"/gnu/store/471qh0h8jpaq3m8kw861dr8ii19f5r8g-ghc-4.08.2/lib/hscpp -I/gnu/store/471qh0h8jpaq3m8kw861dr8ii19f5r8g-ghc-4.08.2/lib/ghc/compiler -I/gnu/store/471qh0h8jpaq3m8kw861dr8ii19f5r8g-ghc-4.08.2/lib/includes -D__HUGS__"

We also set the module search path:

Prelude> :set +P"/gnu/store/471qh0h8jpaq3m8kw861dr8ii19f5r8g-ghc-4.08.2/lib/ghc/lib/std:/gnu/store/471qh0h8jpaq3m8kw861dr8ii19f5r8g-ghc-4.08.2/lib/ghc/compiler/main/:/gnu/store/471qh0h8jpaq3m8kw861dr8ii19f5r8g-ghc-4.08.2/lib/ghc/compiler/absCSyn:/gnu/store/471qh0h8jpaq3m8kw861dr8ii19f5r8g-ghc-4.08.2/lib/ghc/compiler/utils:/gnu/store/471qh0h8jpaq3m8kw861dr8ii19f5r8g-ghc-4.08.2/lib/ghc/compiler/types:/gnu/store/471qh0h8jpaq3m8kw861dr8ii19f5r8g-ghc-4.08.2/lib/hslibs/lang"

And then try to load parts of the compiler:

Prelude> :load AbsCSyn
:load AbsCSyn
Reading source file "/gnu/store/471qh0h8jpaq3m8kw861dr8ii19f5r8g-ghc-4.08.2/lib/ghc/compiler/absCSyn/AbsCSyn.lhs"
Reading source file "/gnu/store/471qh0h8jpaq3m8kw861dr8ii19f5r8g-ghc-4.08.2/lib/ghc/compiler/utils/BitSet.lhs"
Reading source file "./Word.lhs"
Reading source file "./Numeric.lhs"
Reading source file "./Char.lhs"
Reading source file "./Ratio.lhs"
Reading source file "./Bits.lhs"
Reading source file "/gnu/store/471qh0h8jpaq3m8kw861dr8ii19f5r8g-ghc-4.08.2/lib/ghc/compiler/types/TyCon.lhs"
Reading source file "/gnu/store/471qh0h8jpaq3m8kw861dr8ii19f5r8g-ghc-4.08.2/lib/ghc/compiler/utils/Outputable.lhs"
Reading source file "/gnu/store/471qh0h8jpaq3m8kw861dr8ii19f5r8g-ghc-4.08.2/lib/hslibs/lang/Foreign.lhs"
Reading source file "./IOExts.lhs"
Reading source file "./IO.lhs"
Reading source file "./Monad.lhs"
Reading source file "./ST.lhs"
Reading source file "/gnu/store/471qh0h8jpaq3m8kw861dr8ii19f5r8g-ghc-4.08.2/lib/hslibs/lang/Storable.lhs"
Parsing
ERROR "Storable.lhs" (line 690): Syntax error in module definition (unexpected comma)

This is stupid: Storable.lhs (and other files) contain syntax errors and Hugs is picky about those.  That file contains some lines with a trailing comma followed by another comma on the next line.  This needs patching... We also need to patch a whole bunch of other files that use trailing #, which are for GHC-internal values.  We replace them with ' or remove them.

Hugs also doesn't like the "foreign import" lines in Storable.lhs that contain the word "unsafe".  Hugs is rather cautious.

Ultimately, I feel that we might be doomed because we cannot use the "combined mode", which requires compiled Haskell code.  We're stuck with standalone mode.

My goal is to load at part of the compiler sources and then try to cobble together a working compiler.

Is this all worth the effort when we could get generated C files for the hslib and the compiler sources here[1]...?  Using those would still be slightly better than depending on a big GHC binary, no...?

[1]: http://downloads.haskell.org/~ghc/4.08.2/ghc-4.08.2-hc-unreg.tar.bz2

Generated by Ricardo Wurmus using scpaste at Sat Feb 5 01:19:44 2022. CET. (original)