Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compile Haskell programs to C

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)

like image 652
OrenIshShalom Avatar asked Aug 27 '18 11:08

OrenIshShalom


People also ask

How to compile Haskell code to a standalone executable?

Using a Haskell compiler, such as GHC, you can compile the code to a standalone executable. Create a source file hello.hs containing: main = putStrLn "Hello, World!"

What is Haskell programming language?

Haskell is a general purpose, purely functional programming language. This page will help you get started as quickly as possible. The recommended way to get started with programming Haskell is the Haskell Platform. The Platform comes with GHC, the de-facto standard Haskell compiler, and other tools that will help you program Haskell.

How do I make a Hello World program in Haskell?

Prelude> putStrLn "Hello World" Hello World. Using a Haskell compiler, such as GHC, you can compile the code to a standalone executable. Create a source file hello.hs containing: main = putStrLn "Hello, World!". And compile it with: $ ghc -o hello hello.hs.

How do I start Haskell in Linux terminal?

Start Haskell. If you have installed the Haskell Platform, open a terminal and type ghci (the name of the executable of the GHC interpreter) at the command prompt. Alternatively, if you are on Windows, you may choose WinGHCi in the Start menu.


2 Answers

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.

like image 107
Daniel Wagner Avatar answered Nov 10 '22 22:11

Daniel Wagner


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 "unregistered" 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.

like image 36
chi Avatar answered Nov 10 '22 21:11

chi