Development using the CakeML Compiler

The user experience with the CakeML compiler can be elevated as documented in the following.

Throughout cake refers to the compiler executable, for example obtained from the CakeML Github releases.

Read-eval-print loop (REPL)

REPL with syntax highlighting

To amend the CakeML REPL with syntax highlighting use bat together with rlwrap’s makefilter (installed to $XDG_CONFIG_HOME/rlwrap/filters) as follows. In the REPL any CakeML abstract syntax will be highlighted after pressing return, once the command is evaluated.

env RLWRAP_FILTERDIR="$XDG_CONFIG_HOME/rlwrap/filters" \
  rlwrap -z "makefilter bat --language=sml --pager=never" \
    cake --repl

Loading modules or libraries

In the REPL the pragmas #load are interpreted to load the file at the respective location. These presumeably originate from the work on Candle.

#load "jsonModule.cml";

Prior to that one may adjust the load path:

val paths = ["../modules/json/"];
CakeML.loadPath := paths @ (!CakeML.loadPath);

Signature

The compiler prints the signature of a file.cml by

cake --types < file.cml

To print the standard library signatures with syntax highlighting by bat run:

echo | cake --types 2>&1 | bat --language=sml

Compiling any *.cml or *.sexp file

To obtain an executable from a *.cml or *.sexp file two steps are neccessary: compilation by CakeML followed by compilation/linking against the standard FFI and setup C-code.

The following setup with one single Makefile.bake avoids duplication of these steps. We assume the compiler within the directory pointed to by a CAKEDIR environment variable, i.e. $CAKEDIR/cake and $CAKEDIR/basis_ffi.c point to the compiler and the basis FFI, respectively.

# file: $CAKEDIR/Makefile.bake
%.S : %.cml
    $(CAKEDIR)/cake $(CAKEFLAGS) <$< >$@

%.S : %.sexp
    $(CAKEDIR)/cake --sexp=true $(CAKEFLAGS) <$< >$@

% : %.S
    $(CC) $(LDFLAGS) $^ $(CAKEDIR)/basis_ffi.c $(LOADLIBES) $(LDLIBS) -o $@

For example, this can be used in any directory to compile testProg.cml to testProg executable:

alias cakebake="make --makefile=$CAKEDIR/Makefile.bake"
cakebake CAKEFLAGS='--exclude_prelude=true' testProg

Setting sml filetype via modeline

The cml file extension is not commonly recognised as Standard ML (compatible), however a comment at the top/bottom of the file can help editors as vim and emacs to properly set the filetype to SML:

(* vim: set ft=sml: *)

Also Github uses these modeline for various features, like syntax highlighting. Documentation on modelines in vim-modeline or emacs modes.