Skip to content

Instantly share code, notes, and snippets.

@dbuenzli
Last active May 14, 2017 13:48
Show Gist options
  • Select an option

  • Save dbuenzli/4c1c5c51ea16b8b7fa2e to your computer and use it in GitHub Desktop.

Select an option

Save dbuenzli/4c1c5c51ea16b8b7fa2e to your computer and use it in GitHub Desktop.
Freer monad derivations.
(*---------------------------------------------------------------------------
Copyright (c) 2015 Daniel C. Bünzli. All rights reserved.
Distributed under the BSD3 license, see license at the end of the file.
%%NAME%% release %%VERSION%%
---------------------------------------------------------------------------*)
(* An OCaml derivation of http://okmij.org/ftp/Computation/free-monad.html *)
module type FUNCTOR = sig
type 'a t
val map : ('a -> 'b) -> 'a t -> 'b t
end
module type APPLICATIVE = sig
type 'a t
include FUNCTOR with type 'a t := 'a t
val lift : 'a -> 'a t
val app : ('a -> 'b) t -> 'a t -> 'b t
end
module type MONAD = sig
type 'a t
include APPLICATIVE with type 'a t := 'a t
val bind : 'a t -> ('a -> 'b t) -> 'b t
end
module type STATE = sig
type state
type 'a t
val get : state t
val set : state -> unit t
val run : 'a t -> state -> ('a * state)
end
module type STATE_FUNCTOR = sig
include STATE
include FUNCTOR with type 'a t := 'a t
end
module type STATE_MONAD = sig
include STATE
include MONAD with type 'a t := 'a t
end
module type FREE = sig
type 'a f (* The underlying functor *)
type 'a t =
| Pure of 'a
| Impure of 'a t f
val eta : 'a f -> 'a t
include MONAD with type 'a t := 'a t
end
(* We start by deriving APPLICATIVE and MONAD from a FUNCTOR *)
module Free (F : FUNCTOR) : FREE with type 'a f = 'a F.t = struct
type 'a f = 'a F.t
type 'a t =
| Pure of 'a
| Impure of 'a t f
let eta m = Impure (F.map (fun v -> Pure v) m)
let lift v = Pure v
let rec map f = function
| Pure v -> Pure (f v)
| Impure m -> Impure (F.map (fun v -> map f v) m)
let rec app f v = match f with
| Pure f -> map f v
| Impure m -> Impure (F.map (fun f -> app f v) m)
let rec bind v k = match v with
| Pure v -> k v
| Impure m -> Impure (F.map (fun v -> bind v k) m)
end
module State_functor (S : sig type t end) : STATE_FUNCTOR with type state = S.t
= struct
(* We are still deriving FUNCTOR manually here. *)
type state = S.t
type 'a t = { run : state -> ('a * state) }
let map f { run } = { run = fun s -> let (v, s') = run s in (f v, s') }
let get = { run = fun s -> (s, s) }
let set s = { run = fun _ -> ((), s) }
let run sm s = sm.run s
end
module State_monad (State_functor : STATE_FUNCTOR) : STATE_MONAD
with type state = State_functor.state
= struct
(* We derive a state monad from a STATE_FUNCTOR using FREE *)
type state = State_functor.state
include Free (State_functor)
let get = eta State_functor.get
let set v = eta (State_functor.set v)
let rec run f s = match f with
| Pure v -> (v, s)
| Impure m -> let (m', s') = State_functor.run m s in run m' s'
end
module Int = struct type t = int end
let () =
let module Istate = State_monad (State_functor (Int)) in
let fs = Istate.(bind (bind (set 10) (fun () -> get)) lift) in
assert (Istate.run fs 0 = (10, 10))
(* Now we want to derive FUNCTOR aswell. *)
module type LAN = sig
type 'a base
type 'a t = Lan : 'b base * ('b -> 'a) -> 'a t
val lan : 'a base -> 'a t
include FUNCTOR with type 'a t := 'a t
end
module Lan (T : sig type 'a t end) : LAN with type 'a base = 'a T.t = struct
type 'a base = 'a T.t
type 'a t = Lan : 'b base * ('b -> 'a) -> 'a t
let lan m = Lan (m, fun v -> v)
let map g = function Lan (m, f) -> Lan (m, fun v -> g (f v))
end
module State (S : sig type t end) : STATE with type state = S.t = struct
(* Now our state is not a FUNCTOR *)
type state = S.t
type 'a t = { run : S.t -> ('a * S.t) }
let get = { run = fun s -> (s, s) }
let set s = { run = fun _ -> ((), s) }
let run m s = m.run s
end
module LState_functor (S : STATE) : STATE_FUNCTOR with type state = S.state =
struct
(* We derive a FUNCTOR using LAN *)
type state = S.state
include Lan (S)
let get = lan S.get
let set s = lan (S.set s)
let run : type a. a t -> state -> (a * state) =
fun (Lan (m, f)) s -> let (v, s) = (S.run m s) in (f v, s)
end
module LState_monad (S : STATE) : STATE_MONAD with type state = S.state
= State_monad (LState_functor (S)) (* We derive MONAD using LAN and FREE *)
let () =
let module Istate = LState_monad (State (Int)) in
let fs = Istate.(bind (bind (set 10) (fun () -> get)) lift) in
assert (Istate.run fs 0 = (10, 10))
(* FREER, fused version of LAN and FREE *)
module type FREER = sig
type 'a base
type 'a t =
| Pure : 'a -> 'a t
| Impure : 'b base * ('b -> 'a t) -> 'a t
val eta : 'a base -> 'a t
include MONAD with type 'a t := 'a t
end
module Freer (T : sig type 'a t end) : FREER with type 'a base = 'a T.t =
struct
type 'a base = 'a T.t
type 'a t =
| Pure : 'a -> 'a t
| Impure : 'b base * ('b -> 'a t) -> 'a t
let eta m = Impure (m, fun v -> Pure v)
let lift v = Pure v
let rec map : type a b. (a -> b) -> a t -> b t =
fun f v -> match v with
| Pure v -> Pure (f v)
| Impure (m, k) -> Impure (m, fun v -> map f (k v))
let rec app : type a b. (a -> b) t -> a t -> b t =
fun f v -> match f with
| Pure f -> map f v
| Impure (m, k) -> Impure (m, fun f -> app (k f) v)
let rec bind : type a b. a t -> (a -> b t) -> b t =
fun v k -> match v with
| Pure v -> k v
| Impure (v, k') -> Impure (v, fun v -> bind (k' v) k)
end
module State_freer (S : STATE) : STATE_MONAD with type state = S.state = struct
type state = S.state
include Freer (S)
let get = eta S.get
let set s = eta (S.set s)
let rec run : type a. a t -> state -> (a * state) =
fun m s -> match m with
| Pure v -> (v, s)
| Impure (m, k) ->
let m', s' = S.run m s in
run (k m') s'
end
let () =
let module Istate = State_freer (State (Int)) in
let fs = Istate.(bind (bind (set 10) (fun () -> get)) lift) in
assert (Istate.run fs 0 = (10, 10))
(*---------------------------------------------------------------------------
Copyright (c) 2015 Daniel C. Bünzli.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
3. Neither the name of Daniel C. Bünzli nor the names of
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
---------------------------------------------------------------------------*)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment