Distributing a Haskell program as C source

后端 未结 7 1781
时光取名叫无心
时光取名叫无心 2021-02-06 22: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

相关标签:
7条回答
  • 2021-02-06 23:42

    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.

    0 讨论(0)
  • 2021-02-06 23:50

    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.

    0 讨论(0)
  • 2021-02-06 23:50

    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.

    0 讨论(0)
  • 2021-02-06 23:51

    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.

    0 讨论(0)
  • 2021-02-06 23:54

    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.

    0 讨论(0)
  • 2021-02-06 23:54

    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.

    0 讨论(0)
提交回复
热议问题