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
|
(****************************************************************************)
(* the diy toolsuite *)
(* *)
(* Jade Alglave, University College London, UK. *)
(* Luc Maranget, INRIA Paris-Rocquencourt, France. *)
(* *)
(* Copyright 2010-present Institut National de Recherche en Informatique et *)
(* en Automatique, ARM Ltd and the authors. All rights reserved. *)
(* *)
(* This software is governed by the CeCILL-B license under French law and *)
(* abiding by the rules of distribution of free software. You can use, *)
(* modify and/ or redistribute the software under the terms of the CeCILL-B *)
(* license as circulated by CEA, CNRS and INRIA at the following URL *)
(* "http://www.cecill.info". We also give a copy in LICENSE.txt. *)
(****************************************************************************)
(** Extending built-in / base modules, either to port future features into
* earlier versions of OCaml, or to add extra functionality. *)
module Fun = struct
exception Finally_raised of exn
let negate f =
fun a -> not (f a)
let protect ~finally f =
let finally' () =
try finally ()
with e -> raise (Finally_raised e)
in
let ret =
try
f ()
with e -> begin
finally' () ;
raise e
end
in
finally' () ;
ret
let open_out_protect f name =
let out = open_out name in
protect ~finally:(fun () -> close_out out) (fun () -> f out)
end
module List = struct
include List
let rec compare cf xs ys =
match xs, ys with
| [], [] -> 0
| [], _ -> -1
| _, [] -> 1
| x :: xs, y :: ys ->
match cf x y with
| 0 -> compare cf xs ys
| n -> n
let to_ocaml_string f xs =
Printf.sprintf "[%s]" (String.concat "; " (List.map f xs))
end
module Option = struct
type 'a t = 'a option
let get o =
match o with
| None -> invalid_arg "option is None"
| Some v -> v
let value o ~default =
match o with
| None -> default
| Some v -> v
let map f o =
match o with
| None -> None
| Some v -> Some (f v)
let is_none o =
match o with
| None -> true
| Some _ -> false
let compare cf a b =
match a, b with
| None, None -> 0
| Some _, None -> 1
| None, Some _ -> -1
| Some a, Some b -> cf a b
let to_ocaml_string f o =
match o with
| None -> "None"
| Some a -> Printf.sprintf "Some (%s)" (f a)
end
module String = struct
include String
let to_ocaml_string s = Printf.sprintf "%S" s
end
module Iter = struct
type 'a t = unit -> 'a option
let of_list xs =
let r = ref xs in
fun () ->
match !r with
| [] -> None
| x::xs -> r := xs ; Some x
end
|