BT

New Early adopter or innovator? InfoQ has been working on some new features for you. Learn more

OCaml Now Ready for iOS Development

| by Sergio De Simone on Jan 28, 2016. Estimated reading time: 3 minutes |

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 Stage
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread
Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Discuss

Login to InfoQ to interact with what matters most to you.


Recover your password...

Follow

Follow your favorite topics and editors

Quick overview of most important highlights in the industry and on the site.

Like

More signal, less noise

Build your own feed by choosing topics you want to read about and editors you want to hear from.

Notifications

Stay up-to-date with curated articles from top InfoQ editors

"You dont know what you dont know" change that by browsing what our editors pick for you.

BT