2014-12-05 23:47:04 +00:00
/-
Copyright (c) 2014 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
2015-02-21 00:30:32 +00:00
Module: init.logic
2014-12-05 23:47:04 +00:00
Authors: Leonardo de Moura
-/
2015-02-26 18:19:54 +00:00
2014-12-05 23:47:04 +00:00
prelude
import init.datatypes init.reserved_notation
2014-12-20 04:50:53 +00:00
definition not.{l} (a : Type.{l}) := a → empty.{l}
2014-12-05 23:47:04 +00:00
prefix `¬` := not
definition absurd {a : Type} {b : Type} (H₁ : a) (H₂ : ¬a) : b :=
empty.rec (λ e, b) (H₂ H₁)
2014-12-08 20:12:19 +00:00
definition mt {a b : Type} (H₁ : a → b) (H₂ : ¬b) : ¬a :=
2014-12-05 23:47:04 +00:00
assume Ha : a, absurd (H₁ Ha) H₂
2015-02-21 00:30:32 +00:00
/- not -/
2014-12-05 23:47:04 +00:00
2014-12-16 19:22:54 +00:00
protected definition not_empty : ¬ empty :=
2014-12-05 23:47:04 +00:00
assume H : empty, H
2014-12-08 20:12:19 +00:00
definition not_not_intro {a : Type} (Ha : a) : ¬¬a :=
2014-12-05 23:47:04 +00:00
assume Hna : ¬a, absurd Ha Hna
2014-12-16 19:22:54 +00:00
definition not.intro {a : Type} (H : a → empty) : ¬a := H
2014-12-05 23:47:04 +00:00
2014-12-16 19:22:54 +00:00
definition not.elim {a : Type} (H₁ : ¬a) (H₂ : a) : empty := H₁ H₂
2014-12-05 23:47:04 +00:00
2014-12-16 19:22:54 +00:00
definition not_not_of_not_implies {a b : Type} (H : ¬(a → b)) : ¬¬a :=
2014-12-05 23:47:04 +00:00
assume Hna : ¬a, absurd (assume Ha : a, absurd Ha Hna) H
2014-12-16 19:22:54 +00:00
definition not_of_not_implies {a b : Type} (H : ¬(a → b)) : ¬b :=
2014-12-05 23:47:04 +00:00
assume Hb : b, absurd (assume Ha : a, Hb) H
2015-02-21 00:30:32 +00:00
/- eq -/
2014-12-05 23:47:04 +00:00
notation a = b := eq a b
definition rfl {A : Type} {a : A} := eq.refl a
namespace eq
variables {A : Type}
2014-12-16 19:22:54 +00:00
variables {a b c a' : A}
2014-12-05 23:47:04 +00:00
2014-12-08 20:12:19 +00:00
definition subst {P : A → Type} (H₁ : a = b) (H₂ : P a) : P b :=
2015-02-11 20:49:27 +00:00
eq.rec H₂ H₁
2014-12-05 23:47:04 +00:00
2014-12-08 20:12:19 +00:00
definition trans (H₁ : a = b) (H₂ : b = c) : a = c :=
2014-12-05 23:47:04 +00:00
subst H₂ H₁
definition symm (H : a = b) : b = a :=
subst H (refl a)
namespace ops
notation H `⁻¹` := symm H --input with \sy or \-1 or \inv
notation H1 ⬝ H2 := trans H1 H2
notation H1 ▸ H2 := subst H1 H2
end ops
end eq
calc_subst eq.subst
calc_refl eq.refl
calc_trans eq.trans
calc_symm eq.symm
2014-12-08 20:09:41 +00:00
namespace lift
definition down_up.{l₁ l₂} {A : Type.{l₁}} (a : A) : down (up.{l₁ l₂} a) = a :=
rfl
definition up_down.{l₁ l₂} {A : Type.{l₁}} (a : lift.{l₁ l₂} A) : up (down a) = a :=
lift.rec_on a (λ d, rfl)
end lift
2015-02-21 00:30:32 +00:00
/- ne -/
2014-12-05 23:47:04 +00:00
definition ne {A : Type} (a b : A) := ¬(a = b)
notation a ≠ b := ne a b
namespace ne
open eq.ops
variable {A : Type}
variables {a b : A}
2014-12-08 20:12:19 +00:00
definition intro : (a = b → empty) → a ≠ b :=
2014-12-05 23:47:04 +00:00
assume H, H
2014-12-08 20:12:19 +00:00
definition elim : a ≠ b → a = b → empty :=
2014-12-05 23:47:04 +00:00
assume H₁ H₂, H₁ H₂
2014-12-08 20:12:19 +00:00
definition irrefl : a ≠ a → empty :=
2014-12-05 23:47:04 +00:00
assume H, H rfl
2014-12-08 20:12:19 +00:00
definition symm : a ≠ b → b ≠ a :=
2014-12-05 23:47:04 +00:00
assume (H : a ≠ b) (H₁ : b = a), H (H₁⁻¹)
end ne
section
open eq.ops
variables {A : Type} {a b c : A}
2014-12-08 20:12:19 +00:00
definition false.of_ne : a ≠ a → empty :=
2014-12-05 23:47:04 +00:00
assume H, H rfl
2014-12-08 20:12:19 +00:00
definition ne.of_eq_of_ne : a = b → b ≠ c → a ≠ c :=
2014-12-05 23:47:04 +00:00
assume H₁ H₂, H₁⁻¹ ▸ H₂
2014-12-08 20:12:19 +00:00
definition ne.of_ne_of_eq : a ≠ b → b = c → a ≠ c :=
2014-12-05 23:47:04 +00:00
assume H₁ H₂, H₂ ▸ H₁
end
calc_trans ne.of_eq_of_ne
calc_trans ne.of_ne_of_eq
2015-02-21 00:30:32 +00:00
/- iff -/
2014-12-05 23:47:04 +00:00
definition iff (a b : Type) := prod (a → b) (b → a)
notation a <-> b := iff a b
notation a ↔ b := iff a b
namespace iff
variables {a b c : Type}
definition def : (a ↔ b) = (prod (a → b) (b → a)) :=
rfl
definition intro (H₁ : a → b) (H₂ : b → a) : a ↔ b :=
prod.mk H₁ H₂
definition elim (H₁ : (a → b) → (b → a) → c) (H₂ : a ↔ b) : c :=
prod.rec H₁ H₂
definition elim_left (H : a ↔ b) : a → b :=
elim (assume H₁ H₂, H₁) H
definition mp := @elim_left
definition elim_right (H : a ↔ b) : b → a :=
elim (assume H₁ H₂, H₂) H
definition flip_sign (H₁ : a ↔ b) : ¬a ↔ ¬b :=
intro
(assume Hna, mt (elim_right H₁) Hna)
(assume Hnb, mt (elim_left H₁) Hnb)
definition refl (a : Type) : a ↔ a :=
intro (assume H, H) (assume H, H)
definition rfl {a : Type} : a ↔ a :=
refl a
2014-12-08 20:12:19 +00:00
definition trans (H₁ : a ↔ b) (H₂ : b ↔ c) : a ↔ c :=
2014-12-05 23:47:04 +00:00
intro
(assume Ha, elim_left H₂ (elim_left H₁ Ha))
(assume Hc, elim_right H₁ (elim_right H₂ Hc))
2014-12-08 20:12:19 +00:00
definition symm (H : a ↔ b) : b ↔ a :=
2014-12-05 23:47:04 +00:00
intro
(assume Hb, elim_right H Hb)
(assume Ha, elim_left H Ha)
2014-12-08 20:12:19 +00:00
definition true_elim (H : a ↔ unit) : a :=
2014-12-05 23:47:04 +00:00
mp (symm H) unit.star
2014-12-08 20:12:19 +00:00
definition false_elim (H : a ↔ empty) : ¬a :=
2014-12-05 23:47:04 +00:00
assume Ha : a, mp H Ha
open eq.ops
2014-12-08 20:12:19 +00:00
definition of_eq {a b : Type} (H : a = b) : a ↔ b :=
2014-12-05 23:47:04 +00:00
iff.intro (λ Ha, H ▸ Ha) (λ Hb, H⁻¹ ▸ Hb)
end iff
calc_refl iff.refl
calc_trans iff.trans
2015-02-21 00:30:32 +00:00
/- inhabited -/
2014-12-05 23:47:04 +00:00
inductive inhabited [class] (A : Type) : Type :=
mk : A → inhabited A
namespace inhabited
protected definition destruct {A : Type} {B : Type} (H1 : inhabited A) (H2 : A → B) : B :=
inhabited.rec H2 H1
2015-02-25 20:34:49 +00:00
definition inhabited_fun [instance] (A : Type) {B : Type} [H : inhabited B] : inhabited (A → B) :=
2014-12-05 23:47:04 +00:00
destruct H (λb, mk (λa, b))
2015-02-25 20:34:49 +00:00
definition inhabited_Pi [instance] (A : Type) {B : A → Type} [H : Πx, inhabited (B x)] :
2014-12-05 23:47:04 +00:00
inhabited (Πx, B x) :=
mk (λa, destruct (H a) (λb, b))
definition default (A : Type) [H : inhabited A] : A := destruct H (take a, a)
end inhabited
2015-02-21 00:30:32 +00:00
/- decidable -/
2014-12-05 23:47:04 +00:00
2014-12-20 04:50:53 +00:00
inductive decidable.{l} [class] (p : Type.{l}) : Type.{l} :=
2015-02-26 01:00:10 +00:00
| inl : p → decidable p
| inr : ¬p → decidable p
2014-12-05 23:47:04 +00:00
namespace decidable
variables {p q : Type}
definition pos_witness [C : decidable p] (H : p) : p :=
2015-02-11 20:49:27 +00:00
decidable.rec_on C (λ Hp, Hp) (λ Hnp, absurd H Hnp)
2014-12-05 23:47:04 +00:00
definition neg_witness [C : decidable p] (H : ¬ p) : ¬ p :=
2015-02-11 20:49:27 +00:00
decidable.rec_on C (λ Hp, absurd Hp H) (λ Hnp, Hnp)
2014-12-05 23:47:04 +00:00
definition by_cases {q : Type} [C : decidable p] (Hpq : p → q) (Hnpq : ¬p → q) : q :=
2015-02-11 20:49:27 +00:00
decidable.rec_on C (assume Hp, Hpq Hp) (assume Hnp, Hnpq Hnp)
2014-12-05 23:47:04 +00:00
2014-12-08 20:12:19 +00:00
definition em (p : Type) [H : decidable p] : sum p ¬p :=
2014-12-05 23:47:04 +00:00
by_cases (λ Hp, sum.inl Hp) (λ Hnp, sum.inr Hnp)
2014-12-08 20:12:19 +00:00
definition by_contradiction [Hp : decidable p] (H : ¬p → empty) : p :=
2014-12-05 23:47:04 +00:00
by_cases
(assume H₁ : p, H₁)
(assume H₁ : ¬p, empty.rec (λ e, p) (H H₁))
definition decidable_iff_equiv (Hp : decidable p) (H : p ↔ q) : decidable q :=
2015-02-11 20:49:27 +00:00
decidable.rec_on Hp
2014-12-05 23:47:04 +00:00
(assume Hp : p, inl (iff.elim_left H Hp))
(assume Hnp : ¬p, inr (iff.elim_left (iff.flip_sign H) Hnp))
definition decidable_eq_equiv.{l} {p q : Type.{l}} (Hp : decidable p) (H : p = q) : decidable q :=
decidable_iff_equiv Hp (iff.of_eq H)
end decidable
section
variables {p q : Type}
open decidable (rec_on inl inr)
2015-02-25 20:34:49 +00:00
definition decidable_unit [instance] : decidable unit :=
2014-12-05 23:47:04 +00:00
inl unit.star
2015-02-25 20:34:49 +00:00
definition decidable_empty [instance] : decidable empty :=
2014-12-05 23:47:04 +00:00
inr not_empty
2015-02-25 20:34:49 +00:00
definition decidable_prod [instance] [Hp : decidable p] [Hq : decidable q] : decidable (prod p q) :=
2014-12-05 23:47:04 +00:00
rec_on Hp
(assume Hp : p, rec_on Hq
(assume Hq : q, inl (prod.mk Hp Hq))
(assume Hnq : ¬q, inr (λ H : prod p q, prod.rec_on H (λ Hp Hq, absurd Hq Hnq))))
(assume Hnp : ¬p, inr (λ H : prod p q, prod.rec_on H (λ Hp Hq, absurd Hp Hnp)))
2015-02-25 20:34:49 +00:00
definition decidable_sum [instance] [Hp : decidable p] [Hq : decidable q] : decidable (sum p q) :=
2014-12-05 23:47:04 +00:00
rec_on Hp
(assume Hp : p, inl (sum.inl Hp))
(assume Hnp : ¬p, rec_on Hq
(assume Hq : q, inl (sum.inr Hq))
(assume Hnq : ¬q, inr (λ H : sum p q, sum.rec_on H (λ Hp, absurd Hp Hnp) (λ Hq, absurd Hq Hnq))))
2015-02-25 20:34:49 +00:00
definition decidable_not [instance] [Hp : decidable p] : decidable (¬p) :=
2014-12-05 23:47:04 +00:00
rec_on Hp
(assume Hp, inr (not_not_intro Hp))
(assume Hnp, inl Hnp)
2015-02-25 20:34:49 +00:00
definition decidable_implies [instance] [Hp : decidable p] [Hq : decidable q] : decidable (p → q) :=
2014-12-05 23:47:04 +00:00
rec_on Hp
(assume Hp : p, rec_on Hq
(assume Hq : q, inl (assume H, Hq))
(assume Hnq : ¬q, inr (assume H : p → q, absurd (H Hp) Hnq)))
(assume Hnp : ¬p, inl (assume Hp, absurd Hp Hnp))
2015-02-25 20:34:49 +00:00
definition decidable_if [instance] [Hp : decidable p] [Hq : decidable q] : decidable (p ↔ q) :=
2015-02-24 22:09:20 +00:00
show decidable (prod (p → q) (q → p)), from _
2014-12-05 23:47:04 +00:00
end
2015-02-24 22:09:20 +00:00
definition decidable_pred [reducible] {A : Type} (R : A → Type) := Π (a : A), decidable (R a)
definition decidable_rel [reducible] {A : Type} (R : A → A → Type) := Π (a b : A), decidable (R a b)
definition decidable_eq [reducible] (A : Type) := decidable_rel (@eq A)
2015-02-24 23:25:02 +00:00
definition decidable_ne [instance] {A : Type} [H : decidable_eq A] : decidable_rel (@ne A) :=
2015-02-24 22:09:20 +00:00
show ∀ x y : A, decidable (x = y → empty), from _
2014-12-05 23:47:04 +00:00
definition ite (c : Type) [H : decidable c] {A : Type} (t e : A) : A :=
decidable.rec_on H (λ Hc, t) (λ Hnc, e)
definition if_pos {c : Type} [H : decidable c] (Hc : c) {A : Type} {t e : A} : (if c then t else e) = t :=
decidable.rec
(λ Hc : c, eq.refl (@ite c (decidable.inl Hc) A t e))
(λ Hnc : ¬c, absurd Hc Hnc)
H
definition if_neg {c : Type} [H : decidable c] (Hnc : ¬c) {A : Type} {t e : A} : (if c then t else e) = e :=
decidable.rec
(λ Hc : c, absurd Hc Hnc)
(λ Hnc : ¬c, eq.refl (@ite c (decidable.inr Hnc) A t e))
H
definition if_t_t (c : Type) [H : decidable c] {A : Type} (t : A) : (if c then t else t) = t :=
decidable.rec
(λ Hc : c, eq.refl (@ite c (decidable.inl Hc) A t t))
(λ Hnc : ¬c, eq.refl (@ite c (decidable.inr Hnc) A t t))
H
definition if_unit {A : Type} (t e : A) : (if unit then t else e) = t :=
if_pos unit.star
definition if_empty {A : Type} (t e : A) : (if empty then t else e) = e :=
if_neg not_empty
2014-12-08 20:12:19 +00:00
definition if_cond_congr {c₁ c₂ : Type} [H₁ : decidable c₁] [H₂ : decidable c₂] (Heq : c₁ ↔ c₂) {A : Type} (t e : A)
2014-12-05 23:47:04 +00:00
: (if c₁ then t else e) = (if c₂ then t else e) :=
decidable.rec_on H₁
(λ Hc₁ : c₁, decidable.rec_on H₂
(λ Hc₂ : c₂, if_pos Hc₁ ⬝ (if_pos Hc₂)⁻¹)
(λ Hnc₂ : ¬c₂, absurd (iff.elim_left Heq Hc₁) Hnc₂))
(λ Hnc₁ : ¬c₁, decidable.rec_on H₂
(λ Hc₂ : c₂, absurd (iff.elim_right Heq Hc₂) Hnc₁)
(λ Hnc₂ : ¬c₂, if_neg Hnc₁ ⬝ (if_neg Hnc₂)⁻¹))
2014-12-08 20:12:19 +00:00
definition if_congr_aux {c₁ c₂ : Type} [H₁ : decidable c₁] [H₂ : decidable c₂] {A : Type} {t₁ t₂ e₁ e₂ : A}
2014-12-05 23:47:04 +00:00
(Hc : c₁ ↔ c₂) (Ht : t₁ = t₂) (He : e₁ = e₂) :
(if c₁ then t₁ else e₁) = (if c₂ then t₂ else e₂) :=
Ht ▸ He ▸ (if_cond_congr Hc t₁ e₁)
2014-12-08 20:12:19 +00:00
definition if_congr {c₁ c₂ : Type} [H₁ : decidable c₁] {A : Type} {t₁ t₂ e₁ e₂ : A} (Hc : c₁ ↔ c₂) (Ht : t₁ = t₂) (He : e₁ = e₂) :
2014-12-05 23:47:04 +00:00
(if c₁ then t₁ else e₁) = (@ite c₂ (decidable.decidable_iff_equiv H₁ Hc) A t₂ e₂) :=
have H2 [visible] : decidable c₂, from (decidable.decidable_iff_equiv H₁ Hc),
if_congr_aux Hc Ht He
-- We use "dependent" if-then-else to be able to communicate the if-then-else condition
-- to the branches
definition dite (c : Type) [H : decidable c] {A : Type} (t : c → A) (e : ¬ c → A) : A :=
decidable.rec_on H (λ Hc, t Hc) (λ Hnc, e Hnc)
definition dif_pos {c : Type} [H : decidable c] (Hc : c) {A : Type} {t : c → A} {e : ¬ c → A} : (if H : c then t H else e H) = t (decidable.pos_witness Hc) :=
decidable.rec
(λ Hc : c, eq.refl (@dite c (decidable.inl Hc) A t e))
(λ Hnc : ¬c, absurd Hc Hnc)
H
definition dif_neg {c : Type} [H : decidable c] (Hnc : ¬c) {A : Type} {t : c → A} {e : ¬ c → A} : (if H : c then t H else e H) = e (decidable.neg_witness Hnc) :=
decidable.rec
(λ Hc : c, absurd Hc Hnc)
(λ Hnc : ¬c, eq.refl (@dite c (decidable.inr Hnc) A t e))
H
-- Remark: dite and ite are "definitionally equal" when we ignore the proofs.
2014-12-08 20:12:19 +00:00
definition dite_ite_eq (c : Type) [H : decidable c] {A : Type} (t : A) (e : A) : dite c (λh, t) (λh, e) = ite c t e :=
2014-12-05 23:47:04 +00:00
rfl