Library HoTT.Spaces.SInt
Require Import Basics.Overture Basics.Nat Basics.Tactics Basics.Decidable.
Require Import Basics.Numerals.Decimal Basics.Numeral.
Require Import Spaces.Nat.Core.
Unset Elimination Schemes.
Set Universe Minimization ToSet.
Require Import Basics.Numerals.Decimal Basics.Numeral.
Require Import Spaces.Nat.Core.
Unset Elimination Schemes.
Set Universe Minimization ToSet.
The signed integers
Definition
Symmetrically, we can send n to "-n" in this way:
Parsing and printing
Definition sint_to_number_int (n : SInt) : Numeral.int :=
match n with
| sint_PosS m ⇒ IntDec (Pos (to_uint (S m)))
| sint_zero ⇒ IntDec (Pos (to_uint 0))
| sint_NegS m ⇒ IntDec (Neg (to_uint (S m)))
end.
match n with
| sint_PosS m ⇒ IntDec (Pos (to_uint (S m)))
| sint_zero ⇒ IntDec (Pos (to_uint 0))
| sint_NegS m ⇒ IntDec (Neg (to_uint (S m)))
end.
Parsing
Definition sint_of_number_int (d : Numeral.int) : SInt :=
match d with
| IntDec (Pos u) ⇒ sint_of_nat (of_uint u)
| IntDec (Neg u) ⇒ negsint_of_nat (of_uint u)
| IntHex (Hexadecimal.Pos u) ⇒ sint_of_nat (of_hex_uint u)
| IntHex (Hexadecimal.Neg u) ⇒ negsint_of_nat (of_hex_uint u)
end.
match d with
| IntDec (Pos u) ⇒ sint_of_nat (of_uint u)
| IntDec (Neg u) ⇒ negsint_of_nat (of_uint u)
| IntHex (Hexadecimal.Pos u) ⇒ sint_of_nat (of_hex_uint u)
| IntHex (Hexadecimal.Neg u) ⇒ negsint_of_nat (of_hex_uint u)
end.
Definition sint_succ (n : SInt) : SInt :=
match n with
| sint_PosS n ⇒ sint_PosS (S n)
| sint_zero ⇒ sint_PosS 0
| sint_NegS n ⇒ negsint_of_nat n
end.
Definition sint_pred (n : SInt) : SInt :=
match n with
| sint_PosS n ⇒ sint_of_nat n
| sint_zero ⇒ sint_NegS 0
| sint_NegS n ⇒ sint_NegS (S n)
end.
Definition sint_neg@{} (x : SInt) : SInt :=
match x with
| sint_PosS x ⇒ sint_NegS x
| sint_zero ⇒ sint_zero
| sint_NegS x ⇒ sint_PosS x
end.
The successor of a predecessor is the identity.
Definition sint_succ_pred@{} (x : SInt) : sint_succ (sint_pred x) = x.
Proof.
by destruct x as [ | | []].
Defined.
Proof.
by destruct x as [ | | []].
Defined.
The predecessor of a successor is the identity.
Definition sint_pred_succ@{} (x : SInt) : sint_pred (sint_succ x) = x.
Proof.
by destruct x as [[] | | ].
Defined.
Proof.
by destruct x as [[] | | ].
Defined.
Instance decidablepaths_sint@{} : DecidablePaths SInt.
Proof.
intros [x | | x] [y | | y].
2-4,6-8: right; intros; discriminate.
2: by left.
1,2: napply decidable_iff.
1,3: split.
1,3: napply ap.
1,2: intros H; by injection H.
1,2: exact _. (* Uses decidable equality of nat. *)
Defined.
Proof.
intros [x | | x] [y | | y].
2-4,6-8: right; intros; discriminate.
2: by left.
1,2: napply decidable_iff.
1,3: split.
1,3: napply ap.
1,2: intros H; by injection H.
1,2: exact _. (* Uses decidable equality of nat. *)
Defined.
By Hedberg's theorem, we have that the signed integers are a set.
Signed integer induction
Definition SInt_ind@{i} (P : SInt → Type@{i})
(H0 : P sint_zero)
(HP : ∀ n : nat, P (sint_of_nat n) → P (sint_PosS n))
(HN : ∀ n : nat, P (sint_neg (sint_of_nat n)) → P (sint_NegS n))
: ∀ x, P x.
Proof.
intros [x | | x].
- induction x as [|x IHx].
+ apply (HN 0%nat), H0.
+ apply (HN x.+1%nat), IHx.
- exact H0.
- induction x as [|x IHx].
+ apply (HP 0%nat), H0.
+ apply (HP x.+1%nat), IHx.
Defined.
(H0 : P sint_zero)
(HP : ∀ n : nat, P (sint_of_nat n) → P (sint_PosS n))
(HN : ∀ n : nat, P (sint_neg (sint_of_nat n)) → P (sint_NegS n))
: ∀ x, P x.
Proof.
intros [x | | x].
- induction x as [|x IHx].
+ apply (HN 0%nat), H0.
+ apply (HN x.+1%nat), IHx.
- exact H0.
- induction x as [|x IHx].
+ apply (HP 0%nat), H0.
+ apply (HP x.+1%nat), IHx.
Defined.
We record these so that they can be used with the induction tactic.