Functors in SML
Table of Contents
1. Basic Idea
A functor in ML means a "parametrized module". It's an unfortunate choice of words, because "functor" has come to mean something completely different in functional programming. I will discuss that "something completely different" implemented in Standard ML.
In Haskell, the functor class consists of the fmap
function and the
<$
infix operator. I do not believe the <$
infix operator is
necessary. We can then implement this as a signature:
signature FUNCTOR = sig type 'a f; val fmap : ('a -> 'b) -> 'a f -> 'b f; end;
There are two constraints to a functor:
fmap id = id
it preserves the identity function, andfmap (f o g) = (fmap f) o (fmap g)
it preserves composition.
2. References
- Implicit Functors in OCaml
- Joel Björnson, More type classes which implements functors, monoids, and applicatives in OCaml