2014-11-28 12:06:46 +00:00
|
|
|
/-
|
|
|
|
Copyright (c) 2014 Microsoft Corporation. All rights reserved.
|
|
|
|
Released under Apache 2.0 license as described in the file LICENSE.
|
|
|
|
|
|
|
|
Module: data.sum
|
|
|
|
Authors: Leonardo de Moura, Jeremy Avigad
|
|
|
|
|
|
|
|
The sum type, aka disjoint union.
|
|
|
|
-/
|
2014-12-12 21:20:27 +00:00
|
|
|
import logic.connectives
|
2014-10-02 00:51:17 +00:00
|
|
|
open inhabited decidable eq.ops
|
2014-11-19 22:37:45 +00:00
|
|
|
|
2014-09-04 23:36:06 +00:00
|
|
|
namespace sum
|
2014-10-21 21:08:07 +00:00
|
|
|
notation A ⊎ B := sum A B
|
2014-11-19 22:37:45 +00:00
|
|
|
notation A + B := sum A B
|
|
|
|
namespace low_precedence_plus
|
2014-11-28 12:06:46 +00:00
|
|
|
reserve infixr `+`:25 -- conflicts with notation for addition
|
2014-10-21 22:27:45 +00:00
|
|
|
infixr `+` := sum
|
2014-11-19 22:37:45 +00:00
|
|
|
end low_precedence_plus
|
2014-09-05 05:31:52 +00:00
|
|
|
|
2014-10-05 20:20:04 +00:00
|
|
|
variables {A B : Type}
|
|
|
|
variables {a a₁ a₂ : A} {b b₁ b₂ : B}
|
2014-09-05 05:31:52 +00:00
|
|
|
|
2014-11-09 02:58:56 +00:00
|
|
|
theorem inl_neq_inr : inl B a ≠ inr A b :=
|
|
|
|
assume H, no_confusion H
|
2014-09-05 05:31:52 +00:00
|
|
|
|
2014-10-05 20:20:04 +00:00
|
|
|
theorem inl_inj : inl B a₁ = inl B a₂ → a₁ = a₂ :=
|
2014-11-09 02:58:56 +00:00
|
|
|
assume H, no_confusion H (λe, e)
|
2014-09-05 05:31:52 +00:00
|
|
|
|
2014-10-05 20:20:04 +00:00
|
|
|
theorem inr_inj : inr A b₁ = inr A b₂ → b₁ = b₂ :=
|
2014-11-09 02:58:56 +00:00
|
|
|
assume H, no_confusion H (λe, e)
|
2014-09-05 05:31:52 +00:00
|
|
|
|
2014-11-28 12:06:46 +00:00
|
|
|
protected definition is_inhabited_left [instance] : inhabited A → inhabited (A + B) :=
|
2014-10-05 20:20:04 +00:00
|
|
|
assume H : inhabited A, inhabited.mk (inl B (default A))
|
2014-09-05 05:31:52 +00:00
|
|
|
|
2014-11-28 12:06:46 +00:00
|
|
|
protected definition is_inhabited_right [instance] : inhabited B → inhabited (A + B) :=
|
2014-10-05 20:20:04 +00:00
|
|
|
assume H : inhabited B, inhabited.mk (inr A (default B))
|
2014-09-05 05:31:52 +00:00
|
|
|
|
2014-11-28 12:06:46 +00:00
|
|
|
protected definition has_eq_decidable [instance] :
|
|
|
|
decidable_eq A → decidable_eq B → decidable_eq (A + B) :=
|
2014-10-05 20:20:04 +00:00
|
|
|
assume (H₁ : decidable_eq A) (H₂ : decidable_eq B),
|
2014-11-28 12:06:46 +00:00
|
|
|
take s₁ s₂ : A + B,
|
2014-10-05 20:20:04 +00:00
|
|
|
rec_on s₁
|
|
|
|
(take a₁, show decidable (inl B a₁ = s₂), from
|
|
|
|
rec_on s₂
|
|
|
|
(take a₂, show decidable (inl B a₁ = inl B a₂), from
|
|
|
|
decidable.rec_on (H₁ a₁ a₂)
|
|
|
|
(assume Heq : a₁ = a₂, decidable.inl (Heq ▸ rfl))
|
|
|
|
(assume Hne : a₁ ≠ a₂, decidable.inr (mt inl_inj Hne)))
|
|
|
|
(take b₂,
|
|
|
|
have H₃ : (inl B a₁ = inr A b₂) ↔ false,
|
2014-12-12 21:20:27 +00:00
|
|
|
from iff.intro inl_neq_inr (assume H₄, !false.rec H₄),
|
2014-10-05 20:20:04 +00:00
|
|
|
show decidable (inl B a₁ = inr A b₂), from decidable_iff_equiv _ (iff.symm H₃)))
|
|
|
|
(take b₁, show decidable (inr A b₁ = s₂), from
|
|
|
|
rec_on s₂
|
|
|
|
(take a₂,
|
|
|
|
have H₃ : (inr A b₁ = inl B a₂) ↔ false,
|
2014-12-12 21:20:27 +00:00
|
|
|
from iff.intro (assume H₄, inl_neq_inr (H₄⁻¹)) (assume H₄, !false.rec H₄),
|
2014-10-05 20:20:04 +00:00
|
|
|
show decidable (inr A b₁ = inl B a₂), from decidable_iff_equiv _ (iff.symm H₃))
|
|
|
|
(take b₂, show decidable (inr A b₁ = inr A b₂), from
|
|
|
|
decidable.rec_on (H₂ b₁ b₂)
|
|
|
|
(assume Heq : b₁ = b₂, decidable.inl (Heq ▸ rfl))
|
|
|
|
(assume Hne : b₁ ≠ b₂, decidable.inr (mt inr_inj Hne))))
|
2014-08-28 01:39:55 +00:00
|
|
|
end sum
|