Skip to content

Instantly share code, notes, and snippets.

@ghuysmans
Created June 4, 2020 23:46
Show Gist options
  • Save ghuysmans/24db17bc8e26ea5651f3fccbabc581ab to your computer and use it in GitHub Desktop.
Save ghuysmans/24db17bc8e26ea5651f3fccbabc581ab to your computer and use it in GitHub Desktop.
Equivalent definitions of applicative functor
module type PA = sig
type 'a t
val pure : 'a -> 'a t
val apply : ('a -> 'b) t -> 'a t -> 'b t (* <*> *)
end
module type NMP = sig
type 'a t
val nil : unit t
val map : ('a -> 'b) -> 'a t -> 'b t (* <$> *)
val product : 'a t -> 'b t -> ('a * 'b) t
end
module PA_of_NMP (S : NMP) : PA = struct
type 'a t = 'a S.t
open S
let pure x = map (fun () -> x) nil
let apply f x = map (fun (f, x) -> f x) (product f x)
end
module NMP_of_PA (S : PA) : NMP = struct
type 'a t = 'a S.t
open S
let nil = pure ()
let map f = apply (pure f)
let product a b = apply (map (fun x y -> x, y) a) b
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment