BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News OCaml Now Ready for iOS Development

OCaml Now Ready for iOS Development

San Francisco-based programmer Edgar Aroutiounian has recently created a package for OCamliOS, an OCaml cross-compiler for iOS recently made available by Jeffrey A. Scofield. InfoQ has spoken with Aroutiounian.

Scofield explains that he has been maintaining for years a set of patches to the OCaml compiler that, thanks to the help of Gerd Stolpmann, have recently found their way into a branch of the official OCaml GitHub repository. At some point, this branch should be merged into the official OCaml release, says Scofield.

Building on Scofield’s and Stolpmann’s work, Aroutiounian wrapped OCamliOS in an OPAM package to make it easier for OCaml developers to install it. He provides a simple example that shows how you can integrate OCaml code into an iOS Objective-C app. The following OCaml snippet will register a callback that is later called from Objective-C:

let make_string () =
  print_endline "Hello Word from OCaml";
  "Hello World "

let () =
  Callback.register "make_string" make_string
#define CAML_NAME_SPACE

#import <Foundation/Foundation.h>

#include <caml/callback.h>
#include <caml/mlvalues.h>

int main (int argc, char **argv)
{
  caml_startup(argv);
  caml_callback(*caml_named_value("make_string"), Val_unit);
  NSLog(@"Now using objective-c code");
  return 0;
}

OCaml code can be compiled as a standalone executable for iOS, which is not suitable for creating apps to be published through the App Store, or as a C static library. At the moment, the only package that is available to use from OCaml is its standard library. Support for additional packages will be added in future.

InfoQ has spoken with Aroutiounian to learn more about using opam-ios and OCaml for iOS.

What exactly does opam-ios bring?

It brings a convenience to programmers. OPAM is the advanced package manager for OCaml and I wanted to have the iOS OCaml compiler be a first class citizen in the OCaml world.

What advantages could the use of opam-ios bring to iOS app development? How do you envision an iOS app that uses OCaml through opam-ios?

The advantage is that OCaml is an actual functional programming language, created with that in mind from the start. Its very flexible and I could reuse some existing code with C calls to Objective-C when I need it, like for the GUI. I envision having the business logic done in OCaml and the objective-c just showing off the GUI, although I could also wrap Objective-C objects as OCaml objects as OCaml also has support for object oriented programming.

Can OCaml support for iOS be considered mature? What features do you plan to add next?

I didn’t do the hard work of writing the patches to the compiler for iOS. Basically the situation is this, Apple uses an old and essentially forked version of an ARM assembler. So their ARM assembly is slightly different than Linux’s assembler output for ARM. So the real heroes are Gerd Stolpmann and the guys at Psellos.com, they got it up and running with the compiler and patches and make it reproducible. So right now this is a patched version of the compiler but pretty soon it will be officially part of the compiler.

So features that I plan to add next is to create more packages for each version. For example, the one currently up there is for up to iOS SDK 8.3 but I can add more compilers that are for up to say SDK 9.2. Jeff, at Psellos, has done an amazing job. He just released again, this time for OCaml 4.02.3 which is the latest and greatest and for iOS SDK 9.2. Essentially bleeding edge.

OCamliOS runs under OS X 10.11 and generate code for iOS 7.0+. Opam-ios can be installed from opam, OCaml’s package manager.

Rate this Article

Adoption
Style

BT