diff --git a/bij.ml b/bij.ml index 3f0bf337..916448d9 100644 --- a/bij.ml +++ b/bij.ml @@ -39,7 +39,7 @@ type _ t = | Map : ('a -> 'b) * ('b -> 'a) * 'b t -> 'a t | Switch : ('a -> char * 'a inject_branch) * (char -> 'a extract_branch) -> 'a t and _ inject_branch = - | BranchTo : 'b t * 'b * 'a -> 'a inject_branch + | BranchTo : 'b t * 'b -> 'a inject_branch and _ extract_branch = | BranchFrom : 'b t * ('b -> 'a) -> 'a extract_branch @@ -275,7 +275,7 @@ module SexpEncode(Sink : SINK) = struct let y = inject x in encode bij' y | Switch (inject, _), x -> - let c, BranchTo (bij', y, _) = inject x in + let c, BranchTo (bij', y) = inject x in Sink.write_char sink c; encode bij' y in encode bij x diff --git a/bij.mli b/bij.mli index d880985d..c8c193ea 100644 --- a/bij.mli +++ b/bij.mli @@ -44,16 +44,16 @@ val triple : 'a t -> 'b t -> 'c t -> ('a * 'b * 'c) t val map : inject:('a -> 'b) -> extract:('b -> 'a) -> 'b t -> 'a t type _ inject_branch = - | BranchTo : 'b t * 'b * 'a -> 'a inject_branch + | BranchTo : 'b t * 'b -> 'a inject_branch type _ extract_branch = | BranchFrom : 'b t * ('b -> 'a) -> 'a extract_branch val switch : inject:('a -> char * 'a inject_branch) -> extract:(char -> 'a extract_branch) -> 'a t - (** discriminates based on the next character. - The selection function, with type ['a -> char], is used to select a - bijection depending on the value. - ' ' means "default" *) + (** Discriminates unions based on the next character. + [inject] is used to select a character, as well as mapping to another + type (the argument of the algebraic constructor); + [extract] retrieves which type to parse based on the character. *) exception EOF diff --git a/tests/test_bij.ml b/tests/test_bij.ml index f7c2d61a..81dec3e0 100644 --- a/tests/test_bij.ml +++ b/tests/test_bij.ml @@ -34,10 +34,10 @@ type term = let bij_term = let rec mk_bij () = switch - ~inject:(fun t -> match t with - | Const s -> 'c', BranchTo (string_, s, t) - | Int i -> 'i', BranchTo (int_, i, t) - | App l -> 'a', BranchTo (list_ (mk_bij ()), l, t)) + ~inject:(function + | Const s -> 'c', BranchTo (string_, s) + | Int i -> 'i', BranchTo (int_, i) + | App l -> 'a', BranchTo (list_ (mk_bij ()), l)) ~extract:(function | 'c' -> BranchFrom (string_, fun x -> Const x) | 'i' -> BranchFrom (int_, fun x -> Int x)