From 4f06874e39748c746f8e3ef93e9177ee36dc8fb9 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Wed, 28 Aug 2013 14:57:28 +0200 Subject: [PATCH] more natural functors to adapt Set and Map --- sequence.ml | 30 +++++++++++++----------------- sequence.mli | 27 ++++++++++++++------------- 2 files changed, 27 insertions(+), 30 deletions(-) diff --git a/sequence.ml b/sequence.ml index 7af1e12..549d1e2 100644 --- a/sequence.ml +++ b/sequence.ml @@ -28,6 +28,8 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. (** Sequence abstract iterator type *) type 'a t = ('a -> unit) -> unit +type 'a sequence = 'a t + type (+'a, +'b) t2 = ('a -> 'b -> unit) -> unit (** Sequence of pairs of values of type ['a] and ['b]. *) @@ -539,16 +541,13 @@ let to_set (type s) (type v) m seq = module Set = struct module type S = sig - type set - include Set.S with type t := set - val of_seq : elt t -> set - val to_seq : set -> elt t + include Set.S + val of_seq : elt sequence -> t + val to_seq : t -> elt sequence end (** Create an enriched Set module from the given one *) - module Adapt(X : Set.S) : S with type elt = X.elt and type set = X.t = struct - type set = X.t - + module Adapt(X : Set.S) = struct let to_seq set = from_iter (fun k -> X.iter k set) let of_seq seq = fold (fun set x -> X.add x set) X.empty seq @@ -557,7 +556,7 @@ module Set = struct end (** Functor to build an extended Set module from an ordered type *) - module Make(X : Set.OrderedType) : S with type elt = X.t = struct + module Make(X : Set.OrderedType) = struct module MySet = Set.Make(X) include Adapt(MySet) end @@ -567,18 +566,15 @@ end module Map = struct module type S = sig - type +'a map - include Map.S with type 'a t := 'a map - val to_seq : 'a map -> (key * 'a) t - val of_seq : (key * 'a) t -> 'a map - val keys : 'a map -> key t - val values : 'a map -> 'a t + include Map.S + val to_seq : 'a t -> (key * 'a) sequence + val of_seq : (key * 'a) sequence -> 'a t + val keys : 'a t -> key sequence + val values : 'a t -> 'a sequence end (** Adapt a pre-existing Map module to make it sequence-aware *) - module Adapt(M : Map.S) : S with type key = M.key and type 'a map = 'a M.t = struct - type 'a map = 'a M.t - + module Adapt(M : Map.S) = struct let to_seq m = from_iter (fun k -> M.iter (fun x y -> k (x,y)) m) let of_seq seq = fold (fun m (k,v) -> M.add k v m) M.empty seq diff --git a/sequence.mli b/sequence.mli index fa56dbf..16df438 100644 --- a/sequence.mli +++ b/sequence.mli @@ -42,6 +42,8 @@ type +'a t = ('a -> unit) -> unit (** Sequence abstract iterator type, representing a finite sequence of values of type ['a]. *) +type +'a sequence = 'a t + type (+'a, +'b) t2 = ('a -> 'b -> unit) -> unit (** Sequence of pairs of values of type ['a] and ['b]. *) @@ -70,7 +72,8 @@ val forever : (unit -> 'b) -> 'b t (** Sequence that calls the given function to produce elements *) val cycle : 'a t -> 'a t - (** Cycle forever through the given sequence. *) + (** Cycle forever through the given sequence. Assume the + given sequence can be traversed any amount of times (not transient). *) (** {2 Consume a sequence} *) @@ -286,14 +289,13 @@ val to_set : (module Set.S with type elt = 'a and type t = 'b) -> 'a t -> 'b module Set : sig module type S = sig - type set - include Set.S with type t := set - val of_seq : elt t -> set - val to_seq : set -> elt t + include Set.S + val of_seq : elt sequence -> t + val to_seq : t -> elt sequence end (** Create an enriched Set module from the given one *) - module Adapt(X : Set.S) : S with type elt = X.elt and type set = X.t + module Adapt(X : Set.S) : S with type elt = X.elt and type t = X.t (** Functor to build an extended Set module from an ordered type *) module Make(X : Set.OrderedType) : S with type elt = X.t @@ -303,16 +305,15 @@ end module Map : sig module type S = sig - type +'a map - include Map.S with type 'a t := 'a map - val to_seq : 'a map -> (key * 'a) t - val of_seq : (key * 'a) t -> 'a map - val keys : 'a map -> key t - val values : 'a map -> 'a t + include Map.S + val to_seq : 'a t -> (key * 'a) sequence + val of_seq : (key * 'a) sequence -> 'a t + val keys : 'a t -> key sequence + val values : 'a t -> 'a sequence end (** Adapt a pre-existing Map module to make it sequence-aware *) - module Adapt(M : Map.S) : S with type key = M.key and type 'a map = 'a M.t + module Adapt(M : Map.S) : S with type key = M.key and type 'a t = 'a M.t (** Create an enriched Map module, with sequence-aware functions *) module Make(V : Map.OrderedType) : S with type key = V.t