quox/examples/list.quox
rhiannon morris 3f06e8d68b allow multiple names in a binder
e.g. "(x y : ℕ) × plus x y ≡ 10 : ℕ"

fixes #2
2023-04-19 21:37:51 +02:00

41 lines
1.2 KiB
Text
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.

load "nat.quox";
namespace list {
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;
def nil : 0.(A : ★₀) → List A =
λ A ⇒ (0, 'nil);
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) };
def foldr' : 0.(A B : ★₀) →
1.B → ω.(1.A → 1.B → B) → 1.(n : ) → 1.(Vec n A) → B =
λ A B z c n ⇒
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 =
λ A B z c xs ⇒
case1 xs return B of { (len, elems) ⇒ foldr' A B z c len elems };
def sum : 1.(List ) → = foldr 0 nat.plus;
def numbers : List = (5, (0, 1, 2, 3, 4, 'nil));
def number-sum : sum numbers ≡ 10 : = δ _ ⇒ 10;
}