diff --git a/.gitignore b/.gitignore index 5fbd0e27..71c179e7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .*.swp +.*.swo _build *.native .session diff --git a/gen.ml b/gen.ml index f0e088de..c95afe14 100644 --- a/gen.ml +++ b/gen.ml @@ -398,6 +398,21 @@ let max ?(lt=fun x y -> x < y) enum = let first = try gen () with EOG -> raise Not_found in Gen.fold (fun max x -> if lt max x then x else max) first gen +let lexico ?(cmp=compare) e1 e2 = + let gen1 = e1() in + let gen2 = e2() in + let rec lexico () = + let x1 = try Some (gen1 ()) with EOG -> None in + let x2 = try Some (gen2 ()) with EOG -> None in + match x1, x2 with + | None, None -> 0 + | Some x1, Some x2 -> + let c = cmp x1 x2 in + if c <> 0 then c else lexico () + | Some _, None -> 1 + | None, Some _ -> -1 + in lexico () + (** {2 Complex combinators} *) (** Pick elements fairly in each sub-enum *) diff --git a/gen.mli b/gen.mli index 48dd2ffe..2a1f7550 100644 --- a/gen.mli +++ b/gen.mli @@ -193,6 +193,9 @@ val min : ?lt:('a -> 'a -> bool) -> 'a t -> 'a val max : ?lt:('a -> 'a -> bool) -> 'a t -> 'a (** Maximum element *) +val lexico : ?cmp:('a -> 'a -> int) -> 'a t -> 'a t -> int + (** Lexicographic comparison of generators *) + (** {2 Complex combinators} *) val merge : 'a t t -> 'a t