2015-04-16 18:31:39 +00:00
|
|
|
/-
|
|
|
|
Copyright (c) 2015 Microsoft Corporation. All rights reserved.
|
|
|
|
Released under Apache 2.0 license as described in the file LICENSE.
|
|
|
|
Authors: Leonardo de Moura
|
|
|
|
|
|
|
|
Choice function for decidable predicates on natural numbers.
|
|
|
|
|
|
|
|
This module provides the following two declarations:
|
|
|
|
|
|
|
|
choose {p : nat → Prop} [d : decidable_pred p] : (∃ x, p x) → nat
|
|
|
|
choose_spec {p : nat → Prop} [d : decidable_pred p] (ex : ∃ x, p x) : p (choose ex)
|
|
|
|
-/
|
|
|
|
import data.subtype data.nat.basic data.nat.order
|
|
|
|
open nat subtype decidable well_founded
|
|
|
|
|
|
|
|
namespace nat
|
2015-04-22 02:13:19 +00:00
|
|
|
section find_x
|
2015-04-16 18:31:39 +00:00
|
|
|
parameter {p : nat → Prop}
|
|
|
|
|
|
|
|
private definition lbp (x : nat) : Prop := ∀ y, y < x → ¬ p y
|
|
|
|
|
2015-05-09 19:15:30 +00:00
|
|
|
private lemma lbp_zero : lbp 0 :=
|
2015-04-16 18:31:39 +00:00
|
|
|
λ y h, absurd h (not_lt_zero y)
|
|
|
|
|
2015-05-09 19:15:30 +00:00
|
|
|
private lemma lbp_succ {x : nat} : lbp x → ¬ p x → lbp (succ x) :=
|
2015-04-16 18:31:39 +00:00
|
|
|
λ lx npx y yltsx,
|
2015-06-04 23:16:28 +00:00
|
|
|
or.elim (eq_or_lt_of_le (le_of_succ_le_succ yltsx))
|
2015-07-21 16:10:56 +00:00
|
|
|
(suppose y = x, by substvars; assumption)
|
|
|
|
(suppose y < x, lx y this)
|
2015-04-16 18:31:39 +00:00
|
|
|
|
|
|
|
private definition gtb (a b : nat) : Prop :=
|
|
|
|
a > b ∧ lbp a
|
|
|
|
|
|
|
|
local infix `≺`:50 := gtb
|
|
|
|
|
2015-05-09 19:15:30 +00:00
|
|
|
private lemma acc_of_px {x : nat} : p x → acc gtb x :=
|
2015-04-16 18:31:39 +00:00
|
|
|
assume h,
|
|
|
|
acc.intro x (λ (y : nat) (l : y ≺ x),
|
2015-05-26 01:14:52 +00:00
|
|
|
obtain (h₁ : y > x) (h₂ : ∀ a, a < y → ¬ p a), from l,
|
2015-04-16 18:31:39 +00:00
|
|
|
absurd h (h₂ x h₁))
|
|
|
|
|
2015-05-09 19:15:30 +00:00
|
|
|
private lemma acc_of_acc_succ {x : nat} : acc gtb (succ x) → acc gtb x :=
|
2015-04-16 18:31:39 +00:00
|
|
|
assume h,
|
|
|
|
acc.intro x (λ (y : nat) (l : y ≺ x),
|
|
|
|
by_cases
|
2015-07-21 16:10:56 +00:00
|
|
|
(suppose y = succ x, by substvars; assumption)
|
|
|
|
(suppose y ≠ succ x,
|
2015-07-18 18:36:05 +00:00
|
|
|
have x < y, from and.elim_left l,
|
2015-07-21 16:10:56 +00:00
|
|
|
have succ x < y, from lt_of_le_and_ne this (ne.symm `y ≠ succ x`),
|
2015-07-18 18:36:05 +00:00
|
|
|
acc.inv h (and.intro this (and.elim_right l))))
|
2015-04-16 18:31:39 +00:00
|
|
|
|
2015-05-09 19:15:30 +00:00
|
|
|
private lemma acc_of_px_of_gt {x y : nat} : p x → y > x → acc gtb y :=
|
2015-04-16 18:31:39 +00:00
|
|
|
assume px ygtx,
|
|
|
|
acc.intro y (λ (z : nat) (l : z ≺ y),
|
2015-05-26 01:14:52 +00:00
|
|
|
obtain (zgty : z > y) (h : ∀ a, a < z → ¬ p a), from l,
|
2015-04-16 18:31:39 +00:00
|
|
|
absurd px (h x (lt.trans ygtx zgty)))
|
|
|
|
|
2015-05-09 19:15:30 +00:00
|
|
|
private lemma acc_of_acc_of_lt : ∀ {x y : nat}, acc gtb x → y < x → acc gtb y
|
2015-04-16 18:31:39 +00:00
|
|
|
| 0 y a0 ylt0 := absurd ylt0 !not_lt_zero
|
|
|
|
| (succ x) y asx yltsx :=
|
2015-07-21 16:10:56 +00:00
|
|
|
assert acc gtb x, from acc_of_acc_succ asx,
|
2015-04-16 18:31:39 +00:00
|
|
|
by_cases
|
2015-07-20 04:15:20 +00:00
|
|
|
(suppose y = x, by substvars; assumption)
|
2015-07-21 16:10:56 +00:00
|
|
|
(suppose y ≠ x, acc_of_acc_of_lt `acc gtb x` (lt_of_le_and_ne (le_of_lt_succ yltsx) this))
|
2015-04-16 18:31:39 +00:00
|
|
|
|
|
|
|
parameter (ex : ∃ a, p a)
|
|
|
|
parameter [dp : decidable_pred p]
|
|
|
|
include dp
|
|
|
|
|
2015-05-09 19:15:30 +00:00
|
|
|
private lemma acc_of_ex (x : nat) : acc gtb x :=
|
2015-04-16 18:31:39 +00:00
|
|
|
obtain (w : nat) (pw : p w), from ex,
|
|
|
|
lt.by_cases
|
2015-07-20 04:15:20 +00:00
|
|
|
(suppose x < w, acc_of_acc_of_lt (acc_of_px pw) this)
|
|
|
|
(suppose x = w, by subst x; exact (acc_of_px pw))
|
|
|
|
(suppose x > w, acc_of_px_of_gt pw this)
|
2015-04-16 18:31:39 +00:00
|
|
|
|
2015-05-09 19:15:30 +00:00
|
|
|
private lemma wf_gtb : well_founded gtb :=
|
2015-04-16 18:31:39 +00:00
|
|
|
well_founded.intro acc_of_ex
|
|
|
|
|
|
|
|
private definition find.F (x : nat) : (Π x₁, x₁ ≺ x → lbp x₁ → {a : nat | p a}) → lbp x → {a : nat | p a} :=
|
|
|
|
match x with
|
|
|
|
| 0 := λ f l0, by_cases
|
|
|
|
(λ p0 : p 0, tag 0 p0)
|
2015-07-21 16:10:56 +00:00
|
|
|
(suppose ¬ p 0,
|
|
|
|
have lbp 1, from lbp_succ l0 this,
|
|
|
|
have 1 ≺ 0, from and.intro (lt.base 0) `lbp 1`,
|
|
|
|
f 1 `1 ≺ 0` `lbp 1`)
|
2015-04-16 18:31:39 +00:00
|
|
|
| (succ n) := λ f lsn, by_cases
|
2015-07-21 16:10:56 +00:00
|
|
|
(suppose p (succ n), tag (succ n) this)
|
|
|
|
(suppose ¬ p (succ n),
|
|
|
|
have lss : lbp (succ (succ n)), from lbp_succ lsn this,
|
2015-07-20 04:15:20 +00:00
|
|
|
have succ (succ n) ≺ succ n, from and.intro (lt.base (succ n)) lss,
|
|
|
|
f (succ (succ n)) this lss)
|
2015-04-16 18:31:39 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private definition find_x : {x : nat | p x} :=
|
|
|
|
@fix _ _ _ wf_gtb find.F 0 lbp_zero
|
|
|
|
end find_x
|
|
|
|
|
|
|
|
protected definition choose {p : nat → Prop} [d : decidable_pred p] : (∃ x, p x) → nat :=
|
|
|
|
assume h, elt_of (find_x h)
|
|
|
|
|
2015-05-19 05:35:18 +00:00
|
|
|
protected theorem choose_spec {p : nat → Prop} [d : decidable_pred p] (ex : ∃ x, p x) : p (nat.choose ex) :=
|
2015-04-16 18:31:39 +00:00
|
|
|
has_property (find_x ex)
|
|
|
|
end nat
|