Source file stdlib_ext.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
(*————————————————————————————————————————————————————————————————————————————
   Copyright (c) 2020–2021 Craig Ferguson <me@craigfe.io>
   Distributed under the MIT license. See terms at the end of this file.
  ————————————————————————————————————————————————————————————————————————————*)

type 'a pp = Format.formatter -> 'a -> unit

let ( >> ) f g x = g (f x)

let tap f x =
  f x;
  x

let trace fmt x =
  Fmt.epr fmt x;
  x

module type Eq = sig
  type t

  val equal : t -> t -> bool
end

module type Comparable_infix = sig
  type t

  val ( = ) : t -> t -> bool
  val ( <= ) : t -> t -> bool
  val ( >= ) : t -> t -> bool
  val ( < ) : t -> t -> bool
  val ( > ) : t -> t -> bool
end

module Poly = struct
  let ( = ) = Stdlib.( = )
  let ( <= ) = Stdlib.( <= )
  let ( >= ) = Stdlib.( >= )
  let ( < ) = Stdlib.( < )
  let ( > ) = Stdlib.( > )
end

include Stdlib.StdLabels
include Stdlib.MoreLabels

(** Shadow polymorphic operators in the Stdlib. *)
include struct
  let min : int -> int -> int = min
  let max : int -> int -> int = max
  let compare : int -> int -> int = compare

  include (Poly : Comparable_infix with type t := int)
end

module Int = struct
  include Int
  include (Poly : Comparable_infix with type t := t)

  let float_div a b = to_float a /. to_float b
end

module Int32 = struct
  include Int32
  include (Poly : Comparable_infix with type t := t)

  let float_div a b = to_float a /. to_float b
end

module Int63 = struct
  include Optint.Int63
  include (Poly : Comparable_infix with type t := t)

  let float_div a b = to_float a /. to_float b
end

type int63 = Int63.t

module Int64 = struct
  include Int64
  include (Poly : Comparable_infix with type t := t)

  let float_div a b = to_float a /. to_float b
end

module Float = struct
  include Float
  include (Poly : Comparable_infix with type t := t)

  let float_div = ( /. )
  let to_float x = x
  let of_float x = x
end

module Option = struct
  include Option

  let ( || ) a b = match a with Some _ -> a | None -> b
end

module Result = struct
  let get_or_invalid_arg = function
    | Ok x -> x
    | Error (`Msg s) -> invalid_arg s

  let errorf fmt = Format.kasprintf (fun s -> Error (`Msg s)) fmt
end

module List = struct
  include List

  let rec intersperse ~sep = function
    | ([] | [ _ ]) as l -> l
    | h1 :: (_ :: _ as tl) -> h1 :: sep :: intersperse ~sep tl
end

module Staged : sig
  type 'a t
  type ('a, 'b) endo := 'a t -> 'b t

  external map : f:('a -> 'b) -> ('a, 'b) endo = "%identity"
  external inj : 'a -> 'a t = "%identity"
  external prj : 'a t -> 'a = "%identity"

  module Syntax : sig
    val ( let$ ) : 'a t -> ('a -> 'b) -> 'b t
    val ( and$ ) : 'a t -> 'b t -> ('a * 'b) t
  end
end = struct
  type 'a t = 'a
  type ('a, 'b) endo = 'a t -> 'b t

  external map : f:('a -> 'b) -> ('a, 'b) endo = "%identity"
  external inj : 'a -> 'a t = "%identity"
  external prj : 'a t -> 'a = "%identity"

  module Syntax = struct
    let ( let$ ) x f = f x
    let ( and$ ) a b = (a, b)
  end
end

module Unique_id () : sig
  type t

  val create : unit -> t
  val equal : t -> t -> bool
  val pp : t pp
end = struct
  let allocated = ref 0

  type t = int

  let create () =
    let v = !allocated in
    incr allocated;
    v

  let equal = Int.equal
  let pp = Fmt.int
end

module Sta_dyn : sig
  type 'a t = Static of 'a | Dynamic of (unit -> 'a)

  val get : 'a t -> 'a
  val lift : ('a -> 'a -> 'a) -> 'a t -> 'a t -> 'a t
  val pp : 'a pp -> 'a t pp
end = struct
  type 'a t = Static of 'a | Dynamic of (unit -> 'a)

  let get = function Static x -> x | Dynamic f -> f ()

  let lift add x y =
    let ( ++ ) = add in
    match (x, y) with
    | Static x, Static y -> Static (x ++ y)
    | Dynamic f, Static x | Static x, Dynamic f -> Dynamic (fun () -> x ++ f ())
    | Dynamic f, Dynamic g -> Dynamic (fun () -> f () ++ g ())

  let pp pp_elt ppf = function
    | Static x -> Fmt.pf ppf "Static %a" pp_elt x
    | Dynamic f -> Fmt.pf ppf "Dynamic %a" pp_elt (f ())
end

(*————————————————————————————————————————————————————————————————————————————
   Copyright (c) 2020–2021 Craig Ferguson <me@craigfe.io>

   Permission to use, copy, modify, and/or distribute this software for any
   purpose with or without fee is hereby granted, provided that the above
   copyright notice and this permission notice appear in all copies.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
   THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
   DEALINGS IN THE SOFTWARE.
  ————————————————————————————————————————————————————————————————————————————*)