From 80522a4959bfcc3e56cf8140b6d7c12731d35999 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Wed, 11 Jun 2014 22:25:59 +0200 Subject: [PATCH] expose the optimization to the user in CCBatch --- core/CCBatch.ml | 13 +++++++++++++ core/CCBatch.mli | 6 ++++++ 2 files changed, 19 insertions(+) diff --git a/core/CCBatch.ml b/core/CCBatch.ml index 036860cc..0102f163 100644 --- a/core/CCBatch.ml +++ b/core/CCBatch.ml @@ -45,6 +45,12 @@ module type S = sig val apply : ('a,'b) op -> 'a t -> 'b t val apply' : 'a t -> ('a,'b) op -> 'b t + val length : (_,_) op -> int + (** Number of intermediate structures needed to compute this operation *) + + val optimize : ('a,'b) op -> ('a,'b) op + (** Try to minimize the length of the operation *) + (** {6 Combinators} *) val id : ('a, 'a) op @@ -154,6 +160,13 @@ module Make(C : COLLECTION) = struct | (Compose _) as op -> Same op (* cannot optimize *) + let rec length : type a b. (a,b) op -> int = function + | Id -> 0 + | Compose (_, Id) -> 0 + | Compose (_, cont) -> 1 + length cont + + let optimize = _optimize + let apply op a = let rec _apply : type a b. (a,b) op -> a t -> b t = fun op a -> match op with diff --git a/core/CCBatch.mli b/core/CCBatch.mli index 9931929f..8f7c0b12 100644 --- a/core/CCBatch.mli +++ b/core/CCBatch.mli @@ -51,6 +51,12 @@ module type S = sig val apply : ('a,'b) op -> 'a t -> 'b t val apply' : 'a t -> ('a,'b) op -> 'b t + val length : (_,_) op -> int + (** Number of intermediate structures needed to compute this operation *) + + val optimize : ('a,'b) op -> ('a,'b) op + (** Try to minimize the length of the operation *) + (** {6 Combinators} *) val id : ('a, 'a) op