2014-12-13 23:36:35 +00:00
|
|
|
/-
|
|
|
|
Copyright (c) 2014 Microsoft Corporation. All rights reserved.
|
|
|
|
Released under Apache 2.0 license as described in the file LICENSE.
|
|
|
|
Author: Leonardo de Moura
|
|
|
|
|
|
|
|
Show that "bounded" quantifiers: (∃x, x < n ∧ P x) and (∀x, x < n → P x)
|
|
|
|
are decidable when P is decidable.
|
|
|
|
|
|
|
|
This module allow us to write if-then-else expressions such as
|
|
|
|
|
|
|
|
if (∀ x : nat, x < n → ∃ y : nat, y < n ∧ y * y = x) then t else s
|
|
|
|
|
|
|
|
without assuming classical axioms.
|
|
|
|
|
|
|
|
More importantly, they can be reduced inside of the Lean kernel.
|
|
|
|
-/
|
2014-12-24 00:46:48 +00:00
|
|
|
import data.nat.order
|
2014-12-13 23:36:35 +00:00
|
|
|
|
|
|
|
namespace nat
|
2015-02-24 22:09:20 +00:00
|
|
|
definition bex [reducible] (n : nat) (P : nat → Prop) : Prop :=
|
2014-12-13 23:36:35 +00:00
|
|
|
∃ x, x < n ∧ P x
|
|
|
|
|
2015-07-15 19:49:47 +00:00
|
|
|
definition bsig [reducible] (n : nat) (P : nat → Prop) : Type₁ :=
|
|
|
|
Σ x, x < n ∧ P x
|
|
|
|
|
2015-02-24 22:09:20 +00:00
|
|
|
definition ball [reducible] (n : nat) (P : nat → Prop) : Prop :=
|
2014-12-13 23:36:35 +00:00
|
|
|
∀ x, x < n → P x
|
|
|
|
|
2015-07-15 19:49:47 +00:00
|
|
|
lemma bex_of_bsig {n : nat} {P : nat → Prop} : bsig n P → bex n P :=
|
|
|
|
assume h, ex_of_sig h
|
|
|
|
|
2015-07-03 06:18:02 +00:00
|
|
|
theorem not_bex_zero (P : nat → Prop) : ¬ bex 0 P :=
|
2014-12-13 23:36:35 +00:00
|
|
|
λ H, obtain (w : nat) (Hw : w < 0 ∧ P w), from H,
|
|
|
|
and.rec_on Hw (λ h₁ h₂, absurd h₁ (not_lt_zero w))
|
|
|
|
|
2015-07-15 19:49:47 +00:00
|
|
|
theorem not_bsig_zero (P : nat → Prop) : bsig 0 P → false :=
|
|
|
|
λ H, absurd (bex_of_bsig H) (not_bex_zero P)
|
|
|
|
|
|
|
|
definition bsig_succ {P : nat → Prop} {n : nat} (H : bsig n P) : bsig (succ n) P :=
|
|
|
|
obtain (w : nat) (Hw : w < n ∧ P w), from H,
|
|
|
|
and.rec_on Hw (λ hlt hp, ⟨w, (and.intro (lt.step hlt) hp)⟩)
|
|
|
|
|
2015-07-03 06:18:02 +00:00
|
|
|
theorem bex_succ {P : nat → Prop} {n : nat} (H : bex n P) : bex (succ n) P :=
|
2014-12-13 23:36:35 +00:00
|
|
|
obtain (w : nat) (Hw : w < n ∧ P w), from H,
|
2014-12-16 03:05:03 +00:00
|
|
|
and.rec_on Hw (λ hlt hp, exists.intro w (and.intro (lt.step hlt) hp))
|
2014-12-13 23:36:35 +00:00
|
|
|
|
2015-07-15 19:49:47 +00:00
|
|
|
definition bsig_succ_of_pred {P : nat → Prop} {a : nat} (H : P a) : bsig (succ a) P :=
|
|
|
|
⟨a, and.intro (lt.base a) H⟩
|
|
|
|
|
2015-07-03 06:18:02 +00:00
|
|
|
theorem bex_succ_of_pred {P : nat → Prop} {a : nat} (H : P a) : bex (succ a) P :=
|
2015-07-15 19:49:47 +00:00
|
|
|
bex_of_bsig (bsig_succ_of_pred H)
|
2014-12-13 23:36:35 +00:00
|
|
|
|
2015-07-03 06:18:02 +00:00
|
|
|
theorem not_bex_succ {P : nat → Prop} {n : nat} (H₁ : ¬ bex n P) (H₂ : ¬ P n) : ¬ bex (succ n) P :=
|
2014-12-13 23:36:35 +00:00
|
|
|
λ H, obtain (w : nat) (Hw : w < succ n ∧ P w), from H,
|
2015-06-04 23:16:28 +00:00
|
|
|
and.rec_on Hw (λ hltsn hp, or.rec_on (eq_or_lt_of_le (le_of_succ_le_succ hltsn))
|
2014-12-13 23:36:35 +00:00
|
|
|
(λ heq : w = n, absurd (eq.rec_on heq hp) H₂)
|
2014-12-16 03:05:03 +00:00
|
|
|
(λ hltn : w < n, absurd (exists.intro w (and.intro hltn hp)) H₁))
|
2014-12-13 23:36:35 +00:00
|
|
|
|
2015-07-15 19:49:47 +00:00
|
|
|
theorem not_bsig_succ {P : nat → Prop} {n : nat} (H₁ : ¬ bex n P) (H₂ : ¬ P n) : bsig (succ n) P → false :=
|
|
|
|
λ H, absurd (bex_of_bsig H) (not_bex_succ H₁ H₂)
|
|
|
|
|
2015-07-03 06:18:02 +00:00
|
|
|
theorem ball_zero (P : nat → Prop) : ball zero P :=
|
2014-12-13 23:36:35 +00:00
|
|
|
λ x Hlt, absurd Hlt !not_lt_zero
|
|
|
|
|
2015-07-03 06:18:02 +00:00
|
|
|
theorem ball_of_ball_succ {n : nat} {P : nat → Prop} (H : ball (succ n) P) : ball n P :=
|
2014-12-13 23:36:35 +00:00
|
|
|
λ x Hlt, H x (lt.step Hlt)
|
|
|
|
|
2015-07-03 06:18:02 +00:00
|
|
|
theorem ball_succ_of_ball {n : nat} {P : nat → Prop} (H₁ : ball n P) (H₂ : P n) : ball (succ n) P :=
|
2015-06-04 23:16:28 +00:00
|
|
|
λ (x : nat) (Hlt : x < succ n), or.elim (eq_or_lt_of_le (le_of_succ_le_succ Hlt))
|
2014-12-13 23:36:35 +00:00
|
|
|
(λ heq : x = n, eq.rec_on (eq.rec_on heq rfl) H₂)
|
|
|
|
(λ hlt : x < n, H₁ x hlt)
|
|
|
|
|
2015-07-03 06:18:02 +00:00
|
|
|
theorem not_ball_of_not {n : nat} {P : nat → Prop} (H₁ : ¬ P n) : ¬ ball (succ n) P :=
|
2014-12-13 23:36:35 +00:00
|
|
|
λ (H : ball (succ n) P), absurd (H n (lt.base n)) H₁
|
|
|
|
|
2015-07-03 06:18:02 +00:00
|
|
|
theorem not_ball_succ_of_not_ball {n : nat} {P : nat → Prop} (H₁ : ¬ ball n P) : ¬ ball (succ n) P :=
|
2014-12-13 23:36:35 +00:00
|
|
|
λ (H : ball (succ n) P), absurd (ball_of_ball_succ H) H₁
|
|
|
|
end nat
|
|
|
|
|
|
|
|
section
|
|
|
|
open nat decidable
|
|
|
|
|
2015-02-24 23:25:02 +00:00
|
|
|
definition decidable_bex [instance] (n : nat) (P : nat → Prop) [H : decidable_pred P] : decidable (bex n P) :=
|
2014-12-13 23:36:35 +00:00
|
|
|
nat.rec_on n
|
|
|
|
(inr (not_bex_zero P))
|
|
|
|
(λ a ih, decidable.rec_on ih
|
|
|
|
(λ hpos : bex a P, inl (bex_succ hpos))
|
|
|
|
(λ hneg : ¬ bex a P, decidable.rec_on (H a)
|
|
|
|
(λ hpa : P a, inl (bex_succ_of_pred hpa))
|
|
|
|
(λ hna : ¬ P a, inr (not_bex_succ hneg hna))))
|
|
|
|
|
2015-02-24 23:25:02 +00:00
|
|
|
definition decidable_ball [instance] (n : nat) (P : nat → Prop) [H : decidable_pred P] : decidable (ball n P) :=
|
2014-12-13 23:36:35 +00:00
|
|
|
nat.rec_on n
|
|
|
|
(inl (ball_zero P))
|
|
|
|
(λ n₁ ih, decidable.rec_on ih
|
|
|
|
(λ ih_pos, decidable.rec_on (H n₁)
|
|
|
|
(λ p_pos, inl (ball_succ_of_ball ih_pos p_pos))
|
|
|
|
(λ p_neg, inr (not_ball_of_not p_neg)))
|
|
|
|
(λ ih_neg, inr (not_ball_succ_of_not_ball ih_neg)))
|
2015-06-04 23:57:56 +00:00
|
|
|
|
|
|
|
definition decidable_bex_le [instance] (n : nat) (P : nat → Prop) [H : decidable_pred P]
|
|
|
|
: decidable (∃ x, x ≤ n ∧ P x) :=
|
|
|
|
decidable_of_decidable_of_iff
|
|
|
|
(decidable_bex (succ n) P)
|
|
|
|
(exists_congr (λn, and_iff_and !lt_succ_iff_le !iff.refl))
|
|
|
|
|
|
|
|
definition decidable_ball_le [instance] (n : nat) (P : nat → Prop) [H : decidable_pred P]
|
|
|
|
: decidable (∀ x, x ≤ n → P x) :=
|
|
|
|
decidable_of_decidable_of_iff
|
|
|
|
(decidable_ball (succ n) P)
|
|
|
|
(forall_congr (λn, imp_iff_imp !lt_succ_iff_le !iff.refl))
|
2014-12-13 23:36:35 +00:00
|
|
|
end
|
2015-07-03 06:18:02 +00:00
|
|
|
|
|
|
|
namespace nat
|
|
|
|
open decidable
|
|
|
|
variable {P : nat → Prop}
|
|
|
|
variable [decP : decidable_pred P]
|
|
|
|
include decP
|
|
|
|
|
2015-07-15 19:49:47 +00:00
|
|
|
definition bsig_not_of_not_ball : ∀ {n : nat}, ¬ ball n P → Σ i, i < n ∧ ¬ P i
|
2015-07-03 06:18:02 +00:00
|
|
|
| 0 h := absurd (ball_zero P) h
|
|
|
|
| (succ n) h := decidable.by_cases
|
|
|
|
(λ hp : P n,
|
|
|
|
have h₁ : ¬ ball n P, from
|
|
|
|
assume b : ball n P, absurd (ball_succ_of_ball b hp) h,
|
2015-07-15 19:49:47 +00:00
|
|
|
have h₂ : Σ i, i < n ∧ ¬ P i, from bsig_not_of_not_ball h₁,
|
|
|
|
bsig_succ h₂)
|
|
|
|
(λ hn : ¬ P n, bsig_succ_of_pred hn)
|
|
|
|
|
|
|
|
theorem bex_not_of_not_ball {n : nat} (H : ¬ ball n P) : bex n (λ n, ¬ P n) :=
|
|
|
|
bex_of_bsig (bsig_not_of_not_ball H)
|
2015-07-03 06:18:02 +00:00
|
|
|
|
|
|
|
theorem ball_not_of_not_bex : ∀ {n : nat}, ¬ bex n P → ball n (λ n, ¬ P n)
|
|
|
|
| 0 h := ball_zero _
|
|
|
|
| (succ n) h := by_cases
|
|
|
|
(λ hp : P n, absurd (bex_succ_of_pred hp) h)
|
|
|
|
(λ hn : ¬ P n,
|
|
|
|
have h₁ : ¬ bex n P, from
|
|
|
|
assume b : bex n P, absurd (bex_succ b) h,
|
|
|
|
have h₂ : ball n (λ n, ¬ P n), from ball_not_of_not_bex h₁,
|
|
|
|
ball_succ_of_ball h₂ hn)
|
|
|
|
end nat
|