package octez-libs
A package that contains multiple base libraries used by the Octez suite
Install
Dune Dependency
Authors
Maintainers
Sources
tezos-octez-v20.1.tag.bz2
sha256=ddfb5076eeb0b32ac21c1eed44e8fc86a6743ef18ab23fff02d36e365bb73d61
sha512=d22a827df5146e0aa274df48bc2150b098177ff7e5eab52c6109e867eb0a1f0ec63e6bfbb0e3645a6c2112de3877c91a17df32ccbff301891ce4ba630c997a65
doc/src/octez-libs.base/bounded.ml.html
Source file bounded.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
(*****************************************************************************) (* *) (* Open Source License *) (* Copyright (c) 2022 Trili Tech, <contact@trili.tech> *) (* Copyright (c) 2022 Nomadic Labs, <contact@nomadic-labs.com> *) (* *) (* Permission is hereby granted, free of charge, to any person obtaining a *) (* copy of this software and associated documentation files (the "Software"),*) (* to deal in the Software without restriction, including without limitation *) (* the rights to use, copy, modify, merge, publish, distribute, sublicense, *) (* and/or sell copies of the Software, and to permit persons to whom the *) (* Software is furnished to do so, subject to the following conditions: *) (* *) (* The above copyright notice and this permission notice shall be included *) (* in all copies or substantial portions of the Software. *) (* *) (* 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. *) (* *) (*****************************************************************************) (* This datatype aims to represent encodings from [Data_encoding.t]. The GADT is used to know what is the underlying ocaml datatype. This allows to write the logic of this module in a generic manner, as long as the tests. *) type 'ocaml ty = | Int64 : int64 ty | Int32 : int32 ty | Int31 : int ty | Int16 : int ty | Uint16 : int ty | Int8 : int ty | Uint8 : int ty let encoding : type a. a ty -> a Data_encoding.t = function | Int64 -> Data_encoding.int64 | Int32 -> Data_encoding.int32 | Int31 -> Data_encoding.int31 | Int16 -> Data_encoding.int16 | Uint16 -> Data_encoding.uint16 | Int8 -> Data_encoding.int8 | Uint8 -> Data_encoding.uint8 let compare : type a. a ty -> (module Compare.S with type t = a) = function | Int64 -> (module Compare.Int64) | Int32 -> (module Compare.Int32) | Int31 -> (module Compare.Int) | Int16 -> (module Compare.Int) | Uint16 -> (module Compare.Int) | Int8 -> (module Compare.Int) | Uint8 -> (module Compare.Int) let pp_ty : type a. Format.formatter -> a ty -> unit = fun fmt ty -> match ty with | Int64 -> Format.fprintf fmt "int64" | Int32 -> Format.fprintf fmt "int32" | Int31 -> Format.fprintf fmt "int31" | Int16 -> Format.fprintf fmt "int16" | Uint16 -> Format.fprintf fmt "uint16" | Int8 -> Format.fprintf fmt "int8" | Uint8 -> Format.fprintf fmt "uint8" let pp : type a. a ty -> Format.formatter -> a -> unit = function | Int64 -> fun fmt value -> Format.fprintf fmt "%Ld" value | Int32 -> fun fmt value -> Format.fprintf fmt "%ld" value | Int31 -> Format.pp_print_int | Int16 -> Format.pp_print_int | Uint16 -> Format.pp_print_int | Int8 -> Format.pp_print_int | Uint8 -> Format.pp_print_int let ty_max_value : type a. a ty -> a = function | Int64 -> Int64.max_int | Int32 -> Int32.max_int | Int31 -> (1 lsl 30) - 1 | Int16 -> (1 lsl 15) - 1 | Uint16 -> (1 lsl 16) - 1 | Int8 -> (1 lsl 7) - 1 | Uint8 -> (1 lsl 8) - 1 let ty_min_value : type a. a ty -> a = function | Int64 -> Int64.min_int | Int32 -> Int32.min_int | Int31 -> -(1 lsl 30) | Int16 -> -(1 lsl 15) | Uint16 -> 0 | Int8 -> -(1 lsl 7) | Uint8 -> 0 module type BOUNDS = sig type ocaml_type val min_value : ocaml_type val max_value : ocaml_type end module type S = sig type t type ocaml_type include BOUNDS with type ocaml_type := ocaml_type include Compare.S with type t := t val encoding : t Data_encoding.t val pp : Format.formatter -> t -> unit val to_value : t -> ocaml_type val of_value : ocaml_type -> t option end (* If the encoding choosen can represent strictly less values than the underlying ocaml datatype, some exceptions could be raised at encoding time. Those static checks aims to be executed when the functor is instantiated to detect those cases sooner. *) let checks (type ocaml_type) (ty : ocaml_type ty) ( < ) ( > ) ~min_value ~max_value = let pp = pp ty in if max_value > ty_max_value ty then invalid_arg (Format.asprintf "Tezos-base.Bounded(%a): Maximum encodable value: %a. Bound given: %a" pp_ty ty pp (ty_max_value ty) pp max_value) ; if min_value < ty_min_value ty then invalid_arg (Format.asprintf "Tezos-base.Bounded(%a): Minimum encodable value: %a. Bound given: %a" pp_ty ty pp (ty_max_value ty) pp max_value) [@@inline always] (* A partial encoding that ensures the decoded value is in the specified bounds. *) let guarded_encoding ty ~to_value ~of_value = let open Data_encoding in conv_with_guard to_value (fun x -> match of_value x with None -> Error "Out of bounds" | Some x -> Ok x) (encoding ty) [@@inline always] let of_value ( < ) ( > ) ~min_value ~max_value x = if x < min_value then None else if x > max_value then None else Some x [@@inline always] (* We introduce one functor by OCaml datatype so that comparison functions are statically known and consequently inlined. Using the GADT, we could generalise this, but OCaml (without flambda) is unable to make the correct optimisations to inline the comparison functions. All the business code has been factored out so only the declaration is duplicated. *) module Int64 (B : BOUNDS with type ocaml_type := int64) = struct include Compare.Int64 include B let to_value = Fun.id let of_value = of_value ( < ) ( > ) ~min_value ~max_value let encoding = guarded_encoding Int64 ~to_value ~of_value let pp = pp Int64 let () = checks Int64 ( < ) ( > ) ~min_value ~max_value end module Int32 (B : BOUNDS with type ocaml_type := int32) = struct include Compare.Int32 include B let to_value = Fun.id let of_value = of_value ( < ) ( > ) ~min_value ~max_value let encoding = guarded_encoding Int32 ~to_value ~of_value let pp = pp Int32 let () = checks Int32 ( < ) ( > ) ~min_value ~max_value end (* A specifialisation of the functor above where the interval is restricted to the non negative integer that can be represented on 4 bytes. *) module Non_negative_int32 = Int32 (struct let min_value = 0l let max_value = Stdlib.Int32.max_int end) (* The parameter [T] of this functor allows to choose the desired encoding without duplicating the interface. *) module Make31 (T : sig val ty : int ty end) (B : BOUNDS with type ocaml_type := int) = struct include Compare.Int include B let to_value = Fun.id let of_value = of_value ( < ) ( > ) ~min_value ~max_value let encoding = guarded_encoding T.ty ~to_value ~of_value let pp = pp T.ty let () = checks T.ty ( < ) ( > ) ~min_value ~max_value end module Int31 = Make31 (struct let ty = Int31 end) module Int16 = Make31 (struct let ty = Int16 end) module Uint16 = Make31 (struct let ty = Uint16 end) module Int8 = Make31 (struct let ty = Int8 end) module Uint8 = Make31 (struct let ty = Uint8 end) module Internal_for_tests = struct type 'ocaml t = 'ocaml ty = | Int64 : int64 t | Int32 : int32 t | Int31 : int t | Int16 : int t | Uint16 : int t | Int8 : int t | Uint8 : int t let min_value = ty_min_value let max_value = ty_max_value let compare = compare let pp_ty = pp_ty end
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>