1
1
mirror of https://github.com/anoma/juvix.git synced 2025-01-05 22:46:08 +03:00
juvix/tests/Internal/positive/QuickSort.juvix
Łukasz Czajka 43d114f9b1
Adapt Juvix programs to the new pipeline (#1746)
Progress for #1742 

* Remove putStr and putStrLn
* Remove named Nats (one, two, ...)
2023-01-23 14:57:01 +01:00

40 lines
1.1 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

module QuickSort;
open import Stdlib.Prelude;
open import Stdlib.Data.Nat.Ord;
open import Stdlib.Data.Ord;
qsHelper : {A : Type} → A → List A × List A → List A;
qsHelper a (l , r) := l ++ (a :: nil) ++ r;
terminating
quickSort : {A : Type} → (A → A → Ordering) → List A → List A;
quickSort _ nil := nil;
quickSort _ (x :: nil) := x :: nil;
quickSort cmp (x :: xs) := qsHelper x
(both (quickSort cmp) (partition (isGT ∘ cmp x) xs));
uniq : {A : Type} -> (A -> A -> Ordering) -> List A -> List A;
uniq _ nil := nil;
uniq _ y@(x :: nil) := y;
uniq cmp (x :: x' :: xs) := if (isEQ (cmp x x')) (uniq cmp (x' :: xs)) (x :: (uniq cmp (x' :: xs)));
gen : Nat -> (Nat -> Nat) -> List Nat -> List Nat;
gen zero _ acc := acc;
gen n@(suc n') f acc := gen n' f (f n :: acc);
gen2 : Nat -> Nat -> List (List Nat) -> List (List Nat);
gen2 _ zero acc := acc;
gen2 m n@(suc n') acc := gen2 m n' ((gen m ((+) n) nil) :: acc);
three : Nat;
three := suc (suc (suc zero));
four : Nat;
four := suc three;
main : List Nat;
main := uniq compare (quickSort compare (flatten (gen2 three four nil)));
end;