11

I have to following Haskell program I'm trying to compile to C. I've looked at this SO post, but could not get an answer there.

quicksort [] = []
quicksort (p:xs) = (quicksort lesser) ++ [p] ++ (quicksort greater)
  where
    lesser  = filter (<  p) xs
    greater = filter (>= p) xs

main = print(quicksort([5,2,1,0,8,3]))

Here's what I tried:

$ ghc -C main.hs

And what I get is:

ghc: the option -C is only available with an unregisterised GHC
Usage: For basic information, try the `--help' option.

This is a bit weird because when I look at the help I see this:

-C stop after generating C (.hc output)

OrenIshShalom
  • 5,974
  • 9
  • 37
  • 87
  • 5
    Note that you can compile Haskell to object files and C _header_ files using the Foreign Function Interface. These can then just be _linked_ into a C program. This is much better than literally trans-compiling to C source code. – leftaroundabout Aug 27 '18 at 11:31

2 Answers2

12

Compiling to C is now a special-purpose trick, used primarily for bootstrapping on new architectures. Consequently by default it is not supported. The GHC wiki has some instructions for building GHC yourself with this support enabled; the chief difference between a standard build and a build that enables compiling to C is to configure with the --enable-unregisterised flag. See also the full list of pages on building GHC -- it is quite complicated, so you'll want to keep this handy if you decide to do so.

Daniel Wagner
  • 145,880
  • 9
  • 220
  • 380
11

That option is ancient.

Serveral years ago, GHC used to compile via C, but no longer does that in normal scenarios. Instead of generating C code and compiling that with gcc, nowadays GHC uses its own native code generator (or LLVM).

Technically, it is possible to compile GHC itself as "unregisterised" to re-enable that option. This requires a custom build of GHC from its source code. This will however produce a rather inefficient C code. Pragmatically, this is only done when cross-compiling or when porting GHC to a new architecture.

chi
  • 111,837
  • 3
  • 133
  • 218