package accessor

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

Source file dictionary.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
open! Base
open! Import
open Subtyping

(* This module is inspired by the approach described in the paper "Profunctor Optics", by
   Matthew Pickering, Jeremy Gibbons, and Nicolas Wu. Although there are significant
   differences, it's probably worth at least taking a look at in order to understand the
   ideas behind this module. *)

module Create = struct
  (* These types stand in for type class dictionaries. For example, an [Isomorphism.t] is
     like an instance of the following type class as would be written in Haskell:

     {v
        class Isomorphism w where
          f : (at -> a) -> (b -> bt) -> w a b -> w at bt
      v} *)

  module Isomorphism = struct
    type 'w t =
      { f :
          'a 'b 'at 'bt. get:('at -> 'a) -> construct:('b -> 'bt) -> ('a, 'b, 'w) Hk.t2
          -> ('at, 'bt, 'w) Hk.t2
      }
    [@@unboxed]
  end

  module Field = struct
    type 'w t =
      { f :
          'a 'b 'at 'bt. ('at -> 'a * ('b -> 'bt)) -> ('a, 'b, 'w) Hk.t2
          -> ('at, 'bt, 'w) Hk.t2
      }
    [@@unboxed]
  end

  module Variant = struct
    type 'w t =
      { f :
          'a 'b 'at 'bt. match_:('at -> ('a, 'bt) Either.t) -> construct:('b -> 'bt)
          -> ('a, 'b, 'w) Hk.t2 -> ('at, 'bt, 'w) Hk.t2
      }
    [@@unboxed]
  end

  module Constructor = struct
    type 'w t =
      { f : 'a 'b 'at 'bt. ('b -> 'bt) -> ('a, 'b, 'w) Hk.t2 -> ('at, 'bt, 'w) Hk.t2 }
    [@@unboxed]
  end

  module Getter = struct
    type 'w t =
      { f : 'a 'b 'at 'bt. ('at -> 'a) -> ('a, 'b, 'w) Hk.t2 -> ('at, 'bt, 'w) Hk.t2 }
    [@@unboxed]
  end

  module Optional = struct
    type 'w t =
      { f :
          'a 'b 'at 'bt. ('at -> ('a * ('b -> 'bt), 'bt) Either.t) -> ('a, 'b, 'w) Hk.t2
          -> ('at, 'bt, 'w) Hk.t2
      }
    [@@unboxed]
  end

  module Optional_getter = struct
    type 'w t =
      { f :
          'a 'b 'at 'bt. ('at -> 'a option) -> ('a, 'b, 'w) Hk.t2 -> ('at, 'bt, 'w) Hk.t2
      }
    [@@unboxed]
  end

  module Nonempty = struct
    type 'w t =
      { f :
          'a 'b 'at 'bt. ('at -> ('bt, 'a, 'b) Nonempty.t) -> ('a, 'b, 'w) Hk.t2
          -> ('at, 'bt, 'w) Hk.t2
      }
    [@@unboxed]
  end

  module Nonempty_getter = struct
    type 'w t =
      { f :
          'a 'b 'at 'bt. ('at -> 'a Nonempty_getter.t) -> ('a, 'b, 'w) Hk.t2
          -> ('at, 'bt, 'w) Hk.t2
      }
    [@@unboxed]
  end

  module Many = struct
    type 'w t =
      { f :
          'a 'b 'at 'bt. ('at -> ('bt, 'a, 'b) Many.t) -> ('a, 'b, 'w) Hk.t2
          -> ('at, 'bt, 'w) Hk.t2
      }
    [@@unboxed]
  end

  module Many_getter = struct
    type 'w t =
      { f :
          'a 'b 'at 'bt. ('at -> 'a Many_getter.t) -> ('a, 'b, 'w) Hk.t2
          -> ('at, 'bt, 'w) Hk.t2
      }
    [@@unboxed]
  end

  module Mapper = struct
    type 'w t =
      { f :
          'a 'b 'at 'bt. ('at -> f:('a -> 'b) -> 'bt) -> ('a, 'b, 'w) Hk.t2
          -> ('at, 'bt, 'w) Hk.t2
      }
    [@@unboxed]
  end

  (* The type classes above are related with this GADT, using a phantom polymorphic
     variant type index. *)
  type ('c, 'w) t =
    | Equality : ([> equality ], _) t
    | Isomorphism : 'w Isomorphism.t -> ([> isomorphism ], 'w) t
    | Field : 'w Field.t -> ([> field ], 'w) t
    | Variant : 'w Variant.t -> ([> variant ], 'w) t
    | Constructor : 'w Constructor.t -> ([> constructor ], 'w) t
    | Getter : 'w Getter.t -> ([> getter ], 'w) t
    | Optional : 'w Optional.t -> ([> optional ], 'w) t
    | Optional_getter : 'w Optional_getter.t -> ([> optional_getter ], 'w) t
    | Nonempty : 'w Nonempty.t -> ([> nonempty ], 'w) t
    | Nonempty_getter : 'w Nonempty_getter.t -> ([> nonempty_getter ], 'w) t
    | Many : 'w Many.t -> ([> many ], 'w) t
    | Many_getter : 'w Many_getter.t -> ([> many_getter ], 'w) t
    | Mapper : 'w Mapper.t -> ([> mapper ], 'w) t

  let equality = Equality
  let isomorphism isomorphism = Isomorphism isomorphism
  let field field = Field field
  let variant variant = Variant variant
  let constructor constructor = Constructor constructor
  let getter getter = Getter getter
  let optional optional = Optional optional
  let optional_getter optional_getter = Optional_getter optional_getter
  let nonempty nonempty = Nonempty nonempty
  let nonempty_getter nonempty_getter = Nonempty_getter nonempty_getter
  let many many = Many many
  let many_getter many_getter = Many_getter many_getter
  let mapper mapper = Mapper mapper
end

type nonrec ('c, 'w) t = ('c, 'w) Create.t

module Run = struct
  (* These functions define how the coercions between different kinds of accessors
     actually work. For example, an accessor that is defined as a field can be used as a
     getter, so the [field] function includes a case for [Getter], and in that case it
     only uses the [get] ability of the defined field. *)

  let equality (_ : _ t) = Fn.id

  type 'w constructor_hack =
    | Constructor_hack : ([< constructor ], 'w) t -> 'w constructor_hack
  [@@unboxed]

  let constructor t =
    let (Constructor_hack (Constructor { f })) = Constructor_hack t in
    f
  ;;

  type 'w field_hack = Field_hack : ([< field ], 'w) t -> 'w field_hack [@@unboxed]

  let field t field_f =
    let (Field_hack t) = Field_hack t in
    match t with
    | Field { f } -> f field_f
    | Getter { f } -> f (fun at -> fst (field_f at))
    | Optional { f } -> f (fun at -> First (field_f at))
    | Optional_getter { f } -> f (fun at -> Some (fst (field_f at)))
    | Nonempty { f } ->
      f (fun at ->
        let open Nonempty.Let_syntax in
        let a, construct = field_f at in
        let%map b = Nonempty.access a in
        construct b)
    | Nonempty_getter { f } -> f (fun at -> Nonempty_getter.access (fst (field_f at)))
    | Many { f } ->
      f (fun at ->
        let open Many.Let_syntax in
        let a, construct = field_f at in
        let%map b = Many.access a in
        construct b)
    | Many_getter { f } -> f (fun at -> Many_getter.access (fst (field_f at)))
    | Mapper { f } ->
      f (fun at ~f ->
        let a, construct = field_f at in
        construct (f a))
  ;;

  type 'w getter_hack = Getter_hack : ([< getter ], 'w) t -> 'w getter_hack [@@unboxed]

  let getter t get =
    let (Getter_hack t) = Getter_hack t in
    match t with
    | Getter { f } -> f get
    | Optional_getter { f } -> f (fun at -> Some (get at))
    | Nonempty_getter { f } -> f (fun at -> Nonempty_getter.access (get at))
    | Many_getter { f } -> f (fun at -> Many_getter.access (get at))
  ;;

  type 'w isomorphism_hack =
    | Isomorphism_hack : ([< isomorphism ], 'w) t -> 'w isomorphism_hack
  [@@unboxed]

  let isomorphism t ~get ~construct =
    let (Isomorphism_hack t) = Isomorphism_hack t in
    match t with
    | Isomorphism { f } -> f ~get ~construct
    | Field { f } -> f (fun at -> get at, construct)
    | Variant { f } -> f ~match_:(fun at -> First (get at)) ~construct
    | Constructor { f } -> f construct
    | Getter { f } -> f get
    | Optional { f } -> f (fun at -> First (get at, construct))
    | Optional_getter { f } -> f (fun at -> Some (get at))
    | Nonempty { f } ->
      f (fun at ->
        let open Nonempty.Let_syntax in
        let%map b = Nonempty.access (get at) in
        construct b)
    | Nonempty_getter { f } -> f (fun at -> Nonempty_getter.access (get at))
    | Many { f } ->
      f (fun at ->
        let open Many.Let_syntax in
        let%map b = Many.access (get at) in
        construct b)
    | Many_getter { f } -> f (fun at -> Many_getter.access (get at))
    | Mapper { f } -> f (fun at ~f -> construct (f (get at)))
  ;;

  type 'w mapper_hack = Mapper_hack : ([< mapper ], 'w) t -> 'w mapper_hack [@@unboxed]

  let mapper t =
    let (Mapper_hack (Mapper { f })) = Mapper_hack t in
    f
  ;;

  type 'w many_hack = Many_hack : ([< many ], 'w) t -> 'w many_hack [@@unboxed]

  let many t traverse =
    let (Many_hack t) = Many_hack t in
    match t with
    | Many { f } -> f traverse
    | Many_getter { f } -> f (fun at -> Many_getter.of_many (traverse at))
    | Mapper { f } -> f (fun at ~f -> Ident.of_many (traverse at) ~access:f)
  ;;

  type 'w many_getter_hack =
    | Many_getter_hack : ([< many_getter ], 'w) t -> 'w many_getter_hack
  [@@unboxed]

  let many_getter t =
    let (Many_getter_hack (Many_getter { f })) = Many_getter_hack t in
    f
  ;;

  type 'w nonempty_hack = Nonempty_hack : ([< nonempty ], 'w) t -> 'w nonempty_hack
  [@@unboxed]

  let nonempty t traverse =
    let (Nonempty_hack t) = Nonempty_hack t in
    match t with
    | Nonempty { f } -> f traverse
    | Nonempty_getter { f } -> f (fun at -> Nonempty_getter.of_nonempty (traverse at))
    | Many { f } -> f (fun at -> Many.of_nonempty (traverse at))
    | Many_getter { f } -> f (fun at -> Many_getter.of_nonempty (traverse at))
    | Mapper { f } -> f (fun at ~f -> Ident.of_nonempty (traverse at) ~access:f)
  ;;

  type 'w nonempty_getter_hack =
    | Nonempty_getter_hack : ([< nonempty_getter ], 'w) t -> 'w nonempty_getter_hack
  [@@unboxed]

  let nonempty_getter t traverse =
    let (Nonempty_getter_hack t) = Nonempty_getter_hack t in
    match t with
    | Nonempty_getter { f } -> f traverse
    | Many_getter { f } -> f (fun at -> Many_getter.of_nonempty_getter (traverse at))
  ;;

  type 'w optional_hack = Optional_hack : ([< optional ], 'w) t -> 'w optional_hack
  [@@unboxed]

  let optional t optional_f =
    let (Optional_hack t) = Optional_hack t in
    match t with
    | Optional { f } -> f optional_f
    | Optional_getter { f } ->
      f (fun at ->
        match optional_f at with
        | First (a, _) -> Some a
        | Second _ -> None)
    | Many { f } ->
      f (fun at ->
        let open Many.Let_syntax in
        match optional_f at with
        | Either.First (a, construct) -> Many.access a >>| construct
        | Second bt -> return bt)
    | Many_getter { f } ->
      f (fun at ->
        match optional_f at with
        | Either.First (a, _) -> Many_getter.access a
        | Second _ -> Many_getter.empty)
    | Mapper { f } ->
      f (fun at ~f ->
        match optional_f at with
        | Either.First (a, construct) -> construct (f a)
        | Second bt -> bt)
  ;;

  type 'w optional_getter_hack =
    | Optional_getter_hack : ([< optional_getter ], 'w) t -> 'w optional_getter_hack
  [@@unboxed]

  let optional_getter t get_option =
    let (Optional_getter_hack t) = Optional_getter_hack t in
    match t with
    | Optional_getter { f } -> f get_option
    | Many_getter { f } ->
      f (fun at ->
        match get_option at with
        | Some a -> Many_getter.access a
        | None -> Many_getter.empty)
  ;;

  type 'w variant_hack = Variant_hack : ([< variant ], 'w) t -> 'w variant_hack
  [@@unboxed]

  let variant t ~match_ ~construct =
    let (Variant_hack t) = Variant_hack t in
    match t with
    | Variant { f } -> f ~match_ ~construct
    | Constructor { f } -> f construct
    | Optional { f } ->
      f (fun at ->
        match match_ at with
        | First a -> First (a, construct)
        | Second _ as bt -> bt)
    | Optional_getter { f } ->
      f (fun at ->
        match match_ at with
        | First a -> Some a
        | Second _ -> None)
    | Many { f } ->
      f (fun at ->
        let open Many.Let_syntax in
        match match_ at with
        | Either.First a -> Many.access a >>| construct
        | Second bt -> return bt)
    | Many_getter { f } ->
      f (fun at ->
        match match_ at with
        | Either.First a -> Many_getter.access a
        | Second _ -> Many_getter.empty)
    | Mapper { f } ->
      f (fun at ~f ->
        match match_ at with
        | Either.First a -> construct (f a)
        | Second bt -> bt)
  ;;
end
OCaml

Innovation. Community. Security.