Timings for naturals.v
Require Import
HoTT.Basics.Decidable.
Require Import
HoTT.Classes.interfaces.orders
HoTT.Classes.implementations.peano_naturals
HoTT.Classes.theory.rings
HoTT.Classes.isomorphisms.rings.
Require Export
HoTT.Classes.interfaces.naturals.
Generalizable Variables A N R SR f.
(* This grabs a coercion. *)
Lemma to_semiring_unique `{Naturals N} `{IsSemiCRing SR} (f: N -> SR)
`{!IsSemiRingPreserving f} x
: f x = naturals_to_semiring N SR x.
Lemma to_semiring_unique_alt `{Naturals N} `{IsSemiCRing SR} (f g: N -> SR)
`{!IsSemiRingPreserving f} `{!IsSemiRingPreserving g} x
: f x = g x.
rewrite (to_semiring_unique f), (to_semiring_unique g);reflexivity.
Lemma to_semiring_involutive N `{Naturals N} N2 `{Naturals N2} x :
naturals_to_semiring N2 N (naturals_to_semiring N N2 x) = x.
change (Compose (naturals_to_semiring N2 N) (naturals_to_semiring N N2) x = id x).
apply to_semiring_unique_alt;exact _.
Lemma morphisms_involutive `{Naturals N} `{IsSemiCRing R} (f : R -> N) (g : N -> R)
`{!IsSemiRingPreserving f} `{!IsSemiRingPreserving g} x : f (g x) = x.
exact (to_semiring_unique_alt (f ∘ g) id _).
Lemma to_semiring_twice `{Naturals N} `{IsSemiCRing R1} `{IsSemiCRing R2}
(f : R1 -> R2) (g : N -> R1) (h : N -> R2)
`{!IsSemiRingPreserving f} `{!IsSemiRingPreserving g} `{!IsSemiRingPreserving h} x
: f (g x) = h x.
exact (to_semiring_unique_alt (f ∘ g) h _).
Lemma to_semiring_self `{Naturals N} (f : N -> N) `{!IsSemiRingPreserving f} x
: f x = x.
exact (to_semiring_unique_alt f id _).
Lemma to_semiring_injective `{Naturals N} `{IsSemiCRing A}
(f: A -> N) (g: N -> A) `{!IsSemiRingPreserving f} `{!IsSemiRingPreserving g}
: IsInjective g.
rewrite <-(to_semiring_twice f g id x), <-(to_semiring_twice f g id y).
Instance naturals_to_naturals_injective `{Naturals N} `{Naturals N2}
(f: N -> N2) `{!IsSemiRingPreserving f}
: IsInjective f | 15.
exact (to_semiring_injective (naturals_to_semiring N2 N) _).
Context `{Naturals N} `{IsSemiCRing SR}
{SRap : Apart SR} {SRle SRlt} `{!FullPseudoSemiRingOrder (A:=SR) SRle SRlt}.
Context (f : N -> SR) `{!IsEquiv f}
`{!IsSemiRingPreserving f} `{!IsSemiRingPreserving (f^-1)}.
(* If we make this an instance, instance resolution will loop *)
Definition retract_is_nat_to_sr : NaturalsToSemiRing SR
:= fun R _ _ _ _ _ => naturals_to_semiring N R ∘ f^-1.
Section for_another_semirings.
Context `{IsSemiCRing R}.
Instance issemiringpreserving_naturals_to_semiring : IsSemiRingPreserving (naturals_to_semiring N R ∘ f^-1) := {}.
Context (h : SR -> R) `{!IsSemiRingPreserving h}.
Lemma same_morphism x : (naturals_to_semiring N R ∘ f^-1) x = h x.
transitivity ((h ∘ (f ∘ f^-1)) x).
apply (to_semiring_unique (h ∘ f)).
End for_another_semirings.
(* If we make this an instance, instance resolution will loop *)
Lemma retract_is_nat : Naturals SR (U:=retract_is_nat_to_sr).
unfold naturals_to_semiring, retract_is_nat_to_sr.
intros;apply same_morphism;exact _.
Context `{Naturals N1} `{Naturals N2}.
#[export] Instance nat_to_nat_equiv : IsEquiv (naturals_to_semiring N1 N2).
apply Equivalences.isequiv_adjointify with (naturals_to_semiring N2 N1);
red;exact (to_semiring_involutive _ _).
(* {U U} because we do forall n : N, {id} n = nat_to_sr N N n *)
Context `{Funext} `{Univalence} {N : Type@{U} } `{Naturals@{U U U U U U U U} N}.
Lemma from_nat_stmt (N':Type@{U}) `{Naturals@{U U U U U U U U} N'}
: forall (P : SemiRings.Operations -> Type),
P (SemiRings.BuildOperations N') -> P (SemiRings.BuildOperations N).
apply SemiRings.iso_leibnitz with (naturals_to_semiring N' N);exact _.
Section borrowed_from_nat.
Lemma induction
: forall (P: N -> Type),
P 0 -> (forall n, P n -> P (1 + n)) -> forall n, P n.
pose (Q := fun s : SemiRings.Operations =>
forall P : s -> Type, P 0 -> (forall n, P n -> P (1 + n)) -> forall n, P n).
change (Q (SemiRings.BuildOperations N)).
apply (from_nat_stmt nat).
Lemma case : forall x : N, x = 0 |_| exists y : N, (x = 1 + y).
refine (from_nat_stmt nat
(fun s => forall x : s, x = 0 |_| exists y : s, x = 1 + y) _).
#[export] Instance biinduction_nat : Biinduction N.
#[export] Instance nat_plus_cancel_l : forall z : N, LeftCancellation (+) z.
refine (from_nat_stmt@{i U}
nat (fun s => forall z : s, LeftCancellation plus z) _).
first [exact nat_plus_cancel_l@{U i}|exact nat_plus_cancel_l@{U}].
#[export] Instance rightcancellation_plus_nat : forall z : N, RightCancellation (+) z.
exact (right_cancel_from_left (+)).
#[export] Instance leftcancellation_plus_nat : forall z : N, PropHolds (z <> 0) -> LeftCancellation (.*.) z.
refine (from_nat_stmt nat (fun s =>
forall z : s, PropHolds (z <> 0) -> LeftCancellation mult z) _).
#[export] Instance rightcancellation_mult_nat : forall z : N, PropHolds (z <> 0) -> RightCancellation (.*.) z.
exact (right_cancel_from_left (.*.)).
Instance nat_nontrivial : PropHolds ((1:N) <> 0).
refine (from_nat_stmt nat (fun s => PropHolds ((1:s) <> 0)) _).
Instance nat_nontrivial_apart `{Apart N} `{!TrivialApart N} :
PropHolds ((1:N) ≶ 0).
apply apartness.ne_apart.
Lemma zero_sum : forall (x y : N), x + y = 0 -> x = 0 /\ y = 0.
refine (from_nat_stmt nat
(fun s => forall x y : s, x + y = 0 -> x = 0 /\ y = 0) _).
Lemma one_sum : forall (x y : N), x + y = 1 -> (x = 1 /\ y = 0) |_| (x = 0 /\ y = 1).
refine (from_nat_stmt nat (fun s =>
forall (x y : s), x + y = 1 -> (x = 1 /\ y = 0) |_| (x = 0 /\ y = 1)) _).
rewrite add_S_l,add_0_r in E.
rewrite add_S_l,add_S_r in E.
#[export] Instance zeroproduct_nat : ZeroProduct N.
refine (from_nat_stmt nat (fun s => ZeroProduct s) _).
Lemma nat_1_plus_ne_0 x : 1 + x <> 0.
destruct (zero_sum 1 x E).
#[export] Instance slow_naturals_dec : DecidablePaths N.
apply decidablepaths_equiv with nat (naturals_to_semiring nat N);exact _.
Context `{IsCRing R} `{!IsSemiRingPreserving (f : N -> R)} `{!IsInjective f}.
Lemma to_ring_zero_sum x y :
-f x = f y -> x = 0 /\ y = 0.
apply zero_sum, (injective f).
rewrite rings.preserves_0, rings.preserves_plus, <-E.
Lemma negate_to_ring x y :
-f x = f y -> f x = f y.
destruct (to_ring_zero_sum x y E) as [E2 E3].
#[export]
Hint Extern 6 (PropHolds (1 <> 0)) =>
eapply @nat_nontrivial : typeclass_instances.
#[export]
Hint Extern 6 (PropHolds (1 ≶ 0)) =>
eapply @nat_nontrivial_apart : typeclass_instances.