MyNixOS website logo
Description

Inline C & Objective-C code in Haskell for language interoperability.

This library provides inline C & Objective-C code using GHC's support for quasi-quotation. In particular, it enables the use of foreign libraries without a dedicated bridge or binding. Here is a tiny example:

nslog :: String -> IO ()
nslog msg = $(objc ['msg :> ''String] (void [cexp| NSLog(@"Here is a message from Haskell: %@", msg) |]))

For more information, see https://github.com/mchakravarty/language-c-inline/wiki.

Known bugs: https://github.com/mchakravarty/language-c-inline/issues

  • New in 0.7.10: Distinction between Class (NSObject pointers) and Struct (C pointers) in both hints and marhsallers.

  • New in 0.7.9: C wrapper names include the filename to disambiguate linker symbols.

  • New in 0.7.8: (1) Record marshalling; (2) foreign pointer-class marshalling; (3) custom type marshallers; (4) marshall raw pointer types. Generated _objc.m files now always include "HsFFI.h" (as the tool can generate type names declared in that header). Marshals Bool as BOOL. Marshal Int and Word to NSInteger and NSUInteger, respectively.

  • New in 0.6.0: Introduction of explicit marshalling hints (for more flexibility and support of GHC 7.8's untyped Template Haskell quotations)

  • New in 0.5.0: Marshalling of numeric types

  • New in 0.4.0: Maybe types are marshalled as pointers that may be nil & bug fixes.

  • New in 0.3.0: Boxed Haskell types without a dedicated type mapping are marshalled using stable pointers.

  • New in 0.2.0: Support for multiple free variables in one inline expression as well as for inline code returning void.

  • New in 0.1.0: We are just getting started! This is just a ROUGH AND HIGHLY EXPERIMENTAL PROTOTYPE.

Inline C & Objective-C in Haskell

This library uses Template Haskell and language-c-quote, a quasi-quotation library for C-like languages, to provide inline C and Objective-C in Haskell. It extracts the C/Objective-C code automatically, when compiling the Haskell program, and generates marshalling code for common types. The wiki on GitHub details the motivation for this approach.

For further motivation, have a look at the slides of my YOW! Lambda Jam 2014 talk Foreign Inline Code in Haskell.

Building

To build the library, just use cabal install as usual from the source code directory or by installing from Hackage.

You may like to have a look at a minimal example of its use, which you can build as follows:

  • Execute cd tests/objc/minimal; make.
  • Now run the demo executable with ./Minimal.

To build the proof of concept example, do the following:

  • Execute cd tests/objc/concept; make.
  • Now run the demo executable with ./InlineObjC.

To build an example class wrapping a Haskell record, do the following:

  • Execute cd tests/objc/record; make.
  • Now run the demo executable with ./Particle.

To build the more complex Haskell interpreter app:

  • Execute cd tests/objc/app; make.
  • Now open -a HSApp.app.

I tested it with Haskell Platform 2013.2.0.0 (which includes GHC 7.6.3) and it requires the latest version of language-c-quote, which is currently 0.7.7.

Status

The library is in beta and so far focuses on Objective-C. Automatic marshalling support is still somewhat limited. However, it is quite easy to add more types, and I do welcome pull request!

Metadata

Version

0.7.11.0

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows