| 1 | (************************************************************************) |
| 2 | (* * The Rocq Prover / The Rocq Development Team *) |
| 3 | (* v * Copyright INRIA, CNRS and contributors *) |
| 4 | (* <O___,, * (see version control and CREDITS file for authors & dates) *) |
| 5 | (* \VV/ **************************************************************) |
| 6 | (* // * This file is distributed under the terms of the *) |
| 7 | (* * GNU Lesser General Public License Version 2.1 *) |
| 8 | (* * (see LICENSE file for the text of the license) *) |
| 9 | (************************************************************************) |
| 10 | |
| 11 | (** {5 Extended version of OCaml's maps} *) |
| 12 | |
| 13 | module type OrderedType = |
| 14 | sig |
| 15 | type t |
| 16 | val compare : t -> t -> int |
| 17 | end |
| 18 | |
| 19 | module type MonadS = |
| 20 | sig |
| 21 | type +'a t |
| 22 | val return : 'a -> 'a t |
| 23 | val (>>=) : 'a t -> ('a -> 'b t) -> 'b t |
| 24 | end |
| 25 | |
| 26 | module type S = Map.S |
| 27 | |
| 28 | module type UExtS = |
| 29 | sig |
| 30 | include CSig.UMapS |
| 31 | (** The underlying Map library *) |
| 32 | |
| 33 | module Set : CSig.USetS with type elt = key |
| 34 | (** Sets used by the domain function *) |
| 35 | |
| 36 | val get : key -> 'a t -> 'a |
| 37 | (** Same as {!find} but fails an assertion instead of raising [Not_found] *) |
| 38 | |
| 39 | val set : key -> 'a -> 'a t -> 'a t |
| 40 | (** Same as [add], but expects the key to be present, and thus faster. |
| 41 | @raise Not_found when the key is unbound in the map. *) |
| 42 | |
| 43 | val modify : key -> (key -> 'a -> 'a) -> 'a t -> 'a t |
| 44 | (** Apply the given function to the binding of the given key. |
| 45 | @raise Not_found when the key is unbound in the map. *) |
| 46 | |
| 47 | val domain : 'a t -> Set.t |
| 48 | (** Recover the set of keys defined in the map. *) |
| 49 | |
| 50 | val bind : (key -> 'a) -> Set.t -> 'a t |
| 51 | (** [bind f s] transform the set [x1; ...; xn] into [x1 := f x1; ...; |
| 52 | xn := f xn]. *) |
| 53 | |
| 54 | val height : 'a t -> int |
| 55 | (** An indication of the logarithmic size of a map *) |
| 56 | |
| 57 | val filter_range : (key -> int) -> 'a t -> 'a t |
| 58 | (** [find_range in_range m] Given a comparison function [in_range x], |
| 59 | that tests if [x] is below, above, or inside a given range |
| 60 | [filter_range] returns the submap of [m] whose keys are in |
| 61 | range. Note that [in_range] has to define a continouous range. *) |
| 62 | |
| 63 | val of_list : (key * 'a) list -> 'a t |
| 64 | (** Turns an association list into a map *) |
| 65 | |
| 66 | val symmetric_diff_fold : |
| 67 | (key -> 'a option -> 'a option -> 'b -> 'b) -> |
| 68 | 'a t -> 'a t -> 'b -> 'b |
| 69 | (** [symmetric_diff f ml mr acc] will efficiently fold over the difference |
| 70 | between [ml] and [mr], assumed that they share most of their internal |
| 71 | structure. A call to [f k vl vr] means that if [vl] is [Some], then [k] exists |
| 72 | in [ml]. Similarly, if [vr] is [Some], then [k] exists in [mr]. If both [vl] |
| 73 | and [vr] are [Some], then [vl != vr]. *) |
| 74 | |
| 75 | module Smart : |
| 76 | sig |
| 77 | val map : ('a -> 'a) -> 'a t -> 'a t |
| 78 | (** As [map] but tries to preserve sharing. *) |
| 79 | |
| 80 | val mapi : (key -> 'a -> 'a) -> 'a t -> 'a t |
| 81 | (** As [mapi] but tries to preserve sharing. *) |
| 82 | end |
| 83 | |
| 84 | module Monad(M : MonadS) : |
| 85 | sig |
| 86 | val fold : (key -> 'a -> 'b -> 'b M.t) -> 'a t -> 'b -> 'b M.t |
| 87 | val mapi : (key -> 'a -> 'b M.t) -> 'a t -> 'b t M.t |
| 88 | end |
| 89 | (** Fold operators parameterized by any monad. *) |
| 90 | |
| 91 | end |
| 92 | |
| 93 | module type ExtS = sig |
| 94 | include CSig.MapS |
| 95 | |
| 96 | module Set : CSig.SetS with type elt = key |
| 97 | |
| 98 | include UExtS with type key := key and type 'a t := 'a t and module Set := Set |
| 99 | |
| 100 | module Monad(M:MonadS) : sig |
| 101 | include module type of Monad(M) |
| 102 | val fold_left : (key -> 'a -> 'b -> 'b M.t) -> 'a t -> 'b -> 'b M.t |
| 103 | val fold_right : (key -> 'a -> 'b -> 'b M.t) -> 'a t -> 'b -> 'b M.t |
| 104 | end |
| 105 | |
| 106 | val fold_left : (key -> 'a -> 'b -> 'b) -> 'a t -> 'b -> 'b |
| 107 | (** Alias for {!fold}, to easily track where we depend on fold order. *) |
| 108 | |
| 109 | val fold_right : (key -> 'a -> 'b -> 'b) -> 'a t -> 'b -> 'b |
| 110 | (** Folding keys in decreasing order. *) |
| 111 | |
| 112 | val fold_left_map : (key -> 'a -> 'b -> 'b * 'c) -> 'a t -> 'b -> 'b * 'c t |
| 113 | (** Combination of fold_left and map *) |
| 114 | |
| 115 | val fold_right_map : (key -> 'a -> 'b -> 'b * 'c) -> 'a t -> 'b -> 'b * 'c t |
| 116 | (** Combination of fold_right and map *) |
| 117 | end |
| 118 | |
| 119 | module Make(M : Map.OrderedType) : ExtS with |
| 120 | type key = M.t |
| 121 | and type 'a t = 'a Map.Make(M).t |
| 122 | and module Set := Set.Make(M) |