Created
February 16, 2015 19:29
-
-
Save tel/434503f222fd8b93a477 to your computer and use it in GitHub Desktop.
More F-algebra things in OCaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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