\( \newcommand\D{\mathrm{d}} \newcommand\E{\mathrm{e}} \newcommand\I{\mathrm{i}} \newcommand\bigOh{\mathcal{O}} \newcommand{\cat}[1]{\mathbf{#1}} \newcommand\curl{\vec{\nabla}\times} \newcommand{\CC}{\mathbb{C}} \newcommand{\NN}{\mathbb{N}} \newcommand{\QQ}{\mathbb{Q}} \newcommand{\RR}{\mathbb{R}} \newcommand{\ZZ}{\mathbb{Z}} \)
UP | HOME

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:

  1. fmap id = id it preserves the identity function, and
  2. fmap (f o g) = (fmap f) o (fmap g) it preserves composition.

2. References

Last Updated 2023-02-06 Mon 10:26.