Source file genT.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
module type S = sig
  type 'a m

  type 'a t

  val lift : 'a m -> 'a t

  val return : 'a -> 'a t

  val fmap : ('a m -> 'b m) -> 'a t -> 'b t

  val make : 'a m Seq.t -> 'a m -> 'a t

  val bind : 'a t -> ('a m -> 'b t) -> 'b t

  val default : 'a t -> 'a m

  module Syntax : sig
    val ( let* ) : 'a t -> ('a -> 'b m) -> 'b t

    val return : 'a -> 'a m
  end
end

module Make (M : sig
  type 'a t

  val bind : 'a t -> ('a -> 'b t) -> 'b t

  val return : 'a -> 'a t
end) : S with type 'a m = 'a M.t = struct
  type 'a m = 'a M.t

  type 'a t = 'a M.t Tree.t

  let lift v = Tree.return v

  let return v = M.return v |> Tree.return

  let bind x f = Tree.bind x f

  let fmap f t = bind t (fun gen -> lift @@ f gen)

  let make seq root = Tree.of_seq seq root

  let default = Tree.root

  module Syntax = struct
    let ( let* ) (x : 'a t) (f : 'a -> 'b M.t) : 'b t =
      fmap (fun gen -> M.bind gen f) x

    let return = M.return
  end
end