2020-05-18 15:59:07 +03:00
|
|
|
||| Contains:
|
|
|
|
|||
|
|
|
|
||| 1. Tail recursive versions of the list processing functions from
|
2022-03-23 16:33:13 +03:00
|
|
|
||| List.
|
2020-07-14 14:15:57 +03:00
|
|
|
|||
|
2020-05-18 15:59:07 +03:00
|
|
|
||| 2. Extensional equality proofs that these variants are
|
|
|
|
||| (extensionally) equivalent to their non-tail-recursive
|
|
|
|
||| counterparts.
|
|
|
|
|||
|
|
|
|
||| Note:
|
|
|
|
|||
|
|
|
|
||| 1. Written in one sitting, feel free to refactor
|
2020-07-14 14:15:57 +03:00
|
|
|
|||
|
2020-05-18 15:59:07 +03:00
|
|
|
||| 2. The proofs below also work on non-publicly exported
|
|
|
|
||| definitions. This could be due to a bug, and will need to be
|
|
|
|
||| moved elsewhere if it's fixed.
|
|
|
|
module Data.List.TailRec
|
|
|
|
|
|
|
|
import Syntax.PreorderReasoning
|
|
|
|
import Syntax.WithProof
|
|
|
|
|
2020-06-16 22:36:15 +03:00
|
|
|
import Data.List
|
2020-08-04 22:03:18 +03:00
|
|
|
import Data.List1
|
2020-05-18 15:59:07 +03:00
|
|
|
|
2022-02-03 20:41:03 +03:00
|
|
|
%default total
|
|
|
|
|
2020-07-08 02:55:55 +03:00
|
|
|
lengthAcc : List a -> Nat -> Nat
|
|
|
|
lengthAcc [] acc = acc
|
|
|
|
lengthAcc (_::xs) acc = lengthAcc xs $ S acc
|
2020-05-18 15:59:07 +03:00
|
|
|
|
2022-02-03 20:41:03 +03:00
|
|
|
export
|
2020-05-18 15:59:07 +03:00
|
|
|
length : List a -> Nat
|
2020-07-08 02:55:55 +03:00
|
|
|
length xs = lengthAcc xs Z
|
2020-05-18 15:59:07 +03:00
|
|
|
|
2020-07-08 02:55:55 +03:00
|
|
|
lengthAccSucc : (xs : List a) -> (n : Nat) -> lengthAcc xs (S n) = S (lengthAcc xs n)
|
|
|
|
lengthAccSucc [] _ = Refl
|
|
|
|
lengthAccSucc (_::xs) n = rewrite lengthAccSucc xs (S n) in cong S Refl
|
|
|
|
|
2022-02-03 20:41:03 +03:00
|
|
|
export
|
2022-03-23 16:33:13 +03:00
|
|
|
length_ext : (xs : List a) -> List.length xs = List.TailRec.length xs
|
2020-07-08 02:55:55 +03:00
|
|
|
length_ext [] = Refl
|
|
|
|
length_ext (_::xs) = rewrite length_ext xs in sym $ lengthAccSucc xs Z
|
2020-05-18 15:59:07 +03:00
|
|
|
|
|
|
|
take_aux : Nat -> List a -> List a -> List a
|
|
|
|
take_aux Z xs acc = reverseOnto [] acc
|
|
|
|
take_aux (S n) [] acc = reverseOnto [] acc
|
|
|
|
take_aux (S n) (x :: xs) acc = take_aux n xs (x :: acc)
|
2020-07-14 14:15:57 +03:00
|
|
|
|
|
|
|
export
|
2020-05-18 15:59:07 +03:00
|
|
|
take : Nat -> List a -> List a
|
|
|
|
take n xs = take_aux n xs []
|
|
|
|
|
|
|
|
export
|
2020-07-14 14:15:57 +03:00
|
|
|
take_ext :
|
2020-05-18 15:59:07 +03:00
|
|
|
(n : Nat) -> (xs : List a) ->
|
2022-03-23 16:33:13 +03:00
|
|
|
List.take n xs = List.TailRec.take n xs
|
2020-06-16 22:09:31 +03:00
|
|
|
take_ext n xs = Calc $
|
2022-03-23 16:33:13 +03:00
|
|
|
|~ List.take n xs
|
|
|
|
~~ reverse [] ++ (List.take n xs) ...( Refl )
|
|
|
|
~~ reverseOnto (List.take n xs) [] ...( sym (revOnto (List.take n xs) []) )
|
2020-06-16 22:09:31 +03:00
|
|
|
~~ take_aux n xs [] ...( sym (lemma n xs []) )
|
2020-05-18 15:59:07 +03:00
|
|
|
where
|
2020-07-14 14:15:57 +03:00
|
|
|
lemma : (n : Nat) -> (xs, acc : List a) ->
|
2022-03-23 16:33:13 +03:00
|
|
|
take_aux n xs acc = reverseOnto (List.take n xs) acc
|
2020-05-18 15:59:07 +03:00
|
|
|
lemma Z xs acc = Refl
|
|
|
|
lemma (S n) [] acc = Refl
|
2020-07-14 14:15:57 +03:00
|
|
|
lemma (S n) (x::xs) acc = lemma n xs (x :: acc)
|
2020-05-18 15:59:07 +03:00
|
|
|
|
|
|
|
|
|
|
|
span_aux : (a -> Bool) -> List a -> List a -> (List a, List a)
|
|
|
|
span_aux p [] acc = (reverseOnto [] acc, [])
|
2020-07-14 14:15:57 +03:00
|
|
|
span_aux p (x::xs) acc =
|
2020-05-18 15:59:07 +03:00
|
|
|
if p x then
|
2022-03-23 16:33:13 +03:00
|
|
|
List.TailRec.span_aux p xs (x :: acc)
|
2020-05-18 15:59:07 +03:00
|
|
|
else
|
|
|
|
(reverseOnto [] acc, x::xs)
|
2020-07-14 14:15:57 +03:00
|
|
|
|
2020-05-18 15:59:07 +03:00
|
|
|
export
|
|
|
|
span : (a -> Bool) -> List a -> (List a, List a)
|
|
|
|
span p xs = span_aux p xs []
|
|
|
|
|
|
|
|
coe : (f : (i : a) -> Type) -> i = i' -> f i -> f i'
|
|
|
|
coe f Refl x = x
|
|
|
|
|
2020-07-14 14:15:57 +03:00
|
|
|
span_aux_ext : (p : a -> Bool) -> (xs, acc : List a) ->
|
2022-03-23 16:33:13 +03:00
|
|
|
(reverseOnto (fst $ List.span p xs) acc, snd $ List.span p xs)
|
2020-05-18 15:59:07 +03:00
|
|
|
=
|
|
|
|
span_aux p xs acc
|
|
|
|
span_aux_ext p [] acc = Refl
|
|
|
|
-- This is disgusting. Please teach me a better way.
|
2022-03-23 16:33:13 +03:00
|
|
|
span_aux_ext p (x::xs) acc with (@@(p x), @@(List.span p xs))
|
2020-07-14 14:15:57 +03:00
|
|
|
span_aux_ext p (x::xs) acc | ((True ** px_tru), ((pre, rest)**dl_pf)) =
|
|
|
|
rewrite px_tru in
|
|
|
|
rewrite dl_pf in
|
2020-05-18 15:59:07 +03:00
|
|
|
let u = span_aux_ext p xs (x::acc) in
|
2020-07-14 14:15:57 +03:00
|
|
|
coe (\u => (reverseOnto (x :: fst u) acc, snd u) =
|
2022-03-23 16:33:13 +03:00
|
|
|
List.TailRec.span_aux p xs (x :: acc)) dl_pf u
|
2020-07-14 14:15:57 +03:00
|
|
|
span_aux_ext p (x::xs) acc | ((False**px_fls), ((pre,rest)**dl_pf)) =
|
2020-05-18 15:59:07 +03:00
|
|
|
rewrite px_fls in
|
|
|
|
Refl
|
|
|
|
|
|
|
|
export
|
2020-07-14 14:15:57 +03:00
|
|
|
span_ext : (p : a -> Bool) -> (xs : List a) ->
|
2022-03-23 16:33:13 +03:00
|
|
|
List.span p xs = List.TailRec.span p xs
|
|
|
|
span_ext p xs with (@@(List.span p xs))
|
2020-07-14 14:15:57 +03:00
|
|
|
span_ext p xs | ((pre, rest) ** pf) =
|
|
|
|
rewrite pf in
|
|
|
|
let u = span_aux_ext p xs [] in
|
|
|
|
coe (\u => (fst u, snd u) = span_aux p xs []) pf u
|
2020-05-18 15:59:07 +03:00
|
|
|
|
|
|
|
export
|
|
|
|
break : (a -> Bool) -> List a -> (List a, List a)
|
2022-03-23 16:33:13 +03:00
|
|
|
break p xs = List.TailRec.span (not . p) xs
|
2020-05-18 15:59:07 +03:00
|
|
|
|
|
|
|
export
|
|
|
|
break_ext : (p : a -> Bool) -> (xs : List a) ->
|
2022-03-23 16:33:13 +03:00
|
|
|
List.break p xs = List.TailRec.break p xs
|
2020-05-18 15:59:07 +03:00
|
|
|
break_ext p xs = span_ext (not . p) xs
|
|
|
|
|
2020-08-04 22:03:18 +03:00
|
|
|
splitOnto : List (List a) -> (a -> Bool) -> List a -> List1 (List a)
|
2020-07-14 14:15:57 +03:00
|
|
|
splitOnto acc p xs =
|
2022-03-23 16:33:13 +03:00
|
|
|
case List.break p xs of
|
2020-09-22 17:07:40 +03:00
|
|
|
(chunk, [] ) => reverseOnto (chunk ::: []) acc
|
2022-02-03 20:41:03 +03:00
|
|
|
(chunk, (c::rest)) => splitOnto (chunk::acc) p $ assert_smaller xs rest
|
2020-05-18 15:59:07 +03:00
|
|
|
|
2020-07-14 14:15:57 +03:00
|
|
|
export
|
2020-08-04 22:03:18 +03:00
|
|
|
split : (a -> Bool) -> List a -> List1 (List a)
|
2020-05-18 15:59:07 +03:00
|
|
|
split p xs = splitOnto [] p xs
|
|
|
|
|
|
|
|
splitOnto_ext : (acc : List (List a)) -> (p : a -> Bool) -> (xs : List a) ->
|
2022-03-23 16:33:13 +03:00
|
|
|
reverseOnto (List.split p xs) acc
|
|
|
|
= List.TailRec.splitOnto acc p xs
|
|
|
|
splitOnto_ext acc p xs with (@@(List.break p xs))
|
2020-07-14 14:15:57 +03:00
|
|
|
splitOnto_ext acc p xs | ((chunk, [] )**pf) =
|
|
|
|
rewrite pf in
|
2020-05-18 15:59:07 +03:00
|
|
|
Refl
|
2020-07-14 14:15:57 +03:00
|
|
|
splitOnto_ext acc p xs | ((chunk, c::rest)**pf) =
|
2020-05-18 15:59:07 +03:00
|
|
|
rewrite pf in
|
2022-02-03 20:41:03 +03:00
|
|
|
rewrite splitOnto_ext (chunk::acc) p $ assert_smaller xs rest in
|
2020-05-18 15:59:07 +03:00
|
|
|
Refl
|
|
|
|
|
|
|
|
export
|
|
|
|
split_ext : (p : a -> Bool) -> (xs : List a) ->
|
2022-03-23 16:33:13 +03:00
|
|
|
List.split p xs = List.TailRec.split p xs
|
2020-05-18 15:59:07 +03:00
|
|
|
split_ext p xs = splitOnto_ext [] p xs
|
|
|
|
|
|
|
|
|
|
|
|
splitAtOnto : List a -> (n : Nat) -> (xs : List a) -> (List a, List a)
|
|
|
|
splitAtOnto acc Z xs = (reverseOnto [] acc, xs)
|
|
|
|
splitAtOnto acc (S n) [] = (reverseOnto [] acc, [])
|
|
|
|
splitAtOnto acc (S n) (x::xs) = splitAtOnto (x::acc) n xs
|
|
|
|
|
|
|
|
export
|
|
|
|
splitAt : (n : Nat) -> (xs : List a) -> (List a, List a)
|
|
|
|
splitAt n xs = splitAtOnto [] n xs
|
|
|
|
|
|
|
|
splitAtOnto_ext : (acc : List a) -> (n : Nat) -> (xs : List a) ->
|
2022-03-23 16:33:13 +03:00
|
|
|
(reverseOnto (fst $ List.splitAt n xs) acc, snd $ List.splitAt n xs)
|
2020-05-18 15:59:07 +03:00
|
|
|
= splitAtOnto acc n xs
|
|
|
|
splitAtOnto_ext acc Z xs = Refl
|
|
|
|
splitAtOnto_ext acc (S n) [] = Refl
|
2022-03-23 16:33:13 +03:00
|
|
|
splitAtOnto_ext acc (S n) (x::xs) with (@@(List.splitAt n xs))
|
2020-07-14 14:15:57 +03:00
|
|
|
splitAtOnto_ext acc (S n) (x::xs) | ((tk, dr)**pf) =
|
2020-05-18 15:59:07 +03:00
|
|
|
rewrite pf in
|
|
|
|
let u = splitAtOnto_ext (x::acc) n xs in
|
2020-07-14 14:15:57 +03:00
|
|
|
coe (\u => (reverseOnto (x :: fst u) acc, snd u) =
|
|
|
|
splitAtOnto (x :: acc) n xs) pf u
|
2020-05-18 15:59:07 +03:00
|
|
|
|
|
|
|
export
|
2020-07-14 14:15:57 +03:00
|
|
|
splitAt_ext : (n : Nat) -> (xs : List a) ->
|
2022-03-23 16:33:13 +03:00
|
|
|
List.splitAt n xs =
|
|
|
|
List.TailRec.splitAt n xs
|
|
|
|
splitAt_ext n xs with (@@(List.splitAt n xs))
|
2020-07-14 14:15:57 +03:00
|
|
|
splitAt_ext n xs | ((tk, dr)**pf) =
|
2020-05-18 15:59:07 +03:00
|
|
|
rewrite pf in
|
|
|
|
rewrite sym $ splitAtOnto_ext [] n xs in
|
2020-07-14 14:15:57 +03:00
|
|
|
rewrite pf in
|
2020-05-18 15:59:07 +03:00
|
|
|
Refl
|
|
|
|
|
|
|
|
partitionOnto : List a -> List a -> (a -> Bool) -> List a -> (List a, List a)
|
|
|
|
partitionOnto lfts rgts p [] = (reverseOnto [] lfts, reverseOnto [] rgts)
|
2020-07-14 14:15:57 +03:00
|
|
|
partitionOnto lfts rgts p (x::xs) =
|
2020-05-18 15:59:07 +03:00
|
|
|
if p x then
|
|
|
|
partitionOnto (x :: lfts) rgts p xs
|
|
|
|
else
|
|
|
|
partitionOnto lfts (x::rgts) p xs
|
2020-07-14 14:15:57 +03:00
|
|
|
|
2020-05-18 15:59:07 +03:00
|
|
|
export
|
|
|
|
partition : (a -> Bool) -> List a -> (List a, List a)
|
|
|
|
partition p xs = partitionOnto [] [] p xs
|
|
|
|
|
2020-07-14 14:15:57 +03:00
|
|
|
partitionOnto_ext : (lfts, rgts : List a) -> (p : a -> Bool) -> (xs : List a) ->
|
2022-03-23 16:33:13 +03:00
|
|
|
(reverseOnto (fst $ List.partition p xs) lfts
|
|
|
|
,reverseOnto (snd $ List.partition p xs) rgts)
|
|
|
|
= List.TailRec.partitionOnto lfts rgts p xs
|
2020-05-18 15:59:07 +03:00
|
|
|
partitionOnto_ext lfts rgts p [] = Refl
|
2022-03-23 16:33:13 +03:00
|
|
|
partitionOnto_ext lfts rgts p (x::xs) with (@@(p x), @@(List.partition p xs))
|
2020-05-18 15:59:07 +03:00
|
|
|
partitionOnto_ext lfts rgts p (x::xs) | ((True **px_tru), ((dl_l, dl_r)**dl_pf))
|
2020-07-14 14:15:57 +03:00
|
|
|
= rewrite px_tru in
|
2020-05-18 15:59:07 +03:00
|
|
|
rewrite dl_pf in
|
|
|
|
rewrite px_tru in
|
|
|
|
let u = partitionOnto_ext (x :: lfts) rgts p xs in
|
|
|
|
coe (\u => (reverseOnto (x :: fst u) lfts
|
2020-07-14 14:15:57 +03:00
|
|
|
,reverseOnto ( snd u) rgts)
|
2020-05-18 15:59:07 +03:00
|
|
|
= partitionOnto (x :: lfts) rgts p xs) dl_pf u
|
|
|
|
|
|
|
|
partitionOnto_ext lfts rgts p (x::xs) | ((False**px_fls), ((dl_l, dl_r)**dl_pf))
|
|
|
|
= rewrite px_fls in
|
|
|
|
rewrite dl_pf in
|
2020-07-14 14:15:57 +03:00
|
|
|
rewrite px_fls in
|
2020-05-18 15:59:07 +03:00
|
|
|
let u = partitionOnto_ext lfts (x :: rgts) p xs in
|
|
|
|
coe (\u => (reverseOnto ( fst u) lfts
|
2020-07-14 14:15:57 +03:00
|
|
|
,reverseOnto (x :: snd u) rgts)
|
2020-05-18 15:59:07 +03:00
|
|
|
= partitionOnto lfts (x::rgts) p xs) dl_pf u
|
|
|
|
|
|
|
|
mergeReplicate_aux : a -> List a -> List a -> List a
|
|
|
|
mergeReplicate_aux sep [] acc = reverseOnto [] acc
|
|
|
|
mergeReplicate_aux sep (x::xs) acc = mergeReplicate_aux sep xs (x :: sep :: acc)
|
|
|
|
|
|
|
|
mergeReplicate_ext : (sep : a) -> (xs : List a) -> (acc : List a) ->
|
2020-07-14 14:15:57 +03:00
|
|
|
mergeReplicate_aux sep xs acc =
|
2020-05-18 15:59:07 +03:00
|
|
|
reverseOnto (mergeReplicate sep xs) acc
|
|
|
|
mergeReplicate_ext sep [] acc = Refl
|
2020-06-16 22:09:31 +03:00
|
|
|
mergeReplicate_ext sep (x::xs) acc = Calc $
|
|
|
|
|~ mergeReplicate_aux sep xs (x :: sep :: acc)
|
2020-07-14 14:15:57 +03:00
|
|
|
~~ reverseOnto (sep :: x :: mergeReplicate sep xs) acc
|
2020-06-16 22:09:31 +03:00
|
|
|
...( mergeReplicate_ext sep xs (x :: sep :: acc) )
|
2020-05-18 15:59:07 +03:00
|
|
|
|
|
|
|
export
|
|
|
|
intersperse : a -> List a -> List a
|
|
|
|
intersperse sep [] = []
|
|
|
|
intersperse sep (y::ys) = y :: mergeReplicate_aux sep ys []
|
2020-07-14 14:15:57 +03:00
|
|
|
|
2020-05-18 15:59:07 +03:00
|
|
|
export
|
2020-07-14 14:15:57 +03:00
|
|
|
intersperse_ext : (sep : a) -> (xs : List a) ->
|
2022-03-23 16:33:13 +03:00
|
|
|
List.intersperse sep xs =
|
|
|
|
List.TailRec.intersperse sep xs
|
2020-05-18 15:59:07 +03:00
|
|
|
intersperse_ext sep [] = Refl
|
|
|
|
intersperse_ext sep (y::ys) = cong (y::) (sym $ mergeReplicate_ext sep ys [])
|
|
|
|
|
|
|
|
mapMaybeOnto : List b -> (a -> Maybe b) -> List a -> List b
|
|
|
|
mapMaybeOnto acc f [] = reverseOnto [] acc
|
2020-07-14 14:15:57 +03:00
|
|
|
mapMaybeOnto acc f (x::xs) =
|
2020-05-18 15:59:07 +03:00
|
|
|
case f x of
|
|
|
|
Nothing => mapMaybeOnto acc f xs
|
|
|
|
Just y => mapMaybeOnto (y :: acc) f xs
|
|
|
|
|
|
|
|
export
|
|
|
|
mapMaybe : (a -> Maybe b) -> List a -> List b
|
|
|
|
mapMaybe f xs = mapMaybeOnto [] f xs
|
|
|
|
|
|
|
|
mapMaybeOnto_ext : (acc : List b) -> (f : a -> Maybe b) -> (xs : List a) ->
|
2022-03-23 16:33:13 +03:00
|
|
|
reverseOnto (List.mapMaybe f xs) acc
|
2020-07-14 14:15:57 +03:00
|
|
|
=
|
2020-05-18 15:59:07 +03:00
|
|
|
mapMaybeOnto acc f xs
|
|
|
|
mapMaybeOnto_ext acc f [] = Refl
|
|
|
|
mapMaybeOnto_ext acc f (x::xs) with (f x)
|
|
|
|
mapMaybeOnto_ext acc f (x::xs) | Nothing = mapMaybeOnto_ext acc f xs
|
|
|
|
mapMaybeOnto_ext acc f (x::xs) | Just y = mapMaybeOnto_ext (y :: acc) f xs
|
2020-07-14 14:15:57 +03:00
|
|
|
|
|
|
|
export
|
|
|
|
mapMaybe_ext : (f : a -> Maybe b) -> (xs : List a) ->
|
2022-03-23 16:33:13 +03:00
|
|
|
List.mapMaybe f xs = List.TailRec.mapMaybe f xs
|
2020-05-18 15:59:07 +03:00
|
|
|
mapMaybe_ext f xs = mapMaybeOnto_ext [] f xs
|
|
|
|
|
|
|
|
export
|
|
|
|
sorted : Ord a => List a -> Bool
|
|
|
|
sorted [ ] = True
|
|
|
|
sorted [x] = True
|
|
|
|
sorted (x :: xs@(y :: ys)) = case (x <= y) of
|
|
|
|
False => False
|
2022-03-23 16:33:13 +03:00
|
|
|
True => List.TailRec.sorted xs
|
2020-07-14 14:15:57 +03:00
|
|
|
|
2020-05-18 15:59:07 +03:00
|
|
|
export
|
2022-02-03 20:41:03 +03:00
|
|
|
covering
|
2020-07-14 14:15:57 +03:00
|
|
|
sorted_ext : Ord a => (xs : List a) ->
|
2022-03-23 16:33:13 +03:00
|
|
|
List.sorted xs = List.TailRec.sorted xs
|
2020-05-18 15:59:07 +03:00
|
|
|
sorted_ext [] = Refl
|
|
|
|
sorted_ext [x] = Refl
|
2020-07-14 14:15:57 +03:00
|
|
|
sorted_ext (x :: y :: ys) with (x <= y)
|
2020-05-18 15:59:07 +03:00
|
|
|
sorted_ext (x :: y :: ys) | False = Refl
|
|
|
|
sorted_ext (x :: y :: ys) | True = sorted_ext (y::ys)
|
2020-07-14 14:15:57 +03:00
|
|
|
|
2020-05-18 15:59:07 +03:00
|
|
|
mergeByOnto : List a -> (a -> a -> Ordering) -> List a -> List a -> List a
|
|
|
|
mergeByOnto acc order [] right = reverseOnto right acc
|
|
|
|
mergeByOnto acc order left [] = reverseOnto left acc
|
2020-07-14 14:15:57 +03:00
|
|
|
mergeByOnto acc order left@(x::xs) right@(y::ys) =
|
2020-06-16 22:09:31 +03:00
|
|
|
-- We need the variant annotations (bug #300)
|
2020-07-14 14:15:57 +03:00
|
|
|
case order x y of
|
2020-06-16 22:09:31 +03:00
|
|
|
LT => mergeByOnto (x :: acc) order (assert_smaller left xs) right
|
|
|
|
_ => mergeByOnto (y :: acc) order left (assert_smaller right ys)
|
2020-07-14 14:15:57 +03:00
|
|
|
|
2022-02-03 20:41:03 +03:00
|
|
|
covering
|
2020-07-14 14:15:57 +03:00
|
|
|
mergeByOnto_ext : (acc : List a)
|
|
|
|
-> (order : a -> a -> Ordering)
|
|
|
|
-> (left, right : List a)
|
|
|
|
-> reverseOnto (mergeBy order left right) acc
|
2020-05-18 15:59:07 +03:00
|
|
|
= mergeByOnto acc order left right
|
|
|
|
mergeByOnto_ext acc order [] right = Refl
|
2020-07-14 14:15:57 +03:00
|
|
|
mergeByOnto_ext acc order left [] with( left)
|
2020-06-16 22:09:31 +03:00
|
|
|
mergeByOnto_ext acc order _ [] | [] = Refl
|
|
|
|
mergeByOnto_ext acc order _ [] | (_::_) = Refl
|
2020-05-18 15:59:07 +03:00
|
|
|
mergeByOnto_ext acc order left@(x::xs) right@(y::ys) with (order x y)
|
2020-07-14 14:15:57 +03:00
|
|
|
mergeByOnto_ext acc order left@(x::xs) right@(y::ys) | LT =
|
2020-05-18 15:59:07 +03:00
|
|
|
mergeByOnto_ext (x :: acc) order xs (y::ys)
|
|
|
|
-- We need to exhaust the two other cases explicitly to convince the
|
|
|
|
-- typecheker. See #140
|
2020-07-14 14:15:57 +03:00
|
|
|
mergeByOnto_ext acc order left@(x::xs) right@(y::ys) | EQ =
|
2020-05-18 15:59:07 +03:00
|
|
|
mergeByOnto_ext (y :: acc) order (x::xs) ys
|
2020-07-14 14:15:57 +03:00
|
|
|
mergeByOnto_ext acc order left@(x::xs) right@(y::ys) | GT =
|
2020-05-18 15:59:07 +03:00
|
|
|
mergeByOnto_ext (y :: acc) order (x::xs) ys
|
|
|
|
|
|
|
|
export
|
|
|
|
mergeBy : (a -> a -> Ordering) -> List a -> List a -> List a
|
|
|
|
mergeBy order left right = mergeByOnto [] order left right
|
|
|
|
|
|
|
|
export
|
2022-02-03 20:41:03 +03:00
|
|
|
covering
|
2020-05-18 15:59:07 +03:00
|
|
|
mergeBy_ext : (order : a -> a -> Ordering) -> (left, right : List a) ->
|
2022-03-23 16:33:13 +03:00
|
|
|
List.mergeBy order left right =
|
|
|
|
List.TailRec.mergeBy order left right
|
2020-05-18 15:59:07 +03:00
|
|
|
mergeBy_ext order left right = mergeByOnto_ext [] order left right
|
|
|
|
|
|
|
|
export
|
|
|
|
merge : Ord a => List a -> List a -> List a
|
2022-03-23 16:33:13 +03:00
|
|
|
merge = List.TailRec.mergeBy compare
|
2020-05-18 15:59:07 +03:00
|
|
|
|
|
|
|
export
|
2022-02-03 20:41:03 +03:00
|
|
|
covering
|
2020-05-18 15:59:07 +03:00
|
|
|
merge_ext : Ord a => (left, right : List a) ->
|
2022-03-23 16:33:13 +03:00
|
|
|
List.merge left right = List.TailRec.merge left right
|
2020-05-18 15:59:07 +03:00
|
|
|
merge_ext left right = mergeBy_ext compare left right
|
|
|
|
|
|
|
|
|
2020-07-14 14:15:57 +03:00
|
|
|
-- Not quite finished due to a bug.
|
2020-05-18 15:59:07 +03:00
|
|
|
|
|
|
|
sortBy_splitRec : List a -> List a -> (List a -> List a) -> (List a, List a)
|
|
|
|
sortBy_splitRec (_::_::xs) (y::ys) zs = sortBy_splitRec xs ys (zs . ((::) y))
|
|
|
|
sortBy_splitRec _ ys zs = (zs [], ys)
|
|
|
|
|
|
|
|
sortBy_split : List a -> (List a, List a)
|
|
|
|
sortBy_split xs = sortBy_splitRec xs xs id
|
|
|
|
|
|
|
|
export
|
|
|
|
sortBy : (cmp : a -> a -> Ordering) -> (xs : List a) -> List a
|
|
|
|
sortBy cmp [] = []
|
|
|
|
sortBy cmp [x] = [x]
|
2020-06-16 22:09:31 +03:00
|
|
|
sortBy cmp zs = let (xs, ys) = sortBy_split zs in
|
2022-03-23 16:33:13 +03:00
|
|
|
List.TailRec.mergeBy cmp
|
|
|
|
(List.TailRec.sortBy cmp (assert_smaller zs xs))
|
|
|
|
(List.TailRec.sortBy cmp (assert_smaller zs ys))
|
2020-05-18 15:59:07 +03:00
|
|
|
|
|
|
|
|
2020-06-16 22:09:31 +03:00
|
|
|
{- Can't really finish this proof because Data.List doesn't export the definition of sortBy. -}
|
|
|
|
{-
|
2020-05-18 15:59:07 +03:00
|
|
|
export
|
2020-07-14 14:15:57 +03:00
|
|
|
sortBy_ext : (cmp : a -> a -> Ordering) -> (xs : List a) ->
|
2022-03-23 16:33:13 +03:00
|
|
|
List.sortBy cmp xs = List.TailRec.sortBy cmp xs
|
2020-06-16 22:09:31 +03:00
|
|
|
sortBy_ext cmp [] = Refl
|
|
|
|
sortBy_ext cmp [x] = Refl
|
|
|
|
sortBy_ext cmp zs'@(z::zs) =
|
|
|
|
Calc $
|
2022-03-23 16:33:13 +03:00
|
|
|
|~ List.sortBy cmp (z::zs)
|
2020-06-16 22:09:31 +03:00
|
|
|
~~ (let (xs, ys) = sortBy_split zs' in
|
2022-03-23 16:33:13 +03:00
|
|
|
List.mergeBy cmp
|
|
|
|
(List.sortBy cmp xs)
|
|
|
|
(List.sortBy cmp ys))
|
2020-07-14 14:15:57 +03:00
|
|
|
...( ?help0 )
|
2020-06-16 22:09:31 +03:00
|
|
|
~~
|
|
|
|
let (xs, ys) = sortBy_split (z::zs) in
|
2022-03-23 16:33:13 +03:00
|
|
|
List.TailRec.mergeBy cmp
|
|
|
|
(List.TailRec.sortBy cmp xs)
|
|
|
|
(List.TailRec.sortBy cmp ys)
|
2020-06-16 22:09:31 +03:00
|
|
|
...( ?help1 )
|
|
|
|
-}
|