From ed9a966cefc9be11422c33b9659f20816c08c76d Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Sat, 2 Feb 2019 22:38:59 -0600 Subject: [PATCH] refactor: require 4.03 at least, add inline annotations --- .travis.yml | 1 - README.md | 2 +- containers.opam | 4 +- doc/containers.md | 4 +- src/core/CCFormat.ml | 4 +- src/core/CCFormat.mli | 4 +- src/core/CCHashtbl.ml | 18 ++++----- src/core/CCIO.ml | 12 +++--- src/core/CCIO.mli | 2 +- src/core/CCList.ml | 60 ++++++++++++++-------------- src/core/CCList.mli | 4 +- src/core/CCListLabels.mli | 4 +- src/core/CCMap.ml | 26 ++++++------ src/core/CCOpt.ml | 83 +++++++++++++++++++-------------------- src/core/CCOpt.mli | 6 +-- src/core/CCParse.ml | 8 ++-- src/core/CCParse.mli | 2 +- src/core/CCResult.ml | 72 ++++++++++++++++----------------- src/core/CCResult.mli | 8 ++-- src/core/CCVector.ml | 56 +++++++++++++------------- src/core/dune | 2 +- src/iter/dune | 3 +- src/sexp/CCSexp.ml | 24 +++++------ src/sexp/CCSexp.mli | 2 +- src/sexp/dune | 3 +- src/unix/CCUnix.ml | 2 +- src/unix/CCUnix.mli | 2 +- src/unix/dune | 4 +- 28 files changed, 207 insertions(+), 215 deletions(-) diff --git a/.travis.yml b/.travis.yml index db290d32..1a064439 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,7 +8,6 @@ env: - PINS="containers:." - DISTRO="ubuntu-16.04" matrix: - - PACKAGE="containers" OCAML_VERSION="4.02.3" EXTRA_DEPS="base-threads base-unix" TESTS=false - PACKAGE="containers" OCAML_VERSION="4.03" EXTRA_DEPS="base-threads base-unix" - PACKAGE="containers" OCAML_VERSION="4.04" EXTRA_DEPS="base-threads base-unix" - PACKAGE="containers" OCAML_VERSION="4.05" EXTRA_DEPS="base-threads base-unix" diff --git a/README.md b/README.md index 3015bc42..392a3b03 100644 --- a/README.md +++ b/README.md @@ -167,7 +167,7 @@ per-version doc [there](http://c-cube.github.io/ocaml-containers/). ## Build -You will need OCaml `>=` 4.02.0. +You will need OCaml `>=` 4.03.0, `dune`, `base-bytes`. ### Via opam diff --git a/containers.opam b/containers.opam index d4d4fefa..011e5f4f 100644 --- a/containers.opam +++ b/containers.opam @@ -11,8 +11,6 @@ build: [ ] depends: [ "dune" {build} - "result" - "uchar" "qtest" { with-test } "qcheck" { with-test } "ounit" { with-test } @@ -21,7 +19,7 @@ depends: [ "uutf" { with-test } "mdx" { with-test } "odoc" { with-doc } - "ocaml" { >= "4.02.0" } + "ocaml" { >= "4.03.0" } ] depopts: [ "base-unix" diff --git a/doc/containers.md b/doc/containers.md index d96f7683..7b288d37 100644 --- a/doc/containers.md +++ b/doc/containers.md @@ -73,9 +73,9 @@ Now we can parse strings using `expr`: ```ocaml # P.parse_string expr "4*1+2";; (* Ok 6 *) -- : int P.or_error = Result.Ok 6 +- : int P.or_error = Ok 6 # P.parse_string expr "4*(1+2)";; (* Ok 12 *) -- : int P.or_error = Result.Ok 12 +- : int P.or_error = Ok 12 ``` diff --git a/src/core/CCFormat.ml b/src/core/CCFormat.ml index dbc2447d..67fb027b 100644 --- a/src/core/CCFormat.ml +++ b/src/core/CCFormat.ml @@ -417,8 +417,8 @@ module Dump = struct let triple p1 p2 p3 = within "(" ")" (hovbox (triple p1 p2 p3)) let quad p1 p2 p3 p4 = within "(" ")" (hovbox (quad p1 p2 p3 p4)) let result' pok perror out = function - | Result.Ok x -> Format.fprintf out "(@[Ok %a@])" pok x - | Result.Error e -> Format.fprintf out "(@[Error %a@])" perror e + | Ok x -> Format.fprintf out "(@[Ok %a@])" pok x + | Error e -> Format.fprintf out "(@[Error %a@])" perror e let result pok = result' pok string let to_string = to_string end diff --git a/src/core/CCFormat.mli b/src/core/CCFormat.mli index 506b6aa7..3fdb0b27 100644 --- a/src/core/CCFormat.mli +++ b/src/core/CCFormat.mli @@ -329,8 +329,8 @@ module Dump : sig val quad : 'a t -> 'b t -> 'c t -> 'd t -> ('a * 'b * 'c * 'd) t - val result : 'a t -> ('a, string) Result.result t - val result' : 'a t -> 'e t -> ('a, 'e) Result.result t + val result : 'a t -> ('a, string) result t + val result' : 'a t -> 'e t -> ('a, 'e) result t val to_string : 'a t -> 'a -> string (** Alias to {!CCFormat.to_string}. *) end diff --git a/src/core/CCHashtbl.ml b/src/core/CCHashtbl.ml index e092b912..99f23948 100644 --- a/src/core/CCHashtbl.ml +++ b/src/core/CCHashtbl.ml @@ -247,11 +247,11 @@ module Make(X : Hashtbl.HashedType) = struct include Hashtbl.Make(X) - let get tbl x = + let[@inline] get tbl x = try Some (find tbl x) with Not_found -> None - let get_or tbl x ~default = + let[@inline] get_or tbl x ~default = try find tbl x with Not_found -> default @@ -292,12 +292,10 @@ module Make(X : Hashtbl.HashedType) else replace tbl x (n-by) with Not_found -> () - let keys tbl k = iter (fun key _ -> k key) tbl - - let values tbl k = iter (fun _ v -> k v) tbl - - let keys_list tbl = fold (fun k _ a -> k::a) tbl [] - let values_list tbl = fold (fun _ v a -> v::a) tbl [] + let[@inline] keys tbl k = iter (fun key _ -> k key) tbl + let[@inline] values tbl k = iter (fun _ v -> k v) tbl + let[@inline] keys_list tbl = fold (fun k _ a -> k::a) tbl [] + let[@inline] values_list tbl = fold (fun _ v a -> v::a) tbl [] let map_list f h = fold @@ -319,9 +317,9 @@ module Make(X : Hashtbl.HashedType) add tbl k v; v - let to_seq tbl k = iter (fun key v -> k (key,v)) tbl + let[@inline] to_seq tbl k = iter (fun key v -> k (key,v)) tbl - let add_seq tbl seq = seq (fun (k,v) -> add tbl k v) + let[@inline] add_seq tbl seq = seq (fun (k,v) -> add tbl k v) let of_seq seq = let tbl = create 32 in diff --git a/src/core/CCIO.ml b/src/core/CCIO.ml index f48cd127..09de4d43 100644 --- a/src/core/CCIO.ml +++ b/src/core/CCIO.ml @@ -3,7 +3,7 @@ (** {1 IO Utils} *) -type 'a or_error = ('a, string) Result.result +type 'a or_error = ('a, string) result type 'a gen = unit -> 'a option let gen_empty () = None @@ -255,28 +255,28 @@ module File = struct let remove_exn f = Sys.remove f let remove f = - try Result.Ok (Sys.remove f) + try Ok (Sys.remove f) with exn -> - Result.Error (Printexc.to_string exn) + Error (Printexc.to_string exn) let read_exn f = with_in f (read_all_ ~op:Ret_string ~size:4096) let read f = - try Result.Ok (read_exn f) with e -> Result.Error (Printexc.to_string e) + try Ok (read_exn f) with e -> Error (Printexc.to_string e) let append_exn f x = with_out ~flags:[Open_append; Open_creat; Open_text] f (fun oc -> output_string oc x; flush oc) let append f x = - try Result.Ok (append_exn f x) with e -> Result.Error (Printexc.to_string e) + try Ok (append_exn f x) with e -> Error (Printexc.to_string e) let write_exn f x = with_out f (fun oc -> output_string oc x; flush oc) let write f x = - try Result.Ok (write_exn f x) with e -> Result.Error (Printexc.to_string e) + try Ok (write_exn f x) with e -> Error (Printexc.to_string e) let remove_noerr f = try Sys.remove f with _ -> () diff --git a/src/core/CCIO.mli b/src/core/CCIO.mli index 3a848f54..f7d18b04 100644 --- a/src/core/CCIO.mli +++ b/src/core/CCIO.mli @@ -36,7 +36,7 @@ *) -type 'a or_error = ('a, string) Result.result +type 'a or_error = ('a, string) result type 'a gen = unit -> 'a option (** See {!Gen} in the gen library. *) (** {2 Input} *) diff --git a/src/core/CCList.ml b/src/core/CCList.ml index 395e9df7..5289e31c 100644 --- a/src/core/CCList.ml +++ b/src/core/CCList.ml @@ -25,7 +25,7 @@ let nth_opt l n = nth_opt l i = get_at_idx i l) *) -let rec find_opt p l = match l with +let[@unroll 2] rec find_opt p l = match l with | [] -> None | x :: _ when p x -> Some x | _ :: tl -> find_opt p tl @@ -70,7 +70,7 @@ include List let empty = [] -let is_empty = function +let[@inline] is_empty = function | [] -> true | _::_ -> false @@ -123,11 +123,11 @@ let map f l = List.rev (List.rev_map f l) = map f l) *) -let (>|=) l f = map f l +let[@inline] (>|=) l f = map f l let direct_depth_append_ = 10_000 -let cons x l = x::l +let[@inline] cons x l = x::l let append l1 l2 = let rec direct i l1 l2 = match l1 with @@ -150,7 +150,7 @@ let (@) = append (1-- 10_000) @ (10_001 -- 20_000) = 1 -- 20_000 *) -let cons_maybe o l = match o with +let[@inline] cons_maybe o l = match o with | Some x -> x :: l | None -> l @@ -522,15 +522,15 @@ let split l = split l = List.split l) *) -let return x = [x] +let[@inline] return x = [x] -let (>>=) l f = flat_map f l +let[@inline] (>>=) l f = flat_map f l let (<$>) = map let pure = return -let (<*>) funs l = product (fun f x -> f x) funs l +let[@inline] (<*>) funs l = product (fun f x -> f x) funs l let cartesian_product l = (* [left]: elements picked so far @@ -593,7 +593,7 @@ let sorted_merge ~cmp l1 l2 = List.length (sorted_merge ~cmp:CCInt.compare l1 l2) = List.length l1 + List.length l2) *) -let sort_uniq ~cmp l = List.sort_uniq cmp l +let[@inline] sort_uniq ~cmp l = List.sort_uniq cmp l (*$T sort_uniq ~cmp:CCInt.compare [1;2;5;3;6;1;4;2;3] = [1;2;3;4;5;6] @@ -748,7 +748,7 @@ let rec drop n l = match l with | _ when n=0 -> l | _::l' -> drop (n-1) l' -let hd_tl = function +let[@inline] hd_tl = function | [] -> failwith "hd_tl" | x :: l -> x, l @@ -757,7 +757,7 @@ let hd_tl = function hd_tl [1;2;3] = (1, [2;3]) *) -let take_drop n l = take n l, drop n l +let[@inline] take_drop n l = take n l, drop n l (*$Q (Q.pair (Q.list Q.small_int) Q.int) (fun (l,i) -> \ @@ -909,11 +909,11 @@ let last n l = let len = List.length l in if len < n then l else drop (len-n) l -let head_opt = function +let[@inline] head_opt = function | [] -> None | x::_ -> Some x -let tail_opt = function +let[@inline] tail_opt = function | [] -> None | _ :: tail -> Some tail @@ -951,7 +951,7 @@ let find_pred_exn p l = match find_pred p l with *) let find_mapi f l = - let rec aux f i = function + let[@specialise] rec aux f i = function | [] -> None | x::l' -> match f i x with @@ -959,7 +959,7 @@ let find_mapi f l = | None -> aux f (i+1) l' in aux f 0 l -let find_map f l = find_mapi (fun _ -> f) l +let[@inline] find_map f l = find_mapi (fun _ -> f) l let find_idx p l = find_mapi (fun i x -> if p x then Some (i, x) else None) l @@ -1003,8 +1003,8 @@ let keep_some l = filter_map (fun x->x) l let keep_ok l = filter_map (function - | Result.Ok x -> Some x - | Result.Error _ -> None) + | Ok x -> Some x + | Error _ -> None) l let all_some l = @@ -1020,14 +1020,14 @@ let all_some l = let all_ok l = let err = ref None in try - Result.Ok + Ok (map - (function Result.Ok x -> x | Result.Error e -> err := Some e; raise Exit) + (function Ok x -> x | Error e -> err := Some e; raise Exit) l) with Exit -> begin match !err with | None -> assert false - | Some e -> Result.Error e + | Some e -> Error e end let group_by (type k) ?(hash=Hashtbl.hash) ?(eq=Pervasives.(=)) l = @@ -1216,11 +1216,11 @@ let inter ~eq l1 l2 = let mapi f l = let r = ref 0 in - map + (map[@specialised]) (fun x -> let y = f !r x in - incr r; y - ) l + incr r; y) + l (*$T mapi (fun i x -> i*x) [10;10;10] = [0;10;20] @@ -1527,7 +1527,7 @@ let remove_assoc = Assoc.remove module Ref = struct type 'a t = 'a list ref - let push l x = l := x :: !l + let[@inline] push l x = l := x :: !l let pop l = match !l with | [] -> None @@ -1541,13 +1541,13 @@ module Ref = struct l := tail; x - let create() = ref [] + let[@inline] create() = ref [] - let clear l = l := [] + let[@inline] clear l = l := [] - let lift f l = f !l + let[@inline] lift f l = f !l - let push_list r l = + let[@inline] push_list r l = r := List.rev_append l !r (*$T @@ -1582,7 +1582,7 @@ module Traverse(M : MONAD) = struct tl' >>= fun tl' -> M.return (x'::tl') - let sequence_m l = map_m (fun x->x) l + let[@inline] sequence_m l = map_m (fun x->x) l let rec fold_m f acc l = match l with | [] -> return acc @@ -1590,7 +1590,7 @@ module Traverse(M : MONAD) = struct f acc x >>= fun acc' -> fold_m f acc' l' -end +end [@@inline] (** {2 Conversions} *) diff --git a/src/core/CCList.mli b/src/core/CCList.mli index 9d331dbe..7656c894 100644 --- a/src/core/CCList.mli +++ b/src/core/CCList.mli @@ -394,7 +394,7 @@ val keep_some : 'a option t -> 'a t @since 1.3, but only @since 2.2 with labels *) -val keep_ok : ('a, _) Result.result t -> 'a t +val keep_ok : ('a, _) result t -> 'a t (** [keep_ok l] retains only elements of the form [Ok x]. @since 1.3, but only @since 2.2 with labels *) @@ -405,7 +405,7 @@ val all_some : 'a option t -> 'a t option @since 1.3, but only @since 2.2 with labels *) -val all_ok : ('a, 'err) Result.result t -> ('a t, 'err) Result.result +val all_ok : ('a, 'err) result t -> ('a t, 'err) result (** [all_ok l] returns [Ok l'] if all elements of [l] are of the form [Ok x], or [Error e] otherwise (with the first error met). @since 1.3, but only diff --git a/src/core/CCListLabels.mli b/src/core/CCListLabels.mli index fbfe7bd7..4d3fd6dd 100644 --- a/src/core/CCListLabels.mli +++ b/src/core/CCListLabels.mli @@ -392,7 +392,7 @@ val keep_some : 'a option t -> 'a t @since 1.3, but only @since 2.2 with labels *) -val keep_ok : ('a, _) Result.result t -> 'a t +val keep_ok : ('a, _) result t -> 'a t (** [keep_ok l] retains only elements of the form [Ok x]. @since 1.3, but only @since 2.2 with labels *) @@ -403,7 +403,7 @@ val all_some : 'a option t -> 'a t option @since 1.3, but only @since 2.2 with labels *) -val all_ok : ('a, 'err) Result.result t -> ('a t, 'err) Result.result +val all_ok : ('a, 'err) result t -> ('a t, 'err) result (** [all_ok l] returns [Ok l'] if all elements of [l] are of the form [Ok x], or [Error e] otherwise (with the first error met). @since 1.3, but only diff --git a/src/core/CCMap.ml b/src/core/CCMap.ml index 214b00e7..3928d095 100644 --- a/src/core/CCMap.ml +++ b/src/core/CCMap.ml @@ -108,19 +108,19 @@ module Make(O : Map.OrderedType) = struct | Some v1, Some v2 -> f k v1 v2) a b - let choose_opt m = + let[@inline] choose_opt m = try Some (M.choose m) with Not_found -> None - let find_opt k m = + let[@inline] find_opt k m = try Some (M.find k m) with Not_found -> None - let max_binding_opt m = + let[@inline] max_binding_opt m = try Some (M.max_binding m) with Not_found -> None - let min_binding_opt m = + let[@inline] min_binding_opt m = try Some (M.min_binding m) with Not_found -> None @@ -140,7 +140,7 @@ module Make(O : Map.OrderedType) = struct with Find_binding_exit -> !res - let find_first f m = match find_first_opt f m with + let[@inline] find_first f m = match find_first_opt f m with | None -> raise Not_found | Some (k,v) -> k, v @@ -152,7 +152,7 @@ module Make(O : Map.OrderedType) = struct m; !res - let find_last f m = match find_last_opt f m with + let[@inline] find_last f m = match find_last_opt f m with | None -> raise Not_found | Some (k,v) -> k, v @@ -160,7 +160,7 @@ module Make(O : Map.OrderedType) = struct let get = find_opt - let get_or k m ~default = + let[@inline] get_or k m ~default = try find k m with Not_found -> default @@ -187,22 +187,22 @@ module Make(O : Map.OrderedType) = struct s (fun (k,v) -> m := add k v !m); !m - let of_seq s = add_seq empty s + let[@inline] of_seq s = add_seq empty s - let to_seq m yield = + let[@inline] to_seq m yield = iter (fun k v -> yield (k,v)) m - let keys m yield = + let[@inline] keys m yield = iter (fun k _ -> yield k) m - let values m yield = + let[@inline] values m yield = iter (fun _ v -> yield v) m let add_list m l = List.fold_left (fun m (k,v) -> add k v m) m l - let of_list l = add_list empty l + let[@inline] of_list l = add_list empty l - let to_list m = + let[@inline] to_list m = fold (fun k v acc -> (k,v)::acc) m [] let pp ?(start="") ?(stop="") ?(arrow="->") ?(sep=", ") pp_k pp_v fmt m = diff --git a/src/core/CCOpt.ml b/src/core/CCOpt.ml index d35a1e7d..3929f533 100644 --- a/src/core/CCOpt.ml +++ b/src/core/CCOpt.ml @@ -5,77 +5,76 @@ type 'a t = 'a option -let map f = function +let[@inline] map f = function | None -> None | Some x -> Some (f x) -let map_or ~default f = function +let[@inline] map_or ~default f = function | None -> default | Some x -> f x -let map_lazy default_fn f = function +let[@inline] map_lazy default_fn f = function | None -> default_fn () | Some x -> f x -let is_some = function +let[@inline] is_some = function | None -> false | Some _ -> true -let is_none = function +let[@inline] is_none = function | None -> true | Some _ -> false -let compare f o1 o2 = match o1, o2 with +let[@inline] compare f o1 o2 = match o1, o2 with | None, None -> 0 | Some _, None -> 1 | None, Some _ -> -1 | Some x, Some y -> f x y -let equal f o1 o2 = match o1, o2 with +let[@inline] equal f o1 o2 = match o1, o2 with | None, None -> true | Some _, None | None, Some _ -> false | Some x, Some y -> f x y -let return x = Some x +let[@inline] return x = Some x -let (>|=) x f = map f x +let[@inline] (>|=) x f = map f x -let (>>=) o f = match o with +let[@inline] (>>=) o f = match o with | None -> None | Some x -> f x -let flat_map f o = match o with +let[@inline] flat_map f o = match o with | None -> None | Some x -> f x -let pure x = Some x +let pure = return -let (<*>) f x = match f, x with +let[@inline] (<*>) f x = match f, x with | None, _ | _, None -> None | Some f, Some x -> Some (f x) let (<$>) = map -let or_ ~else_ a = match a with +let[@inline] or_ ~else_ a = match a with | None -> else_ | Some _ -> a -let or_lazy ~else_ a = match a with +let[@inline] or_lazy ~else_ a = match a with | None -> else_ () | Some _ -> a -let (<+>) a b = or_ ~else_:b a +let[@inline] (<+>) a b = or_ ~else_:b a -let choice l = List.fold_left (<+>) None l +let[@inline] choice l = List.fold_left (<+>) None l -let map2 f o1 o2 = match o1, o2 with - | None, _ - | _, None -> None +let[@inline] map2 f o1 o2 = match o1, o2 with + | None, _ | _, None -> None | Some x, Some y -> Some (f x y) -let filter p = function +let[@inline] filter p = function | Some x as o when p x -> o | _ -> None @@ -85,33 +84,33 @@ let filter p = function None (filter (fun _ -> true) None) *) -let if_ p x = if p x then Some x else None +let[@inline] if_ p x = if p x then Some x else None -let exists p = function +let[@inline] exists p = function | None -> false | Some x -> p x -let for_all p = function +let[@inline] for_all p = function | None -> true | Some x -> p x -let iter f o = match o with +let[@inline] iter f o = match o with | None -> () | Some x -> f x -let fold f acc o = match o with +let[@inline] fold f acc o = match o with | None -> acc | Some x -> f acc x -let get_or ~default x = match x with +let[@inline] get_or ~default x = match x with | None -> default | Some y -> y -let get_exn = function +let[@inline] get_exn = function | Some x -> x | None -> invalid_arg "CCOpt.get_exn" -let get_lazy default_fn x = match x with +let[@inline] get_lazy default_fn x = match x with | None -> default_fn () | Some y -> y @@ -139,25 +138,25 @@ let wrap2 ?(handler=fun _ -> true) f x y = with e -> if handler e then None else raise e -let to_list o = match o with +let[@inline] to_list o = match o with | None -> [] | Some x -> [x] -let of_list = function +let[@inline] of_list = function | x::_ -> Some x | [] -> None -let to_result err = function - | None -> Result.Error err - | Some x -> Result.Ok x +let[@inline] to_result err = function + | None -> Error err + | Some x -> Ok x -let to_result_lazy err_fn = function - | None -> Result.Error (err_fn ()) - | Some x -> Result.Ok x +let[@inline] to_result_lazy err_fn = function + | None -> Error (err_fn ()) + | Some x -> Ok x -let of_result = function - | Result.Error _ -> None - | Result.Ok x -> Some x +let[@inline] of_result = function + | Error _ -> None + | Ok x -> Some x module Infix = struct let (>|=) = (>|=) @@ -209,7 +208,7 @@ let pp ppx out = function | None -> Format.pp_print_string out "None" | Some x -> Format.fprintf out "@[Some %a@]" ppx x -let flatten = function +let[@inline] flatten = function | Some x -> x | None -> None @@ -219,7 +218,7 @@ let flatten = function flatten (Some (Some 1)) = Some 1 *) -let return_if b x = +let[@inline] return_if b x = if b then Some x else diff --git a/src/core/CCOpt.mli b/src/core/CCOpt.mli index e6041177..126a4407 100644 --- a/src/core/CCOpt.mli +++ b/src/core/CCOpt.mli @@ -160,13 +160,13 @@ val to_list : 'a t -> 'a list val of_list : 'a list -> 'a t (** Head of list, or [None]. *) -val to_result : 'e -> 'a t -> ('a, 'e) Result.result +val to_result : 'e -> 'a t -> ('a, 'e) result (** @since 1.2 *) -val to_result_lazy : (unit -> 'e) -> 'a t -> ('a, 'e) Result.result +val to_result_lazy : (unit -> 'e) -> 'a t -> ('a, 'e) result (** @since 1.2 *) -val of_result : ('a, _) Result.result -> 'a t +val of_result : ('a, _) result -> 'a t (** @since 1.2 *) type 'a sequence = ('a -> unit) -> unit diff --git a/src/core/CCParse.ml b/src/core/CCParse.ml index 50dd98ca..f79ca550 100644 --- a/src/core/CCParse.ml +++ b/src/core/CCParse.ml @@ -99,7 +99,7 @@ () *) -type 'a or_error = ('a, string) Result.result +type 'a or_error = ('a, string) result type line_num = int type col_num = int @@ -430,10 +430,10 @@ let parse_exn p st = | None -> assert false | Some x -> x -let exn_to_err e =Result.Error (Printexc.to_string e) +let exn_to_err e = Error (Printexc.to_string e) let parse p st = - try Result.Ok (parse_exn p st) + try Ok (parse_exn p st) with e -> exn_to_err e let parse_string_exn p s = parse_exn p (state_of_string s) @@ -466,7 +466,7 @@ let parse_file_exn p file = raise e let parse_file p file = - try Result.Ok (parse_file_exn p file) + try Ok (parse_file_exn p file) with e -> exn_to_err e module Infix = struct diff --git a/src/core/CCParse.mli b/src/core/CCParse.mli index 066a4ead..1aa787ee 100644 --- a/src/core/CCParse.mli +++ b/src/core/CCParse.mli @@ -48,7 +48,7 @@ *) -type 'a or_error = ('a, string) Result.result +type 'a or_error = ('a, string) result type line_num = int type col_num = int diff --git a/src/core/CCResult.ml b/src/core/CCResult.ml index 05a23f23..908fc40b 100644 --- a/src/core/CCResult.ml +++ b/src/core/CCResult.ml @@ -10,15 +10,15 @@ type 'a printer = Format.formatter -> 'a -> unit (** {2 Basics} *) -include Result - -type (+'good, +'bad) t = ('good, 'bad) Result.result = +type (+'good, +'bad) t = ('good, 'bad) Pervasives.result = | Ok of 'good | Error of 'bad -let return x = Ok x +type ('a,'b) result = ('a,'b) t -let fail s = Error s +let[@inline] return x = Ok x + +let[@inline] fail s = Error s let fail_printf format = let buf = Buffer.create 64 in @@ -67,23 +67,23 @@ let of_exn_trace e = in Error res -let map f e = match e with +let[@inline] map f e = match e with | Ok x -> Ok (f x) | Error s -> Error s -let map_err f e = match e with +let[@inline] map_err f e = match e with | Ok _ as res -> res | Error y -> Error (f y) -let map2 f g e = match e with +let[@inline] map2 f g e = match e with | Ok x -> Ok (f x) | Error s -> Error (g s) -let iter f e = match e with +let[@inline] iter f e = match e with | Ok x -> f x | Error _ -> () -let iter_err f e = match e with +let[@inline] iter_err f e = match e with | Ok _ -> () | Error err -> f err @@ -98,15 +98,15 @@ let iter_err f e = match e with exception Get_error -let get_exn = function +let[@inline] get_exn = function | Ok x -> x | Error _ -> raise Get_error -let get_or e ~default = match e with +let[@inline] get_or e ~default = match e with | Ok x -> x | Error _ -> default -let get_or_failwith = function +let[@inline] get_or_failwith = function | Ok x -> x | Error msg -> failwith msg @@ -115,38 +115,38 @@ let get_or_failwith = function try ignore @@ get_or_failwith (Error "e"); false with Failure msg -> msg = "e" *) -let map_or f e ~default = match e with +let[@inline] map_or f e ~default = match e with | Ok x -> f x | Error _ -> default -let catch e ~ok ~err = match e with +let[@inline] catch e ~ok ~err = match e with | Ok x -> ok x | Error y -> err y -let flat_map f e = match e with +let[@inline] flat_map f e = match e with | Ok x -> f x | Error s -> Error s -let (>|=) e f = map f e +let[@inline] (>|=) e f = map f e -let (>>=) e f = flat_map f e +let[@inline] (>>=) e f = flat_map f e let equal ~err eq a b = match a, b with | Ok x, Ok y -> eq x y | Error s, Error s' -> err s s' | _ -> false -let compare ~err cmp a b = match a, b with +let[@inline] compare ~err cmp a b = match a, b with | Ok x, Ok y -> cmp x y | Ok _, _ -> 1 | _, Ok _ -> -1 | Error s, Error s' -> err s s' -let fold ~ok ~error x = match x with +let[@inline] fold ~ok ~error x = match x with | Ok x -> ok x | Error s -> error s -let fold_ok f acc r = match r with +let[@inline] fold_ok f acc r = match r with | Ok x -> f acc x | Error _ -> acc @@ -155,37 +155,37 @@ let fold_ok f acc r = match r with 40 (fold_ok (+) 40 (Error "foo")) *) -let is_ok = function +let[@inline] is_ok = function | Ok _ -> true | Error _ -> false -let is_error = function +let[@inline] is_error = function | Ok _ -> false | Error _ -> true (** {2 Wrappers} *) -let guard f = +let[@inline] guard f = try Ok (f ()) with e -> Error e -let guard_str f = +let[@inline] guard_str f = try Ok (f()) with e -> of_exn e -let guard_str_trace f = +let[@inline] guard_str_trace f = try Ok (f()) with e -> of_exn_trace e -let wrap1 f x = +let[@inline] wrap1 f x = try return (f x) with e -> Error e -let wrap2 f x y = +let[@inline] wrap2 f x y = try return (f x y) with e -> Error e -let wrap3 f x y z = +let[@inline] wrap3 f x y z = try return (f x y z) with e -> Error e @@ -193,7 +193,7 @@ let wrap3 f x y z = let pure = return -let (<*>) f x = match f with +let[@inline] (<*>) f x = match f with | Error s -> fail s | Ok f -> map f x @@ -232,7 +232,7 @@ let fold_seq f acc seq = with LocalExit -> match !err with None -> assert false | Some s -> Error s -let fold_l f acc l = fold_seq f acc (fun k -> List.iter k l) +let[@inline] fold_l f acc l = fold_seq f acc (fun k -> List.iter k l) (** {2 Misc} *) @@ -279,7 +279,7 @@ module Traverse(M : MONAD) = struct | Error s -> M.return (Error s) | Ok x -> f x >>= fun y -> M.return (Ok y) - let sequence_m m = map_m (fun x->x) m + let[@inline] sequence_m m = map_m (fun x->x) m let fold_m f acc e = match e with | Error _ -> M.return acc @@ -293,19 +293,19 @@ module Traverse(M : MONAD) = struct | Ok x -> M.return (Ok x) | Error e -> retry (n-1) (e::acc) in retry n [] -end +end[@@inline] (** {2 Conversions} *) -let to_opt = function +let[@inline] to_opt = function | Ok x -> Some x | Error _ -> None -let of_opt = function +let[@inline] of_opt = function | None -> Error "of_opt" | Some x -> Ok x -let to_seq e k = match e with +let[@inline] to_seq e k = match e with | Ok x -> k x | Error _ -> () diff --git a/src/core/CCResult.mli b/src/core/CCResult.mli index a8faf8a5..1f14c8fb 100644 --- a/src/core/CCResult.mli +++ b/src/core/CCResult.mli @@ -14,13 +14,13 @@ type 'a printer = Format.formatter -> 'a -> unit (** {2 Basics} *) -include module type of struct include Result end -(** @since 1.5 *) - -type (+'good, +'bad) t = ('good, 'bad) Result.result = +type (+'good, +'bad) t = ('good, 'bad) Pervasives.result = | Ok of 'good | Error of 'bad +type ('a,'b) result = ('a,'b) t +(** @since 1.5 *) + val return : 'a -> ('a, 'err) t (** Successfully return a value. *) diff --git a/src/core/CCVector.ml b/src/core/CCVector.ml index 1f1763b0..b419f251 100644 --- a/src/core/CCVector.ml +++ b/src/core/CCVector.ml @@ -23,22 +23,22 @@ type 'a vector = ('a, rw) t type 'a ro_vector = ('a, ro) t -let freeze v = { +let[@inline] freeze v = { size=v.size; vec=v.vec; } -let freeze_copy v = { +let[@inline] freeze_copy v = { size=v.size; vec=Array.sub v.vec 0 v.size; } -let create () = { +let[@inline] create () = { size = 0; vec = [| |]; } -let create_with ?(capacity=128) x = { +let[@inline] create_with ?(capacity=128) x = { size = 0; vec = Array.make capacity x; } @@ -47,7 +47,7 @@ let create_with ?(capacity=128) x = { (create_with ~capacity:200 1 |> capacity) >= 200 *) -let return x = { +let[@inline] return x = { size=1; vec= [| x |]; } @@ -57,22 +57,22 @@ let return x = { return 42 |> length = 1 *) -let make n x = { +let[@inline] make n x = { size=n; vec=Array.make n x; } -let init n f = { +let[@inline] init n f = { size=n; vec=Array.init n f; } (* is the underlying array empty? *) -let array_is_empty_ v = +let[@inline] array_is_empty_ v = Array.length v.vec = 0 (* assuming the underlying array isn't empty, resize it *) -let resize_ v newcapacity = +let[@inline never] resize_ v newcapacity = assert (newcapacity >= v.size); assert (not (array_is_empty_ v)); let new_vec = Array.make newcapacity v.vec.(0) in @@ -121,7 +121,7 @@ let ensure v size = ensure_assuming_not_empty_ v ~size ) -let clear v = +let[@inline] clear v = v.size <- 0 (*$R @@ -132,13 +132,13 @@ let clear v = OUnit.assert_bool "empty_after_clear" (Sequence.is_empty (to_seq v)); *) -let is_empty v = v.size = 0 +let[@inline] is_empty v = v.size = 0 -let push_unsafe_ v x = +let[@inline] push_unsafe_ v x = Array.unsafe_set v.vec v.size x; v.size <- v.size + 1 -let push v x = +let[@inline] push v x = if v.size = Array.length v.vec then grow_with_ v ~filler:x; push_unsafe_ v x @@ -184,11 +184,11 @@ let append a b = OUnit.assert_equal (Sequence.to_array Sequence.(6 -- 10)) (to_array b); *) -let get v i = +let[@inline] get v i = if i < 0 || i >= v.size then invalid_arg "CCVector.get"; Array.unsafe_get v.vec i -let set v i x = +let[@inline] set v i x = if i < 0 || i >= v.size then invalid_arg "CCVector.set"; Array.unsafe_set v.vec i x @@ -200,7 +200,7 @@ let remove v i = (* remove one element *) v.size <- v.size - 1 -let append_seq a seq = +let[@inline] append_seq a seq = seq (fun x -> push a x) let append_array a b = @@ -330,14 +330,14 @@ let pop_exn v = if new_size > 0 then v.vec.(new_size) <- v.vec.(0); (* free last element *) x -let pop v = +let[@inline] pop v = try Some (pop_exn v) with Empty -> None -let top v = +let[@inline] top v = if v.size = 0 then None else Some v.vec.(v.size-1) -let top_exn v = +let[@inline] top_exn v = if v.size = 0 then raise Empty; v.vec.(v.size-1) @@ -609,7 +609,7 @@ let for_all p v = for_all f v = List.for_all f l) *) -let member ~eq x v = +let[@inline] member ~eq x v = exists (eq x) v let find_internal_ p v = @@ -734,9 +734,9 @@ let flat_map_list f v = v; v' -let (>>=) x f = flat_map f x +let[@inline] (>>=) x f = flat_map f x -let (>|=) x f = map f x +let[@inline] (>|=) x f = map f x let rev_in_place v = if v.size > 0 @@ -791,13 +791,13 @@ let rev_iter f v = (fun f->rev_iter f v) |> Sequence.to_list = List.rev l) *) -let size v = v.size +let[@inline] size v = v.size -let length v = v.size +let[@inline] length v = v.size -let capacity v = Array.length v.vec +let[@inline] capacity v = Array.length v.vec -let unsafe_get_array v = v.vec +let[@inline] unsafe_get_array v = v.vec let of_seq ?(init=create ()) seq = append_seq init seq; @@ -807,7 +807,7 @@ let of_seq ?(init=create ()) seq = of_seq Sequence.(1 -- 10) |> to_list = CCList.(1 -- 10) *) -let to_seq v k = iter k v +let[@inline] to_seq v k = iter k v let to_seq_rev v k = let n = v.size in @@ -904,7 +904,7 @@ let of_list l = match l with of_list CCList.(1--300_000) |> to_list = CCList.(1--300_000) *) -let to_array v = +let[@inline] to_array v = Array.sub v.vec 0 v.size let to_list v = diff --git a/src/core/dune b/src/core/dune index fcd0cc4f..f2cf3f0d 100644 --- a/src/core/dune +++ b/src/core/dune @@ -34,5 +34,5 @@ (wrapped false) (flags :standard -w +a-4-42-44-48-50-58-32-60@8 -safe-string -nolabels -open CCMonomorphic) (ocamlopt_flags (:include ../flambda.flags)) - (libraries result uchar containers.monomorphic) + (libraries containers.monomorphic) ) diff --git a/src/iter/dune b/src/iter/dune index 795ddc8b..c42ecb13 100644 --- a/src/iter/dune +++ b/src/iter/dune @@ -5,5 +5,4 @@ (wrapped false) (flags :standard -w +a-4-42-44-48-50-58-32-60@8 -safe-string) (ocamlopt_flags :standard (:include ../flambda.flags)) - (libraries result) - ) +) diff --git a/src/sexp/CCSexp.ml b/src/sexp/CCSexp.ml index 25857c31..69cc473f 100644 --- a/src/sexp/CCSexp.ml +++ b/src/sexp/CCSexp.ml @@ -3,7 +3,7 @@ (** {1 Simple S-expression parsing/printing} *) -type 'a or_error = ('a, string) Result.result +type 'a or_error = ('a, string) result type 'a sequence = ('a -> unit) -> unit type 'a gen = unit -> 'a option @@ -147,7 +147,7 @@ let _with_in filename f = x with e -> close_in ic; - Result.Error (Printexc.to_string e) + Error (Printexc.to_string e) (** A parser of ['a] can return [Yield x] when it parsed a value, or [Fail e] when a parse error was encountered, or @@ -221,9 +221,9 @@ let parse_string s : t or_error = let buf = Lexing.from_string s in let d = Decoder.of_lexbuf buf in match Decoder.next d with - | End -> Result.Error "unexpected end of file" - | Yield x -> Result.Ok x - | Fail s -> Result.Error s + | End -> Error "unexpected end of file" + | Yield x -> Ok x + | Fail s -> Error s (*$T CCResult.to_opt (parse_string "(abc d/e/f \"hello \\\" () world\" )") <> None @@ -272,17 +272,17 @@ let parse_chan ic : sexp or_error = let buf = Lexing.from_channel ic in let d = Decoder.of_lexbuf buf in match Decoder.next d with - | End -> Result.Error "unexpected end of file" - | Yield x -> Result.Ok x - | Fail e -> Result.Error e + | End -> Error "unexpected end of file" + | Yield x -> Ok x + | Fail e -> Error e let parse_chan_list ic = let buf = Lexing.from_channel ic in let d = Decoder.of_lexbuf buf in let rec iter acc = match Decoder.next d with - | End -> Result.Ok (List.rev acc) + | End -> Ok (List.rev acc) | Yield x -> iter (x::acc) - | Fail e -> Result.Error e + | Fail e -> Error e in iter [] @@ -291,8 +291,8 @@ let parse_chan_gen ic = let d = Decoder.of_lexbuf buf in fun () -> match Decoder.next d with | End -> None - | Fail e -> Some (Result.Error e) - | Yield x -> Some (Result.Ok x) + | Fail e -> Some (Error e) + | Yield x -> Some (Ok x) let parse_file filename = _with_in filename parse_chan diff --git a/src/sexp/CCSexp.mli b/src/sexp/CCSexp.mli index b1cd713e..c3ef7cb2 100644 --- a/src/sexp/CCSexp.mli +++ b/src/sexp/CCSexp.mli @@ -3,7 +3,7 @@ (** {1 Handling S-expressions} *) -type 'a or_error = ('a, string) Result.result +type 'a or_error = ('a, string) result type 'a sequence = ('a -> unit) -> unit type 'a gen = unit -> 'a option diff --git a/src/sexp/dune b/src/sexp/dune index 0066e698..cbb1e1d8 100644 --- a/src/sexp/dune +++ b/src/sexp/dune @@ -5,7 +5,6 @@ (wrapped false) (flags :standard -w +a-4-42-44-48-50-58-32-60@8 -safe-string) (ocamlopt_flags :standard (:include ../flambda.flags)) - (libraries result) - ) +) (ocamllex (modules CCSexp_lex)) diff --git a/src/unix/CCUnix.ml b/src/unix/CCUnix.ml index 905cd359..23692b0e 100644 --- a/src/unix/CCUnix.ml +++ b/src/unix/CCUnix.ml @@ -3,7 +3,7 @@ (** {1 High-level Functions on top of Unix} *) -type 'a or_error = ('a, string) Result.result +type 'a or_error = ('a, string) result type 'a gen = unit -> 'a option (** {2 Calling Commands} *) diff --git a/src/unix/CCUnix.mli b/src/unix/CCUnix.mli index 77657dd4..a66f32d9 100644 --- a/src/unix/CCUnix.mli +++ b/src/unix/CCUnix.mli @@ -8,7 +8,7 @@ {b status: unstable} @since 0.10 *) -type 'a or_error = ('a, string) Result.result +type 'a or_error = ('a, string) result type 'a gen = unit -> 'a option (** {2 Calling Commands} *) diff --git a/src/unix/dune b/src/unix/dune index 5bc04571..90f5c209 100644 --- a/src/unix/dune +++ b/src/unix/dune @@ -6,5 +6,5 @@ (optional) (flags :standard -w +a-4-42-44-48-50-58-32-60@8 -safe-string) (ocamlopt_flags :standard (:include ../flambda.flags)) - (libraries result unix) - ) + (libraries unix) +)