grew_base.mli 10.6 KB
Newer Older
bguillaum's avatar
bguillaum committed
1 2 3 4 5 6 7 8 9
(**********************************************************************************)
(*    Libcaml-grew - a Graph Rewriting library dedicated to NLP applications      *)
(*                                                                                *)
(*    Copyright 2011-2013 Inria, Université de Lorraine                           *)
(*                                                                                *)
(*    Webpage: http://grew.loria.fr                                               *)
(*    License: CeCILL (see LICENSE folder or "http://www.cecill.info")            *)
(*    Authors: see AUTHORS file                                                   *)
(**********************************************************************************)
pj2m's avatar
pj2m committed
10

bguillaum's avatar
bguillaum committed
11 12
module String_map : Map.S with type key = string
module String_set : Set.S with type elt = string
bguillaum's avatar
bguillaum committed
13

bguillaum's avatar
bguillaum committed
14 15
module Int_set : Set.S with type elt = int
module Int_map : Map.S with type key = int
pj2m's avatar
pj2m committed
16

bguillaum's avatar
bguillaum committed
17 18 19 20 21 22 23 24
(* ================================================================================ *)
(* [Pid_set] *)
module String_: sig
  (* [to_float]: robust conversion of string to float whatever is the locale *)
  val to_float: string -> float

  (* [to_float]: robust conversion of float to string whatever is the locale *)
  val of_float: float -> string
25

26 27
  val re_match: Str.regexp -> string -> bool

28 29 30
  (* [rm_first_char s] returns the string [s] without the first charater if s is not empty.
     If s in empty, the empty string is returned  *)
  val rm_first_char: string -> string
bguillaum's avatar
bguillaum committed
31 32 33 34

  (* [rm_peripheral_white s] returns the string [s] without any white space ot tab
    at the beginning or at the end of the string. *)
  val rm_peripheral_white: string -> string
bguillaum's avatar
bguillaum committed
35 36 37 38 39 40 41 42
end


(* ================================================================================ *)
(* [Dot] function to manipulate the dot format *)
module Dot: sig
  val to_png_file: string -> string -> unit
end
pj2m's avatar
pj2m committed
43

44

bguillaum's avatar
bguillaum committed
45 46
(* ================================================================================ *)
(* [Loc] general module to describe errors location: (file name, line number in file) *)
pj2m's avatar
pj2m committed
47
module Loc: sig
bguillaum's avatar
bguillaum committed
48 49
  type t

50 51
  val empty: t

bguillaum's avatar
bguillaum committed
52
  val file_line: string -> int -> t
53
  val file_opt_line: string option -> int -> t
54
  val file_opt_line_opt: string option -> int option -> t
bguillaum's avatar
bguillaum committed
55
  val file: string -> t
pj2m's avatar
pj2m committed
56 57 58 59 60

  val to_string: t -> string
end


bguillaum's avatar
bguillaum committed
61
(* ================================================================================ *)
bguillaum's avatar
bguillaum committed
62
(* [File] functions to read/write file *)
bguillaum's avatar
bguillaum committed
63 64
module File: sig
  (** [write data file_name] write [data] in file named [file_name] *)
pj2m's avatar
pj2m committed
65
  val write: string -> string -> unit
bguillaum's avatar
bguillaum committed
66

bguillaum's avatar
bguillaum committed
67
  (** [read file_name] read the content of [file_name] line by line.
bguillaum's avatar
bguillaum committed
68 69
      Blanks lines (empty or only with spaces and tabs) are ignored.
      Lines with '%' as the first char are ignored. *)
bguillaum's avatar
bguillaum committed
70
  val read: string -> string list
71

bguillaum's avatar
bguillaum committed
72 73 74 75
  (** [read_ln file_name] read the content of [file_name] line by line.
      Blanks lines (empty or only with spaces and tabs) are ignored.
      Lines with '%' as the first char are ignored.
      Each line is returned with its position in the original file. *)
76
  val read_ln: string -> (int * string) list
bguillaum's avatar
bguillaum committed
77 78 79 80 81 82 83 84

  (** [load file_name] load the content of [file_name] as a string. *)
  val load: string -> string

  (** [get_suffix file_name] returns the suffix in [file_name].
      "x.y.z" -> Some ".z"
      "xyz" -> None  *)
  val get_suffix: string -> string option
bguillaum's avatar
bguillaum committed
85
end
bguillaum's avatar
bguillaum committed
86

bguillaum's avatar
bguillaum committed
87 88
(* ================================================================================ *)
(* [Array_] contains additional functions on the caml [array] type. *)
pj2m's avatar
pj2m committed
89
module Array_: sig
bguillaum's avatar
bguillaum committed
90 91
  (* [dicho_mem elt array] returns true iff [elt] belongs to [array].
     Warning: the array MUST be sorted and without duplicates. *)
pj2m's avatar
pj2m committed
92 93
  val dicho_mem: 'a -> 'a array -> bool

bguillaum's avatar
bguillaum committed
94 95 96
  (* [dicho_find elt array] returns the index of the position where [elt] is found in the [array].
     [Not found] is raised if [elt] is not in [array].
     Warning: the array MUST be sorted and without duplicates. *)
pj2m's avatar
pj2m committed
97
  val dicho_find: 'a -> 'a array -> int
bguillaum's avatar
bguillaum committed
98 99 100 101

  (* [dicho_find_assoc key array] returns the value associated with [key] in the assoc [array].
     [Not found] is raised if [key] is not defined in [array].
     Warning: the array MUST be sorted (with respect to the first component) and without duplicates. *)
pj2m's avatar
pj2m committed
102 103 104
  val dicho_find_assoc: 'a -> ('a*'b) array -> int
end

bguillaum's avatar
bguillaum committed
105 106
(* ================================================================================ *)
(* [List_] contains additional functions on the caml [list] type. *)
pj2m's avatar
pj2m committed
107 108 109 110
module List_: sig
  (** [rm elt list] removes the first occurence of [elt] in [list]. [Not_found] can be raised. *)
  val rm: 'a -> 'a list -> 'a list
  val opt: 'a option list -> 'a list
bguillaum's avatar
bguillaum committed
111

112 113
  val set: int -> 'a -> 'a list -> 'a list

114 115 116 117
  (** [cut size list] returns a list with the [size] first elements of [list].
      If [list] contains less than [size] elements, the input list is returned *)
  val cut: int -> 'a list -> 'a list

118
  (** [index elt list] return [Some index] if [index] is the smallest position in the [list] equals to [elt].
bguillaum's avatar
bguillaum committed
119
      None is returned if [elt] is not in the [list] *)
120
  val index: 'a -> 'a list -> int option
pj2m's avatar
pj2m committed
121 122 123

  val opt_map: ('a -> 'b option) -> 'a list -> 'b list

bguillaum's avatar
bguillaum committed
124 125
  val try_map: exn -> ('a -> 'b) -> 'a list -> 'b list

126 127
  val opt_mapi: (int -> 'a -> 'b option) -> 'a list -> 'b list

pj2m's avatar
pj2m committed
128
  val flat_map: ('a -> 'b list) -> 'a list -> 'b list
bguillaum's avatar
bguillaum committed
129
  (* remove [elt] from [list]. raise Not_found if [elt] is not in [list] *)
pj2m's avatar
pj2m committed
130 131 132 133 134 135 136 137
  val remove: 'a -> 'a list -> 'a list

  val foldi_left: (int -> 'a -> 'b -> 'a) -> 'a -> 'b list -> 'a

  (** [disjoint_list] returns true iff the two strictly ordered list are disjoint *)
  val sort_disjoint: 'a list -> 'a list -> bool

  val to_string: ('a -> string) -> string -> 'a list -> string
138
  val rev_to_string: ('a -> string) -> string -> 'a list -> string
pj2m's avatar
pj2m committed
139

140 141
  val sort_mem: 'a -> 'a list -> bool

pj2m's avatar
pj2m committed
142 143 144
  (* Insert an element in a sorted list. *)
  val sort_insert: 'a -> 'a list -> 'a list

145 146 147
  (* may raise [Not_found] *)
  val usort_remove: 'a -> 'a list -> 'a list

pj2m's avatar
pj2m committed
148 149 150 151 152
  (* Insert an element in a usort list. Return Some l or None if the element is already in the list *)
  val usort_insert: ?compare:('a -> 'a -> int) -> 'a -> 'a list -> 'a list option

  val sort_is_empty_inter: 'a list -> 'a list -> bool
  val sort_inter: 'a list -> 'a list -> 'a list
bguillaum's avatar
bguillaum committed
153
  val sort_union: 'a list -> 'a list -> 'a list
pj2m's avatar
pj2m committed
154 155 156 157 158
  val sort_disjoint_union: ?compare:('a -> 'a -> int) -> 'a list -> 'a list -> 'a list
  val sort_include: 'a list -> 'a list -> bool
  val sort_included_diff: 'a list -> 'a list -> 'a list
  val sort_diff: 'a list -> 'a list -> 'a list

159 160
  val sort_assoc: 'a -> ('a * 'b) list -> 'b option

bguillaum's avatar
bguillaum committed
161
   (* [sort_remove_assoc k ass_list] returns the input list without the [key] element,
162 163 164
      if [key] not found, the unchanged input list is returned *)
  val sort_remove_assoc: 'a -> ('a * 'b) list -> ('a * 'b) list

165 166
  val sort_remove_assoc_opt: 'a -> ('a * 'b) list -> ('a * 'b) list option

167
  val foldi_left: (int -> 'a -> 'b -> 'a) -> 'a -> 'b list -> 'a
bguillaum's avatar
bguillaum committed
168 169

  val prev_next_iter: (?prev:'a -> ?next:'a -> 'a -> unit) -> 'a list -> unit
pj2m's avatar
pj2m committed
170 171
end

bguillaum's avatar
bguillaum committed
172
(* ================================================================================ *)
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
module type OrderedType =
  sig
    type t
      (** The type of the map keys. *)
    val compare : t -> t -> int
      (** A total ordering function over the keys.
          This is a two-argument function [f] such that
          [f e1 e2] is zero if the keys [e1] and [e2] are equal,
          [f e1 e2] is strictly negative if [e1] is smaller than [e2],
          and [f e1 e2] is strictly positive if [e1] is greater than [e2].
          Example: a suitable ordering function is the generic structural
          comparison function {!Pervasives.compare}. *)
  end
(** Input signature of the functor {!Map.Make}. *)

bguillaum's avatar
bguillaum committed
188
(* ================================================================================ *)
189 190 191
module type S =
  sig
    type key
bguillaum's avatar
bguillaum committed
192

193 194 195 196
    type +'a t

    val empty: 'a t

bguillaum's avatar
bguillaum committed
197
    (* an empty list returned if the key is undefined *)
198 199 200 201 202 203 204 205
    val assoc: key -> 'a t -> 'a list

    val is_empty: 'a t -> bool

    val to_string: ('a -> string) -> 'a t -> string

    val iter: (key -> 'a -> unit) -> 'a t -> unit

Bruno Guillaume's avatar
Bruno Guillaume committed
206
    val add_opt: key -> 'a -> 'a t -> 'a t option
207

208 209
    val replace: key -> 'a list -> 'a t -> 'a t

210 211 212 213
    val fold: ('b -> key -> 'a -> 'b) -> 'b -> 'a t -> 'b

    (* raise Not_found if no (key,elt) *)
    val remove: key -> 'a -> 'a t -> 'a t
Bruno Guillaume's avatar
Bruno Guillaume committed
214
    val remove_opt: key -> 'a -> 'a t -> 'a t option
215 216 217 218 219 220 221 222 223 224 225

    (* raise Not_found if no (key,elt) *)
    val remove_key: key -> 'a t -> 'a t

    (* [mem key value t ] test if the couple (key, value) is in the massoc [t]. *)
    val mem: key -> 'a -> 'a t -> bool

    (* mem_key key t] tests is [key] is associated to at least one value in [t]. *)
    val mem_key: key -> 'a t -> bool

    exception Not_disjoint
bguillaum's avatar
bguillaum committed
226
    val disjoint_union: 'a t -> 'a t -> 'a t
227 228 229 230 231

    exception Duplicate
    val merge_key: key -> key -> 'a t -> 'a t

    val exists: (key -> 'a -> bool) -> 'a t -> bool
232 233

    val rename: (key * key) list -> 'a t -> 'a t
234 235
  end

bguillaum's avatar
bguillaum committed
236
(* ================================================================================ *)
237 238
module Massoc_make (Ord : OrderedType) : S with type key = Ord.t

bguillaum's avatar
bguillaum committed
239
(* ================================================================================ *)
pj2m's avatar
pj2m committed
240
module Error: sig
bguillaum's avatar
bguillaum committed
241
  exception Build of (string * Loc.t option)
pj2m's avatar
pj2m committed
242
  val build: ?loc: Loc.t -> ('a, unit, string, 'b) format4 -> 'a
243 244

  exception Run of (string * Loc.t option)
pj2m's avatar
pj2m committed
245
  val run: ?loc: Loc.t -> ('a, unit, string, 'b) format4 -> 'a
246 247

  exception Bug of (string * Loc.t option)
bguillaum's avatar
bguillaum committed
248
  val bug: ?loc: Loc.t -> ('a, unit, string, 'b) format4 -> 'a
249 250 251

  exception Parse of (string * Loc.t option)
  val parse: ?loc: Loc.t -> ('a, unit, string, 'b) format4 -> 'a
pj2m's avatar
pj2m committed
252 253
end

bguillaum's avatar
bguillaum committed
254
(* ================================================================================ *)
pj2m's avatar
pj2m committed
255 256 257
module Id: sig
  type t = int

bguillaum's avatar
bguillaum committed
258 259 260 261 262 263 264 265 266
  type 'a gtable = 'a array * ('a -> string)

  (* [Stop] is raised if [string] is not in [gtable] *)
  val gbuild: ?loc:Loc.t -> 'a -> 'a gtable -> t

  val gbuild_opt: 'a -> 'a gtable -> t option

  type name = string
  type table = string array
pj2m's avatar
pj2m committed
267 268 269 270

  (* [Stop] is raised if [string] is not in [table] *)
  val build: ?loc:Loc.t -> name -> table -> t

bguillaum's avatar
bguillaum committed
271
  val build_opt: name -> table -> t option
bguillaum's avatar
bguillaum committed
272

273
  val grewpy_compare: name -> name -> int
pj2m's avatar
pj2m committed
274 275
end

bguillaum's avatar
bguillaum committed
276
(* ================================================================================ *)
bguillaum's avatar
bguillaum committed
277 278 279 280 281 282 283 284
module Timeout: sig
  exception Stop

  val timeout: float option ref
  val start: unit -> unit

  val check: unit -> unit
end
bguillaum's avatar
bguillaum committed
285 286 287

(* ================================================================================ *)
module Global: sig
Bruno Guillaume's avatar
Bruno Guillaume committed
288 289 290
  val new_file: string -> unit
  val new_string: unit -> unit
  val new_line: unit -> unit
bguillaum's avatar
bguillaum committed
291

Bruno Guillaume's avatar
Bruno Guillaume committed
292
  val get_loc: unit -> Loc.t
Bruno Guillaume's avatar
Bruno Guillaume committed
293
  val loc_string: unit -> string
Bruno Guillaume's avatar
Bruno Guillaume committed
294
  val label_flag: bool ref
Bruno Guillaume's avatar
Bruno Guillaume committed
295

bguillaum's avatar
bguillaum committed
296
  val debug: bool ref
Bruno Guillaume's avatar
Bruno Guillaume committed
297
  val strict: bool ref
bguillaum's avatar
bguillaum committed
298
end