Advertisement
Guest User

Untitled

a guest
Mar 25th, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. module type Functor = sig
  2. type 'a t
  3. val map : ('a -> 'b) -> ('a t -> 'b t)
  4. end
  5.  
  6. module Mu (F : Functor) : sig
  7. type t = { mu : t F.t }
  8. val cata : ('a F.t -> 'a) -> (t -> 'a)
  9. end = struct
  10. type t = { mu : t F.t }
  11. let rec cata phi t = phi (F.map (cata phi) t.mu)
  12. end
  13.  
  14. module ListF (T : sig type a end) = struct
  15. type 'a t = Nil | Cons of T.a * 'a
  16. let map f = function
  17. | Nil -> Nil
  18. | Cons (x, a) -> Cons (x, f a)
  19. end
  20.  
  21. module MuList (T : sig type a end) = struct
  22. module ListA = ListF (T)
  23. module Base = Mu (ListA)
  24. include Base
  25.  
  26. let of_list l s =
  27. let cons hd tl = { mu = ListA.Cons (hd, tl) } in
  28. let nil = { mu = ListA.Nil } in
  29. List.fold_right cons s nil
  30.  
  31. let to_list =
  32. cata begin function
  33. | ListA.Nil -> []
  34. | ListA.Cons (hd, tl) -> hd :: tl
  35. end
  36. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement