Timings for fields.v
Require Import
HoTT.Classes.interfaces.abstract_algebra
HoTT.Classes.theory.apartness.
Require Export
HoTT.Classes.theory.rings.
Generalizable Variables F f.
Section field_properties.
Definition recip' (x : F) (apx : x ≶ 0) : F := //(x;apx).
(* Add Ring F : (stdlib_ring_theory F). *)
Lemma recip_inverse' (x : F) (Px : x ≶ 0) : x // (x; Px) = 1.
exact (recip_inverse (x;Px)).
Lemma reciperse_alt (x : F) Px : x // (x;Px) = 1.
rewrite <-(recip_inverse (x;Px)).
Lemma recip_proper_alt x y Px Py : x = y -> // (x;Px) = // (y;Py).
apply Sigma.path_sigma with E.
Lemma recip_proper x y Py : x // (y;Py) = 1 -> x = y.
rewrite (mult_assoc y x (//(y;Py))).
rewrite <- (mult_assoc x y (//(y;Py))).
rewrite (recip_inverse (y;Py)).
Lemma recip_irrelevant x Px1 Px2 : // (x;Px1) = // (x;Px2).
Lemma apart_0_proper {x y} : x ≶ 0 -> x = y -> y ≶ 0.
#[export] Instance isstronginjective_negation_field: IsStrongInjective (-).
repeat (split; try exact _); intros x y E.
apply (strong_extensionality (+ x + y)).
rewrite simple_associativity, left_inverse, plus_0_l.
rewrite (commutativity (f:=plus) x y), simple_associativity,
left_inverse, plus_0_l.
apply (strong_extensionality (+ -x + -y)).
rewrite simple_associativity, right_inverse, plus_0_l.
rewrite (commutativity (f:=plus) (- x) (- y)),
simple_associativity, right_inverse, plus_0_l.
#[export] Instance isstronginjective_recip_field: IsStrongInjective (//).
repeat (split; try exact _); intros x y E.
apply (strong_extensionality (x.1 *.)).
rewrite recip_inverse, (commutativity (f:=mult)).
apply (strong_extensionality (y.1 *.)).
rewrite simple_associativity, recip_inverse.
rewrite mult_1_l,mult_1_r.
apply (strong_extensionality (.* // x)).
rewrite recip_inverse, (commutativity (f:=mult)).
apply (strong_extensionality (.* // y)).
rewrite <-simple_associativity, recip_inverse.
rewrite mult_1_l,mult_1_r.
#[export] Instance strongleftcancellation_plus_field: forall z, StrongLeftCancellation (+) z.
apply (strong_extensionality (+ -z)).
do 2 rewrite (commutativity (f:=plus) z _),
<-simple_associativity,right_inverse,plus_0_r.
#[export] Instance strongrightcancellation_plus_field : forall z, StrongRightCancellation (+) z.
exact (strong_right_cancel_from_left (+)).
#[export] Instance strongleftcancellation_mult_field : forall z, PropHolds (z ≶ 0) -> StrongLeftCancellation (.*.) z.
rewrite !(commutativity z).
apply (strong_extensionality (.* // (z;(Ez : (≶0) z)))).
rewrite <-!simple_associativity, !reciperse_alt.
rewrite !mult_1_r;trivial.
#[export] Instance strongrightcancellation_mult_field : forall z, PropHolds (z ≶ 0) -> StrongRightCancellation (.*.) z.
exact (strong_right_cancel_from_left (.*.)).
Lemma mult_apart_zero_l x y : x * y ≶ 0 -> x ≶ 0.
apply (strong_extensionality (.* y)).
Lemma mult_apart_zero_r x y : x * y ≶ 0 -> y ≶ 0.
apply (strong_extensionality (x *.)).
Instance mult_apart_zero x y :
PropHolds (x ≶ 0) -> PropHolds (y ≶ 0) -> PropHolds (x * y ≶ 0).
apply (strong_extensionality (.* // (y;(Ey : (≶0) y)))).
rewrite <-simple_associativity, reciperse_alt, mult_1_r, mult_0_l.
Instance nozerodivisors_field : NoZeroDivisors F.
intros x [x_nonzero [y [y_nonzero E]]].
assert (~ ~ apart y 0) as Ey.
intros E';apply y_nonzero,tight_apart,E'.
rewrite <- (reciperse_alt y y_apartzero).
rewrite simple_associativity, E.
#[export] Instance isintegraldomain_field : IsIntegralDomain F := {}.
#[export] Instance apart_0_sig_apart_0 : forall (x : ApartZero F), PropHolds (x.1 ≶ 0).
Instance recip_apart_zero x : PropHolds (// x ≶ 0).
apply mult_apart_zero_r with (x.1).
Lemma field_div_0_l x y : x = 0 -> x // y = 0.
Lemma field_div_diag x y : x = y.1 -> x // y = 1.
Lemma equal_quotients (a c: F) b d : a * d.1 = c * b.1 <-> a // b = c // d.
rewrite <-(mult_1_l (a // b)),
<- (recip_inverse d),
(commutativity (f:=mult) d.1 (// d)),
<-simple_associativity,
(simple_associativity d.1),
(commutativity (f:=mult) d.1 a),
E,
<-simple_associativity,
simple_associativity,
recip_inverse,
mult_1_r.
rewrite <-(mult_1_r (a * d.1)),
<- (recip_inverse b),
<-simple_associativity,
(commutativity (f:=mult) b.1 (// b)),
(simple_associativity d.1),
(commutativity (f:=mult) d.1),
!simple_associativity,
E,
<-(simple_associativity c),
(commutativity (f:=mult) (// d)),
recip_inverse,
mult_1_r.
Lemma recip_distr_alt (x y : F) Px Py Pxy :
// (x * y ; Pxy) = // (x;Px) * // (y;Py).
apply (left_cancellation_ne_0 (.*.) (x * y)).
transitivity ((x // (x;Px)) * (y // (y;Py))).
rewrite 3!reciperse_alt,mult_1_r.
rewrite <-simple_associativity,<-simple_associativity.
rewrite simple_associativity.
rewrite (commutativity (f:=mult) _ y).
rewrite <-simple_associativity.
Lemma apart_negate (x : F) (Px : x ≶ 0) : (-x) ≶ 0.
(* Have: x <> 0 *)
(* Want to show: -x <> 0 *)
(* Since x=x+0 <> 0=x-x, have x<>x or 0<>-x *)
assert (ap : x + 0 ≶ x - x).
rewrite (plus_negate_r x).
refine (Trunc_rec _ (field_plus_ext F x 0 x (-x) ap)).
destruct (apart_ne x x apxx); reflexivity.
Definition negate_apart : ApartZero F -> ApartZero F.
exact ((apart_negate x Px)).
Lemma recip_negate (x : F) (Px : x ≶ 0) : (-//(x;Px))=//(negate_apart(x;Px)).
apply (left_cancellation (.*.) x).
rewrite <- negate_mult_distr_r.
rewrite negate_mult_distr_l.
Lemma recip_apart (x : F) (Px : x ≶ 0) : // (x;Px) ≶ 0.
apply (strong_extensionality (x*.) (// (x; Px)) 0).
rewrite (recip_inverse (x;Px)).
Definition recip_on_apart (x : ApartZero F) : ApartZero F.
#[export] Instance recip_involutive: Involutive recip_on_apart.
apply (left_cancellation (.*.) (// (x; apx0))).
rewrite (recip_inverse' (// (x; apx0)) (recip_apart x apx0)).
rewrite (recip_inverse (x;apx0)).
#[export]
Hint Extern 8 (PropHolds (// _ ≶ 0)) =>
eapply @recip_apart_zero : typeclass_instances.
#[export]
Hint Extern 8 (PropHolds (_ * _ ≶ 0)) =>
eapply @mult_apart_zero : typeclass_instances.
Context `{IsField F1} `{IsField F2} `{!IsSemiRingStrongPreserving (f : F1 -> F2)}.
(* Add Ring F1 : (stdlib_ring_theory F1). *)
Lemma strong_injective_preserves_0 : (forall x, x ≶ 0 -> f x ≶ 0) -> IsStrongInjective f.
apply (strong_extensionality (+ -f y)).
rewrite plus_negate_r, <-preserves_minus.
apply (strong_extensionality (+ y)).
rewrite <-simple_associativity,left_inverse,plus_0_l,plus_0_r.
(* We have the following for morphisms to non-trivial strong rings as well.
However, since we do not have an interface for strong rings, we ignore it. *)
#[export] Instance isstronginjective_field_homomorphism : IsStrongInjective f.
apply strong_injective_preserves_0.
apply mult_apart_zero_l with (f (// exist (≶0) x Ex)).
rewrite <-rings.preserves_mult.
rewrite (rings.preserves_1 (f:=f)).
Lemma preserves_recip x Px Pfx : f (// (x;Px)) = // (f x;Pfx).
apply (left_cancellation_ne_0 (.*.) (f x)).
rewrite <-rings.preserves_mult.