Programming, etcStatelessTumblr (3.0; @alexleighton)http://alexleighton.tumblr.com/Functors for the Beginner<p>Let’s talk about Functors in the OCaml programming language. I will assume that you know about modules in OCaml, if not you can refresh by reading the <a title="OCaml Manual - The Module System" target="_blank" href="http://caml.inria.fr/pub/docs/manual-ocaml/manual004.html">manual</a>.</p>
<blockquote>
<p>Functors are “functions” from structures to structures.</p>
</blockquote>
<p>The above is probably the best definition I’ve seen of a functor. It touches on what I see as the main motivation behind using a functor. A Functor being a function from structure to structure means that if you keep the signature of the input structure and the signature of the output structure constant, you are free to change the implementation details without affecting any of the modules that use it. Functors allow the software designer an easy way of avoiding duplication, increasing orthogonality, all in a type safe package.</p>
<p>Let’s write an algorithm to determine if a string contains balanced parentheses. To start, we’ll define the type of stack we want to work with:</p>
<pre class="prettyprint lang-ml">module type STACK = sig
type 'a t
exception Empty
val create : unit -> 'a t
val push : 'a -> 'a t -> unit
val pop : 'a t -> 'a
val is_empty : 'a t -> bool
end</pre>
<p>Here we have the basic operations on a very minimalist stack. Now, we’ll create a functor that will take a stack structure of this type and return a module containing our parenthesis matching algorithm.</p>
<pre class="prettyprint lang-ml">module Matcher (S : STACK) = struct
let is_balanced str =
let s = S.create () in try
String.iter
(fun c -> match c with
'(' -> S.push c s
| ')' -> ignore (S.pop s)
| _ -> ()) str;
S.is_empty s
with S.Empty -> false
end</pre>
<p>While we rely on the Stack with the STACK type to not change the semantics of its functions, we can guarantee that for any stack with this kind of structure and with the right semantics, our algorithm will work. Any use of this functor will create a module with one function: <code class="prettyprint lang-ml">val is_balanced : string -> bool</code>. This also means that if we maintain the signature of the functor we just wrote, we can change any of the implementation details and not affect the outside world.</p>
<p>To use a different stack, all we have to do is change one line of code from <code class="prettyprint lang-ml">module M = Matcher(Stack)</code> to <code class="prettyprint lang-ml">module M = Matcher(CStack)</code> to change our algorithm to use a stack that keeps a count of the number of things pushed onto it in its lifetime.</p>
<p><strong>Conclusion:</strong> functors allow you to write code that will work irrespective of the implementation of the given structure (so long as it does what it should), and produces a structure containing functions that we are free to modify, but only have to change in one location, that of the functor.</p>http://alexleighton.tumblr.com/post/1185209057http://alexleighton.tumblr.com/post/1185209057Sat, 25 Sep 2010 08:01:00 -0700ocamlfunctorstackprogramming