diff --git a/library/data/bool.lean b/library/data/bool.lean index f149f79996..4068355b43 100644 --- a/library/data/bool.lean +++ b/library/data/bool.lean @@ -10,13 +10,13 @@ inductive bool : Type := ff : bool, tt : bool namespace bool - definition rec_on [protected] {C : bool → Type} (b : bool) (H₁ : C ff) (H₂ : C tt) : C b := + abbreviation rec_on [protected] {C : bool → Type} (b : bool) (H₁ : C ff) (H₂ : C tt) : C b := rec H₁ H₂ b - theorem cases_on [protected] {p : bool → Prop} (b : bool) (H₁ : p ff) (H₂ : p tt) : p b := + abbreviation cases_on [protected] {p : bool → Prop} (b : bool) (H₁ : p ff) (H₂ : p tt) : p b := rec H₁ H₂ b - definition cond {A : Type} (b : bool) (t e : A) := + abbreviation cond {A : Type} (b : bool) (t e : A) := rec_on b e t theorem dichotomy (b : bool) : b = ff ∨ b = tt := diff --git a/library/data/num.lean b/library/data/num.lean index 51b69bde13..9a276c2748 100644 --- a/library/data/num.lean +++ b/library/data/num.lean @@ -3,7 +3,8 @@ -- Released under Apache 2.0 license as described in the file LICENSE. -- Author: Leonardo de Moura ---------------------------------------------------------------------------------------------------- -import logic.classes.inhabited +import logic.classes.inhabited data.bool +open bool -- pos_num and num are two auxiliary datatypes used when parsing numerals such as 13, 0, 26. -- The parser will generate the terms (pos (bit1 (bit1 (bit0 one)))), zero, and (pos (bit0 (bit1 (bit1 one)))). @@ -13,14 +14,76 @@ one : pos_num, bit1 : pos_num → pos_num, bit0 : pos_num → pos_num +theorem pos_num.is_inhabited [instance] : inhabited pos_num := +inhabited.mk pos_num.one + namespace pos_num - theorem is_inhabited [instance] : inhabited pos_num := - inhabited.mk one + theorem induction_on [protected] {P : pos_num → Prop} (a : pos_num) + (H₁ : P one) (H₂ : ∀ (n : pos_num), P n → P (bit1 n)) (H₃ : ∀ (n : pos_num), P n → P (bit0 n)) : P a := + rec H₁ H₂ H₃ a + + abbreviation rec_on [protected] {P : pos_num → Type} (a : pos_num) + (H₁ : P one) (H₂ : ∀ (n : pos_num), P n → P (bit1 n)) (H₃ : ∀ (n : pos_num), P n → P (bit0 n)) : P a := + rec H₁ H₂ H₃ a + + abbreviation succ (a : pos_num) : pos_num := + rec_on a (bit0 one) (λn r, bit0 r) (λn r, bit1 n) + + abbreviation is_one (a : pos_num) : bool := + rec_on a tt (λn r, ff) (λn r, ff) + + abbreviation pred (a : pos_num) : pos_num := + rec_on a one (λn r, bit0 n) (λn r, cond (is_one n) one (bit1 r)) + + abbreviation size (a : pos_num) : pos_num := + rec_on a one (λn r, succ r) (λn r, succ r) + + theorem succ_not_is_one {a : pos_num} : is_one (succ a) = ff := + induction_on a rfl (take n iH, rfl) (take n iH, rfl) + + theorem pred_succ {a : pos_num} : pred (succ a) = a := + rec_on a + rfl + (take (n : pos_num) (iH : pred (succ n) = n), + calc + pred (succ (bit1 n)) = cond ff one (bit1 (pred (succ n))) : {succ_not_is_one} + ... = bit1 (pred (succ n)) : rfl + ... = bit1 n : {iH}) + (take (n : pos_num) (iH : pred (succ n) = n), rfl) end pos_num inductive num : Type := zero : num, pos : pos_num → num -theorem num_inhabited [instance] : inhabited num := +theorem num.is_inhabited [instance] : inhabited num := inhabited.mk num.zero + +namespace num + open pos_num + theorem induction_on [protected] {P : num → Prop} (a : num) + (H₁ : P zero) (H₂ : ∀ (p : pos_num), P (pos p)) : P a := + rec H₁ H₂ a + + abbreviation rec_on [protected] {P : num → Type} (a : num) + (H₁ : P zero) (H₂ : ∀ (p : pos_num), P (pos p)) : P a := + rec H₁ H₂ a + + abbreviation succ (a : num) : num := + rec_on a (pos one) (λp, pos (succ p)) + + abbreviation pred (a : num) : num := + rec_on a zero (λp, cond (is_one p) zero (pos (pred p))) + + abbreviation size (a : num) : num := + rec_on a (pos one) (λp, pos (size p)) + + theorem pred_succ (a : num) : pred (succ a) = a := + rec_on a + rfl + (λp, calc + pred (succ (pos p)) = pred (pos (pos_num.succ p)) : rfl + ... = cond ff zero (pos (pos_num.pred (pos_num.succ p))) : {succ_not_is_one} + ... = pos (pos_num.pred (pos_num.succ p)) : cond_ff _ _ + ... = pos p : {pos_num.pred_succ}) +end num diff --git a/tests/lean/run/tactic26.lean b/tests/lean/run/tactic26.lean index 4d98a5c5a8..c2b5771470 100644 --- a/tests/lean/run/tactic26.lean +++ b/tests/lean/run/tactic26.lean @@ -14,7 +14,7 @@ theorem inr_inhabited (A : Type) {B : Type} (H : inhabited B) : inhabited (sum A definition my_tac := fixpoint (λ t, [ apply @inl_inhabited; t | apply @inr_inhabited; t - | apply @num_inhabited + | apply @num.is_inhabited ]) tactic_hint [inhabited] my_tac diff --git a/tests/lean/run/tactic28.lean b/tests/lean/run/tactic28.lean index 7fb0139939..d2a665d6a0 100644 --- a/tests/lean/run/tactic28.lean +++ b/tests/lean/run/tactic28.lean @@ -17,7 +17,7 @@ infixl `..`:100 := append definition my_tac := repeat (trace "iteration"; state; ( apply @inl_inhabited; trace "used inl" .. apply @inr_inhabited; trace "used inr" - .. apply @num_inhabited; trace "used num")) ; now + .. apply @num.is_inhabited; trace "used num")) ; now tactic_hint [inhabited] my_tac