2023-04-18 18:42:40 -04:00
|
|
|
|
load "nat.quox";
|
|
|
|
|
|
|
|
|
|
namespace list {
|
|
|
|
|
|
2023-03-31 13:31:49 -04:00
|
|
|
|
def0 Vec : 0.ℕ → 0.★₀ → ★₀ =
|
|
|
|
|
λ n A ⇒
|
|
|
|
|
caseω n return ★₀ of {
|
|
|
|
|
zero ⇒ {nil};
|
|
|
|
|
succ _, 0.Tail ⇒ A × Tail
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
def0 List : 0.★₀ → ★₀ =
|
|
|
|
|
λ A ⇒ (len : ℕ) × Vec len A;
|
|
|
|
|
|
2023-04-17 15:44:16 -04:00
|
|
|
|
def nil : 0.(A : ★₀) → List A =
|
2023-03-31 13:31:49 -04:00
|
|
|
|
λ A ⇒ (0, 'nil);
|
|
|
|
|
|
2023-04-17 15:44:16 -04:00
|
|
|
|
def cons : 0.(A : ★₀) → 1.A → 1.(List A) → List A =
|
|
|
|
|
λ A x xs ⇒ case1 xs return List A of { (len, elems) ⇒ (succ len, x, elems) };
|
|
|
|
|
|
2023-04-19 15:36:57 -04:00
|
|
|
|
def foldr' : 0.(A B : ★₀) →
|
2023-04-17 15:44:16 -04:00
|
|
|
|
1.B → ω.(1.A → 1.B → B) → 1.(n : ℕ) → 1.(Vec n A) → B =
|
|
|
|
|
λ A B z c n ⇒
|
2023-04-19 15:36:57 -04:00
|
|
|
|
case1 n return n' ⇒ 1.(Vec n' A) → B of {
|
|
|
|
|
zero ⇒
|
|
|
|
|
λ nil ⇒ case1 nil return B of { 'nil ⇒ z };
|
|
|
|
|
succ n, 1.ih ⇒
|
|
|
|
|
λ cons ⇒ case1 cons return B of { (first, rest) ⇒ c first (ih rest) }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
def foldr : 0.(A B : ★₀) → 1.B → ω.(1.A → 1.B → B) → 1.(List A) → B =
|
2023-04-17 15:44:16 -04:00
|
|
|
|
λ A B z c xs ⇒
|
2023-04-19 15:36:57 -04:00
|
|
|
|
case1 xs return B of { (len, elems) ⇒ foldr' A B z c len elems };
|
2023-03-31 13:31:49 -04:00
|
|
|
|
|
2023-04-18 18:42:40 -04:00
|
|
|
|
def sum : 1.(List ℕ) → ℕ = foldr ℕ ℕ 0 nat.plus;
|
2023-03-31 13:31:49 -04:00
|
|
|
|
|
2023-04-18 18:42:40 -04:00
|
|
|
|
def numbers : List ℕ = (5, (0, 1, 2, 3, 4, 'nil));
|
2023-03-31 13:31:49 -04:00
|
|
|
|
|
2023-04-17 15:44:16 -04:00
|
|
|
|
def number-sum : sum numbers ≡ 10 : ℕ = δ _ ⇒ 10;
|
2023-04-18 18:42:40 -04:00
|
|
|
|
|
|
|
|
|
}
|