Source file OBApplicative.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
module type S1 = sig
  type 'a t

  module Core: sig
    val apply: ('a -> 'b) t -> 'a t -> 'b t
    val map: ('a -> 'b) -> 'a t -> 'b t
    val pure: 'a -> 'a t
  end
  include module type of Core

  module Infix: sig
    val (<$>): ('a -> 'b) -> 'a t -> 'b t
    val (>>|): 'a t -> ('a -> 'b) -> 'b t
    val (<*>): ('a -> 'b) t -> 'a t -> 'b t
  end
end

module Make1(Kernel: OBMonad.Kernel1): S1
  with type 'a t = 'a Kernel.t
= struct
  type 'a t = 'a Kernel.t

  module Core = struct
    let apply wrapped_f wrapped_x =
      Kernel.bind wrapped_f (fun f ->
        Kernel.bind wrapped_x (fun x ->
          Kernel.return (f x)))

    let map f a = Kernel.bind a (fun x -> Kernel.return (f x))

    let pure = Kernel.return
  end
  include Core

  module Infix = struct
    let (<$>) = map
    let (<*>) = apply
    let (>>|) a f = map f a
  end
end

module type S2 = sig
  type ('a, 'b) t

  module Core: sig
    val apply: ('a -> 'b, 'c) t -> ('a, 'c) t -> ('b, 'c) t
    val map: ('a -> 'b) -> ('a, 'c) t -> ('b, 'c) t
    val pure: 'a -> ('a, _) t
  end
  include module type of Core

  module Infix: sig
    val (<$>): ('a -> 'b) -> ('a, 'c) t -> ('b, 'c) t
    val (>>|): ('a, 'c) t -> ('a -> 'b) -> ('b, 'c) t
    val (<*>): ('a -> 'b, 'c) t -> ('a, 'c) t -> ('b, 'c) t
  end
end

module Make2(Kernel: OBMonad.Kernel2): S2
  with type ('a, 'b) t = ('a, 'b) Kernel.t
= struct
  type ('a, 'b) t = ('a, 'b) Kernel.t

  module Core = struct
    let apply wrapped_f wrapped_x =
      Kernel.bind wrapped_f (fun f ->
        Kernel.bind wrapped_x (fun x ->
          Kernel.return (f x)))

    let map f a = Kernel.bind a (fun x -> Kernel.return (f x))

    let pure = Kernel.return
  end
  include Core

  module Infix = struct
    let (<$>) = map
    let (<*>) = apply
    let (>>|) a f = map f a
  end
end