util.mli 4.81 KB
Newer Older
1 2
(**************************************************************************)
(*                                                                        *)
3
(*  Copyright (C) 2010-                                                   *)
MARCHE Claude's avatar
MARCHE Claude committed
4 5 6
(*    François Bobot                                                     *)
(*    Jean-Christophe Filliâtre                                          *)
(*    Claude Marché                                                      *)
7
(*    Andrei Paskevich                                                    *)
8 9 10 11 12 13 14 15 16 17 18
(*                                                                        *)
(*  This software is free software; you can redistribute it and/or        *)
(*  modify it under the terms of the GNU Library General Public           *)
(*  License version 2.1, with the special exception on linking            *)
(*  described in file LICENSE.                                            *)
(*                                                                        *)
(*  This software is distributed in the hope that it will be useful,      *)
(*  but WITHOUT ANY WARRANTY; without even the implied warranty of        *)
(*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                  *)
(*                                                                        *)
(**************************************************************************)
19

François Bobot's avatar
François Bobot committed
20
open Stdlib
21

22
(** Useful functions *)
Francois Bobot's avatar
Francois Bobot committed
23

24
val ($) : ('a -> 'b) -> 'a -> 'b
François Bobot's avatar
François Bobot committed
25
val (|>) : 'a -> ('a -> 'b) -> 'b
26 27 28

val const : 'a -> 'b -> 'a

29 30 31 32
val const2 : 'a -> 'b -> 'c -> 'a

val const3 : 'a -> 'b -> 'c -> 'd -> 'a

33 34
val flip : ('a -> 'b -> 'c) -> 'b -> 'a -> 'c

35 36
val cons : ('a -> 'b) -> 'b list -> 'a -> 'b list

Andrei Paskevich's avatar
Andrei Paskevich committed
37
(* useful option combinators *)
38

39 40
val of_option : 'a option -> 'a

41 42
val default_option : 'a -> 'a option -> 'a

43 44
val option_map : ('a -> 'b) -> 'a option -> 'b option

Andrei Paskevich's avatar
Andrei Paskevich committed
45 46
val option_iter : ('a -> unit) -> 'a option -> unit

47 48
val option_apply : 'b -> ('a -> 'b) -> 'a option -> 'b

MARCHE Claude's avatar
MARCHE Claude committed
49 50 51 52
val option_fold : ('b -> 'a -> 'b) -> 'b -> 'a option -> 'b
(** [option_fold f d o] returns [d] if [o] is [None], and
    [f d x] if [o] is [Some x] *)

François Bobot's avatar
François Bobot committed
53 54
val option_map2 : ('a -> 'b -> 'c) -> 'a option -> 'b option -> 'c option

55 56
val option_eq : ('a -> 'b -> bool) -> 'a option -> 'b option -> bool

57 58 59
val option_map_fold :
  ('a -> 'b -> 'a * 'b) -> 'a -> 'b option -> 'a * 'b option

60 61 62 63
(* useful int iterator *)
val foldi : ('a -> int -> 'a) -> 'a -> int -> int -> 'a
val mapi : (int -> 'a) -> int -> int -> 'a list

Andrei Paskevich's avatar
Andrei Paskevich committed
64 65
(* useful list combinators *)

66 67 68
val rev_map_fold_left :
  ('acc -> 'a -> 'acc * 'b) -> 'acc -> 'a list -> 'acc * 'b list

Andrei Paskevich's avatar
Andrei Paskevich committed
69 70 71 72 73
val map_fold_left :
  ('acc -> 'a -> 'acc * 'b) -> 'acc -> 'a list -> 'acc * 'b list

val list_all2 : ('a -> 'b -> bool) -> 'a list -> 'b list -> bool

Andrei Paskevich's avatar
Andrei Paskevich committed
74 75
val map_join_left : ('a -> 'b) -> ('b -> 'b -> 'b) -> 'a list -> 'b

76
val list_apply : ('a -> 'b list) -> 'a list -> 'b list
77
(** [list_apply f [a1;..;an]] returns (f a1)@...@(f an) *)
78

79
val list_fold_product :
80 81 82
  ('a -> 'b -> 'c -> 'a) -> 'a -> 'b list -> 'c list -> 'a
  (** [list_fold_product f acc l1 l2] apply the function [f] with the
      accumulator [acc] on all the pair of elements of [l1] and [l2]
83
      tail-reccursive *)
84

85
val list_fold_product_l :
86
  ('a -> 'b list -> 'a) -> 'a -> 'b list list -> 'a
87
  (** generalisation of {! list_fold_product}
88
      not tail-reccursive *)
89

90 91
val list_compare : ('a -> 'a -> int) -> 'a list -> 'a list -> int

92 93
val list_flatten_rev : 'a list list -> 'a list

94
(* boolean fold accumulators *)
Andrei Paskevich's avatar
Andrei Paskevich committed
95

96 97
exception FoldSkip

98
val all_fn : ('a -> bool) -> 'b -> 'a -> bool
François Bobot's avatar
François Bobot committed
99
(* [all_fn pr b a] return true if pr is true on a, otherwise raise FoldSkip *)
100
val any_fn : ('a -> bool) -> 'b -> 'a -> bool
François Bobot's avatar
François Bobot committed
101 102
(* [all_fn pr b a] return false if pr is false on a,
   otherwise raise FoldSkip *)
103

Francois Bobot's avatar
Francois Bobot committed
104 105
val ffalse : 'a -> bool
(** [ffalse] constant function [false] *)
106

Francois Bobot's avatar
Francois Bobot committed
107 108 109
val ttrue : 'a -> bool
(** [ttrue] constant function [true] *)

110 111 112
(* useful function on string *)
val split_string_rev : string -> char -> string list

113 114 115
(* Set and Map on ints and strings *)

module Mint : Map.S with type key = int
116
module Sint : Mint.Set
Andrei Paskevich's avatar
Andrei Paskevich committed
117

Andrei Paskevich's avatar
Andrei Paskevich committed
118
module Mstr : Map.S with type key = string
119
module Sstr : Mstr.Set
Andrei Paskevich's avatar
Andrei Paskevich committed
120

121 122
val memo_int : int -> (int -> 'a) -> int -> 'a

123
(* Set, Map, Hashtbl on structures with a unique tag *)
124 125 126 127 128 129

module type Tagged =
sig
  type t
  val tag : t -> int
end
Francois Bobot's avatar
 
Francois Bobot committed
130

131
module type OrderedHash =
Francois Bobot's avatar
 
Francois Bobot committed
132
sig
133
  type t
Francois Bobot's avatar
 
Francois Bobot committed
134
  val hash : t -> int
135
  val equal : t -> t -> bool
Francois Bobot's avatar
 
Francois Bobot committed
136 137 138
  val compare : t -> t -> int
end

139 140 141
module OrderedHash (X : Tagged) : OrderedHash with type t = X.t
module OrderedHashList (X : Tagged) : OrderedHash with type t = X.t list

Andrei Paskevich's avatar
Andrei Paskevich committed
142
module StructMake (X : Tagged) :
Francois Bobot's avatar
 
Francois Bobot committed
143 144
sig
  module M : Map.S with type key = X.t
145
  module S : M.Set
Francois Bobot's avatar
 
Francois Bobot committed
146 147
  module H : Hashtbl.S with type key = X.t
end
Andrei Paskevich's avatar
Andrei Paskevich committed
148

Andrei Paskevich's avatar
Andrei Paskevich committed
149 150 151
module WeakStructMake (X : Hashweak.Weakey) :
sig
  module M : Map.S with type key = X.t
152
  module S : M.Set
Andrei Paskevich's avatar
Andrei Paskevich committed
153 154 155 156
  module H : Hashtbl.S with type key = X.t
  module W : Hashweak.S with type key = X.t
end