package prbnmcn-basic-structures

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

Source file impl_monad.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
type 'a cont = { fa : 'b. ('a -> 'b) -> 'b } [@@unboxed]

module Identity : Intf_monad.Monad with type 'a t = 'a and type 'a res = 'a =
struct
  type 'a t = 'a

  type 'a res = 'a

  let return x = x [@@inline]

  let bind m f = f m [@@inline]

  let map m f = f m [@@inline]

  let run x = x [@@inline]

  module Infix = struct
    let ( >>= ) = bind

    let ( >|= ) = map

    let ( let* ) = bind

    let return = return
  end
end

module Cps : Intf_monad.Monad with type 'a t = 'a cont and type 'a res = 'a =
struct
  type 'a t = 'a cont

  type 'a res = 'a

  let return x = { fa = (fun k -> k x) } [@@inline]

  let bind m f = { fa = (fun k -> m.fa (fun m -> (f m).fa k)) } [@@inline]

  let map m f = { fa = (fun k -> m.fa (fun m -> k (f m))) } [@@inline]

  let run x = x.fa (fun x -> x) [@@inline]

  module Infix = struct
    let ( >>= ) = bind

    let ( >|= ) = map

    let ( let* ) = bind

    let return = return
  end
end

module Codegen_cps
    (Repr : Intf_lang.Empty)
    (M : Intf_lang.Sequencing with type 'a m = 'a Repr.m) : sig
  type 'a cont = { fa : 'b. ('a -> 'b Repr.m) -> 'b Repr.m } [@@unboxed]

  include
    Intf_monad.Codegen_monad with type 'a m = 'a Repr.m and type 'a t = 'a cont
end = struct
  type 'a m = 'a Repr.m

  type 'a cont = { fa : 'b. ('a -> 'b Repr.m) -> 'b Repr.m } [@@unboxed]

  type 'a t = 'a cont

  let return x = { fa = (fun k -> k x) } [@@inline]

  let bind : 'a t -> ('a -> 'b t) -> 'b t =
   fun m f -> { fa = (fun k -> m.fa (fun m -> (f m).fa k)) }
   [@@inline]

  let run x = x.fa (fun x -> x) [@@inline]

  module Infix = struct
    let ( >>= ) = bind

    let ( let* ) = bind

    let ( let*! ) : 'a m -> ('a m -> 'b t) -> 'b t =
     fun m f ->
      bind
        { fa =
            (fun k ->
              M.(
                let* x = m in
                k x))
        }
        f
     [@@inline]

    let ( >> ) (m : unit m t) f =
      let* u = m in
      let*! _u = u in
      f ()

    let ( >>! ) (m : unit m) f =
      bind { fa = (fun k -> M.(seq m k)) } f
      [@@inline]
  end

  open Infix

  let lift1 f x =
    let* x = x in
    return (f x)

  let lift2 f x y =
    let* x = x in
    let* y = y in
    return (f x y)

  let lift3 f x y z =
    let* x = x in
    let* y = y in
    let* z = z in
    return (f x y z)
end

module Codegen_identity
    (Repr : Intf_lang.Empty with type 'a m = 'a)
    (M : Intf_lang.Sequencing with type 'a m = 'a) :
  Intf_monad.Codegen_monad with type 'a m = 'a and type 'a t = 'a = struct
  type 'a m = 'a

  type 'a t = 'a

  let return x = x [@@inline]

  let bind : 'a t -> ('a -> 'b t) -> 'b t = fun m f -> f m [@@inline]

  let run x = x [@@inline]

  module Infix = struct
    let ( >>= ) = bind

    let ( >> ) (m : unit m t) f = bind m @@ fun _u -> f ()

    let ( let* ) = bind

    let ( let*! ) = bind

    let ( >>! ) (m : unit m) f = f m
  end

  open Infix

  let lift1 f x =
    let* x = x in
    return (f x)

  let lift2 f x y =
    let* x = x in
    let* y = y in
    return (f x y)

  let lift3 f x y z =
    let* x = x in
    let* y = y in
    let* z = z in
    return (f x y z)
end
OCaml

Innovation. Community. Security.