Once OCaml has passed the type checking stage, it can stop emitting syntax and type errors and begin the process of compiling the well-formed modules into executable code.
In this chapter, we’ll cover the following topics:
The untyped intermediate lambda code where pattern matching is optimized
The bytecode ocamlc compiler and ocamlrun interpreter
The native code ocamlopt code generator, and debugging and profiling native code
The first code generation phase eliminates all the static type information into a simpler intermediate lambda form. The lambda form discards higher-level constructs such as modules and objects and replaces them with simpler values such as records and function pointers. Pattern matches are also analyzed and compiled into highly optimized automata.
The lambda form is the key stage that discards the OCaml type information and maps the source code to the runtime memory model described in Chapter 20. This stage also performs some optimizations, most notably converting pattern-match statements into more optimized but low-level statements.
The compiler dumps the lambda form in an s-expression syntax if you add the
-dlambda directive to the
command line. Let’s use this to learn more about how the OCaml pattern-matching engine works
by building three different pattern matches and comparing their lambda forms.
Let’s start by creating a straightforward exhaustive pattern ...