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

type 'a t = { f : 'r. combine:('r -> 'r -> 'r) -> f:('a -> 'r) -> 'r } [@@unboxed]

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

include Monad.Make (struct
    type nonrec 'a t = 'a t

    let return = access
    let map t ~f = { f = (fun ~combine ~f:g -> t.f ~combine ~f:(fun a -> g (f a))) }

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

    let map = `Custom map
  end)

let map_reduce t = t.f

let append t1 t2 =
  { f = (fun ~combine ~f -> combine (t1.f ~combine ~f) (t2.f ~combine ~f)) }
;;

module O = struct
  let ( @ ) = append
end

include O

include Nonempty.Of_applicative_without_return2 (struct
    type nonrec (_, 'a) t = 'a t

    let map t ~f:_ = t
    let apply = append
  end)

let of_nonempty nonempty = of_nonempty nonempty ~access