Source file many.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
open! Base
open! Import

module Applicative = struct
  type 'w t =
    { return : 'a. 'a -> ('a, 'w) Hk.t1
    ; map : 'a 'b. ('a, 'w) Hk.t1 -> f:('a -> 'b) -> ('b, 'w) Hk.t1
    ; apply : 'a 'b. ('a -> 'b, 'w) Hk.t1 -> ('a, 'w) Hk.t1 -> ('b, 'w) Hk.t1
    }
end

module T = struct
  type ('bt, 'a, 'b) t =
    { f : 'w. 'w Applicative.t -> access:('a -> ('b, 'w) Hk.t1) -> ('bt, 'w) Hk.t1 }
  [@@unboxed]

  let access a = { f = (fun _ ~access -> access a) }

  include Base.Applicative.Make3 (struct
      type nonrec ('bt, 'a, 'b) t = ('bt, 'a, 'b) t

      let return a = { f = (fun applicative ~access:_ -> applicative.return a) }

      let map t ~f =
        { f = (fun applicative ~access -> applicative.map (t.f applicative ~access) ~f) }
      ;;

      let map = `Custom map

      let apply t1 t2 =
        { f =
            (fun applicative ~access ->
               applicative.apply (t1.f applicative ~access) (t2.f applicative ~access))
        }
      ;;
    end)
end

include T

module Open_on_rhs_intf = struct
  module type S = sig
    val access : 'a -> ('b, 'a, 'b) t
  end
end

include
  Base.Applicative.Make_let_syntax3 (T) (Open_on_rhs_intf)
    (struct
      let access = access
    end)

module Accessed = Monad.Make_indexed (struct
    type nonrec ('a, 'bt, 'b) t = ('bt, 'a, 'b) t

    let return = access

    let map t ~f =
      { f = (fun applicative ~access -> t.f applicative ~access:(fun a -> access (f a))) }
    ;;

    let bind t ~f =
      { f =
          (fun applicative ~access ->
             t.f applicative ~access:(fun a -> (f a).f applicative ~access))
      }
    ;;

    let map = `Custom map
  end)

module Of_applicative3 (A : sig
    type ('a, 'd, 'e) t

    val return : 'a -> ('a, _, _) t
    val map : ('a, 'd, 'e) t -> f:('a -> 'b) -> ('b, 'd, 'e) t
    val apply : ('a -> 'b, 'd, 'e) t -> ('a, 'd, 'e) t -> ('b, 'd, 'e) t
  end) =
struct
  module H = Hk.Make3 (A)

  let applicative =
    { Applicative.return = (fun a -> H.inject (A.return a))
    ; map = (fun a ~f -> H.inject (A.map (H.project a) ~f))
    ; apply = (fun a b -> H.inject (A.apply (H.project a) (H.project b)))
    }
  ;;

  let of_many t ~access =
    H.project (t.f applicative ~access:(fun a -> H.inject (access a)))
  ;;
end

module Of_applicative2 (A : sig
    type ('a, 'e) t

    val return : 'a -> ('a, _) t
    val map : ('a, 'e) t -> f:('a -> 'b) -> ('b, 'e) t
    val apply : ('a -> 'b, 'e) t -> ('a, 'e) t -> ('b, 'e) t
  end) =
  Of_applicative3 (struct
    type ('a, _, 'e) t = ('a, 'e) A.t

    include (A : module type of A with type ('a, 'e) t := ('a, 'e) A.t)
  end)

module Of_applicative (A : sig
    type 'a t

    val return : 'a -> 'a t
    val map : 'a t -> f:('a -> 'b) -> 'b t
    val apply : ('a -> 'b) t -> 'a t -> 'b t
  end) =
  Of_applicative2 (struct
    type ('a, _) t = 'a A.t

    include (A : module type of A with type 'a t := 'a A.t)
  end)

include Nonempty.Of_applicative_without_return3 (T)

let of_nonempty nonempty = of_nonempty nonempty ~access