Applications that need to communicate with the outside world inadvertently end up accumulating a range of dependencies – things like database connection-strings, logging facilities, or configuration options. Running an application in a specific setting means instantiating a particular set of configurations. For example, for testing purposes, we may want to provide mock implementations of some…
At work, we are heavy users of the OCaml Lwt library for promised based concurrent programming. Lwt is popular, actively developed, has exceptional performance, and can run on different platforms! In this post, however, I would like to discuss some of its limitations when it comes to asynchronous programming. The typical use case would be writing a service that aggregates a bunch of data from…
Parser combinators are sets of functions for building parsers in a composable fashion. Haskell’s Parsec library and OCaml’s Angstrom are two examples. Both of these libraries expose monadic interfaces for describing context-sensitive grammars. This post looks at implementing a more restricted parsing library, structured around applicative functors rather than monads. What could justify giving up…
The next major release of the OCaml compiler, version 4.08, will be equipped with a new syntax extension for monadic and applicative composition. Practically it means that it will be a bit more convenient to work with APIs structured around these patterns. The design draws inspiration from ppx_let but offers lighter syntax, and removes the need of running the code through a ppx preprocessor .…
Memoization is a strategy for preventing values to be computed multiple times. The sledgehammer approach in OCaml is a function with the signature: val memoize : ( ' a -> ' b ) -> ' a -> ' b That is, memoize extends any given function with memory so that anytime it’s called with the same input, it’s going to return a cached result. Whether or not one should apply this technique depends on the call…
Following is a continuation of the topic of modular implicits , introduced in the previous post on implicit functors . This time we’ll look at how the extension can help simplifying lenses . I covered lenses in OCaml lenses via modules , where a rather verbose definition of a (van Laarhoven) lens was given in the form of a module signature LENS : module type LENS = sig type a type b module Mk :…
Modular implicits is an experimental feature of OCaml that has yet to land on the master branch. In this and upcomings posts I’m going to give a few examples of what it brings to the table. For an introduction to the topic it’s best to read the original paper . To run the code below I’m using a branch of the OCaml compiler available via: opam switch 4 . 02 . 0 + modular - implicits eval `opam…
Lenses, often described as first class getters and setters, can help simplify code for manipulating nested data structures. In this post I’m going to look at how to map the most popular Haskell representation, van Laarhoven lenses , to OCaml. Lenses ala van Laarhoven I won’t cover lenses in Haskell but a good starting point is this talk by Simon Peyton Jones. Following is the the basic definition…
A few weeks ago I came across a logic puzzle handed out as a holiday challenge . I didn’t solve it by hand but instead turned to Haskell for some help. As it proved to be a fun exercise I decided to pass it on and invited some friends to contribute with solutions in a language of their choice. I here present the given puzzle along with the set of submissions received. The problem There are two…
The following is a write-up on an implementation of the Knuth-Morris-Pratt (or KMP ) text search algorithm in OCaml. The algorithm itself is rather straight forward but implementing it in a functional style serves as a good example of how lazy data structures may be used as an optimization technique. The algorithm KMP solves the problem of efficiently searching for occurrences of one string within…
In a previous post I gave an example of how to represent algebraic data types using church encoding. In this post I’ll pick up the thread and show how this technique can be used to mimic any type. There are two ways of constructing new types in functional programming languages; Either by using a sum type or a product type. Products are composite types such a records and tuples. Sum types are for…
I have more than once found myself in need of a function for pretty-printing some recursive data type; Be it a prefix search tree , an abstract syntax tree for a domain specific language , XML or something else. Getting tired of having to implement the same type of logic over and again I decided to generalize the pattern. In the following sections I discuss the design of a tiny library for…
Using immutable data structures enables equational reasoning and assures that update operations are atomic. However, purely immutable interfaces are not always feasible. For instance a RESTful service typically needs to propagate the effects of update operations to other clients. In this post I describe a strategy for constructing mutable service interfaces on top of purely immutable data…
Can you imagine an F# compiler that doesn’t understand discriminated unions (aka algebraic data types)? For sure not an attractive scenario but perhaps just not as horrifying as you might expect. For example consider how the familiar option type is defined in F#: type option < ' T > = | Some of ' T | None Along with the ability to pattern match over option values: match someValue with | Some x ->…