2014-12-01 05:16:01 +00:00
|
|
|
|
import data.nat data.prod
|
2014-11-18 21:55:58 +00:00
|
|
|
|
open nat well_founded decidable prod eq.ops
|
|
|
|
|
|
|
|
|
|
-- Auxiliary lemma used to justify recursive call
|
2014-11-22 17:56:47 +00:00
|
|
|
|
private definition lt_aux {x y : nat} (H : 0 < y ∧ y ≤ x) : x - y < x :=
|
|
|
|
|
and.rec_on H (λ ypos ylex,
|
2014-11-30 23:07:09 +00:00
|
|
|
|
sub.lt (lt.of_lt_of_le ypos ylex) ypos)
|
2014-11-18 21:55:58 +00:00
|
|
|
|
|
|
|
|
|
definition wdiv.F (x : nat) (f : Π x₁, x₁ < x → nat → nat) (y : nat) : nat :=
|
2014-12-04 19:13:09 +00:00
|
|
|
|
if H : 0 < y ∧ y ≤ x then f (x - y) (lt_aux H) y + 1 else zero
|
2014-11-18 21:55:58 +00:00
|
|
|
|
|
|
|
|
|
definition wdiv (x y : nat) :=
|
|
|
|
|
fix wdiv.F x y
|
|
|
|
|
|
|
|
|
|
theorem wdiv_def (x y : nat) : wdiv x y = if 0 < y ∧ y ≤ x then wdiv (x - y) y + 1 else 0 :=
|
|
|
|
|
congr_fun (well_founded.fix_eq wdiv.F x) y
|
|
|
|
|
|
2014-11-22 17:56:47 +00:00
|
|
|
|
example : wdiv 5 2 = 2 :=
|
|
|
|
|
rfl
|
|
|
|
|
|
|
|
|
|
example : wdiv 9 3 = 3 :=
|
|
|
|
|
rfl
|
|
|
|
|
|
2014-11-18 21:55:58 +00:00
|
|
|
|
-- There is a little bit of cheating in the definition above.
|
|
|
|
|
-- I avoid the packing/unpacking into tuples.
|
|
|
|
|
-- The actual definitional package would not do that.
|
|
|
|
|
-- It will always pack things.
|
|
|
|
|
|
|
|
|
|
definition pair_nat.lt := lex lt lt -- Could also be (lex lt empty_rel)
|
2014-11-19 01:59:14 +00:00
|
|
|
|
definition pair_nat.lt.wf [instance] : well_founded pair_nat.lt :=
|
2014-11-22 17:56:47 +00:00
|
|
|
|
prod.lex.wf lt.wf lt.wf
|
2014-11-18 21:55:58 +00:00
|
|
|
|
infixl `≺`:50 := pair_nat.lt
|
|
|
|
|
|
|
|
|
|
-- Recursive lemma used to justify recursive call
|
2014-11-22 17:56:47 +00:00
|
|
|
|
definition plt_aux (x y : nat) (H : 0 < y ∧ y ≤ x) : (x - y, y) ≺ (x, y) :=
|
|
|
|
|
!lex.left (lt_aux H)
|
2014-11-18 21:55:58 +00:00
|
|
|
|
|
2014-11-22 17:56:47 +00:00
|
|
|
|
definition pdiv.F (p₁ : nat × nat) : (Π p₂ : nat × nat, p₂ ≺ p₁ → nat) → nat :=
|
|
|
|
|
prod.cases_on p₁ (λ x y f,
|
2014-12-04 19:13:09 +00:00
|
|
|
|
if H : 0 < y ∧ y ≤ x then f (x - y, y) (plt_aux x y H) + 1 else zero)
|
2014-11-18 21:55:58 +00:00
|
|
|
|
|
|
|
|
|
definition pdiv (x y : nat) :=
|
|
|
|
|
fix pdiv.F (x, y)
|
|
|
|
|
|
|
|
|
|
theorem pdiv_def (x y : nat) : pdiv x y = if 0 < y ∧ y ≤ x then pdiv (x - y) y + 1 else zero :=
|
|
|
|
|
well_founded.fix_eq pdiv.F (x, y)
|
|
|
|
|
|
2014-11-22 17:56:47 +00:00
|
|
|
|
example : pdiv 17 2 = 8 :=
|
2014-11-19 01:59:14 +00:00
|
|
|
|
rfl
|