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
|
2015-01-10 20:45:05 +00:00
|
|
|
open inhabited eq.ops
|
2014-11-19 22:37:45 +00:00
|
|
|
|
2015-02-01 16:39:47 +00:00
|
|
|
notation A ⊎ B := sum A B
|
|
|
|
|
2014-09-04 23:36:06 +00:00
|
|
|
namespace sum
|
2015-02-01 19:14:01 +00:00
|
|
|
notation A + B := sum A B
|
2014-11-19 22:37:45 +00:00
|
|
|
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}
|
2014-09-05 05:31:52 +00:00
|
|
|
|
2015-01-10 20:45:05 +00:00
|
|
|
definition inl_ne_inr (a : A) (b : B) : inl a ≠ inr b :=
|
2015-02-11 20:49:27 +00:00
|
|
|
assume H, sum.no_confusion H
|
2014-09-05 05:31:52 +00:00
|
|
|
|
2015-01-10 20:45:05 +00:00
|
|
|
definition inr_ne_inl (b : B) (a : A) : inr b ≠ inl a :=
|
2015-02-11 20:49:27 +00:00
|
|
|
assume H, sum.no_confusion H
|
2015-01-10 20:45:05 +00:00
|
|
|
|
|
|
|
definition inl_inj {a₁ a₂ : A} : intro_left B a₁ = intro_left B a₂ → a₁ = a₂ :=
|
2015-02-11 20:49:27 +00:00
|
|
|
assume H, sum.no_confusion H (λe, e)
|
2014-09-05 05:31:52 +00:00
|
|
|
|
2015-01-10 20:45:05 +00:00
|
|
|
definition inr_inj {b₁ b₂ : B} : intro_right A b₁ = intro_right A b₂ → b₁ = b₂ :=
|
2015-02-11 20:49:27 +00:00
|
|
|
assume H, sum.no_confusion H (λe, e)
|
2014-09-05 05:31:52 +00:00
|
|
|
|
2015-02-24 23:25:02 +00:00
|
|
|
protected definition is_inhabited_left [instance] [h : inhabited A] : inhabited (A + B) :=
|
|
|
|
inhabited.mk (inl (default A))
|
2014-09-05 05:31:52 +00:00
|
|
|
|
2015-02-24 23:25:02 +00:00
|
|
|
protected definition is_inhabited_right [instance] [h : inhabited B] : inhabited (A + B) :=
|
|
|
|
inhabited.mk (inr (default B))
|
2014-09-05 05:31:52 +00:00
|
|
|
|
2015-02-26 00:20:44 +00:00
|
|
|
protected definition has_decidable_eq [instance] [h₁ : decidable_eq A] [h₂ : decidable_eq B] : ∀ s₁ s₂ : A + B, decidable (s₁ = s₂)
|
|
|
|
| has_decidable_eq (inl a₁) (inl a₂) :=
|
2015-01-10 20:45:05 +00:00
|
|
|
match h₁ a₁ a₂ with
|
2015-02-26 00:20:44 +00:00
|
|
|
| decidable.inl hp := decidable.inl (hp ▸ rfl)
|
|
|
|
| decidable.inr hn := decidable.inr (λ he, absurd (inl_inj he) hn)
|
|
|
|
end
|
|
|
|
| has_decidable_eq (inl a₁) (inr b₂) := decidable.inr (λ e, sum.no_confusion e)
|
|
|
|
| has_decidable_eq (inr b₁) (inl a₂) := decidable.inr (λ e, sum.no_confusion e)
|
|
|
|
| has_decidable_eq (inr b₁) (inr b₂) :=
|
2015-01-10 20:45:05 +00:00
|
|
|
match h₂ b₁ b₂ with
|
2015-02-26 00:20:44 +00:00
|
|
|
| decidable.inl hp := decidable.inl (hp ▸ rfl)
|
|
|
|
| decidable.inr hn := decidable.inr (λ he, absurd (inr_inj he) hn)
|
2015-01-10 20:45:05 +00:00
|
|
|
end
|
2014-08-28 01:39:55 +00:00
|
|
|
end sum
|