diff --git a/src/BwdLabels.ml b/src/BwdLabels.ml index 6effb71..b6fb57e 100644 --- a/src/BwdLabels.ml +++ b/src/BwdLabels.ml @@ -20,7 +20,9 @@ let[@inline] init ~len ~f = BwdNoLabels.init len f let append = BwdNoLabels.append -let prepend = BwdNoLabels.prepend +let append_list = BwdNoLabels.append_list + +let prepend_list = BwdNoLabels.prepend_list let[@inline] equal ~eq xs ys = BwdNoLabels.equal eq xs ys diff --git a/src/BwdLabels.mli b/src/BwdLabels.mli index 5f859d6..3958352 100644 --- a/src/BwdLabels.mli +++ b/src/BwdLabels.mli @@ -3,13 +3,12 @@ {ul {- New: {ul - {- {!val:prepend} was added as the {i forward} version of {!val:append}.} + {- {!val:prepend_list} and {!val:append_list} for performing "textual order yoga".} {- {!val:to_list} and {!val:of_list} were added for conversions between standard lists.} {- {!module:Infix} was added for the infix notation.}}} {- Changed: {ul {- [cons] was replaced by {!val:snoc}.} - {- {!val:append} was replaced by a new version that performs the "textual order yoga".} {- All indices count from the right rather than the left: {!val:nth}, {!val:nth_opt}, {!val:init}, {!val:iteri}, {!val:mapi}, and {!val:filteri}.} {- All iteration functions work from the right rather than the left: @@ -52,9 +51,11 @@ val nth_opt : 'a t -> int -> 'a option val init : len:int -> f:(int -> 'a) -> 'a t -val append : 'a t -> 'a list -> 'a t +val append : 'a t -> 'a t -> 'a t -val prepend : 'a t -> 'a list -> 'a list +val append_list : 'a t -> 'a list -> 'a t + +val prepend_list : 'a t -> 'a list -> 'a list (** {1 Comparison} *) diff --git a/src/BwdNoLabels.ml b/src/BwdNoLabels.ml index bcc1389..2759f65 100644 --- a/src/BwdNoLabels.ml +++ b/src/BwdNoLabels.ml @@ -67,7 +67,12 @@ let init len f = in go 0 (len - 1) -let append xs ys = +let[@tail_mod_cons] rec append xs ys = + match ys with + | Emp -> xs + | Snoc (ys, y) -> Snoc(append xs ys, y) + +let append_list xs ys = let rec go = function | xs, [] -> xs @@ -75,7 +80,7 @@ let append xs ys = (go[@tailcall]) (Snoc (xs, y), ys) in go (xs, ys) -let prepend xs ys = +let prepend_list xs ys = let rec go = function | Emp, ys -> ys @@ -163,7 +168,7 @@ let fold_left f init = let fold_right_map f xs init = let rec go xs init ys = match xs with - | Emp -> init, append Emp ys + | Emp -> init, append_list Emp ys | Snoc (xs, x) -> let init, y = f x init in (go[@tailcall]) xs init (y :: ys) @@ -339,7 +344,7 @@ let filteri f = let partition f xs = let rec go xs ys zs = match xs with - | Emp -> append Emp ys, append Emp zs + | Emp -> append_list Emp ys, append_list Emp zs | Snoc (xs, x) -> if f x then (go[@tailcall]) xs (x :: ys) zs @@ -350,7 +355,7 @@ let partition f xs = let partition_map f xs = let rec go xs ys zs = match xs with - | Emp -> append Emp ys, append Emp zs + | Emp -> append_list Emp ys, append_list Emp zs | Snoc (xs, x) -> match f x with | Either.Left y -> @@ -376,19 +381,19 @@ let combine xs ys = in go (xs, ys) let to_list xs = - prepend xs [] + prepend_list xs [] let of_list xs = - append Emp xs + append_list Emp xs module Infix = struct let (<:) = snoc - let (<@) = append - let (@>) = prepend + let (<@) = append_list + let (@>) = prepend_list let (#<) = snoc - let (<><) = append - let (<>>) = prepend + let (<><) = append_list + let (<>>) = prepend_list end module Notation = Infix diff --git a/src/BwdNoLabels.mli b/src/BwdNoLabels.mli index 2ff90b1..5ce6ff9 100644 --- a/src/BwdNoLabels.mli +++ b/src/BwdNoLabels.mli @@ -3,13 +3,12 @@ {ul {- New: {ul - {- {!val:prepend} was added as the {i forward} version of {!val:append}.} + {- {!val:prepend_list} and {!val:append_list} for performing "textual order yoga".} {- {!val:to_list} and {!val:of_list} were added for conversions between standard lists.} {- {!module:Infix} was added for the infix notation.}}} {- Changed: {ul {- [cons] was replaced by {!val:snoc}.} - {- {!val:append} was replaced by a new version that performs the "textual order yoga".} {- All indices count from the right rather than the left: {!val:nth}, {!val:nth_opt}, {!val:init}, {!val:iteri}, {!val:mapi}, and {!val:filteri}.} {- All iteration functions work from the right rather than the left: @@ -52,9 +51,11 @@ val nth_opt : 'a t -> int -> 'a option val init : int -> (int -> 'a) -> 'a t -val append : 'a t -> 'a list -> 'a t +val append : 'a t -> 'a t -> 'a t -val prepend : 'a t -> 'a list -> 'a list +val append_list : 'a t -> 'a list -> 'a t + +val prepend_list : 'a t -> 'a list -> 'a list (** {1 Comparison} *) diff --git a/test/TestBwdLabels.ml b/test/TestBwdLabels.ml index 090dbd9..00a1d8f 100644 --- a/test/TestBwdLabels.ml +++ b/test/TestBwdLabels.ml @@ -65,12 +65,16 @@ let test_init = (fun (len, Fun (_, f)) -> trap (fun () -> to_list (B.init ~len ~f)) = trap (fun () -> L.init ~len ~f)) let test_append = Q.Test.make ~count ~name:"append" Q.Gen.(pair (small_list int) (small_list int)) + ~print:Q.Print.(pair (list int) (list int)) + (fun (xs, ys) -> to_list (B.append (of_list xs) (of_list ys)) = L.append xs ys) +let test_append_list = + Q.Test.make ~count ~name:"append_list" Q.Gen.(pair (small_list int) (small_list int)) ~print:Q.Print.(pair (list int) (list int)) - (fun (xs, ys) -> to_list (B.append (of_list xs) ys) = L.append xs ys) -let test_prepend = - Q.Test.make ~count ~name:"prepend" Q.Gen.(pair (small_list int) (small_list int)) + (fun (xs, ys) -> to_list (B.append_list (of_list xs) ys) = L.append xs ys) +let test_prepend_list = + Q.Test.make ~count ~name:"prepend_list" Q.Gen.(pair (small_list int) (small_list int)) ~print:Q.Print.(pair (list int) (list int)) - (fun (xs, ys) -> B.prepend (of_list xs) ys = L.prepend xs ys) + (fun (xs, ys) -> B.prepend_list (of_list xs) ys = L.prepend xs ys) let test_equal = Q.Test.make ~count ~name:"equal" Q.Gen.(triple (Q.fun2 Q.Observable.int Q.Observable.int bool) (small_list small_nat) (small_list small_nat)) @@ -322,7 +326,8 @@ let () = test_nth_opt; test_init; test_append; - test_prepend; + test_append_list; + test_prepend_list; test_equal; test_compare; test_iter;