\( \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

Monad Transformer - SML

Table of Contents

1. Introduction

The basic idea is we want to combine monads together. Towards that end, we have a SML Functor which will "eat in" a monad, and produce a monad.

We have to implement an individual monad transformer as an individual functor. For example,

functor StateT(type state
               structure M : MONAD) : MONAD =
struct
  type state = state;
  (* Type constructor *)
  type 'a t = (state -> 'a * state) M.t;

  fun return x = fn s => M.return (x,s);

  fun bind m f = fn s => M.bind (m s) (fn (x, s') => f x s');

  fun lift m = fn s => M.bind m (fn x => M.return (x,s));

  (* etc. *)
end;

2. References

  • Sheng Liang, Paul Hudak, Mark Jones,
    "Monad transformers and modular interpreters".
    In POPL '95: Proceedings of the 22nd ACM SIGPLAN-SIGACT symposium on Principles of programming languages, January 1995, pp.333–343 https://doi.org/10.1145/199448.199528
  • Mark P Jones,
    "Functional Programming with Overloading and Higher-Order Polymorphism".
    Eprint, 1995, 40 pages.
  • Martin Grabmuller,
    "Monad Transformers Step by Step".
    Citeseer 2006 draft.

2.1. OCaml Implementations

  • Łukasz Stafiniak,
    "Functional Programming, lecture 8: Monads".
    Slides
  • Xavier Leroy,
    "Functional programming languages, Part IV: monadic transformations, monadic programming".
    slides
  • Daniel Perez, Implementation
  • Jim Pryor,
    Monad Transformers

Last Updated 2022-07-08 Fri 14:12.