2014-12-22 20:33:29 +00:00
|
|
|
/-
|
|
|
|
Copyright (c) 2014 Microsoft Corporation. All rights reserved.
|
|
|
|
Released under Apache 2.0 license as described in the file LICENSE.
|
|
|
|
|
|
|
|
Module: data.fin
|
|
|
|
Author: Leonardo de Moura
|
|
|
|
|
|
|
|
Finite ordinals.
|
|
|
|
-/
|
2015-01-14 00:35:59 +00:00
|
|
|
import data.nat
|
2014-11-18 07:44:57 +00:00
|
|
|
open nat
|
|
|
|
|
|
|
|
inductive fin : nat → Type :=
|
2015-02-26 01:00:10 +00:00
|
|
|
| fz : Π n, fin (succ n)
|
|
|
|
| fs : Π {n}, fin n → fin (succ n)
|
2014-11-18 07:44:57 +00:00
|
|
|
|
|
|
|
namespace fin
|
2015-02-26 00:20:44 +00:00
|
|
|
definition to_nat : Π {n}, fin n → nat
|
2015-03-05 22:42:42 +00:00
|
|
|
| ⌞n+1⌟ (fz n) := zero
|
|
|
|
| ⌞n+1⌟ (fs f) := succ (to_nat f)
|
2015-01-08 01:37:02 +00:00
|
|
|
|
|
|
|
theorem to_nat_fz (n : nat) : to_nat (fz n) = zero :=
|
|
|
|
rfl
|
|
|
|
|
|
|
|
theorem to_nat_fs {n : nat} (f : fin n) : to_nat (fs f) = succ (to_nat f) :=
|
|
|
|
rfl
|
|
|
|
|
2015-02-26 00:20:44 +00:00
|
|
|
theorem to_nat_lt : Π {n} (f : fin n), to_nat f < n
|
2015-03-05 22:42:42 +00:00
|
|
|
| (n+1) (fz n) := calc
|
2015-02-26 00:20:44 +00:00
|
|
|
to_nat (fz n) = 0 : rfl
|
|
|
|
... < n+1 : succ_pos n
|
2015-03-05 22:42:42 +00:00
|
|
|
| (n+1) (fs f) := calc
|
2015-02-26 00:20:44 +00:00
|
|
|
to_nat (fs f) = (to_nat f)+1 : rfl
|
|
|
|
... < n+1 : succ_lt_succ (to_nat_lt f)
|
2015-01-08 01:37:02 +00:00
|
|
|
|
2015-02-26 00:20:44 +00:00
|
|
|
definition lift : Π {n : nat}, fin n → Π (m : nat), fin (m + n)
|
2015-03-05 22:42:42 +00:00
|
|
|
| ⌞n+1⌟ (fz n) m := fz (m + n)
|
|
|
|
| ⌞n+1⌟ (fs f) m := fs (lift f m)
|
2014-11-18 07:44:57 +00:00
|
|
|
|
2015-01-08 01:37:02 +00:00
|
|
|
theorem lift_fz (n m : nat) : lift (fz n) m = fz (m + n) :=
|
2014-11-18 07:44:57 +00:00
|
|
|
rfl
|
|
|
|
|
2015-01-08 01:37:02 +00:00
|
|
|
theorem lift_fs {n : nat} (f : fin n) (m : nat) : lift (fs f) m = fs (lift f m) :=
|
|
|
|
rfl
|
|
|
|
|
2015-02-26 00:20:44 +00:00
|
|
|
theorem to_nat_lift : ∀ {n : nat} (f : fin n) (m : nat), to_nat f = to_nat (lift f m)
|
2015-03-05 22:42:42 +00:00
|
|
|
| (n+1) (fz n) m := rfl
|
|
|
|
| (n+1) (fs f) m := calc
|
2015-01-08 01:37:02 +00:00
|
|
|
to_nat (fs f) = (to_nat f) + 1 : rfl
|
|
|
|
... = (to_nat (lift f m)) + 1 : to_nat_lift f
|
|
|
|
... = to_nat (lift (fs f) m) : rfl
|
|
|
|
|
2015-02-26 00:20:44 +00:00
|
|
|
definition of_nat : Π (p : nat) (n : nat), p < n → fin n
|
2015-03-05 22:42:42 +00:00
|
|
|
| 0 0 h := absurd h (not_lt_zero zero)
|
|
|
|
| 0 (n+1) h := fz n
|
|
|
|
| (p+1) 0 h := absurd h (not_lt_zero (succ p))
|
|
|
|
| (p+1) (n+1) h := fs (of_nat p n (lt_of_succ_lt_succ h))
|
2015-01-08 01:37:02 +00:00
|
|
|
|
|
|
|
theorem of_nat_zero_succ (n : nat) (h : 0 < n+1) : of_nat 0 (n+1) h = fz n :=
|
|
|
|
rfl
|
|
|
|
|
|
|
|
theorem of_nat_succ_succ (p n : nat) (h : p+1 < n+1) :
|
2015-01-08 02:26:51 +00:00
|
|
|
of_nat (p+1) (n+1) h = fs (of_nat p n (lt_of_succ_lt_succ h)) :=
|
2015-01-08 01:37:02 +00:00
|
|
|
rfl
|
2014-11-18 07:44:57 +00:00
|
|
|
|
2015-02-26 00:20:44 +00:00
|
|
|
theorem to_nat_of_nat : ∀ (p : nat) (n : nat) (h : p < n), to_nat (of_nat p n h) = p
|
2015-03-05 22:42:42 +00:00
|
|
|
| 0 0 h := absurd h (not_lt_zero 0)
|
|
|
|
| 0 (n+1) h := rfl
|
|
|
|
| (p+1) 0 h := absurd h (not_lt_zero (p+1))
|
|
|
|
| (p+1) (n+1) h := calc
|
2015-02-26 00:20:44 +00:00
|
|
|
to_nat (of_nat (p+1) (n+1) h)
|
|
|
|
= succ (to_nat (of_nat p n _)) : rfl
|
|
|
|
... = succ p : {to_nat_of_nat p n _}
|
|
|
|
|
|
|
|
theorem of_nat_to_nat : ∀ {n : nat} (f : fin n) (h : to_nat f < n), of_nat (to_nat f) n h = f
|
2015-03-05 22:42:42 +00:00
|
|
|
| (n+1) (fz n) h := rfl
|
|
|
|
| (n+1) (fs f) h := calc
|
2015-02-26 00:20:44 +00:00
|
|
|
of_nat (to_nat (fs f)) (succ n) h = fs (of_nat (to_nat f) n _) : rfl
|
|
|
|
... = fs f : {of_nat_to_nat f _}
|
2015-01-08 01:46:47 +00:00
|
|
|
|
2014-11-18 07:44:57 +00:00
|
|
|
end fin
|