Is there a Haskell/ML-like compiler to C?
Asked Answered
S

3

19

People have written games for the iPhone in Scheme. Because (some) Scheme-compilers compile down to C, it was easy to mix with Objective-C and integrate with XCode.

I am aware of patches for Haskell and OCaml compilers to enable ARM/iOS-backends. But those appear unofficial and experimental/unstable.

I prefer a static haskell/ML-type type-system over Scheme's dynamic typing. Is there a stable ML/SML/Haskell compiler which generates C-code so that it can be used in a similar way as Scheme/Gambit-C?

Synchronous answered 18/1, 2011 at 9:27 Comment(3)
There is a C backend in GHC (-fvia-c option)Klatt
@Klatt yes but the C backend for ghc does not cross compile. It's also really (now that we have native code and llvm backends) only still around for bootstrapping purposes.Freeboard
By the way, the Gambit-C core guy released a REPL on iOS: itunes.apple.com/us/app/gambit-repl/id434534076?mt=8&ls=1Synchronous
M
15

I can't help with ML, but have you looked at JHC? JHC is a whole-program optimizing Haskell compiler that targets portable C, and iPhone support can be added by putting

[iphone]
cc=arm-apple-darwin-gcc
merge=le32

in ~/.jhc/targets.ini

More Haskell information is on the Haskell wiki and the JHC manual.

Maniac answered 18/1, 2011 at 9:57 Comment(3)
I've considered it, thanks. I have the impression jhc is not feature complete. I wouldn't like to start a project in one language and at some later point in time find the compiler does not support what i am doing. See the compilation errors: mirror.seize.it/report.htmlSynchronous
JHC is AFAIK a nearly complete implementation of Haskell98 (there are a few gaps, but very minor IIRC), however it doesn't support all of GHC's extensions, such as MPTC's. Your link to compile errors seems to be against GHC's test suite; I wouldn't expect JHC to be able to compile most of that code as much of it would by necessity require GHC-specific extensions.Maniac
I have quite a bit of experience of using JHC, feature completeness isn't the problem (except for extensions), the problem with the current version of jhc is the stability of code generator which sometimes on complicated expressions generate erroneous C-code, I've been trying to help to identify these problems for which some have been fixed but there are still seems to be issues. You also want to make sure you enable -fjgc which has been tested more (and can help generate valid C code) than without using a GC and if you're using FFI libraries enable --optc='-fno-strict-aliasing'.Monied
B
7

(I am not certain that this toolchain has been attempted specifically on the IPhone yet.)

MLTon is a whole-program optimizing Standard ML compiler. You can keep around your C code by passing -keep g to MLTon at compilation, e.g:

c:/Program Files (x86)/MLton/bin/mlton.bat" -verbose 1 -keep g test.sml

It can also generate ARM code natively.

Bekki answered 18/1, 2011 at 10:36 Comment(0)
F
4

nhc98: http://www.haskell.org/nhc98/

Full Haskell '98. Of course many libs now rely on more advanced ghc-only features. But Haskell '98 is plenty capable for general purpose programming.

Freeboard answered 18/1, 2011 at 11:3 Comment(3)
Is there a reason to prefer nhc over jhc? I haven't used either one extensively.Marchant
@Michael Snoyman I don't know for a fact but I'd assume it is more stable than jhc but you still need to build nhc from source to make it cross-compile where as jhc is a true cross-compiler for which you can define target inf style file to describe platform and which C compiler to use making it trivial to target new platforms.Monied
nhc works as a cross compiler as far as I know -- see the docs here: haskell.org/nhc98/install.html. Although yes, this requires compiling nhc as a cross compiler -- but that shouldn't be hard.Freeboard

© 2022 - 2024 — McMap. All rights reserved.