Skip to content

Instantly share code, notes, and snippets.

@tel
Created February 16, 2015 19:29
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tel/434503f222fd8b93a477 to your computer and use it in GitHub Desktop.
Save tel/434503f222fd8b93a477 to your computer and use it in GitHub Desktop.
More F-algebra things in OCaml
module type Functor = sig
type 'a t
val map : ('a -> 'b) -> ('a t -> 'b t)
end
module Iso = struct
type ('a, 'b) t = { fwd : 'a -> 'b; bck : 'b -> 'a }
let fwd i = i.fwd
let bck i = i.bck
end
module type FAlgebra = sig
type t
type 'a view
module ViewFunctor : Functor with type 'a t = 'a view
val project : t -> t view
val embed : t view -> t
end
module Fix (F : FAlgebra) = struct
let rec cata (phi : 'a F.view -> 'a) (t : F.t) : 'a =
phi (F.ViewFunctor.map (cata phi) (F.project t))
let rec ana (psi : 'a -> 'a F.view) (a : 'a) : F.t =
F.embed (F.ViewFunctor.map (ana psi) (psi a))
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment