|
| 1 | +// Co-Cartesian product (disjoint sum) |
| 2 | + |
| 3 | +require open Stdlib.FOL Stdlib.Eq; |
| 4 | + |
| 5 | +(a: Set) (b: Set) inductive Sum: TYPE ≔ |
| 6 | +| inl: τ a → Sum a b |
| 7 | +| inr: τ b → Sum a b; |
| 8 | + |
| 9 | +constant symbol sum : Set → Set → Set; |
| 10 | + |
| 11 | +rule τ (sum $a $b) ↪ Sum $a $b; |
| 12 | + |
| 13 | +// Universal property: a map out of a sum is given by case analysis |
| 14 | + |
| 15 | +symbol sum_case [a b c] : τ (sum a b) → (τ a → τ c) → (τ b → τ c) → τ c; |
| 16 | + |
| 17 | +rule sum_case (inl $x) $f _ ↪ $f $x |
| 18 | +with sum_case (inr $y) _ $g ↪ $g $y; |
| 19 | + |
| 20 | +opaque symbol sum_case_uniq [a b c] (h: τ (sum a b) → τ c) (s: τ (sum a b)) : |
| 21 | + π (sum_case s (λ x, h (inl x)) (λ y, h (inr y)) = h s) ≔ |
| 22 | +begin |
| 23 | + assume a b c h; induction { reflexivity } { reflexivity } |
| 24 | +end; |
| 25 | + |
| 26 | +opaque symbol η_sum [a b] (s: τ (sum a b)) : |
| 27 | + π (sum_case s (λ x, inl x) (λ y, inr y) = s) ≔ |
| 28 | +begin |
| 29 | + assume a b; induction { reflexivity } { reflexivity } |
| 30 | +end; |
| 31 | + |
| 32 | +// non confusion of constructors |
| 33 | + |
| 34 | +symbol is_inl [a b] : τ (sum a b) → Prop; |
| 35 | + |
| 36 | +rule is_inl (inl _) ↪ ⊤ |
| 37 | +with is_inl (inr _) ↪ ⊥; |
| 38 | + |
| 39 | +opaque symbol inr≠inl [a b] (x: τ a) (y: τ b) : π (@inr a b y ≠ inl x) ≔ |
| 40 | +begin assume a b x y h; refine ind_eq h (λ s, is_inl s) ⊤ᵢ end; |
| 41 | + |
| 42 | +opaque symbol inl≠inr [a b] (x: τ a) (y: τ b) : π (@inl a b x ≠ inr y) ≔ |
| 43 | +begin assume a b x y h; apply inr≠inl x y; symmetry; apply h end; |
| 44 | + |
| 45 | +symbol outl [a b] (s: τ (sum a b)) : τ a ≔ sum_case s (λ x, x) (λ _, el a); |
| 46 | +symbol outr [a b] (s: τ (sum a b)) : τ b ≔ sum_case s (λ _, el b) (λ y, y); |
| 47 | + |
| 48 | +opaque symbol inl_inj [a b] (x x': τ a) : π (@inl a b x = inl x') → π (x = x') ≔ |
| 49 | +begin assume a b x x' h; refine feq (λ s, @outl a b s) h end; |
| 50 | + |
| 51 | +opaque symbol inr_inj [a b] (y y': τ b) : π (@inr a b y = inr y') → π (y = y') ≔ |
| 52 | +begin assume a b y y' h; refine feq (λ s, @outr a b s) h end; |
| 53 | + |
| 54 | +// induction principle with equalities |
| 55 | + |
| 56 | +opaque symbol case_Sum [a b] (s: τ (sum a b)) : |
| 57 | + π ((`∃ x, s = inl x) ∨ (`∃ y, s = inr y)) ≔ |
| 58 | +begin |
| 59 | + assume a b; induction |
| 60 | + { assume x; apply ∨ᵢ₁; apply ∃ᵢ x; reflexivity } |
| 61 | + { assume y; apply ∨ᵢ₂; apply ∃ᵢ y; reflexivity } |
| 62 | +end; |
| 63 | + |
| 64 | +opaque symbol ind_Sum_eq [a b] (p: τ (sum a b) → Prop) (s: τ (sum a b)) : |
| 65 | + (Π x, π (s = inl x) → π (p s)) → (Π y, π (s = inr y) → π (p s)) → π (p s) ≔ |
| 66 | +begin |
| 67 | + assume a b p s hl hr; refine ∨ₑ (case_Sum s) (λ h, ∃ₑ h hl) (λ h, ∃ₑ h hr) |
| 68 | +end; |
| 69 | + |
| 70 | +symbol map_sum [a b c d] (f: τ a → τ c) (g: τ b → τ d) (s: τ (sum a b)) |
| 71 | + : τ (sum c d) ≔ sum_case s (λ x, inl (f x)) (λ y, inr (g y)); |
| 72 | + |
| 73 | +opaque symbol map_sum_id [a b] (s: τ (sum a b)) : |
| 74 | + π (map_sum (λ u, u) (λ v, v) s = s) ≔ |
| 75 | +begin assume a b s; refine η_sum s end; |
| 76 | + |
| 77 | +opaque symbol map_sum_comp [a b c d e f] |
| 78 | + (g: τ c → τ e) (g': τ d → τ f) (h: τ a → τ c) (h': τ b → τ d) (s: τ (sum a b)) : |
| 79 | + π (map_sum g g' (map_sum h h' s) = map_sum (λ u, g (h u)) (λ v, g' (h' v)) s) ≔ |
| 80 | +begin |
| 81 | + assume a b c d e f g g' h h'; |
| 82 | + induction { reflexivity } { reflexivity } |
| 83 | +end; |
| 84 | + |
| 85 | +symbol swap_sum [a b] (s: τ (sum a b)) : τ (sum b a) ≔ |
| 86 | + sum_case s (λ x, inr x) (λ y, inl y); |
| 87 | + |
| 88 | +opaque symbol swap_sum_invol [a b] (s: τ (sum a b)) : |
| 89 | + π (swap_sum (swap_sum s) = s) ≔ |
| 90 | +begin |
| 91 | + assume a b; induction { reflexivity } { reflexivity } |
| 92 | +end; |
| 93 | + |
| 94 | +// currying |
| 95 | + |
| 96 | +symbol sum_curry₁ [a b c] (h: τ (sum a b) → τ c) (x: τ a) : τ c ≔ h (inl x); |
| 97 | +symbol sum_curry₂ [a b c] (h: τ (sum a b) → τ c) (y: τ b) : τ c ≔ h (inr y); |
| 98 | + |
| 99 | +symbol sum_uncurry [a b c] (f: τ a → τ c) (g: τ b → τ c) (s: τ (sum a b)) : τ c ≔ |
| 100 | + sum_case s f g; |
| 101 | + |
| 102 | +opaque symbol sum_uncurry_curry [a b c] (h: τ (sum a b) → τ c) (s: τ (sum a b)) : |
| 103 | + π (sum_uncurry (sum_curry₁ h) (sum_curry₂ h) s = h s) ≔ |
| 104 | +begin |
| 105 | + assume a b c h; induction { reflexivity } { reflexivity } |
| 106 | +end; |
| 107 | + |
| 108 | +opaque symbol sum_curry_uncurry₁ [a b c] (f: τ a → τ c) (g: τ b → τ c) (x: τ a) : |
| 109 | + π (sum_curry₁ (sum_uncurry f g) x = f x) ≔ |
| 110 | +begin reflexivity end; |
| 111 | + |
| 112 | +opaque symbol sum_curry_uncurry₂ [a b c] (f: τ a → τ c) (g: τ b → τ c) (y: τ b) : |
| 113 | + π (sum_curry₂ (sum_uncurry f g) y = g y) ≔ |
| 114 | +begin reflexivity end; |
| 115 | + |
| 116 | +// Relation between the product and the coproduct |
| 117 | + |
| 118 | +require open Stdlib.Prod; |
| 119 | + |
| 120 | +symbol dist_× [a b c] (x: τ (a × sum b c)) : τ (sum (a × b) (a × c)) ≔ |
| 121 | + sum_case (x ₂) (λ y, inl ((x ₁) ‚ y)) (λ z, inr ((x ₁) ‚ z)); |
| 122 | + |
| 123 | +symbol undist_× [a b c] (s: τ (sum (a × b) (a × c))) : τ (a × sum b c) ≔ |
| 124 | + sum_case s (λ u, (u ₁) ‚ inl (u ₂)) (λ v, (v ₁) ‚ inr (v ₂)); |
| 125 | + |
| 126 | +opaque symbol undist_dist_‚ [a b c] (u: τ a) (s: τ (sum b c)) : |
| 127 | + π (undist_× (dist_× (u ‚ s)) = u ‚ s) ≔ |
| 128 | +begin |
| 129 | + assume a b c u; induction { reflexivity } { reflexivity } |
| 130 | +end; |
| 131 | + |
| 132 | +opaque symbol undist_dist_× [a b c] (x: τ (a × sum b c)) : |
| 133 | + π (undist_× (dist_× x) = x) ≔ |
| 134 | +begin |
| 135 | + assume a b c x; rewrite prodExt x; refine undist_dist_‚ (x ₁) (x ₂) |
| 136 | +end; |
| 137 | + |
| 138 | +opaque symbol dist_undist_× [a b c] (s: τ (sum (a × b) (a × c))) : |
| 139 | + π (dist_× (undist_× s) = s) ≔ |
| 140 | +begin |
| 141 | + assume a b c; induction |
| 142 | + { assume u; symmetry; refine feq inl (prodExt u) } |
| 143 | + { assume v; symmetry; refine feq inr (prodExt v) } |
| 144 | +end; |
0 commit comments