-
How to Leverage Pattern Matching (8th June 2007)
"Compared to conventional programming languages, OCaml takes dynamic dispatch to a whole new level using an approach called pattern matching. This article guides the reader through the fundamental concepts that underpin pattern matching before providing some examples demonstrating the power of pattern matching in general programming..."
-
The Essence of Functional Programming (23rd June 2007)
"OCaml is fundamentally a functional programming language and the use of functions instead of objects often leads to shorter and clearer code. Indeed, many of the benefits of functional programming are already known to OO programmers in the form of design patterns..."
-
GUI Programming: A Sudoku solver (10th July 2007)
"Powerful cross-platform GUI applications can be developed quickly and easily in the OCaml programming language. This article describes the design and implementation of a cross-platform GUI application for solving sudoku puzzles..."
-
Data structures in the Standard Library (24th July 2007)
"The OCaml standard library includes a wealth of mutable and immutable data structures that cover a wide variety of needs. This article introduces all of the data structures provided by the OCaml standard library and describes when and how to use each of them..."
-
Exploiting Tail Recursion (9th August 2007)
"Recursion is essential to functional programming and practical use of functional programming languages and a functional style requires the ability to write recursive functions that do not consume stack space. Function calls that require no stack space are called tail calls. This article describes the use of tail calls to write robust and efficient tail recursive functions in OCaml..."
-
Optimizing a simple bytecode interpreter (23rd August 2007)
"As a high-performance functional programming language, OCaml is perfect for writing interpreters. These programs involve a wide variety of different techniques from lexing and parsing to various methods of evaluation, compilation, simplification and rewriting. All of these techniques will be described in future OCaml Journal articles and this article describes the implementation of three progressively optimized interpreters for a minimal byte code language..."
-
Combinator Heaven (8th September 2007)
"The ability to write higher-order functions that compose other functions in interesting ways is one of the most powerful forms of abstraction provided by the functional programming paradigm. Such functions are known as combinators . This article describes the basic concepts behind combinators with a variety of examples to demonstrate how practically useful this form of functional abstraction can be..."
-
Using lex and yacc (24th September 2007)
"The tool stack bundled with the OCaml distribution provides some awesome functionality. The ocamllex and ocamlyacc tools provide a mainstream approach to the parsing of data. This article introduces the concepts of lex and yacc-based parsing and describes the use of these tools in implementing robust and efficient parsers quickly and easily..."
-
A simple FFT implementation (8th October 2007)
"Writing an implementation of the Fourier transform is an excellent lesson in algorithm design and optimization. Moreover, the Fourier transform is one of the most essential tools in numerical computing, with applications ranging from spectral analysis in science to the multiplication of large integers in mathematics. This article describes the design, implementation and optimization of a high-performance FFT implementation in OCaml..."
-
Balanced binary search trees (23rd October 2007)
"Immutable data structures are a core concept in functional programming and balanced binary trees are the single most important such data structure. The OCaml programming language allows balanced binary tree implementations to be written clearly and efficiently. This article describes the design and implementation of a basic balanced binary search tree data structure similar to that of the built-in Set module..."
-
Introduction to OpenGL (12th November 2007)
"OpenGL is the defacto-standard cross-platform graphics API and provides an efficient way to leverage the high-performance hardware accelerators found in almost all modern computers. The OCaml programming language has unusually good support for OpenGL with the LablGL library providing an elegant interface to OpenGL on all three major platforms. This article introduces OpenGL programming using OCaml, demonstrating how functional programming can be leveraged to produce visualization software that is simple and efficient..."
-
Camlp4 3.10: parsers and macros (23rd November 2007)
"The latest OCaml tool stack includes a revamped preprocessor called camlp4 that serves several purposes including providing extensions to the OCaml language that allow parsers to be embedded in ordinary OCaml code as well as the ability to extend the syntax of OCaml itself by writing macros. This article explains how camlp4 can be used to write general parsers and OCaml syntax extensions..."
-
Festive Fun with OpenGL and GTK+ (12th December 2007)
"This article marries the ability to render high-quality graphics in real-time using OpenGL with the ability to build GUI applications using GTK+. Specifically, a small program is developed that builds upon functionality encountered in previous articles to render a decorated christmas tree with snow in real time..."
-
Parser Combinators (28th December 2007)
"Certain applications are extremely well suited to functional programming and parsing is one of them. Specifically, the ability to write functional combinators that allow parsers for everything from integers up to symbolic expressions to be composed is more general and provides more opportunity for code reuse than the use of conventional parser generators such as ocamllex and ocamlyacc. This article explains how parser combinators may be designed and implemented in OCaml, using the standard example of a calculator..."
-
Implementing a simple ray tracer (10th January 2007)
"Ray tracing is a simple but powerful approach to photorealistic rendering and implementing a ray tracer is an excellent way to learn a programming language and, in particular, to learn about graphics and optimization in the context of numerical algorithms. This article walks through the design and implementation of a basic ray tracer that provides real-time visualization a scene using OpenGL in a GUI application that provides the user with rendering options using a GTK+ menu..."
-
Factoring numerical methods using combinators and functors (24th January 2007)
"The same concepts that make functional programming a powerful paradigm for symbolic manipulation can also be applied to numerical methods. This article describes the basic concepts of factoring using higher-order functions and functors in the context of numerical algorithms such as function minimization..."
-
Language-oriented programming: The Term-level Interpreter (9th February 2008)
"Modern software is using a wider variety of languages than ever before. The ability to parse and interpret these languages is of growing importance. Fortunately, OCaml inherits incredibly powerful language features (algebraic data types and pattern matching) for program manipulation from its predecessors and augments these with an excellent suite of tools and libraries. This article explains how OCaml can be harnessed to write a complete term-level interpreter for a programming language in only a tiny amount of code..."
-
High-fidelity graphics with OpenGL 2 (25th February 2008)
"The single most important advancement made in consumer-level graphics accelerators in recent years was the advent of the programmable graphics pipeline. OpenGL 2 introduces the high-level GLSL shader language as a way to program the graphics pipeline. This article describes how the latest version of the GLCaml bindings for OCaml allow GLSL shaders to be used to improve the fidelity of high-performance graphics using per-pixel Phong shading..."
-
Run-time compilation with LLVM (8th March 2008)
"The Low-Level Virtual Machine (LLVM) project brings high-performance static and JIT compilation to code generators written in the OCaml programming language. This article describes the basic use of LLVM from OCaml including both static and JIT compilation, argument passing, control flow and the compilation of expression trees..."
-
Getting the most out of Static Typing (26th March 2008)
"The OCaml language arms programmers with one of the most sophisticated static type systems of any general-purpose programming language. This static type system can be used to remove large classes of common bugs that are otherwise tedious or impossible to track down. The benefits in terms of programmer productivity and program reliability are tremendous. However, leveraging such a type system is an art that requires significant effort to learn. This article describes idiomatic OCaml style and a variety of techniques that can be used to leverage the static type system in order to catch errors earlier and more easily..."
-
Labeled and Optional arguments (10th April 2008)
"The OCaml programming language provides a variety of useful features that are not found in many other functional programming languages. This is why OCaml is widely accepted as a functional language for practical use. Labeled and optional function arguments are two related features that can be used to great effect in simplifying interfaces. This article introduces the syntax required to define and use both labeled and optional arguments in OCaml and describes some pedagogical uses of these features, with references to existing libraries, as well as examining some of the problems often encountered by programmers using these features..."
-
Language oriented programming: Term Rewriting (25th April 2008)
"An interesting and powerful alternative to the conventional term-level interpreter is called term rewriting. Rather than reducing expressions down to values, term rewriting simply evaluates expressions by performing substitutions and the result is another expression. This approach is particularly well suited to computer algebra systems such as Mathematica but is also an underappreciated alternative to dynamically-typed programming languages that can integrate useful features like pattern matching and make techniques like partial specialization far easier. This article describes how a simple term rewriter can be constructed and introduces the concept of private variant type constructors in the process..."
-
Porting and optimizing the SciMark2 benchmark (10th May 2008)
"The SciMark2 benchmark is a suite of numerical algorithms that were originally collated into a benchmark for scientific computing on the Java platform. Porting this benchmark to OCaml provides an excellent tutorial on the implementation of efficient numerical algorithms using the current OCaml implementation. The results illustrate the strengths and weaknesses of the current OCaml implementation in terms of performance..."
-
Implementing a simple XML-RPC client and server (23rd May 2008)
"XML-RPC is a protocol for remote procedure calls that is built upon the XML format. Method calls are packaged up by the client as an XML request sent via HTTP to the server whereupon action is taken and a response is packaged up and returned in the same way. This article describes how a complete client-server pair using XML-RPC can be designed and built from scratch in OCaml using only the XML-Light and OcamlNet libraries..."
-
Real-time Finite Element Materials simulation (10th June 2008)
"Finite Element Materials simulations (FEMs) model a continuous volume of material by breaking it down into a discrete representation with many finite parts. This article describes a simple but fun program that simulates the dynamics of a 2D system of particles and springs in real-time. The program visualizes the results using OpenGL..."
-
Tricks with recursion: knots, modules and polymorphism (22nd June 2008)
"This article introduces some of the design patterns found in functional programming. The phrase "untying the recursive knot" refers to the ability to remove direct recursion from value and type definitions through the use of parameterization. OCaml's recursive modules allow recursion to cross module boundaries. Polymorphic recursion allows functions to call themselves with different type parameters. This article introduces all of these functional design patterns with illustrations and worked examples..."
-
Foreign Function Interfaces: Calling C from OCaml (10th July 2008)
"Although the OCaml programming language offers many improvements over older languages such as C, C++ and Fortran the need to interoperate with native code can still arise. The two most important uses for native code interoperability are performance and legacy. This article describes how native code can be invoked from OCaml programs using both the conventional OCaml approach of writing stub functions in C and also a new approach using LLVM to generate the necessary interface code at run-time..."
-
Metaprogramming with MetaOCaml (27th July 2008)
"Metaprogramming is a term used to describe programs that generate or manipulate other programs. The ML family of languages were bred specifically for this purpose and, indeed, that is where their name MetaLanguage is derived from. Run-time compilation and execution of programs is a notable feature once present in Lisp as EVAL but sacrificed in the design of ML. The MetaOCaml language is a prototype OCaml derivative that is designed to bring statically-typed metaprogramming to the OCaml family of languages. This article examines the use of MetaOCaml for metaprogramming including some of the practical applications of this technology and the challenges faced by users of this technology..."
-
Fork-based Parallelism (10th August 2008)
"As the world transitions to multicore desktop computers over the next few years it is essential that software evolves to take advantage of this new dimension in processing power by exposing parallelism. Although the current OCaml implementation has a serial run-time that limits multithreaded OCaml programs to serial concurrency, it is actually entirely possible to use separate processes to create parallel OCaml programs that do exploit multicores. This article examines a simple but powerful approach to parallelism using Unix process forking..."
-
Writing a bytecode compiler using LLVM (27th August 2008)
"The Low-Level Virtual Machine (LLVM) provides the machinery to generate high-performance native code programmatically and on-demand. One of the most important applications of this technology is the Just In Time (JIT) compilation of bytecode intermediate representations into native code, as found in Sun's Java Virtual Machine (JVM) and Microsoft's Common Language Runtime (CLR). This article describes how a simple bytecode interpreter can be developed into a JIT compiler using LLVM with relatively little effort..."
-
Building a 2D vector graphics library (10th September 2008)
"Vector graphics represent images in terms of lines and curves on a fictional canvas. This resolution-independent representation is ideal for high resolution output devices such as printers but the inevitable march of technology has ossified vector graphics on the desktop as a fundamental component of both Apple's and Microsoft's latest operating systems. This article describes the design and implementation of a simple library for 2D vector graphics written in OCaml and using OpenGL for visualization..."
-
Concurrent web crawling (23rd September 2008)
"Web-enabled technology is now ubiquitous and of huge commercial value. These kinds of programs share two common characteristics: they send information over the internet and they perform tasks concurrently. This article is the first in a series to examine the use of the OCaml programming language and its relatives in the growing area of concurrent web programming..."
-
Triangulated Irregular Networks (TINs) (11th October 2008)
"Adaptive subdivision is a hot topic in computer graphics and forms the foundation of many state-of-the-art algorithms for large scale visualization used for everything from scientific visualization of huge data sets to game graphics that immerse players in expansive virtual worlds. This article describes one of the most popular approaches for the adaptive subdivision of 3D meshes and implements a capable plotting algorithm with real-time OpenGL-based visualization showcasing how this simple algorithm works and can be used to solve many different problems..."
-
Low-level optimization tips and tricks: part 1 (23rd October 2008)
"The OCaml programming language is unusually well suited to high-performance computing among functional programming languages because it provides a highly efficient code generator with a performant run-time and garbage collector. This makes it feasible to write high performance programs entirely in OCaml without having to drop to lower-level languages like C and Fortran for performance-critical sections of code. This article describes the low-level optimizations that underpin the ability to write performant OCaml programs by leveraging knowledge about the OCaml compiler..."
-
Huffman data compression (10th November 2008)
"Data compression algorithms are not only very useful in practice but are also extremely compelling pragmatic examples of programming theory. The popular Huffman data compression algorithm is from the family of entropy encoding compression algorithms. This article walks through the construction of a simple but efficient Huffman compression and decompressor written entirely in OCaml..."
-
Object-oriented programming in OCaml (23rd November 2008)
"The OCaml programming language is unique among member of the ML family because it provides not only the core ML features of variant types, pattern matching and functors but also two different features of the type system that provide subtyping: objects and polymorphic variants. Compared to conventional object-oriented languages, OCaml's objects are unusual because they are structurally typed, a characteristic that removed the need to declare class types but which can adversely affect developer productivity and run-time efficiency if used inappropriately. This article describes the design, implementation and use of OCaml's object system, giving references to examples of good design..."
-
Working with Regular Expressions (10th November 2008)
"Regular expressions are a domain-specific language for pattern matching over sequences of characters. This functionality provides a concise and efficient way to dissect strings and, consequently, is used in many forms of string processing including the definition of lexers. This article describes OCaml's built-in support for regular expressions..."
-
Low-level optimization tips and tricks: part 2 (23rd December 2008)
"The OCaml programming language is unusually well suited to high-performance computing among functional programming languages because it provides a highly efficient code generator with a performant run-time and garbage collector. This makes it feasible to write high performance programs entirely in OCaml without having to drop to lower-level languages like C and Fortran for performance-critical sections of code. This article covers allocation and garbage collection and various forms of specialization including type, function and data structure specialization..."
-
Solving the Traveling Salesman problem using Simulated Annealing (10th January 2009)
"Finding global minima of an arbitrary function is a significantly more challenging problem than local function minimization and has many practical applications from the simulation of molecules to the design of printed circuit board layouts. Several different global function minimization algorithms exist, many of which make repeated use of local function-minimization algorithms. This article describes a simple and elegant solution to the traveling salesman problem that uses the simulated annealing approach to global function minimization. The results are visualized in real time using OpenGL. In particular, we use an efficient purely functional data structure to represent the path..."
-
Building a Virtual Machine using LLVM: part 1 (23rd January 2009)
"This article is the first in a series examining the design and construction of a simple high-level virtual machine (HLVM) written in OCaml and building upon LLVM. Despite its simple design, the HLVM offers excellent performance, rich run-time types with reflection, tail calls, accurate garbage collection and an easy-to-use C-compatible Foreign Function Interface (FFI). This article describes the first step in construction: JIT compilation of programs from a minimal language..."
-
Simulating smoke in real-time using fluid dynamics (10th February 2009)
"In scientific computing, the task of simulating fluid flow accurately by solving the Navier-Stokes equation is notoriously difficult. However, it is possible to compute numerical approximations quickly enough that fluids dynamics may be simulated in real time. This article describes a simple fluid dynamics simulator that uses OpenGL to visualize the results in real time..."
-
Building a Virtual Machine using LLVM: part 2 (23rd February 2009)
"This is the second article in the series about our high-level virtual machine (HLVM) project. This article builds upon the foundations laid in the first article to create a much more practically useful language implementation by adding tail call elimination, first-class structs, a foreign function interface (FFI) and first-class function pointers..."
-
Low-level optimization tips and tricks: part 3 (8th March 2009)
"The OCaml programming language is unusually well suited to high-performance computing among functional programming languages because it provides a highly efficient code generator with a performant run-time and garbage collector. This makes it feasible to write high performance programs entirely in OCaml without having to drop to lower-level languages like C and Fortran for performance-critical sections of code. This article gives a case study that covers many of the techniques described so far before describing how the performance of the FFI can be improved and some optimizations that often provide substantial performance improvements in OCaml..."
-
Building a Virtual Machine with LLVM: part 3 (23rd March 2009)
"This is the third and final article in the series about the design and construction of the high-level virtual machine (HLVM). This article describes the design and implementation of boxed values for sum types and a simple garbage collector. In particular, the difference between the run-time representations of values in OCaml and HLVM is described in detail and the GC implementation described is partially generated at run-time and JIT compiled..."
-
Emergent behaviour: flocking boids (8th April 2009)
"Artificial life became a popular topic in the 1980s. In particular, with the discovery that simple rules defining the behaviour of individuals could give rise to sophisticated behaviour of groups of individuals. This unexpected result was described as "emergent behaviour" and one of the most famous examples was a computer program called "Boids" that used only three simple rules to govern the dynamics of individuals but produced remarkably realistic flocking behaviour of the population as a whole. This article describes an interactive boids simulator with a graphical user interface implemented using GTK+..."
-
Purely functional data structures: streams, queues and catenable lists (23rd April 2009)
"This article is the first in a series describing the design and implementation of some very useful purely functional data structures. Lazy streams, batched and real-time queues and lists with O(1) concatenation are described in detail. In particular, functors are used extensively to provide elegant parameterization over data structures and some of the new features in OCaml 3.11 are used to improve both performance and clarity..."
-
Lempel-Ziv-Welch data compression (8th May 2009)
"LZW is a simple and effective dictionary-based data compression algorithm originally published in 1984 and subsequently used in several prominent places including the GIF image format. This article describes simple purely functional implementations of the compression and corresponding decompression algorithms before examining the optimization of these implementations via the selective inclusion of mutable data structures..."
-
Pretty printing with the Format module and camlp4 stream parsers (23rd May 2009)
"The OCaml programming language has evolved a rich variety of libraries and tools over the past twelve years. The domain of metaprogramming is particularly well supplied. This article describes the basic design of OCaml's Format module which provides customizable formatting and, in particular, user-extensible pretty printing for the top-level as well as the stream parsers provided by camlp4..."
-
Visualizing the Singular Value Decomposition (8th June 2009)
"Singular Value Decomposition (SVD) is an important algorithm from linear algebra that decomposes a matrix into the product of three simpler matrices. The resulting decomposition has several applications. This article describes the design and implementation of an OCaml program that computes the SVD of an image and uses it to create a lossy compression algorithm with controllable quality. This involves numerical methods for computing the eigenvalues of real symmetric matrices and interactive visualization of the result as a GTK application using the camlimages library to load images from file..."
-
Compiler development: part 1 (23rd June 2009)
"This article is the first in a series describing the design and implementation of a complete compiler for a non-trivial language using the freely-available HLVM project described in previous articles. The representation of types, patterns and expressions, the lexer, parser and Read-Evaluate-Print-Loop (REPL) with JIT compilation are described in this article..."