Distributing a Haskell program as C source

2019-03-17 12:57发布

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.

标签: haskell ghc
7条回答
家丑人穷心不美
2楼-- · 2019-03-17 13:17

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.

查看更多
老娘就宠你
3楼-- · 2019-03-17 13:20

I know this is an old post, but I still wanted to also mention ajhc. Ajhc forked jhc with the plans of adding new features and later pushing the updates back to jhc.

查看更多
一夜七次
4楼-- · 2019-03-17 13:22

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

You can compile to C, but the resulting C is not human-readable. You're better off writing header files and using the excellent C FFI alongside it. In any case, distributing the generated C seems like a fool's errand.

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.

I do not know of any solutions that do not involve GHC. You'd have to distribute at the very least the Haskell RTS.

查看更多
ら.Afraid
5楼-- · 2019-03-17 13:25

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.

查看更多
Melony?
6楼-- · 2019-03-17 13:30

Can I compile it to C using GHC and then distribute this as a C source?

No it is not possible but you can easily create interface between haskell and c by using the Foreign Function Interface (FFI) of Haskell.

You can have more example here.

查看更多
▲ chillily
7楼-- · 2019-03-17 13:31

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.

查看更多
登录 后发表回答