package core_extended

  1. Overview
  2. Docs
Extra components that are not as closely vetted or as stable as Core

Install

Dune Dependency

Authors

Maintainers

Sources

v0.17.0.tar.gz
sha256=17de5f7cf59818d757bb0625c55f0afc84509122645b7782fb522ac98c3be446

doc/src/core_extended.immediate_kernel/immediate_kernel.ml.html

Source file immediate_kernel.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
open Core.Core_stable

module Char_option_stable = struct
  module V1 = struct
    type t = int [@@deriving bin_io, compare, hash]

    let is_none t = Core.Int.(t < 0 || t > 255)
    let is_some t = not (is_none t)
    let none = -1
    let some = Core.Char.to_int
    let some_is_representable _ = true
    let unchecked_value = Core.Char.unsafe_of_int
    let value_exn = Core.Char.of_int_exn
    let value t ~default = Core.Bool.select (is_none t) default (unchecked_value t)
    let to_option t = if is_some t then Some (unchecked_value t) else None

    let of_option = function
      | None -> none
      | Some v -> some v
    ;;

    let sexp_of_t t = to_option t |> [%sexp_of: char option]
    let t_of_sexp s = [%of_sexp: char option] s |> of_option
  end
end

module Bool_option_stable = struct
  module V1 = struct
    type t = int [@@deriving bin_io, compare, hash, stable_witness]

    let to_wire t = t
    let of_wire t = t
    let none = -1
    let is_none t = t = none
    let is_some t = t <> none
    let some = Core.Bool.to_int
    let some_is_representable _ = true
    let unchecked_value t = t <> 0

    let value_exn_not_found =
      Not_found_s [%message "[Immediate.Bool.Option.value_exn]: given [none]"]
    ;;

    let value_exn = function
      | 0 -> false
      | 1 -> true
      | _ -> raise value_exn_not_found
    ;;

    let value t ~default = Core.Bool.select (is_none t) default (unchecked_value t)
    let to_option t = if is_some t then Some (unchecked_value t) else None

    let of_option = function
      | None -> none
      | Some v -> some v
    ;;

    let sexp_of_t t = to_option t |> [%sexp_of: bool option]
    let t_of_sexp s = [%of_sexp: bool option] s |> of_option
  end
end

module Int_option_stable = struct
  module V1 = struct
    let typerep_of_int = Core.Typerep.Int

    type t = int
    [@@deriving bin_io, compare, equal, globalize, hash, stable_witness, typerep]

    let none = Core.Int.min_value
    let is_none t = t = none
    let is_some t = not (is_none t)

    let some t =
      assert (is_some t);
      t
    ;;

    let some_is_representable = is_some
    let unchecked_value t = t
    let to_option t = if is_some t then Some (unchecked_value t) else None

    let of_option = function
      | None -> none
      | Some v -> some v
    ;;

    let sexp_of_t t = to_option t |> [%sexp_of: int option]
    let t_of_sexp s = [%of_sexp: int option] s |> of_option
    let to_int (t : t) : int = t
    let of_int (i : int) : t = i
  end
end

module type Intable_sexpable = sig
  include Core.Intable
  include Core.Sexpable with type t := t

  val bin_shape_uuid : Bin_shape.Uuid.t
end

module Intable_sexpable_option_stable = struct
  module Make (I : Intable_sexpable) = struct
    module V1 = struct
      type t = int [@@deriving bin_io, compare, hash, stable_witness]

      let bin_shape_t = Bin_shape.annotate I.bin_shape_uuid bin_shape_t
      let none = Int_option_stable.V1.none
      let is_none t = t = none
      let is_some t = not (is_none t)

      let some i =
        let t = I.to_int_exn i in
        assert (is_some t);
        t
      ;;

      let some_is_representable i = I.to_int_exn i |> is_some
      let unchecked_value t = I.of_int_exn t
      let to_option t = if is_some t then Some (unchecked_value t) else None

      let of_option = function
        | None -> none
        | Some v -> some v
      ;;

      let to_int (t : t) : int = t
      let of_int (i : int) : t = i

      let value_exn_not_found =
        Not_found_s
          [%message "[Immediate.Of_intable.Option.Make(_).value_exn]: given [none]"]
      ;;

      let value_exn t = if is_some t then unchecked_value t else raise value_exn_not_found
      let value t ~default = if is_some t then unchecked_value t else default
      let sexp_of_t t = to_option t |> [%sexp_of: I.t option]
      let t_of_sexp s = [%of_sexp: I.t option] s |> of_option
    end
  end
end

open Core
include Immediate_kernel_intf

module Char = struct
  include (
    struct
      include Char

      let globalize = globalize_char
    end :
      S_no_option with type t = char)

  module Option = struct
    module Stable = Char_option_stable
    include Stable.V1
    include (Int : Typerep_lib.Typerepable.S with type t := t)

    module Optional_syntax = struct
      module Optional_syntax = struct
        let is_none = is_none
        let unsafe_value = unchecked_value
      end
    end

    include Identifiable.Make (struct
      include Stable.V1
      include Sexpable.To_stringable (Stable.V1)

      let hash (t : t) = t
      let module_name = "Immediate.Char.Option"
    end)

    (* Export inlineable comparisons (those from the functor confuse the compiler). *)
    include Int.Replace_polymorphic_compare
  end
end

module Bool = struct
  include (
    struct
      include Bool

      let globalize = globalize_bool
    end :
      S_no_option with type t = bool)

  module Option = struct
    module Stable = Bool_option_stable
    include Stable.V1
    include (Int : Typerep_lib.Typerepable.S with type t := t)

    module Optional_syntax = struct
      module Optional_syntax = struct
        let is_none = is_none
        let unsafe_value = unchecked_value
      end
    end

    include Identifiable.Make (struct
      include Stable.V1
      include Sexpable.To_stringable (Stable.V1)

      let hash (t : t) = t
      let module_name = "Immediate.Bool.Option"
    end)

    (* Export inlineable comparisons (those from the functor confuse the compiler). *)
    include Int.Replace_polymorphic_compare
  end
end

module Int = struct
  include (
    struct
      include Int

      let globalize = globalize_int
    end :
      S_no_option with type t = int)

  let type_immediacy = Type_immediacy.Always.of_typerep_exn [%here] typerep_of_t

  module Option = struct
    module Stable = Int_option_stable
    include Stable.V1
    include (Int : Typerep_lib.Typerepable.S with type t := t)

    let value_exn_not_found =
      Not_found_s [%message "[Immediate.Int.Option.value_exn]: given [none]"]
    ;;

    let value_exn t = if is_some t then t else raise value_exn_not_found
    let value t ~default = Core.Bool.select (is_none t) default t
    let unchecked_some t = t
    let type_immediacy = Type_immediacy.Always.of_typerep_exn [%here] typerep_of_t

    module Optional_syntax = struct
      module Optional_syntax = struct
        let is_none = is_none
        let unsafe_value = unchecked_value
      end
    end

    include Identifiable.Make (struct
      include Stable.V1
      include Sexpable.To_stringable (Stable.V1)

      let hash (t : t) = t
      let module_name = "Immediate.Int.Option"
    end)

    (* Export inlineable comparisons (those from the functor confuse the compiler). *)
    include Int.Replace_polymorphic_compare
  end
end

module Of_intable = struct
  module type S = Intable_sexpable

  module Option = struct
    module Make (I : S) = struct
      module Stable = Intable_sexpable_option_stable.Make (I)
      include Stable.V1
      include (Int : Typerep_lib.Typerepable.S with type t := t)

      module Optional_syntax = struct
        module Optional_syntax = struct
          let is_none = is_none
          let unsafe_value = unchecked_value
        end
      end

      include Identifiable.Make (struct
        include Stable.V1
        include Sexpable.To_stringable (Stable.V1)

        let hash (t : t) = t
        let module_name = "Immediate.Int.Option"
      end)

      (* Export inlineable comparisons (those from the functor confuse the compiler). *)
      include Int.Replace_polymorphic_compare
    end
  end
end

module Immediate_kernel_stable = struct
  module Char = struct
    module Option = Char.Option.Stable
  end

  module Bool = struct
    module Option = Bool.Option.Stable
  end

  module Int = struct
    module Option = Int.Option.Stable
  end
end
OCaml

Innovation. Community. Security.