package irmin
Irmin, a distributed database that follows the same design principles as Git
Install
Dune Dependency
Authors
Maintainers
Sources
irmin-2.2.0.tbz
sha256=a44e018495336e0f632433fcae7b4e84699938a7110212da9e3818b35048fc3f
sha512=8dd9e9f09877a5541ee1be3387e041f63e6b522f9efac388d72199f965b0692f2502e93c1ddc2a5f959289fa2f75f06849582cffbcc201de19e9bd50413f6115
doc/src/irmin.type/type.ml.html
Source file type.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 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
(* * Copyright (c) 2016-2017 Thomas Gazagnaire <thomas@gazagnaire.org> * * Permission to use, copy, modify, and 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" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. *) include Type_core let pre_hash t x = let rec aux : type a. a t -> a bin_seq = fun t v k -> match t with | Self s -> aux s.self_fix v k | Map m -> aux m.x (m.g v) k | Custom c -> c.pre_hash v k | _ -> Type_binary.encode_bin ?headers:(Some false) t v k in aux t x let short_hash t ?seed x = match t with | Custom c -> c.short_hash ?seed x | _ -> let seed = match seed with None -> 0 | Some t -> t in let h = ref seed in pre_hash t x (fun s -> h := Hashtbl.seeded_hash !h s); !h (* Combinators for Irmin types *) let unit = Prim Unit let bool = Prim Bool let char = Prim Char let int = Prim Int let int32 = Prim Int32 let int64 = Prim Int64 let float = Prim Float let string = Prim (String `Int) let bytes = Prim (Bytes `Int) let string_of n = Prim (String n) let bytes_of n = Prim (Bytes n) let list ?(len = `Int) v = List { v; len } let array ?(len = `Int) v = Array { v; len } let pair a b = Tuple (Pair (a, b)) let triple a b c = Tuple (Triple (a, b, c)) let option a = Option a let v ~cli ~json ~bin ~equal ~compare ~short_hash ~pre_hash = let pp, of_string = cli in let encode_json, decode_json = json in let encode_bin, decode_bin, size_of = bin in Custom { cwit = `Witness (Witness.make ()); pp; of_string; pre_hash; encode_json; decode_json; encode_bin; decode_bin; size_of; compare; equal; short_hash; } (* fix points *) let mu : type a. (a t -> a t) -> a t = fun f -> let rec fake_x : a self = { self_unroll = f; self_fix = Self fake_x } in let real_x = f (Self fake_x) in fake_x.self_fix <- real_x; Self fake_x let mu2 : type a b. (a t -> b t -> a t * b t) -> a t * b t = fun f -> let rec fake_x = let self_unroll a = let b = mu (fun b -> f a b |> snd) in f a b |> fst in { self_unroll; self_fix = Self fake_x } in let rec fake_y = let self_unroll b = let a = mu (fun a -> f a b |> fst) in f a b |> snd in { self_unroll; self_fix = Self fake_y } in let real_x, real_y = f (Self fake_x) (Self fake_y) in fake_x.self_fix <- real_x; fake_y.self_fix <- real_y; (Self fake_x, Self fake_y) (* records *) type ('a, 'b, 'c) open_record = ('a, 'c) fields -> string * 'b * ('a, 'b) fields let field fname ftype fget = check_valid_utf8 fname; { fname; ftype; fget } let record : string -> 'b -> ('a, 'b, 'b) open_record = fun n c fs -> (n, c, fs) let app : type a b c d. (a, b, c -> d) open_record -> (a, c) field -> (a, b, d) open_record = fun r f fs -> let n, c, fs = r (F1 (f, fs)) in (n, c, fs) module String_Set = Set.Make (String) (** [check_unique f l] checks that all the strings in [l] are unique. Otherwise, calls [f dup] with [dup] the first duplicate. *) let check_unique f = let rec aux set = function | [] -> () | x :: xs -> ( match String_Set.find_opt x set with | None -> aux (String_Set.add x set) xs | Some _ -> f x ) in aux String_Set.empty let check_unique_field_names rname rfields = let names = List.map (fun (Field { fname; _ }) -> fname) rfields in let failure fname = Fmt.invalid_arg "The name %s was used for two or more fields in record %s." fname rname in check_unique failure names let sealr : type a b. (a, b, a) open_record -> a t = fun r -> let rname, c, fs = r F0 in let rwit = Witness.make () in let sealed = { rwit; rname; rfields = Fields (fs, c) } in check_unique_field_names rname (fields sealed); Record sealed let ( |+ ) = app (* variants *) type 'a case_p = 'a case_v type ('a, 'b) case = int -> 'a a_case * 'b let case0 cname0 c0 = check_valid_utf8 cname0; fun ctag0 -> let c = { ctag0; cname0; c0 } in (C0 c, CV0 c) let case1 cname1 ctype1 c1 = check_valid_utf8 cname1; fun ctag1 -> let c = { ctag1; cname1; ctype1; c1 } in (C1 c, fun v -> CV1 (c, v)) type ('a, 'b, 'c) open_variant = 'a a_case list -> string * 'c * 'a a_case list let variant n c vs = (n, c, vs) let app v c cs = let n, fc, cs = v cs in let c, f = c (List.length cs) in (n, fc f, c :: cs) let check_unique_case_names vname vcases = let n0, n1 = List.partition (function C0 _ -> true | C1 _ -> false) vcases in let names0 = List.map (function C0 { cname0; _ } -> cname0 | _ -> assert false) n0 in let names1 = List.map (function C1 { cname1; _ } -> cname1 | _ -> assert false) n1 in check_unique (fun cname -> Fmt.invalid_arg "The name %s was used for two or more case0 in variant or enum %s." cname vname) names0; check_unique (fun cname -> Fmt.invalid_arg "The name %s was used for two or more case1 in variant or enum %s." cname vname) names1 let sealv v = let vname, vget, vcases = v [] in check_unique_case_names vname vcases; let vwit = Witness.make () in let vcases = Array.of_list (List.rev vcases) in Variant { vwit; vname; vcases; vget } let ( |~ ) = app type empty = | (* Encode [empty] as a variant with no constructors *) let empty = variant "empty" (fun _ -> assert false) |> sealv let enum vname l = let vwit = Witness.make () in let _, vcases, mk = List.fold_left (fun (ctag0, cases, mk) (n, v) -> check_valid_utf8 n; let c = { ctag0; cname0 = n; c0 = v } in (ctag0 + 1, C0 c :: cases, (v, CV0 c) :: mk)) (0, [], []) l in check_unique_case_names vname vcases; let vcases = Array.of_list (List.rev vcases) in Variant { vwit; vname; vcases; vget = (fun x -> List.assq x mk) } let result a b = variant "result" (fun ok error -> function Ok x -> ok x | Error x -> error x) |~ case1 "ok" a (fun a -> Ok a) |~ case1 "error" b (fun b -> Error b) |> sealv let like ?cli ?json ?bin ?equal ?compare ?short_hash:h ?pre_hash:p t = let encode_json, decode_json = let ( >|= ) l f = match l with Ok l -> Ok (f l) | Error _ as e -> e in let join = function Error _ as e -> e | Ok x -> x in match json with | Some (x, y) -> (x, y) | None -> ( let rec is_prim : type a. a t -> bool = function | Self s -> is_prim s.self_fix | Map m -> is_prim m.x | Prim _ -> true | _ -> false in match (t, cli) with | ty, Some (pp, of_string) when is_prim ty -> let ty = string in ( (fun ppf u -> Type_json.encode ty ppf (Fmt.to_to_string pp u)), fun buf -> Type_json.decode ty buf >|= of_string |> join ) | _ -> (Type_json.encode t, Type_json.decode t) ) in let pp, of_string = match cli with | Some (x, y) -> (x, y) | None -> (Type_pp.t t, Type_pp.of_string t) in let encode_bin, decode_bin, size_of = match bin with | Some (x, y, z) -> (x, y, z) | None -> (Type_binary.encode_bin t, Type_binary.decode_bin t, Type_size.t t) in let equal = match equal with | Some x -> x | None -> ( match compare with | Some f -> fun x y -> f x y = 0 | None -> Type_ordered.equal t ) in let compare = match compare with Some x -> x | None -> Type_ordered.compare t in let short_hash ?seed = match h with Some x -> x | None -> short_hash ?seed t in let pre_hash = match p with Some x -> x | None -> encode_bin ?headers:(Some false) in Custom { cwit = `Type t; pp; of_string; encode_json; decode_json; encode_bin; decode_bin; size_of; compare; equal; short_hash; pre_hash; } let map ?cli ?json ?bin ?equal ?compare ?short_hash ?pre_hash x f g = match (cli, json, bin, equal, compare, short_hash, pre_hash) with | None, None, None, None, None, None, None -> Map { x; f; g; mwit = Witness.make () } | _ -> let x = Map { x; f; g; mwit = Witness.make () } in like ?cli ?json ?bin ?equal ?compare ?short_hash ?pre_hash x module type S = sig type t val t : t ty end let equal, compare = Type_ordered.(equal, compare) let pp, pp_ty, to_string, of_string = Type_pp.(t, ty, to_string, of_string) let ( to_json_string, of_json_string, pp_json, encode_json, decode_json, decode_json_lexemes ) = Type_json.(to_string, of_string, pp, encode, decode_jsonm, decode_lexemes) let encode_bin, decode_bin, to_bin_string, of_bin_string = Type_binary.(encode_bin, decode_bin, to_bin_string, of_bin_string) let size_of = Type_size.t
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>