quox/examples/either.quox

75 lines
2 KiB
Text
Raw Normal View History

2023-04-18 18:42:40 -04:00
load "misc.quox";
load "bool.quox";
namespace either {
2023-05-21 14:33:42 -04:00
def0 Tag : ★ = {left, right};
2023-03-31 13:31:49 -04:00
def0 Payload : ★ → ★ → Tag → ★ =
λ A B tag ⇒ case tag return ★ of { 'left ⇒ A; 'right ⇒ B };
2023-03-31 13:31:49 -04:00
def0 Either : ★ → ★ → ★ =
2023-03-31 13:31:49 -04:00
λ A B ⇒ (tag : Tag) × Payload A B tag;
def Left : 0.(A B : ★) → A → Either A B =
2023-03-31 13:31:49 -04:00
λ A B x ⇒ ('left, x);
def Right : 0.(A B : ★) → B → Either A B =
2023-03-31 13:31:49 -04:00
λ A B x ⇒ ('right, x);
2023-04-18 18:42:40 -04:00
def elim' :
2023-05-21 14:33:42 -04:00
0.(A B : ★) → 0.(P : 0.(Either A B) → ★) →
ω.((x : A) → P (Left A B x)) →
ω.((x : B) → P (Right A B x)) →
(t : Tag) → (a : Payload A B t) → P (t, a) =
2023-04-18 18:42:40 -04:00
λ A B P f g t ⇒
case t
return t' ⇒ (a : Payload A B t') → P (t', a)
2023-04-18 18:42:40 -04:00
of { 'left ⇒ f; 'right ⇒ g };
def elim :
2023-05-21 14:33:42 -04:00
0.(A B : ★) → 0.(P : 0.(Either A B) → ★) →
ω.((x : A) → P (Left A B x)) →
ω.((x : B) → P (Right A B x)) →
(x : Either A B) → P x =
2023-04-18 18:42:40 -04:00
λ A B P f g e ⇒
case e return e' ⇒ P e' of { (t, a) ⇒ elim' A B P f g t a };
2023-04-18 18:42:40 -04:00
}
def0 Either = either.Either;
def Left = either.Left;
def Right = either.Right;
namespace dec {
def0 Dec : ★ → ★ = λ A ⇒ Either [0.A] [0.Not A];
2023-04-18 18:42:40 -04:00
2023-05-21 14:33:42 -04:00
def Yes : 0.(A : ★) → 0.A → Dec A = λ A y ⇒ Left [0.A] [0.Not A] [y];
def No : 0.(A : ★) → 0.(Not A) → Dec A = λ A n ⇒ Right [0.A] [0.Not A] [n];
2023-04-18 18:42:40 -04:00
def0 DecEq : ★ → ★ =
2023-04-18 18:42:40 -04:00
λ A ⇒ ω.(x : A) → ω.(y : A) → Dec (x ≡ y : A);
def elim :
2023-05-21 14:33:42 -04:00
0.(A : ★) → 0.(P : 0.(Dec A) → ★) →
2023-04-18 18:42:40 -04:00
ω.(0.(y : A) → P (Yes A y)) →
ω.(0.(n : Not A) → P (No A n)) →
(x : Dec A) → P x =
2023-04-18 18:42:40 -04:00
λ A P f g ⇒
either.elim [0.A] [0.Not A] P
(λ y ⇒ case0 y return y' ⇒ P (Left [0.A] [0.Not A] y') of {[y'] ⇒ f y'})
(λ n ⇒ case0 n return n' ⇒ P (Right [0.A] [0.Not A] n') of {[n'] ⇒ g n'});
def bool : 0.(A : ★) → Dec A → Bool =
2023-04-18 18:42:40 -04:00
λ A ⇒ elim A (λ _ ⇒ Bool) (λ _ ⇒ 'true) (λ _ ⇒ 'false);
}
def0 Dec = dec.Dec;
def0 DecEq = dec.DecEq;
def Yes = dec.Yes;
def No = dec.No;