package accessor

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file nonempty.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
open! Base
open! Import

module Applicative = struct
  type 'w t =
    { 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

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) }

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 A1 = Applicative_without_return.Make3 (struct
  type nonrec ('bt, 'a, 'b) t = ('bt, 'a, 'b) t

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

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

module A2 : module type of A1 with module Let_syntax := A1.Let_syntax = A1
include A2

module Let_syntax = struct
  include A2

  module Let_syntax = struct
    include A2

    module Open_on_rhs = struct
      let access = access
    end
  end
end

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

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

  let applicative =
    { Applicative.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_nonempty t ~access =
    H.project (t.f applicative ~access:(fun a -> H.inject (access a)))
  ;;
end

module Of_applicative_without_return2 (A : sig
  type ('a, 'e) 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_applicative_without_return3 (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_without_return (A : sig
  type 'a t

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

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

include Of_applicative_without_return3 (struct
  type nonrec ('bt, 'a, 'b) t = ('bt, 'a, 'b) t

  let map = map
  let apply = apply
end)