Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Distributing a Haskell program as C source

Tags:

haskell

ghc

Say I have a Haskell program or library that I'd like to make accessible to non-Haskellers, potentially C programmers. Can I compile it to C using GHC and then distribute this as a C source?

If this is possible, can someone provide a minimal example? (e.g., a Makefile)

Is it possible to use GHC to automatically determine what compiler flags and headers and needed, and then perhaps bundle this into a single folder?

Basically I'm interested in being able to write portions of programs in C and Haskell, and then distributing it as a tarball, but without requiring the target to have GHC and Cabal installed.

like image 580
Steve Avatar asked Jan 03 '10 06:01

Steve


People also ask

Does GHC compile to C?

GHC supports multiple backend code generators. This is the part of the compiler responsible for taking the last intermediate representation that GHC uses (a form called Cmm that is a simple, C like language) and compiling it to executable code.

Is Haskell written in C?

GHC itself is written in Haskell, but the runtime system for Haskell, essential to run programs, is written in C and C--.

How do I compile and run a Haskell program?

Open a command window and navigate to the directory where you want to keep your Haskell source files. Run Haskell by typing ghci or ghci MyFile. hs. (The "i" in "GHCi" stands for "interactive", as opposed to compiling and producing an executable file.)


4 Answers

I'm interested in being able to write portions of programs in C and Haskell, and then distributing it as a tarball, but without requiring the target to have GHC and Cabal installed.

You're asking for an awful lot of infrastructure that you're unlikely to find. Remember that any Haskell program, even if it is going to be compiled to C, is almost certain to depend on a large, complex run-time system for its correct operation. At a bare minimum, that run-time system has to support garbage collection and lazy evaluation. So you have more than just a translation problem.

I suggest you tackle this problem as a software-distribution problem. Rather than a tarball, provide a package for your favored distribution platform (Debian, Red Hat, InstallShield, whatever). Personally, in order to reuse other people's efforts, I would aim for something that checks for Cabal, installs Cabal if needed, then uses Cabal to install the rest of what your users will need.

like image 146
Norman Ramsey Avatar answered Oct 04 '22 03:10

Norman Ramsey


You can do this with jhc. It's a full program optimizing compiler that compiles down to C. It doesn't have all the fancy extensions that GHC supports though.

like image 28
wnoise Avatar answered Oct 04 '22 02:10

wnoise


Even if you could, I wouldn't call it "C source". GHC can use C as part of its compilation system, but the generated C code is not even slightly readable. Even if it could be read and understood, it would make no sense to modify it because there is no way (apart from back-porting the changes into Haskell) to incorporate any modifications made by C hackers into future versions of your program.

The term "source" means the code that is written by a human and used to generate the program. In this case that is the Haskell. C generated by a compiler is not "source code", it is an intermediate representation.

like image 40
Paul Johnson Avatar answered Oct 04 '22 03:10

Paul Johnson


You can't get there with GHC. Even when it compiles via C, GHC relies on manipulating the resulting assembly to shuffle segments around, a huge runtime system and a LOT of baggage.

On the other hand, you might have better luck if what you want is supported by somewhat more limited feature set of John Meacham's JHC compiler, however, which generates fairly compact C output.

like image 22
Edward Kmett Avatar answered Oct 04 '22 04:10

Edward Kmett