/-
Copyright (c) 2019 Johan Commelin. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johan Commelin

! This file was ported from Lean 3 source module linear_algebra.general_linear_group
! leanprover-community/mathlib commit 4c19a16e4b705bf135cf9a80ac18fcc99c438514
! Please do not edit these lines, except to modify the commit id
! if you have ported upstream changes.
-/
import Mathbin.Algebra.Module.Equiv

/-!
# The general linear group of linear maps

> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.

The general linear group is defined to be the group of invertible linear maps from `M` to itself.

See also `matrix.general_linear_group`

## Main definitions

* `linear_map.general_linear_group`

-/

variable (R M : Type _)

namespace LinearMap

variable [Semiring R] [AddCommMonoid M] [Module R M]

variable (R M)

#print LinearMap.GeneralLinearGroup /-
/-- The group of invertible linear maps from `M` to itself -/
@[reducible]
def GeneralLinearGroup :=
  (M →ₗ[R] M)ˣ
#align linear_map.general_linear_group LinearMap.GeneralLinearGroup
-/

namespace GeneralLinearGroup

variable {R M}

instance : CoeFun (GeneralLinearGroup R M) fun _ => M → M := by infer_instance

#print LinearMap.GeneralLinearGroup.toLinearEquiv /-
/-- An invertible linear map `f` determines an equivalence from `M` to itself. -/
def toLinearEquiv (f : GeneralLinearGroup R M) : M ≃ₗ[R] M :=
  { f.val with
    invFun := f.inv.toFun
    left_inv := fun m => show (f.inv * f.val) m = m by erw [f.inv_val] <;> simp
    right_inv := fun m => show (f.val * f.inv) m = m by erw [f.val_inv] <;> simp }
#align linear_map.general_linear_group.to_linear_equiv LinearMap.GeneralLinearGroup.toLinearEquiv
-/

#print LinearMap.GeneralLinearGroup.ofLinearEquiv /-
/-- An equivalence from `M` to itself determines an invertible linear map. -/
def ofLinearEquiv (f : M ≃ₗ[R] M) : GeneralLinearGroup R M
    where
  val := f
  inv := (f.symm : M →ₗ[R] M)
  val_inv := LinearMap.ext fun _ => f.apply_symm_apply _
  inv_val := LinearMap.ext fun _ => f.symm_apply_apply _
#align linear_map.general_linear_group.of_linear_equiv LinearMap.GeneralLinearGroup.ofLinearEquiv
-/

variable (R M)

/- warning: linear_map.general_linear_group.general_linear_equiv -> LinearMap.GeneralLinearGroup.generalLinearEquiv is a dubious translation:
lean 3 declaration is
  forall (R : Type.{u1}) (M : Type.{u2}) [_inst_1 : Semiring.{u1} R] [_inst_2 : AddCommMonoid.{u2} M] [_inst_3 : Module.{u1, u2} R M _inst_1 _inst_2], MulEquiv.{u2, u2} (LinearMap.GeneralLinearGroup.{u1, u2} R M _inst_1 _inst_2 _inst_3) (LinearEquiv.{u1, u1, u2, u2} R R _inst_1 _inst_1 (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHomInvPair.ids.{u1} R _inst_1) (RingHomInvPair.ids.{u1} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (MulOneClass.toHasMul.{u2} (LinearMap.GeneralLinearGroup.{u1, u2} R M _inst_1 _inst_2 _inst_3) (Units.mulOneClass.{u2} (LinearMap.{u1, u1, u2, u2} R R _inst_1 _inst_1 (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) M M _inst_2 _inst_2 _inst_3 _inst_3) (Module.End.monoid.{u1, u2} R M _inst_1 _inst_2 _inst_3))) (MulOneClass.toHasMul.{u2} (LinearEquiv.{u1, u1, u2, u2} R R _inst_1 _inst_1 (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHomInvPair.ids.{u1} R _inst_1) (RingHomInvPair.ids.{u1} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (Monoid.toMulOneClass.{u2} (LinearEquiv.{u1, u1, u2, u2} R R _inst_1 _inst_1 (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHomInvPair.ids.{u1} R _inst_1) (RingHomInvPair.ids.{u1} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (DivInvMonoid.toMonoid.{u2} (LinearEquiv.{u1, u1, u2, u2} R R _inst_1 _inst_1 (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHomInvPair.ids.{u1} R _inst_1) (RingHomInvPair.ids.{u1} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (Group.toDivInvMonoid.{u2} (LinearEquiv.{u1, u1, u2, u2} R R _inst_1 _inst_1 (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHomInvPair.ids.{u1} R _inst_1) (RingHomInvPair.ids.{u1} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (LinearEquiv.automorphismGroup.{u1, u2} R M _inst_1 _inst_2 _inst_3)))))
but is expected to have type
  forall (R : Type.{u1}) (M : Type.{u2}) [_inst_1 : Semiring.{u1} R] [_inst_2 : AddCommMonoid.{u2} M] [_inst_3 : Module.{u1, u2} R M _inst_1 _inst_2], MulEquiv.{u2, u2} (LinearMap.GeneralLinearGroup.{u1, u2} R M _inst_1 _inst_2 _inst_3) (LinearEquiv.{u1, u1, u2, u2} R R _inst_1 _inst_1 (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHomInvPair.ids.{u1} R _inst_1) (RingHomInvPair.ids.{u1} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (MulOneClass.toMul.{u2} (LinearMap.GeneralLinearGroup.{u1, u2} R M _inst_1 _inst_2 _inst_3) (Units.instMulOneClassUnits.{u2} (LinearMap.{u1, u1, u2, u2} R R _inst_1 _inst_1 (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) M M _inst_2 _inst_2 _inst_3 _inst_3) (Module.End.monoid.{u1, u2} R M _inst_1 _inst_2 _inst_3))) (MulOneClass.toMul.{u2} (LinearEquiv.{u1, u1, u2, u2} R R _inst_1 _inst_1 (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHomInvPair.ids.{u1} R _inst_1) (RingHomInvPair.ids.{u1} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (Monoid.toMulOneClass.{u2} (LinearEquiv.{u1, u1, u2, u2} R R _inst_1 _inst_1 (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHomInvPair.ids.{u1} R _inst_1) (RingHomInvPair.ids.{u1} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (DivInvMonoid.toMonoid.{u2} (LinearEquiv.{u1, u1, u2, u2} R R _inst_1 _inst_1 (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHomInvPair.ids.{u1} R _inst_1) (RingHomInvPair.ids.{u1} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (Group.toDivInvMonoid.{u2} (LinearEquiv.{u1, u1, u2, u2} R R _inst_1 _inst_1 (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHomInvPair.ids.{u1} R _inst_1) (RingHomInvPair.ids.{u1} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (LinearEquiv.automorphismGroup.{u1, u2} R M _inst_1 _inst_2 _inst_3)))))
Case conversion may be inaccurate. Consider using '#align linear_map.general_linear_group.general_linear_equiv LinearMap.GeneralLinearGroup.generalLinearEquivₓ'. -/
/-- The general linear group on `R` and `M` is multiplicatively equivalent to the type of linear
equivalences between `M` and itself. -/
def generalLinearEquiv : GeneralLinearGroup R M ≃* M ≃ₗ[R] M
    where
  toFun := toLinearEquiv
  invFun := ofLinearEquiv
  left_inv f := by
    ext
    rfl
  right_inv f := by
    ext
    rfl
  map_mul' x y := by
    ext
    rfl
#align linear_map.general_linear_group.general_linear_equiv LinearMap.GeneralLinearGroup.generalLinearEquiv

/- warning: linear_map.general_linear_group.general_linear_equiv_to_linear_map -> LinearMap.GeneralLinearGroup.generalLinearEquiv_to_linearMap is a dubious translation:
lean 3 declaration is
  forall (R : Type.{u1}) (M : Type.{u2}) [_inst_1 : Semiring.{u1} R] [_inst_2 : AddCommMonoid.{u2} M] [_inst_3 : Module.{u1, u2} R M _inst_1 _inst_2] (f : LinearMap.GeneralLinearGroup.{u1, u2} R M _inst_1 _inst_2 _inst_3), Eq.{succ u2} (LinearMap.{u1, u1, u2, u2} R R _inst_1 _inst_1 (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) M M _inst_2 _inst_2 _inst_3 _inst_3) ((fun (a : Type.{u2}) (b : Type.{u2}) [self : HasLiftT.{succ u2, succ u2} a b] => self.0) (LinearEquiv.{u1, u1, u2, u2} R R _inst_1 _inst_1 (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHomInvPair.ids.{u1} R _inst_1) (RingHomInvPair.ids.{u1} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (LinearMap.{u1, u1, u2, u2} R R _inst_1 _inst_1 (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) M M _inst_2 _inst_2 _inst_3 _inst_3) (HasLiftT.mk.{succ u2, succ u2} (LinearEquiv.{u1, u1, u2, u2} R R _inst_1 _inst_1 (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHomInvPair.ids.{u1} R _inst_1) (RingHomInvPair.ids.{u1} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (LinearMap.{u1, u1, u2, u2} R R _inst_1 _inst_1 (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) M M _inst_2 _inst_2 _inst_3 _inst_3) (CoeTCₓ.coe.{succ u2, succ u2} (LinearEquiv.{u1, u1, u2, u2} R R _inst_1 _inst_1 (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHomInvPair.ids.{u1} R _inst_1) (RingHomInvPair.ids.{u1} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (LinearMap.{u1, u1, u2, u2} R R _inst_1 _inst_1 (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) M M _inst_2 _inst_2 _inst_3 _inst_3) (coeBase.{succ u2, succ u2} (LinearEquiv.{u1, u1, u2, u2} R R _inst_1 _inst_1 (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHomInvPair.ids.{u1} R _inst_1) (RingHomInvPair.ids.{u1} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (LinearMap.{u1, u1, u2, u2} R R _inst_1 _inst_1 (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) M M _inst_2 _inst_2 _inst_3 _inst_3) (LinearEquiv.LinearMap.hasCoe.{u1, u1, u2, u2} R R M M _inst_1 _inst_1 _inst_2 _inst_2 _inst_3 _inst_3 (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHomInvPair.ids.{u1} R _inst_1) (RingHomInvPair.ids.{u1} R _inst_1))))) (coeFn.{succ u2, succ u2} (MulEquiv.{u2, u2} (LinearMap.GeneralLinearGroup.{u1, u2} R M _inst_1 _inst_2 _inst_3) (LinearEquiv.{u1, u1, u2, u2} R R _inst_1 _inst_1 (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHomInvPair.ids.{u1} R _inst_1) (RingHomInvPair.ids.{u1} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (MulOneClass.toHasMul.{u2} (LinearMap.GeneralLinearGroup.{u1, u2} R M _inst_1 _inst_2 _inst_3) (Units.mulOneClass.{u2} (LinearMap.{u1, u1, u2, u2} R R _inst_1 _inst_1 (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) M M _inst_2 _inst_2 _inst_3 _inst_3) (Module.End.monoid.{u1, u2} R M _inst_1 _inst_2 _inst_3))) (MulOneClass.toHasMul.{u2} (LinearEquiv.{u1, u1, u2, u2} R R _inst_1 _inst_1 (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHomInvPair.ids.{u1} R _inst_1) (RingHomInvPair.ids.{u1} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (Monoid.toMulOneClass.{u2} (LinearEquiv.{u1, u1, u2, u2} R R _inst_1 _inst_1 (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHomInvPair.ids.{u1} R _inst_1) (RingHomInvPair.ids.{u1} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (DivInvMonoid.toMonoid.{u2} (LinearEquiv.{u1, u1, u2, u2} R R _inst_1 _inst_1 (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHomInvPair.ids.{u1} R _inst_1) (RingHomInvPair.ids.{u1} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (Group.toDivInvMonoid.{u2} (LinearEquiv.{u1, u1, u2, u2} R R _inst_1 _inst_1 (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHomInvPair.ids.{u1} R _inst_1) (RingHomInvPair.ids.{u1} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (LinearEquiv.automorphismGroup.{u1, u2} R M _inst_1 _inst_2 _inst_3)))))) (fun (_x : MulEquiv.{u2, u2} (LinearMap.GeneralLinearGroup.{u1, u2} R M _inst_1 _inst_2 _inst_3) (LinearEquiv.{u1, u1, u2, u2} R R _inst_1 _inst_1 (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHomInvPair.ids.{u1} R _inst_1) (RingHomInvPair.ids.{u1} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (MulOneClass.toHasMul.{u2} (LinearMap.GeneralLinearGroup.{u1, u2} R M _inst_1 _inst_2 _inst_3) (Units.mulOneClass.{u2} (LinearMap.{u1, u1, u2, u2} R R _inst_1 _inst_1 (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) M M _inst_2 _inst_2 _inst_3 _inst_3) (Module.End.monoid.{u1, u2} R M _inst_1 _inst_2 _inst_3))) (MulOneClass.toHasMul.{u2} (LinearEquiv.{u1, u1, u2, u2} R R _inst_1 _inst_1 (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHomInvPair.ids.{u1} R _inst_1) (RingHomInvPair.ids.{u1} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (Monoid.toMulOneClass.{u2} (LinearEquiv.{u1, u1, u2, u2} R R _inst_1 _inst_1 (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHomInvPair.ids.{u1} R _inst_1) (RingHomInvPair.ids.{u1} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (DivInvMonoid.toMonoid.{u2} (LinearEquiv.{u1, u1, u2, u2} R R _inst_1 _inst_1 (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHomInvPair.ids.{u1} R _inst_1) (RingHomInvPair.ids.{u1} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (Group.toDivInvMonoid.{u2} (LinearEquiv.{u1, u1, u2, u2} R R _inst_1 _inst_1 (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHomInvPair.ids.{u1} R _inst_1) (RingHomInvPair.ids.{u1} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (LinearEquiv.automorphismGroup.{u1, u2} R M _inst_1 _inst_2 _inst_3)))))) => (LinearMap.GeneralLinearGroup.{u1, u2} R M _inst_1 _inst_2 _inst_3) -> (LinearEquiv.{u1, u1, u2, u2} R R _inst_1 _inst_1 (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHomInvPair.ids.{u1} R _inst_1) (RingHomInvPair.ids.{u1} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3)) (MulEquiv.hasCoeToFun.{u2, u2} (LinearMap.GeneralLinearGroup.{u1, u2} R M _inst_1 _inst_2 _inst_3) (LinearEquiv.{u1, u1, u2, u2} R R _inst_1 _inst_1 (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHomInvPair.ids.{u1} R _inst_1) (RingHomInvPair.ids.{u1} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (MulOneClass.toHasMul.{u2} (LinearMap.GeneralLinearGroup.{u1, u2} R M _inst_1 _inst_2 _inst_3) (Units.mulOneClass.{u2} (LinearMap.{u1, u1, u2, u2} R R _inst_1 _inst_1 (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) M M _inst_2 _inst_2 _inst_3 _inst_3) (Module.End.monoid.{u1, u2} R M _inst_1 _inst_2 _inst_3))) (MulOneClass.toHasMul.{u2} (LinearEquiv.{u1, u1, u2, u2} R R _inst_1 _inst_1 (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHomInvPair.ids.{u1} R _inst_1) (RingHomInvPair.ids.{u1} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (Monoid.toMulOneClass.{u2} (LinearEquiv.{u1, u1, u2, u2} R R _inst_1 _inst_1 (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHomInvPair.ids.{u1} R _inst_1) (RingHomInvPair.ids.{u1} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (DivInvMonoid.toMonoid.{u2} (LinearEquiv.{u1, u1, u2, u2} R R _inst_1 _inst_1 (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHomInvPair.ids.{u1} R _inst_1) (RingHomInvPair.ids.{u1} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (Group.toDivInvMonoid.{u2} (LinearEquiv.{u1, u1, u2, u2} R R _inst_1 _inst_1 (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHomInvPair.ids.{u1} R _inst_1) (RingHomInvPair.ids.{u1} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (LinearEquiv.automorphismGroup.{u1, u2} R M _inst_1 _inst_2 _inst_3)))))) (LinearMap.GeneralLinearGroup.generalLinearEquiv.{u1, u2} R M _inst_1 _inst_2 _inst_3) f)) ((fun (a : Type.{u2}) (b : Type.{u2}) [self : HasLiftT.{succ u2, succ u2} a b] => self.0) (LinearMap.GeneralLinearGroup.{u1, u2} R M _inst_1 _inst_2 _inst_3) (LinearMap.{u1, u1, u2, u2} R R _inst_1 _inst_1 (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) M M _inst_2 _inst_2 _inst_3 _inst_3) (HasLiftT.mk.{succ u2, succ u2} (LinearMap.GeneralLinearGroup.{u1, u2} R M _inst_1 _inst_2 _inst_3) (LinearMap.{u1, u1, u2, u2} R R _inst_1 _inst_1 (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) M M _inst_2 _inst_2 _inst_3 _inst_3) (CoeTCₓ.coe.{succ u2, succ u2} (LinearMap.GeneralLinearGroup.{u1, u2} R M _inst_1 _inst_2 _inst_3) (LinearMap.{u1, u1, u2, u2} R R _inst_1 _inst_1 (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) M M _inst_2 _inst_2 _inst_3 _inst_3) (coeBase.{succ u2, succ u2} (LinearMap.GeneralLinearGroup.{u1, u2} R M _inst_1 _inst_2 _inst_3) (LinearMap.{u1, u1, u2, u2} R R _inst_1 _inst_1 (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) M M _inst_2 _inst_2 _inst_3 _inst_3) (Units.hasCoe.{u2} (LinearMap.{u1, u1, u2, u2} R R _inst_1 _inst_1 (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) M M _inst_2 _inst_2 _inst_3 _inst_3) (Module.End.monoid.{u1, u2} R M _inst_1 _inst_2 _inst_3))))) f)
but is expected to have type
  forall (R : Type.{u2}) (M : Type.{u1}) [_inst_1 : Semiring.{u2} R] [_inst_2 : AddCommMonoid.{u1} M] [_inst_3 : Module.{u2, u1} R M _inst_1 _inst_2] (f : LinearMap.GeneralLinearGroup.{u2, u1} R M _inst_1 _inst_2 _inst_3), Eq.{succ u1} (LinearMap.{u2, u2, u1, u1} R R _inst_1 _inst_1 (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) M M _inst_2 _inst_2 _inst_3 _inst_3) (LinearEquiv.toLinearMap.{u2, u2, u1, u1} R R _inst_1 _inst_1 (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHomInvPair.ids.{u2} R _inst_1) (RingHomInvPair.ids.{u2} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3 (FunLike.coe.{succ u1, succ u1, succ u1} (MulEquiv.{u1, u1} (LinearMap.GeneralLinearGroup.{u2, u1} R M _inst_1 _inst_2 _inst_3) (LinearEquiv.{u2, u2, u1, u1} R R _inst_1 _inst_1 (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHomInvPair.ids.{u2} R _inst_1) (RingHomInvPair.ids.{u2} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (MulOneClass.toMul.{u1} (LinearMap.GeneralLinearGroup.{u2, u1} R M _inst_1 _inst_2 _inst_3) (Units.instMulOneClassUnits.{u1} (LinearMap.{u2, u2, u1, u1} R R _inst_1 _inst_1 (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) M M _inst_2 _inst_2 _inst_3 _inst_3) (Module.End.monoid.{u2, u1} R M _inst_1 _inst_2 _inst_3))) (MulOneClass.toMul.{u1} (LinearEquiv.{u2, u2, u1, u1} R R _inst_1 _inst_1 (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHomInvPair.ids.{u2} R _inst_1) (RingHomInvPair.ids.{u2} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (Monoid.toMulOneClass.{u1} (LinearEquiv.{u2, u2, u1, u1} R R _inst_1 _inst_1 (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHomInvPair.ids.{u2} R _inst_1) (RingHomInvPair.ids.{u2} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (DivInvMonoid.toMonoid.{u1} (LinearEquiv.{u2, u2, u1, u1} R R _inst_1 _inst_1 (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHomInvPair.ids.{u2} R _inst_1) (RingHomInvPair.ids.{u2} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (Group.toDivInvMonoid.{u1} (LinearEquiv.{u2, u2, u1, u1} R R _inst_1 _inst_1 (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHomInvPair.ids.{u2} R _inst_1) (RingHomInvPair.ids.{u2} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (LinearEquiv.automorphismGroup.{u2, u1} R M _inst_1 _inst_2 _inst_3)))))) (LinearMap.GeneralLinearGroup.{u2, u1} R M _inst_1 _inst_2 _inst_3) (fun (_x : LinearMap.GeneralLinearGroup.{u2, u1} R M _inst_1 _inst_2 _inst_3) => (fun (x._@.Mathlib.Data.FunLike.Embedding._hyg.19 : LinearMap.GeneralLinearGroup.{u2, u1} R M _inst_1 _inst_2 _inst_3) => LinearEquiv.{u2, u2, u1, u1} R R _inst_1 _inst_1 (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHomInvPair.ids.{u2} R _inst_1) (RingHomInvPair.ids.{u2} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) _x) (EmbeddingLike.toFunLike.{succ u1, succ u1, succ u1} (MulEquiv.{u1, u1} (LinearMap.GeneralLinearGroup.{u2, u1} R M _inst_1 _inst_2 _inst_3) (LinearEquiv.{u2, u2, u1, u1} R R _inst_1 _inst_1 (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHomInvPair.ids.{u2} R _inst_1) (RingHomInvPair.ids.{u2} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (MulOneClass.toMul.{u1} (LinearMap.GeneralLinearGroup.{u2, u1} R M _inst_1 _inst_2 _inst_3) (Units.instMulOneClassUnits.{u1} (LinearMap.{u2, u2, u1, u1} R R _inst_1 _inst_1 (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) M M _inst_2 _inst_2 _inst_3 _inst_3) (Module.End.monoid.{u2, u1} R M _inst_1 _inst_2 _inst_3))) (MulOneClass.toMul.{u1} (LinearEquiv.{u2, u2, u1, u1} R R _inst_1 _inst_1 (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHomInvPair.ids.{u2} R _inst_1) (RingHomInvPair.ids.{u2} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (Monoid.toMulOneClass.{u1} (LinearEquiv.{u2, u2, u1, u1} R R _inst_1 _inst_1 (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHomInvPair.ids.{u2} R _inst_1) (RingHomInvPair.ids.{u2} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (DivInvMonoid.toMonoid.{u1} (LinearEquiv.{u2, u2, u1, u1} R R _inst_1 _inst_1 (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHomInvPair.ids.{u2} R _inst_1) (RingHomInvPair.ids.{u2} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (Group.toDivInvMonoid.{u1} (LinearEquiv.{u2, u2, u1, u1} R R _inst_1 _inst_1 (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHomInvPair.ids.{u2} R _inst_1) (RingHomInvPair.ids.{u2} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (LinearEquiv.automorphismGroup.{u2, u1} R M _inst_1 _inst_2 _inst_3)))))) (LinearMap.GeneralLinearGroup.{u2, u1} R M _inst_1 _inst_2 _inst_3) (LinearEquiv.{u2, u2, u1, u1} R R _inst_1 _inst_1 (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHomInvPair.ids.{u2} R _inst_1) (RingHomInvPair.ids.{u2} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (EquivLike.toEmbeddingLike.{succ u1, succ u1, succ u1} (MulEquiv.{u1, u1} (LinearMap.GeneralLinearGroup.{u2, u1} R M _inst_1 _inst_2 _inst_3) (LinearEquiv.{u2, u2, u1, u1} R R _inst_1 _inst_1 (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHomInvPair.ids.{u2} R _inst_1) (RingHomInvPair.ids.{u2} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (MulOneClass.toMul.{u1} (LinearMap.GeneralLinearGroup.{u2, u1} R M _inst_1 _inst_2 _inst_3) (Units.instMulOneClassUnits.{u1} (LinearMap.{u2, u2, u1, u1} R R _inst_1 _inst_1 (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) M M _inst_2 _inst_2 _inst_3 _inst_3) (Module.End.monoid.{u2, u1} R M _inst_1 _inst_2 _inst_3))) (MulOneClass.toMul.{u1} (LinearEquiv.{u2, u2, u1, u1} R R _inst_1 _inst_1 (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHomInvPair.ids.{u2} R _inst_1) (RingHomInvPair.ids.{u2} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (Monoid.toMulOneClass.{u1} (LinearEquiv.{u2, u2, u1, u1} R R _inst_1 _inst_1 (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHomInvPair.ids.{u2} R _inst_1) (RingHomInvPair.ids.{u2} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (DivInvMonoid.toMonoid.{u1} (LinearEquiv.{u2, u2, u1, u1} R R _inst_1 _inst_1 (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHomInvPair.ids.{u2} R _inst_1) (RingHomInvPair.ids.{u2} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (Group.toDivInvMonoid.{u1} (LinearEquiv.{u2, u2, u1, u1} R R _inst_1 _inst_1 (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHomInvPair.ids.{u2} R _inst_1) (RingHomInvPair.ids.{u2} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (LinearEquiv.automorphismGroup.{u2, u1} R M _inst_1 _inst_2 _inst_3)))))) (LinearMap.GeneralLinearGroup.{u2, u1} R M _inst_1 _inst_2 _inst_3) (LinearEquiv.{u2, u2, u1, u1} R R _inst_1 _inst_1 (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHomInvPair.ids.{u2} R _inst_1) (RingHomInvPair.ids.{u2} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (MulEquivClass.toEquivLike.{u1, u1, u1} (MulEquiv.{u1, u1} (LinearMap.GeneralLinearGroup.{u2, u1} R M _inst_1 _inst_2 _inst_3) (LinearEquiv.{u2, u2, u1, u1} R R _inst_1 _inst_1 (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHomInvPair.ids.{u2} R _inst_1) (RingHomInvPair.ids.{u2} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (MulOneClass.toMul.{u1} (LinearMap.GeneralLinearGroup.{u2, u1} R M _inst_1 _inst_2 _inst_3) (Units.instMulOneClassUnits.{u1} (LinearMap.{u2, u2, u1, u1} R R _inst_1 _inst_1 (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) M M _inst_2 _inst_2 _inst_3 _inst_3) (Module.End.monoid.{u2, u1} R M _inst_1 _inst_2 _inst_3))) (MulOneClass.toMul.{u1} (LinearEquiv.{u2, u2, u1, u1} R R _inst_1 _inst_1 (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHomInvPair.ids.{u2} R _inst_1) (RingHomInvPair.ids.{u2} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (Monoid.toMulOneClass.{u1} (LinearEquiv.{u2, u2, u1, u1} R R _inst_1 _inst_1 (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHomInvPair.ids.{u2} R _inst_1) (RingHomInvPair.ids.{u2} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (DivInvMonoid.toMonoid.{u1} (LinearEquiv.{u2, u2, u1, u1} R R _inst_1 _inst_1 (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHomInvPair.ids.{u2} R _inst_1) (RingHomInvPair.ids.{u2} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (Group.toDivInvMonoid.{u1} (LinearEquiv.{u2, u2, u1, u1} R R _inst_1 _inst_1 (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHomInvPair.ids.{u2} R _inst_1) (RingHomInvPair.ids.{u2} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (LinearEquiv.automorphismGroup.{u2, u1} R M _inst_1 _inst_2 _inst_3)))))) (LinearMap.GeneralLinearGroup.{u2, u1} R M _inst_1 _inst_2 _inst_3) (LinearEquiv.{u2, u2, u1, u1} R R _inst_1 _inst_1 (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHomInvPair.ids.{u2} R _inst_1) (RingHomInvPair.ids.{u2} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (MulOneClass.toMul.{u1} (LinearMap.GeneralLinearGroup.{u2, u1} R M _inst_1 _inst_2 _inst_3) (Units.instMulOneClassUnits.{u1} (LinearMap.{u2, u2, u1, u1} R R _inst_1 _inst_1 (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) M M _inst_2 _inst_2 _inst_3 _inst_3) (Module.End.monoid.{u2, u1} R M _inst_1 _inst_2 _inst_3))) (MulOneClass.toMul.{u1} (LinearEquiv.{u2, u2, u1, u1} R R _inst_1 _inst_1 (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHomInvPair.ids.{u2} R _inst_1) (RingHomInvPair.ids.{u2} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (Monoid.toMulOneClass.{u1} (LinearEquiv.{u2, u2, u1, u1} R R _inst_1 _inst_1 (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHomInvPair.ids.{u2} R _inst_1) (RingHomInvPair.ids.{u2} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (DivInvMonoid.toMonoid.{u1} (LinearEquiv.{u2, u2, u1, u1} R R _inst_1 _inst_1 (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHomInvPair.ids.{u2} R _inst_1) (RingHomInvPair.ids.{u2} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (Group.toDivInvMonoid.{u1} (LinearEquiv.{u2, u2, u1, u1} R R _inst_1 _inst_1 (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHomInvPair.ids.{u2} R _inst_1) (RingHomInvPair.ids.{u2} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (LinearEquiv.automorphismGroup.{u2, u1} R M _inst_1 _inst_2 _inst_3))))) (MulEquiv.instMulEquivClassMulEquiv.{u1, u1} (LinearMap.GeneralLinearGroup.{u2, u1} R M _inst_1 _inst_2 _inst_3) (LinearEquiv.{u2, u2, u1, u1} R R _inst_1 _inst_1 (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHomInvPair.ids.{u2} R _inst_1) (RingHomInvPair.ids.{u2} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (MulOneClass.toMul.{u1} (LinearMap.GeneralLinearGroup.{u2, u1} R M _inst_1 _inst_2 _inst_3) (Units.instMulOneClassUnits.{u1} (LinearMap.{u2, u2, u1, u1} R R _inst_1 _inst_1 (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) M M _inst_2 _inst_2 _inst_3 _inst_3) (Module.End.monoid.{u2, u1} R M _inst_1 _inst_2 _inst_3))) (MulOneClass.toMul.{u1} (LinearEquiv.{u2, u2, u1, u1} R R _inst_1 _inst_1 (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHomInvPair.ids.{u2} R _inst_1) (RingHomInvPair.ids.{u2} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (Monoid.toMulOneClass.{u1} (LinearEquiv.{u2, u2, u1, u1} R R _inst_1 _inst_1 (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHomInvPair.ids.{u2} R _inst_1) (RingHomInvPair.ids.{u2} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (DivInvMonoid.toMonoid.{u1} (LinearEquiv.{u2, u2, u1, u1} R R _inst_1 _inst_1 (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHomInvPair.ids.{u2} R _inst_1) (RingHomInvPair.ids.{u2} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (Group.toDivInvMonoid.{u1} (LinearEquiv.{u2, u2, u1, u1} R R _inst_1 _inst_1 (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHomInvPair.ids.{u2} R _inst_1) (RingHomInvPair.ids.{u2} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (LinearEquiv.automorphismGroup.{u2, u1} R M _inst_1 _inst_2 _inst_3))))))))) (LinearMap.GeneralLinearGroup.generalLinearEquiv.{u2, u1} R M _inst_1 _inst_2 _inst_3) f)) (Units.val.{u1} (LinearMap.{u2, u2, u1, u1} R R _inst_1 _inst_1 (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) M M _inst_2 _inst_2 _inst_3 _inst_3) (Module.End.monoid.{u2, u1} R M _inst_1 _inst_2 _inst_3) f)
Case conversion may be inaccurate. Consider using '#align linear_map.general_linear_group.general_linear_equiv_to_linear_map LinearMap.GeneralLinearGroup.generalLinearEquiv_to_linearMapₓ'. -/
@[simp]
theorem generalLinearEquiv_to_linearMap (f : GeneralLinearGroup R M) :
    (generalLinearEquiv R M f : M →ₗ[R] M) = f :=
  by
  ext
  rfl
#align linear_map.general_linear_group.general_linear_equiv_to_linear_map LinearMap.GeneralLinearGroup.generalLinearEquiv_to_linearMap

/- warning: linear_map.general_linear_group.coe_fn_general_linear_equiv -> LinearMap.GeneralLinearGroup.coeFn_generalLinearEquiv is a dubious translation:
lean 3 declaration is
  forall (R : Type.{u1}) (M : Type.{u2}) [_inst_1 : Semiring.{u1} R] [_inst_2 : AddCommMonoid.{u2} M] [_inst_3 : Module.{u1, u2} R M _inst_1 _inst_2] (f : LinearMap.GeneralLinearGroup.{u1, u2} R M _inst_1 _inst_2 _inst_3), Eq.{succ u2} (M -> M) (coeFn.{succ u2, succ u2} (LinearEquiv.{u1, u1, u2, u2} R R _inst_1 _inst_1 (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHomInvPair.ids.{u1} R _inst_1) (RingHomInvPair.ids.{u1} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (fun (_x : LinearEquiv.{u1, u1, u2, u2} R R _inst_1 _inst_1 (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHomInvPair.ids.{u1} R _inst_1) (RingHomInvPair.ids.{u1} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) => M -> M) (LinearEquiv.hasCoeToFun.{u1, u1, u2, u2} R R M M _inst_1 _inst_1 _inst_2 _inst_2 _inst_3 _inst_3 (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHomInvPair.ids.{u1} R _inst_1) (RingHomInvPair.ids.{u1} R _inst_1)) (coeFn.{succ u2, succ u2} (MulEquiv.{u2, u2} (LinearMap.GeneralLinearGroup.{u1, u2} R M _inst_1 _inst_2 _inst_3) (LinearEquiv.{u1, u1, u2, u2} R R _inst_1 _inst_1 (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHomInvPair.ids.{u1} R _inst_1) (RingHomInvPair.ids.{u1} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (MulOneClass.toHasMul.{u2} (LinearMap.GeneralLinearGroup.{u1, u2} R M _inst_1 _inst_2 _inst_3) (Units.mulOneClass.{u2} (LinearMap.{u1, u1, u2, u2} R R _inst_1 _inst_1 (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) M M _inst_2 _inst_2 _inst_3 _inst_3) (Module.End.monoid.{u1, u2} R M _inst_1 _inst_2 _inst_3))) (MulOneClass.toHasMul.{u2} (LinearEquiv.{u1, u1, u2, u2} R R _inst_1 _inst_1 (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHomInvPair.ids.{u1} R _inst_1) (RingHomInvPair.ids.{u1} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (Monoid.toMulOneClass.{u2} (LinearEquiv.{u1, u1, u2, u2} R R _inst_1 _inst_1 (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHomInvPair.ids.{u1} R _inst_1) (RingHomInvPair.ids.{u1} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (DivInvMonoid.toMonoid.{u2} (LinearEquiv.{u1, u1, u2, u2} R R _inst_1 _inst_1 (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHomInvPair.ids.{u1} R _inst_1) (RingHomInvPair.ids.{u1} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (Group.toDivInvMonoid.{u2} (LinearEquiv.{u1, u1, u2, u2} R R _inst_1 _inst_1 (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHomInvPair.ids.{u1} R _inst_1) (RingHomInvPair.ids.{u1} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (LinearEquiv.automorphismGroup.{u1, u2} R M _inst_1 _inst_2 _inst_3)))))) (fun (_x : MulEquiv.{u2, u2} (LinearMap.GeneralLinearGroup.{u1, u2} R M _inst_1 _inst_2 _inst_3) (LinearEquiv.{u1, u1, u2, u2} R R _inst_1 _inst_1 (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHomInvPair.ids.{u1} R _inst_1) (RingHomInvPair.ids.{u1} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (MulOneClass.toHasMul.{u2} (LinearMap.GeneralLinearGroup.{u1, u2} R M _inst_1 _inst_2 _inst_3) (Units.mulOneClass.{u2} (LinearMap.{u1, u1, u2, u2} R R _inst_1 _inst_1 (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) M M _inst_2 _inst_2 _inst_3 _inst_3) (Module.End.monoid.{u1, u2} R M _inst_1 _inst_2 _inst_3))) (MulOneClass.toHasMul.{u2} (LinearEquiv.{u1, u1, u2, u2} R R _inst_1 _inst_1 (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHomInvPair.ids.{u1} R _inst_1) (RingHomInvPair.ids.{u1} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (Monoid.toMulOneClass.{u2} (LinearEquiv.{u1, u1, u2, u2} R R _inst_1 _inst_1 (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHomInvPair.ids.{u1} R _inst_1) (RingHomInvPair.ids.{u1} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (DivInvMonoid.toMonoid.{u2} (LinearEquiv.{u1, u1, u2, u2} R R _inst_1 _inst_1 (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHomInvPair.ids.{u1} R _inst_1) (RingHomInvPair.ids.{u1} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (Group.toDivInvMonoid.{u2} (LinearEquiv.{u1, u1, u2, u2} R R _inst_1 _inst_1 (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHomInvPair.ids.{u1} R _inst_1) (RingHomInvPair.ids.{u1} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (LinearEquiv.automorphismGroup.{u1, u2} R M _inst_1 _inst_2 _inst_3)))))) => (LinearMap.GeneralLinearGroup.{u1, u2} R M _inst_1 _inst_2 _inst_3) -> (LinearEquiv.{u1, u1, u2, u2} R R _inst_1 _inst_1 (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHomInvPair.ids.{u1} R _inst_1) (RingHomInvPair.ids.{u1} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3)) (MulEquiv.hasCoeToFun.{u2, u2} (LinearMap.GeneralLinearGroup.{u1, u2} R M _inst_1 _inst_2 _inst_3) (LinearEquiv.{u1, u1, u2, u2} R R _inst_1 _inst_1 (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHomInvPair.ids.{u1} R _inst_1) (RingHomInvPair.ids.{u1} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (MulOneClass.toHasMul.{u2} (LinearMap.GeneralLinearGroup.{u1, u2} R M _inst_1 _inst_2 _inst_3) (Units.mulOneClass.{u2} (LinearMap.{u1, u1, u2, u2} R R _inst_1 _inst_1 (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) M M _inst_2 _inst_2 _inst_3 _inst_3) (Module.End.monoid.{u1, u2} R M _inst_1 _inst_2 _inst_3))) (MulOneClass.toHasMul.{u2} (LinearEquiv.{u1, u1, u2, u2} R R _inst_1 _inst_1 (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHomInvPair.ids.{u1} R _inst_1) (RingHomInvPair.ids.{u1} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (Monoid.toMulOneClass.{u2} (LinearEquiv.{u1, u1, u2, u2} R R _inst_1 _inst_1 (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHomInvPair.ids.{u1} R _inst_1) (RingHomInvPair.ids.{u1} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (DivInvMonoid.toMonoid.{u2} (LinearEquiv.{u1, u1, u2, u2} R R _inst_1 _inst_1 (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHomInvPair.ids.{u1} R _inst_1) (RingHomInvPair.ids.{u1} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (Group.toDivInvMonoid.{u2} (LinearEquiv.{u1, u1, u2, u2} R R _inst_1 _inst_1 (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHom.id.{u1} R (Semiring.toNonAssocSemiring.{u1} R _inst_1)) (RingHomInvPair.ids.{u1} R _inst_1) (RingHomInvPair.ids.{u1} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (LinearEquiv.automorphismGroup.{u1, u2} R M _inst_1 _inst_2 _inst_3)))))) (LinearMap.GeneralLinearGroup.generalLinearEquiv.{u1, u2} R M _inst_1 _inst_2 _inst_3) f)) (coeFn.{succ u2, succ u2} (LinearMap.GeneralLinearGroup.{u1, u2} R M _inst_1 _inst_2 _inst_3) (fun (_x : LinearMap.GeneralLinearGroup.{u1, u2} R M _inst_1 _inst_2 _inst_3) => M -> M) (LinearMap.GeneralLinearGroup.hasCoeToFun.{u1, u2} R M _inst_1 _inst_2 _inst_3) f)
but is expected to have type
  forall (R : Type.{u2}) (M : Type.{u1}) [_inst_1 : Semiring.{u2} R] [_inst_2 : AddCommMonoid.{u1} M] [_inst_3 : Module.{u2, u1} R M _inst_1 _inst_2] (f : LinearMap.GeneralLinearGroup.{u2, u1} R M _inst_1 _inst_2 _inst_3), Eq.{succ u1} (forall (ᾰ : M), (fun (x._@.Mathlib.Algebra.Hom.GroupAction._hyg.2186 : M) => M) ᾰ) (FunLike.coe.{succ u1, succ u1, succ u1} ((fun (x._@.Mathlib.Data.FunLike.Embedding._hyg.19 : LinearMap.GeneralLinearGroup.{u2, u1} R M _inst_1 _inst_2 _inst_3) => LinearEquiv.{u2, u2, u1, u1} R R _inst_1 _inst_1 (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHomInvPair.ids.{u2} R _inst_1) (RingHomInvPair.ids.{u2} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) f) M (fun (_x : M) => (fun (x._@.Mathlib.Algebra.Hom.GroupAction._hyg.2186 : M) => M) _x) (SMulHomClass.toFunLike.{u1, u2, u1, u1} ((fun (x._@.Mathlib.Data.FunLike.Embedding._hyg.19 : LinearMap.GeneralLinearGroup.{u2, u1} R M _inst_1 _inst_2 _inst_3) => LinearEquiv.{u2, u2, u1, u1} R R _inst_1 _inst_1 (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHomInvPair.ids.{u2} R _inst_1) (RingHomInvPair.ids.{u2} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) f) R M M (SMulZeroClass.toSMul.{u2, u1} R M (AddMonoid.toZero.{u1} M (AddCommMonoid.toAddMonoid.{u1} M _inst_2)) (DistribSMul.toSMulZeroClass.{u2, u1} R M (AddMonoid.toAddZeroClass.{u1} M (AddCommMonoid.toAddMonoid.{u1} M _inst_2)) (DistribMulAction.toDistribSMul.{u2, u1} R M (MonoidWithZero.toMonoid.{u2} R (Semiring.toMonoidWithZero.{u2} R _inst_1)) (AddCommMonoid.toAddMonoid.{u1} M _inst_2) (Module.toDistribMulAction.{u2, u1} R M _inst_1 _inst_2 _inst_3)))) (SMulZeroClass.toSMul.{u2, u1} R M (AddMonoid.toZero.{u1} M (AddCommMonoid.toAddMonoid.{u1} M _inst_2)) (DistribSMul.toSMulZeroClass.{u2, u1} R M (AddMonoid.toAddZeroClass.{u1} M (AddCommMonoid.toAddMonoid.{u1} M _inst_2)) (DistribMulAction.toDistribSMul.{u2, u1} R M (MonoidWithZero.toMonoid.{u2} R (Semiring.toMonoidWithZero.{u2} R _inst_1)) (AddCommMonoid.toAddMonoid.{u1} M _inst_2) (Module.toDistribMulAction.{u2, u1} R M _inst_1 _inst_2 _inst_3)))) (DistribMulActionHomClass.toSMulHomClass.{u1, u2, u1, u1} ((fun (x._@.Mathlib.Data.FunLike.Embedding._hyg.19 : LinearMap.GeneralLinearGroup.{u2, u1} R M _inst_1 _inst_2 _inst_3) => LinearEquiv.{u2, u2, u1, u1} R R _inst_1 _inst_1 (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHomInvPair.ids.{u2} R _inst_1) (RingHomInvPair.ids.{u2} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) f) R M M (MonoidWithZero.toMonoid.{u2} R (Semiring.toMonoidWithZero.{u2} R _inst_1)) (AddCommMonoid.toAddMonoid.{u1} M _inst_2) (AddCommMonoid.toAddMonoid.{u1} M _inst_2) (Module.toDistribMulAction.{u2, u1} R M _inst_1 _inst_2 _inst_3) (Module.toDistribMulAction.{u2, u1} R M _inst_1 _inst_2 _inst_3) (SemilinearMapClass.distribMulActionHomClass.{u2, u1, u1, u1} R M M ((fun (x._@.Mathlib.Data.FunLike.Embedding._hyg.19 : LinearMap.GeneralLinearGroup.{u2, u1} R M _inst_1 _inst_2 _inst_3) => LinearEquiv.{u2, u2, u1, u1} R R _inst_1 _inst_1 (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHomInvPair.ids.{u2} R _inst_1) (RingHomInvPair.ids.{u2} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) f) _inst_1 _inst_2 _inst_2 _inst_3 _inst_3 (SemilinearEquivClass.instSemilinearMapClass.{u2, u2, u1, u1, u1} R R M M ((fun (x._@.Mathlib.Data.FunLike.Embedding._hyg.19 : LinearMap.GeneralLinearGroup.{u2, u1} R M _inst_1 _inst_2 _inst_3) => LinearEquiv.{u2, u2, u1, u1} R R _inst_1 _inst_1 (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHomInvPair.ids.{u2} R _inst_1) (RingHomInvPair.ids.{u2} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) f) _inst_1 _inst_1 _inst_2 _inst_2 _inst_3 _inst_3 (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHomInvPair.ids.{u2} R _inst_1) (RingHomInvPair.ids.{u2} R _inst_1) (LinearEquiv.instSemilinearEquivClassLinearEquiv.{u2, u2, u1, u1} R R M M _inst_1 _inst_1 _inst_2 _inst_2 _inst_3 _inst_3 (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHomInvPair.ids.{u2} R _inst_1) (RingHomInvPair.ids.{u2} R _inst_1)))))) (FunLike.coe.{succ u1, succ u1, succ u1} (MulEquiv.{u1, u1} (LinearMap.GeneralLinearGroup.{u2, u1} R M _inst_1 _inst_2 _inst_3) (LinearEquiv.{u2, u2, u1, u1} R R _inst_1 _inst_1 (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHomInvPair.ids.{u2} R _inst_1) (RingHomInvPair.ids.{u2} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (MulOneClass.toMul.{u1} (LinearMap.GeneralLinearGroup.{u2, u1} R M _inst_1 _inst_2 _inst_3) (Units.instMulOneClassUnits.{u1} (LinearMap.{u2, u2, u1, u1} R R _inst_1 _inst_1 (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) M M _inst_2 _inst_2 _inst_3 _inst_3) (Module.End.monoid.{u2, u1} R M _inst_1 _inst_2 _inst_3))) (MulOneClass.toMul.{u1} (LinearEquiv.{u2, u2, u1, u1} R R _inst_1 _inst_1 (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHomInvPair.ids.{u2} R _inst_1) (RingHomInvPair.ids.{u2} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (Monoid.toMulOneClass.{u1} (LinearEquiv.{u2, u2, u1, u1} R R _inst_1 _inst_1 (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHomInvPair.ids.{u2} R _inst_1) (RingHomInvPair.ids.{u2} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (DivInvMonoid.toMonoid.{u1} (LinearEquiv.{u2, u2, u1, u1} R R _inst_1 _inst_1 (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHomInvPair.ids.{u2} R _inst_1) (RingHomInvPair.ids.{u2} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (Group.toDivInvMonoid.{u1} (LinearEquiv.{u2, u2, u1, u1} R R _inst_1 _inst_1 (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHomInvPair.ids.{u2} R _inst_1) (RingHomInvPair.ids.{u2} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (LinearEquiv.automorphismGroup.{u2, u1} R M _inst_1 _inst_2 _inst_3)))))) (LinearMap.GeneralLinearGroup.{u2, u1} R M _inst_1 _inst_2 _inst_3) (fun (_x : LinearMap.GeneralLinearGroup.{u2, u1} R M _inst_1 _inst_2 _inst_3) => (fun (x._@.Mathlib.Data.FunLike.Embedding._hyg.19 : LinearMap.GeneralLinearGroup.{u2, u1} R M _inst_1 _inst_2 _inst_3) => LinearEquiv.{u2, u2, u1, u1} R R _inst_1 _inst_1 (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHomInvPair.ids.{u2} R _inst_1) (RingHomInvPair.ids.{u2} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) _x) (EmbeddingLike.toFunLike.{succ u1, succ u1, succ u1} (MulEquiv.{u1, u1} (LinearMap.GeneralLinearGroup.{u2, u1} R M _inst_1 _inst_2 _inst_3) (LinearEquiv.{u2, u2, u1, u1} R R _inst_1 _inst_1 (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHomInvPair.ids.{u2} R _inst_1) (RingHomInvPair.ids.{u2} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (MulOneClass.toMul.{u1} (LinearMap.GeneralLinearGroup.{u2, u1} R M _inst_1 _inst_2 _inst_3) (Units.instMulOneClassUnits.{u1} (LinearMap.{u2, u2, u1, u1} R R _inst_1 _inst_1 (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) M M _inst_2 _inst_2 _inst_3 _inst_3) (Module.End.monoid.{u2, u1} R M _inst_1 _inst_2 _inst_3))) (MulOneClass.toMul.{u1} (LinearEquiv.{u2, u2, u1, u1} R R _inst_1 _inst_1 (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHomInvPair.ids.{u2} R _inst_1) (RingHomInvPair.ids.{u2} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (Monoid.toMulOneClass.{u1} (LinearEquiv.{u2, u2, u1, u1} R R _inst_1 _inst_1 (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHomInvPair.ids.{u2} R _inst_1) (RingHomInvPair.ids.{u2} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (DivInvMonoid.toMonoid.{u1} (LinearEquiv.{u2, u2, u1, u1} R R _inst_1 _inst_1 (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHomInvPair.ids.{u2} R _inst_1) (RingHomInvPair.ids.{u2} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (Group.toDivInvMonoid.{u1} (LinearEquiv.{u2, u2, u1, u1} R R _inst_1 _inst_1 (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHomInvPair.ids.{u2} R _inst_1) (RingHomInvPair.ids.{u2} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (LinearEquiv.automorphismGroup.{u2, u1} R M _inst_1 _inst_2 _inst_3)))))) (LinearMap.GeneralLinearGroup.{u2, u1} R M _inst_1 _inst_2 _inst_3) (LinearEquiv.{u2, u2, u1, u1} R R _inst_1 _inst_1 (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHomInvPair.ids.{u2} R _inst_1) (RingHomInvPair.ids.{u2} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (EquivLike.toEmbeddingLike.{succ u1, succ u1, succ u1} (MulEquiv.{u1, u1} (LinearMap.GeneralLinearGroup.{u2, u1} R M _inst_1 _inst_2 _inst_3) (LinearEquiv.{u2, u2, u1, u1} R R _inst_1 _inst_1 (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHomInvPair.ids.{u2} R _inst_1) (RingHomInvPair.ids.{u2} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (MulOneClass.toMul.{u1} (LinearMap.GeneralLinearGroup.{u2, u1} R M _inst_1 _inst_2 _inst_3) (Units.instMulOneClassUnits.{u1} (LinearMap.{u2, u2, u1, u1} R R _inst_1 _inst_1 (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) M M _inst_2 _inst_2 _inst_3 _inst_3) (Module.End.monoid.{u2, u1} R M _inst_1 _inst_2 _inst_3))) (MulOneClass.toMul.{u1} (LinearEquiv.{u2, u2, u1, u1} R R _inst_1 _inst_1 (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHomInvPair.ids.{u2} R _inst_1) (RingHomInvPair.ids.{u2} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (Monoid.toMulOneClass.{u1} (LinearEquiv.{u2, u2, u1, u1} R R _inst_1 _inst_1 (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHomInvPair.ids.{u2} R _inst_1) (RingHomInvPair.ids.{u2} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (DivInvMonoid.toMonoid.{u1} (LinearEquiv.{u2, u2, u1, u1} R R _inst_1 _inst_1 (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHomInvPair.ids.{u2} R _inst_1) (RingHomInvPair.ids.{u2} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (Group.toDivInvMonoid.{u1} (LinearEquiv.{u2, u2, u1, u1} R R _inst_1 _inst_1 (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHomInvPair.ids.{u2} R _inst_1) (RingHomInvPair.ids.{u2} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (LinearEquiv.automorphismGroup.{u2, u1} R M _inst_1 _inst_2 _inst_3)))))) (LinearMap.GeneralLinearGroup.{u2, u1} R M _inst_1 _inst_2 _inst_3) (LinearEquiv.{u2, u2, u1, u1} R R _inst_1 _inst_1 (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHomInvPair.ids.{u2} R _inst_1) (RingHomInvPair.ids.{u2} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (MulEquivClass.toEquivLike.{u1, u1, u1} (MulEquiv.{u1, u1} (LinearMap.GeneralLinearGroup.{u2, u1} R M _inst_1 _inst_2 _inst_3) (LinearEquiv.{u2, u2, u1, u1} R R _inst_1 _inst_1 (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHomInvPair.ids.{u2} R _inst_1) (RingHomInvPair.ids.{u2} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (MulOneClass.toMul.{u1} (LinearMap.GeneralLinearGroup.{u2, u1} R M _inst_1 _inst_2 _inst_3) (Units.instMulOneClassUnits.{u1} (LinearMap.{u2, u2, u1, u1} R R _inst_1 _inst_1 (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) M M _inst_2 _inst_2 _inst_3 _inst_3) (Module.End.monoid.{u2, u1} R M _inst_1 _inst_2 _inst_3))) (MulOneClass.toMul.{u1} (LinearEquiv.{u2, u2, u1, u1} R R _inst_1 _inst_1 (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHomInvPair.ids.{u2} R _inst_1) (RingHomInvPair.ids.{u2} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (Monoid.toMulOneClass.{u1} (LinearEquiv.{u2, u2, u1, u1} R R _inst_1 _inst_1 (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHomInvPair.ids.{u2} R _inst_1) (RingHomInvPair.ids.{u2} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (DivInvMonoid.toMonoid.{u1} (LinearEquiv.{u2, u2, u1, u1} R R _inst_1 _inst_1 (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHomInvPair.ids.{u2} R _inst_1) (RingHomInvPair.ids.{u2} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (Group.toDivInvMonoid.{u1} (LinearEquiv.{u2, u2, u1, u1} R R _inst_1 _inst_1 (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHomInvPair.ids.{u2} R _inst_1) (RingHomInvPair.ids.{u2} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (LinearEquiv.automorphismGroup.{u2, u1} R M _inst_1 _inst_2 _inst_3)))))) (LinearMap.GeneralLinearGroup.{u2, u1} R M _inst_1 _inst_2 _inst_3) (LinearEquiv.{u2, u2, u1, u1} R R _inst_1 _inst_1 (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHomInvPair.ids.{u2} R _inst_1) (RingHomInvPair.ids.{u2} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (MulOneClass.toMul.{u1} (LinearMap.GeneralLinearGroup.{u2, u1} R M _inst_1 _inst_2 _inst_3) (Units.instMulOneClassUnits.{u1} (LinearMap.{u2, u2, u1, u1} R R _inst_1 _inst_1 (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) M M _inst_2 _inst_2 _inst_3 _inst_3) (Module.End.monoid.{u2, u1} R M _inst_1 _inst_2 _inst_3))) (MulOneClass.toMul.{u1} (LinearEquiv.{u2, u2, u1, u1} R R _inst_1 _inst_1 (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHomInvPair.ids.{u2} R _inst_1) (RingHomInvPair.ids.{u2} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (Monoid.toMulOneClass.{u1} (LinearEquiv.{u2, u2, u1, u1} R R _inst_1 _inst_1 (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHomInvPair.ids.{u2} R _inst_1) (RingHomInvPair.ids.{u2} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (DivInvMonoid.toMonoid.{u1} (LinearEquiv.{u2, u2, u1, u1} R R _inst_1 _inst_1 (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHomInvPair.ids.{u2} R _inst_1) (RingHomInvPair.ids.{u2} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (Group.toDivInvMonoid.{u1} (LinearEquiv.{u2, u2, u1, u1} R R _inst_1 _inst_1 (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHomInvPair.ids.{u2} R _inst_1) (RingHomInvPair.ids.{u2} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (LinearEquiv.automorphismGroup.{u2, u1} R M _inst_1 _inst_2 _inst_3))))) (MulEquiv.instMulEquivClassMulEquiv.{u1, u1} (LinearMap.GeneralLinearGroup.{u2, u1} R M _inst_1 _inst_2 _inst_3) (LinearEquiv.{u2, u2, u1, u1} R R _inst_1 _inst_1 (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHomInvPair.ids.{u2} R _inst_1) (RingHomInvPair.ids.{u2} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (MulOneClass.toMul.{u1} (LinearMap.GeneralLinearGroup.{u2, u1} R M _inst_1 _inst_2 _inst_3) (Units.instMulOneClassUnits.{u1} (LinearMap.{u2, u2, u1, u1} R R _inst_1 _inst_1 (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) M M _inst_2 _inst_2 _inst_3 _inst_3) (Module.End.monoid.{u2, u1} R M _inst_1 _inst_2 _inst_3))) (MulOneClass.toMul.{u1} (LinearEquiv.{u2, u2, u1, u1} R R _inst_1 _inst_1 (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHomInvPair.ids.{u2} R _inst_1) (RingHomInvPair.ids.{u2} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (Monoid.toMulOneClass.{u1} (LinearEquiv.{u2, u2, u1, u1} R R _inst_1 _inst_1 (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHomInvPair.ids.{u2} R _inst_1) (RingHomInvPair.ids.{u2} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (DivInvMonoid.toMonoid.{u1} (LinearEquiv.{u2, u2, u1, u1} R R _inst_1 _inst_1 (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHomInvPair.ids.{u2} R _inst_1) (RingHomInvPair.ids.{u2} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (Group.toDivInvMonoid.{u1} (LinearEquiv.{u2, u2, u1, u1} R R _inst_1 _inst_1 (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) (RingHomInvPair.ids.{u2} R _inst_1) (RingHomInvPair.ids.{u2} R _inst_1) M M _inst_2 _inst_2 _inst_3 _inst_3) (LinearEquiv.automorphismGroup.{u2, u1} R M _inst_1 _inst_2 _inst_3))))))))) (LinearMap.GeneralLinearGroup.generalLinearEquiv.{u2, u1} R M _inst_1 _inst_2 _inst_3) f)) (FunLike.coe.{succ u1, succ u1, succ u1} (LinearMap.{u2, u2, u1, u1} R R _inst_1 _inst_1 (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) M M _inst_2 _inst_2 _inst_3 _inst_3) M (fun (_x : M) => (fun (x._@.Mathlib.Algebra.Module.LinearMap._hyg.6190 : M) => M) _x) (LinearMap.instFunLikeLinearMap.{u2, u2, u1, u1} R R M M _inst_1 _inst_1 _inst_2 _inst_2 _inst_3 _inst_3 (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1))) (Units.val.{u1} (LinearMap.{u2, u2, u1, u1} R R _inst_1 _inst_1 (RingHom.id.{u2} R (Semiring.toNonAssocSemiring.{u2} R _inst_1)) M M _inst_2 _inst_2 _inst_3 _inst_3) (Module.End.monoid.{u2, u1} R M _inst_1 _inst_2 _inst_3) f))
Case conversion may be inaccurate. Consider using '#align linear_map.general_linear_group.coe_fn_general_linear_equiv LinearMap.GeneralLinearGroup.coeFn_generalLinearEquivₓ'. -/
@[simp]
theorem coeFn_generalLinearEquiv (f : GeneralLinearGroup R M) :
    ⇑(generalLinearEquiv R M f) = (f : M → M) :=
  rfl
#align linear_map.general_linear_group.coe_fn_general_linear_equiv LinearMap.GeneralLinearGroup.coeFn_generalLinearEquiv

end GeneralLinearGroup

end LinearMap


/-
Copyright (c) 2022 Oliver Nash. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Oliver Nash
-/
import measure_theory.measure.doubling
import measure_theory.covering.vitali
import measure_theory.covering.differentiation

/-!
# Doubling measures and Lebesgue's density theorem

Lebesgue's density theorem states that given a set `S` in a sigma compact metric space with
locally-finite doubling measure `μ` then for almost all points `x` in `S`, for any sequence of
closed balls `B₀, B₁, B₂, ...` containing `x`, the limit `μ (S ∩ Bⱼ) / μ (Bⱼ) → 1` as `j → ∞`.

In this file we combine general results about existence of Vitali families for doubling measures
with results about differentiation along a Vitali family to obtain an explicit form of Lebesgue's
density theorem.

## Main results
  * `is_doubling_measure.ae_tendsto_measure_inter_div`: a version of Lebesgue's density theorem for
  sequences of balls converging on a point but whose centres are not required to be fixed.

-/

noncomputable theory

open set filter metric measure_theory topological_space
open_locale nnreal topology

namespace is_doubling_measure

variables {α : Type*} [metric_space α] [measurable_space α] (μ : measure α) [is_doubling_measure μ]

section
variables [second_countable_topology α] [borel_space α] [is_locally_finite_measure μ]

open_locale topology

/-- A Vitali family in a space with a doubling measure, designed so that the sets at `x` contain
all `closed_ball y r` when `dist x y ≤ K * r`. -/
@[irreducible] def vitali_family (K : ℝ) : vitali_family μ :=
begin
  /- the Vitali covering theorem gives a family that works well at small scales, thanks to the
  doubling property. We enlarge this family to add large sets, to make sure that all balls and not
  only small ones belong to the family, for convenience. -/
  let R := scaling_scale_of μ (max (4 * K + 3) 3),
  have Rpos : 0 < R := scaling_scale_of_pos _ _,
  have A : ∀ (x : α), ∃ᶠ r in 𝓝[>] (0 : ℝ),
    μ (closed_ball x (3 * r)) ≤ scaling_constant_of μ (max (4 * K + 3) 3) * μ (closed_ball x r),
  { assume x,
    apply frequently_iff.2 (λ U hU, _),
    obtain ⟨ε, εpos, hε⟩ := mem_nhds_within_Ioi_iff_exists_Ioc_subset.1 hU,
    refine ⟨min ε R, hε ⟨lt_min εpos Rpos, min_le_left _ _⟩, _⟩,
    exact measure_mul_le_scaling_constant_of_mul μ
      ⟨zero_lt_three, le_max_right _ _⟩ (min_le_right _ _) },
  exact (vitali.vitali_family μ (scaling_constant_of μ (max (4 * K + 3) 3)) A).enlarge
    (R / 4) (by linarith),
end

/-- In the Vitali family `is_doubling_measure.vitali_family K`, the sets based at `x` contain all
balls `closed_ball y r` when `dist x y ≤ K * r`. -/
lemma closed_ball_mem_vitali_family_of_dist_le_mul
  {K : ℝ} {x y : α} {r : ℝ} (h : dist x y ≤ K * r) (rpos : 0 < r) :
  closed_ball y r ∈ (vitali_family μ K).sets_at x :=
begin
  let R := scaling_scale_of μ (max (4 * K + 3) 3),
  simp only [vitali_family, vitali_family.enlarge, vitali.vitali_family, mem_union, mem_set_of_eq,
    is_closed_ball, true_and, (nonempty_ball.2 rpos).mono ball_subset_interior_closed_ball,
    measurable_set_closed_ball],
  /- The measure is doubling on scales smaller than `R`. Therefore, we treat differently small
  and large balls. For large balls, this follows directly from the enlargement we used in the
  definition. -/
  by_cases H : closed_ball y r ⊆ closed_ball x (R / 4),
  swap, { exact or.inr H },
  left,
  /- For small balls, there is the difficulty that `r` could be large but still the ball could be
  small, if the annulus `{y | ε ≤ dist y x ≤ R/4}` is empty. We split between the cases `r ≤ R`
  and `r < R`, and use the doubling for the former and rough estimates for the latter. -/
  rcases le_or_lt r R with hr|hr,
  { refine ⟨(K + 1) * r, _⟩,
    split,
    { apply closed_ball_subset_closed_ball',
      rw dist_comm,
      linarith },
    { have I1 : closed_ball x (3 * ((K + 1) * r)) ⊆ closed_ball y ((4 * K + 3) * r),
      { apply closed_ball_subset_closed_ball',
        linarith },
      have I2 : closed_ball y ((4 * K + 3) * r) ⊆ closed_ball y ((max (4 * K + 3) 3) * r),
      { apply closed_ball_subset_closed_ball,
        exact mul_le_mul_of_nonneg_right (le_max_left _ _) rpos.le },
      apply (measure_mono (I1.trans I2)).trans,
      exact measure_mul_le_scaling_constant_of_mul _
        ⟨zero_lt_three.trans_le (le_max_right _ _), le_rfl⟩ hr } },
  { refine ⟨R / 4, H, _⟩,
    have : closed_ball x (3 * (R / 4)) ⊆ closed_ball y r,
    { apply closed_ball_subset_closed_ball',
      have A : y ∈ closed_ball y r, from mem_closed_ball_self rpos.le,
      have B := mem_closed_ball'.1 (H A),
      linarith },
    apply (measure_mono this).trans _,
    refine le_mul_of_one_le_left (zero_le _) _,
    exact ennreal.one_le_coe_iff.2 (le_max_right _ _) }
end

lemma tendsto_closed_ball_filter_at {K : ℝ} {x : α} {ι : Type*} {l : filter ι}
  (w : ι → α) (δ : ι → ℝ) (δlim : tendsto δ l (𝓝[>] 0))
  (xmem : ∀ᶠ j in l, x ∈ closed_ball (w j) (K * δ j)) :
  tendsto (λ j, closed_ball (w j) (δ j)) l ((vitali_family μ K).filter_at x) :=
begin
  refine (vitali_family μ K).tendsto_filter_at_iff.mpr ⟨_, λ ε hε, _⟩,
  { filter_upwards [xmem, δlim self_mem_nhds_within] with j hj h'j,
    exact closed_ball_mem_vitali_family_of_dist_le_mul μ hj h'j },
  { by_cases l.ne_bot,
    swap, { simp [not_ne_bot.1 h] },
    have hK : 0 ≤ K,
    { resetI,
      rcases (xmem.and (δlim self_mem_nhds_within)).exists with ⟨j, hj, h'j⟩,
      have : 0 ≤ K * δ j := nonempty_closed_ball.1 ⟨x, hj⟩,
      exact (mul_nonneg_iff_left_nonneg_of_pos (mem_Ioi.1 h'j)).1 this },
    have δpos := eventually_mem_of_tendsto_nhds_within δlim,
    replace δlim := tendsto_nhds_of_tendsto_nhds_within δlim,
    replace hK : 0 < K + 1, by linarith,
    apply (((metric.tendsto_nhds.mp δlim _ (div_pos hε hK)).and δpos).and xmem).mono,
    rintros j ⟨⟨hjε, hj₀ : 0 < δ j⟩, hx⟩ y hy,
    replace hjε : (K + 1) * δ j < ε :=
      by simpa [abs_eq_self.mpr hj₀.le] using (lt_div_iff' hK).mp hjε,
    simp only [mem_closed_ball] at hx hy ⊢,
    linarith [dist_triangle_right y x (w j)] }
end

end

section applications
variables [second_countable_topology α] [borel_space α] [is_locally_finite_measure μ]
  {E : Type*} [normed_add_comm_group E]

/-- A version of *Lebesgue's density theorem* for a sequence of closed balls whose centers are
not required to be fixed.

See also `besicovitch.ae_tendsto_measure_inter_div`. -/
lemma ae_tendsto_measure_inter_div (S : set α) (K : ℝ) :
  ∀ᵐ x ∂μ.restrict S, ∀ {ι : Type*} {l : filter ι} (w : ι → α) (δ : ι → ℝ)
    (δlim : tendsto δ l (𝓝[>] 0))
    (xmem : ∀ᶠ j in l, x ∈ closed_ball (w j) (K * δ j)),
    tendsto (λ j, μ (S ∩ closed_ball (w j) (δ j)) / μ (closed_ball (w j) (δ j))) l (𝓝 1) :=
by filter_upwards [(vitali_family μ K).ae_tendsto_measure_inter_div S] with x hx ι l w δ δlim xmem
using hx.comp (tendsto_closed_ball_filter_at μ _ _ δlim xmem)

/-- A version of *Lebesgue differentiation theorem* for a sequence of closed balls whose
centers are not required to be fixed. -/
lemma ae_tendsto_average_norm_sub {f : α → E} (hf : integrable f μ) (K : ℝ) :
  ∀ᵐ x ∂μ, ∀ {ι : Type*} {l : filter ι} (w : ι → α) (δ : ι → ℝ)
    (δlim : tendsto δ l (𝓝[>] 0))
    (xmem : ∀ᶠ j in l, x ∈ closed_ball (w j) (K * δ j)),
    tendsto (λ j, ⨍ y in closed_ball (w j) (δ j), ‖f y - f x‖ ∂μ) l (𝓝 0) :=
by filter_upwards [(vitali_family μ K).ae_tendsto_average_norm_sub hf] with x hx ι l w δ δlim xmem
using hx.comp (tendsto_closed_ball_filter_at μ _ _ δlim xmem)

/-- A version of *Lebesgue differentiation theorem* for a sequence of closed balls whose
centers are not required to be fixed. -/
lemma ae_tendsto_average [normed_space ℝ E] [complete_space E]
  {f : α → E} (hf : integrable f μ) (K : ℝ) :
  ∀ᵐ x ∂μ, ∀ {ι : Type*} {l : filter ι} (w : ι → α) (δ : ι → ℝ)
    (δlim : tendsto δ l (𝓝[>] 0))
    (xmem : ∀ᶠ j in l, x ∈ closed_ball (w j) (K * δ j)),
    tendsto (λ j, ⨍ y in closed_ball (w j) (δ j), f y ∂μ) l (𝓝 (f x)) :=
by filter_upwards [(vitali_family μ K).ae_tendsto_average hf] with x hx ι l w δ δlim xmem
using hx.comp (tendsto_closed_ball_filter_at μ _ _ δlim xmem)

end applications

end is_doubling_measure


/-
Copyright © 2020 Nicolò Cavalleri. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Nicolò Cavalleri

! This file was ported from Lean 3 source module geometry.manifold.algebra.structures
! leanprover-community/mathlib commit d1bd9c5df2867c1cb463bc6364446d57bdd9f7f1
! Please do not edit these lines, except to modify the commit id
! if you have ported upstream changes.
-/
import Mathbin.Geometry.Manifold.Algebra.LieGroup

/-!
# Smooth structures

In this file we define smooth structures that build on Lie groups. We prefer using the term smooth
instead of Lie mainly because Lie ring has currently another use in mathematics.
-/

open Manifold

section SmoothRing

variable {𝕜 : Type _} [NontriviallyNormedField 𝕜] {H : Type _} [TopologicalSpace H] {E : Type _}
  [NormedAddCommGroup E] [NormedSpace 𝕜 E]

/- ./././Mathport/Syntax/Translate/Basic.lean:334:40: warning: unsupported option default_priority -/
set_option default_priority 100

-- see Note [default priority]
-- See note [Design choices about smooth algebraic structures]
/-- A smooth (semi)ring is a (semi)ring `R` where addition and multiplication are smooth.
If `R` is a ring, then negation is automatically smooth, as it is multiplication with `-1`. -/
class SmoothRing (I : ModelWithCorners 𝕜 E H) (R : Type _) [Semiring R] [TopologicalSpace R]
  [ChartedSpace H R] extends HasSmoothAdd I R : Prop where
  smooth_mul : Smooth (I.Prod I) I fun p : R × R => p.1 * p.2
#align smooth_ring SmoothRing

instance SmoothRing.to_hasSmoothMul (I : ModelWithCorners 𝕜 E H) (R : Type _) [Semiring R]
    [TopologicalSpace R] [ChartedSpace H R] [h : SmoothRing I R] : HasSmoothMul I R :=
  { h with }
#align smooth_ring.to_has_smooth_mul SmoothRing.to_hasSmoothMul

instance SmoothRing.to_lieAddGroup (I : ModelWithCorners 𝕜 E H) (R : Type _) [Ring R]
    [TopologicalSpace R] [ChartedSpace H R] [SmoothRing I R] : LieAddGroup I R
    where
  compatible e e' := HasGroupoid.compatible (contDiffGroupoid ⊤ I)
  smooth_add := smooth_add I
  smooth_neg := by simpa only [neg_one_mul] using @smooth_mul_left 𝕜 _ H _ E _ _ I R _ _ _ _ (-1)
#align smooth_ring.to_lie_add_group SmoothRing.to_lieAddGroup

end SmoothRing

instance field_smoothRing {𝕜 : Type _} [NontriviallyNormedField 𝕜] : SmoothRing 𝓘(𝕜) 𝕜 :=
  { normedSpace_lieAddGroup with
    smooth_mul := by
      rw [smooth_iff]
      refine' ⟨continuous_mul, fun x y => _⟩
      simp only [Prod.mk.eta, mfld_simps]
      rw [contDiffOn_univ]
      exact contDiff_mul }
#align field_smooth_ring field_smoothRing

variable {𝕜 R E H : Type _} [TopologicalSpace R] [TopologicalSpace H] [NontriviallyNormedField 𝕜]
  [NormedAddCommGroup E] [NormedSpace 𝕜 E] [ChartedSpace H R] (I : ModelWithCorners 𝕜 E H)

/-- A smooth (semi)ring is a topological (semi)ring. This is not an instance for technical reasons,
see note [Design choices about smooth algebraic structures]. -/
theorem topologicalSemiring_of_smooth [Semiring R] [SmoothRing I R] : TopologicalSemiring R :=
  { continuousMul_of_smooth I, has_continuous_add_of_smooth I with }
#align topological_semiring_of_smooth topologicalSemiring_of_smooth


import linear_algebra.affine_space.basic
import linear_algebra.basis
import .affine_coordinate_space
import data.real.basic

open_locale affine
/-
This file contains
affine_tuple_coord_frame
affine_coord_frame
aff_coord_pt
aff_coord_vec
Also all instances necessary. Missing some proofs 11/20
-/

namespace aff_fr

universes u v w

variables
    -- (id : ℕ)
    (X : Type u)
    (K : Type v)
    (V : Type w)
    (n : ℕ)
    (k : K)
    (ι : Type*)
    (s : finset ι)
    (g : ι → K)
    (v : ι → V)
    [inhabited K]
    [field K]
    [add_comm_group V]
    [module K V]
    [vector_space K V]
    [affine_space V X]
    [is_basis K v]

/-
An affine frame comprises an origin point
and a basis for the vector space.
-/
structure affine_tuple_coord_frame
(K : Type w)
(n : ℕ)
[inhabited K]
[field K]  :=
mk ::
    (origin : aff_pt_coord_tuple K n)
    (basis : (fin n) → aff_vec_coord_tuple K n)
    (proof_is_basis : is_basis K basis)

inductive affine_coord_frame
(K : Type w)
(n : ℕ)
[inhabited K]
[field K]
| tuple (base : affine_tuple_coord_frame K n)
    : affine_coord_frame
| derived
    (origin : aff_pt_coord_tuple K n)
    (basis : (fin n) → aff_vec_coord_tuple K n)
    (proof_is_basis : is_basis K basis)
    (base : affine_coord_frame)
    : affine_coord_frame

@[ext]
structure aff_coord_pt (fr : affine_coord_frame K n)
            extends aff_pt_coord_tuple K n :=
   -- (tuple : aff_pt_coord_tuple K n)
   mk ::

@[ext]
structure aff_coord_vec (fr : affine_coord_frame K n)
            extends aff_vec_coord_tuple K n  :=
   -- (tuple : aff_vec_coord_tuple K n)
   mk ::

variables
    (fr : affine_coord_frame K n)
    (cv1 cv2 : aff_coord_vec K n fr)
    (cp1 cp2 : aff_coord_pt  K n fr)

def pt_zero_coord : aff_coord_pt K n fr := ⟨pt_zero K n⟩
def zero_vector_coord : aff_coord_vec K n fr := ⟨vec_zero K n⟩

/-! ### abelian group operations -/
def vec_add_coord : aff_coord_vec K n fr → aff_coord_vec K n fr → aff_coord_vec K n fr :=
    λ x y, ⟨x.1 + y.1⟩
def vec_zero_coord : aff_coord_vec K n fr := ⟨vec_zero K n⟩
def vec_neg_coord : aff_coord_vec K n fr → aff_coord_vec K n fr
| ⟨x⟩ := ⟨⟨λ m : fin n, -(x.1 m)⟩⟩

/-! ### type class instances for the abelian group operations -/
instance : has_add (aff_coord_vec K n fr) := ⟨vec_add_coord K n fr⟩
instance : has_zero (aff_coord_vec K n fr) := ⟨vec_zero_coord K n fr⟩
instance : has_neg (aff_coord_vec K n fr) := ⟨vec_neg_coord K n fr⟩

-- properties necessary to show aff_vec_coord_tuple K n is an instance of add_comm_group
#print add_comm_group
lemma vec_add_assoc_coord : ∀ x y z : aff_coord_vec K n fr,  x + y + z = x + (y + z) :=
begin
intros,
ext m,
exact add_assoc (x.1.1 m) (y.1.1 m) (z.1.1 m),
end

lemma vec_zero_is_coord : (0 : aff_coord_vec K n fr) = vec_zero_coord K n fr := rfl

lemma vec_zero_add_coord : ∀ x : aff_coord_vec K n fr, 0 + x = x :=
begin
intro x,
ext m,
exact zero_add (x.1.1 m),
end

lemma vec_add_zero_coord : ∀ x : aff_coord_vec K n fr, x + 0 = x :=
begin
intro x,
ext m,
exact add_zero (x.1.1 m),
end

lemma vec_add_left_neg_coord : ∀ x : aff_coord_vec K n fr, -x + x = 0 :=
begin
intro x,
ext m,
have h₀ : -x.to_aff_vec_coord_tuple.vec m + x.to_aff_vec_coord_tuple.vec m = 0 := add_left_neg (x.1.1 m),
have h₁ : (0 : aff_coord_vec K n fr).to_aff_vec_coord_tuple.vec m = (0 : K) := rfl,
have h₂ : (-x + x).to_aff_vec_coord_tuple.vec m = -x.to_aff_vec_coord_tuple.vec m + x.to_aff_vec_coord_tuple.vec m := begin
    have h₃ : (-x + x).1.1 m = (-x).1.1 m + x.1.1 m := rfl,
    have h₄ : -x = vec_neg_coord K n fr ⟨x.1⟩ := begin
        have h : x = ⟨x.1⟩ := by {ext, refl},
        rw h,
        refl
    end,
    have h₅ : vec_neg_coord K n fr ⟨x.1⟩ = ⟨⟨λ m : fin n, -(x.1.1 m)⟩⟩ := rfl,
    rw [h₃, h₄, h₅]
end,
exact eq.trans h₂ (eq.trans h₀ (eq.symm h₁)),
end

lemma vec_add_comm_coord : ∀ x y : aff_coord_vec K n fr, x + y = y + x :=
begin
intros x y,
ext m,
exact add_comm (x.1.1 m) (y.1.1 m),
end

/-! ### Type class instance for abelian group -/
instance aff_comm_group_coord : add_comm_group (aff_coord_vec K n fr) :=
begin
split,
exact vec_add_left_neg_coord K n fr,
exact vec_add_comm_coord K n fr,
exact vec_add_assoc_coord K n fr,
exact vec_zero_add_coord K n fr,
exact vec_add_zero_coord K n fr,
end

/-! ### Scalar action -/

@[ext]
def vec_scalar_coord : K → aff_coord_vec K n fr → aff_coord_vec K n fr :=
    λ a x, ⟨vec_scalar K n a x.1⟩

instance : has_scalar K (aff_coord_vec K n fr) := ⟨vec_scalar_coord K n fr⟩

lemma vec_one_smul_coord : (1 : K) • cv1 = cv1 :=
begin
dsimp only [has_scalar.smul, vec_scalar_coord],
have h₀ : vec_scalar K n 1 cv1.to_aff_vec_coord_tuple = cv1.1 := vec_one_smul K n cv1.1,
rw h₀,
ext m,
refl,
end

lemma vec_mul_smul_coord : ∀ g h : K, ∀ x : aff_coord_vec K n fr, (g * h) • x = g • h • x :=
begin
intros,
have h₀ := vec_mul_smul K n g h x.1,
dsimp only [has_scalar.smul, vec_scalar_coord] at h₀ ⊢,
rw h₀,
end

instance : mul_action K (aff_coord_vec K n fr) := ⟨vec_one_smul_coord K n fr, vec_mul_smul_coord K n fr⟩

lemma vec_smul_add_coord : ∀ g : K, ∀ x y : aff_coord_vec K n fr, g • (x + y) = g•x + g•y :=
begin
intros,
have h₀ := vec_smul_add K n g x.1 y.1,
dsimp only [has_scalar.smul, vec_scalar_coord, has_add.add, vec_add_coord] at h₀ ⊢,
rw h₀,
end

lemma vec_smul_zero_coord : ∀ g : K, g • (0 : aff_coord_vec K n fr) = 0 :=
begin
intros,
have h₀ := vec_smul_zero K n g,
dsimp only [has_scalar.smul, vec_scalar_coord, has_zero.zero, vec_zero_coord] at h₀ ⊢,
rw h₀,
end

instance : distrib_mul_action K (aff_coord_vec K n fr) := ⟨vec_smul_add_coord K n fr, vec_smul_zero_coord K n fr⟩

lemma vec_add_smul_coord : ∀ g h : K, ∀ x : aff_coord_vec K n fr, (g + h) • x = g•x + h•x :=
begin
intros,
have h₀ := vec_add_smul K n g h x.1,
dsimp only [has_scalar.smul, vec_scalar_coord, has_add.add, vec_add_coord] at h₀ ⊢,
rw h₀,
end

lemma vec_zero_smul_coord : ∀ x : aff_coord_vec K n fr, (0 : K) • x = 0 :=
begin
intros,
have h₀ := vec_zero_smul K n x.1,
dsimp only [has_scalar.smul, vec_scalar_coord, has_zero.zero, vec_zero_coord] at h₀ ⊢,
rw h₀,
end

instance aff_semimod_coord : semimodule K (aff_coord_vec K n fr) := ⟨vec_add_smul_coord K n fr, vec_zero_smul_coord K n fr⟩

instance aff_module_coord : module K (aff_coord_vec K n fr) := ⟨vec_add_smul_coord K n fr, vec_zero_smul_coord K n fr⟩

/-! ### group action of aff_vec_coord_tuple on aff_pt_coord_tuple -/

def aff_group_action_coord : aff_coord_vec K n fr → aff_coord_pt K n fr → aff_coord_pt K n fr :=
    λ x y, ⟨aff_group_action K n x.1 y.1⟩

def aff_group_sub_coord : aff_coord_pt K n fr → aff_coord_pt K n fr → aff_coord_vec K n fr :=
    λ x y, ⟨aff_group_sub K n x.1 y.1⟩

#check add_action

instance : has_vadd (aff_coord_vec K n fr) (aff_coord_pt K n fr) := ⟨aff_group_action_coord K n fr⟩

instance : has_vsub (aff_coord_vec K n fr) (aff_coord_pt K n fr) := ⟨aff_group_sub_coord K n fr⟩

lemma aff_zero_sadd_coord : ∀ x : aff_coord_pt K n fr, (0 : aff_coord_vec K n fr) +ᵥ x = x :=
begin
intros,
have h₀ := aff_zero_sadd K n x.1,
dsimp only [has_vadd.vadd, aff_group_action_coord, has_zero.zero, vec_zero_coord] at h₀ ⊢,
rw h₀,
ext m,
refl,
end

lemma aff_add_sadd_coord : ∀ x y : aff_coord_vec K n fr, ∀ a : aff_coord_pt K n fr, x +ᵥ (y +ᵥ a) = x + y +ᵥ a :=
begin
intros,
have h₀ := aff_add_sadd K n x.1 y.1 a.1,
dsimp only [has_add.add, vec_add_coord, has_vadd.vadd, aff_group_action_coord] at h₀ ⊢,
rw h₀,
end

instance : add_action (aff_coord_vec K n fr) (aff_coord_pt K n fr) := ⟨aff_group_action_coord K n fr, aff_zero_sadd_coord K n fr, aff_add_sadd_coord K n fr⟩

lemma aff_add_trans_coord : ∀ a b : aff_coord_pt K n fr, ∃ x : aff_coord_vec K n fr, x +ᵥ a = b :=
begin
intros,
apply exists.intro (b -ᵥ a),
have h₀ := aff_vsub_vadd K n b.1 a.1,
dsimp only [has_vadd.vadd, aff_group_action_coord, has_vsub.vsub, aff_group_sub_coord] at h₀ ⊢,
rw h₀,
ext m,
refl,
end

lemma aff_add_free_coord : ∀ a : aff_coord_pt K n fr, ∀ g h : aff_coord_vec K n fr, g +ᵥ a = h +ᵥ a → g = h :=
begin
intros a g h h₀,
have h₁ := aff_add_free K n a.1 g.1 h.1,
dsimp only [has_vadd.vadd, aff_group_action_coord] at h₀ h₁,
have h₂ : ∀ (x y : aff_coord_pt K n fr), x = y → x.to_aff_pt_coord_tuple = y.to_aff_pt_coord_tuple := begin
    intros x y hy,
    rw hy
end,
have h₃ : aff_coord_pt.mk (aff_group_action K n g.to_aff_vec_coord_tuple a.to_aff_pt_coord_tuple) =
    aff_coord_pt.mk (aff_group_action K n h.to_aff_vec_coord_tuple a.to_aff_pt_coord_tuple) →
    (aff_coord_pt.mk (aff_group_action K n g.to_aff_vec_coord_tuple a.to_aff_pt_coord_tuple)).to_aff_pt_coord_tuple =
    (aff_coord_pt.mk (aff_group_action K n h.to_aff_vec_coord_tuple a.to_aff_pt_coord_tuple)).to_aff_pt_coord_tuple := by apply h₂,
have h₄ := h₃ h₀,
have h₅ : (aff_coord_pt.mk (aff_group_action K n g.to_aff_vec_coord_tuple a.to_aff_pt_coord_tuple)).to_aff_pt_coord_tuple =
    aff_group_action K n g.to_aff_vec_coord_tuple a.to_aff_pt_coord_tuple := rfl,
have h₆ : (aff_coord_pt.mk (aff_group_action K n h.to_aff_vec_coord_tuple a.to_aff_pt_coord_tuple)).to_aff_pt_coord_tuple =
    aff_group_action K n h.to_aff_vec_coord_tuple a.to_aff_pt_coord_tuple := rfl,
rw [h₅, h₆] at h₄,
have h₇ := h₁ h₄,
ext,
rw h₇
end

lemma aff_vadd_vsub_coord : ∀ (x : aff_coord_vec K n fr) (a : aff_coord_pt K n fr), x +ᵥ a -ᵥ a = x :=
begin
intros,
have h₀ := aff_vadd_vsub K n x.1 a.1,
dsimp only [has_vadd.vadd, aff_group_action_coord, has_vsub.vsub, aff_group_sub_coord] at h₀ ⊢,
rw h₀,
ext m,
refl,
end

lemma aff_vsub_vadd_coord : ∀ a b : aff_coord_pt K n fr, (a -ᵥ b) +ᵥ b = a :=
begin
intros,
have h₀ := aff_vsub_vadd K n a.1 b.1,
dsimp only [has_vadd.vadd, aff_group_action_coord, has_vsub.vsub, aff_group_sub_coord] at h₀ ⊢,
rw h₀,
ext m,
refl,
end

instance : nonempty (aff_coord_pt K n fr) := ⟨⟨pt_zero K n⟩⟩

instance aff_torsor_coord : add_torsor (aff_coord_vec K n fr) (aff_coord_pt K n fr) :=
⟨aff_group_action_coord K n fr,
aff_zero_sadd_coord K n fr,
aff_add_sadd_coord K n fr,
aff_group_sub_coord K n fr,
aff_vsub_vadd_coord K n fr,
aff_vadd_vsub_coord K n fr⟩

/-
"THEOREM:" these sets of scalar tuples with the operations defined on them
do have the structure of an affine space.
-/

instance aff_coord_is :
    affine_space
        (aff_coord_vec K n fr)
        (aff_coord_pt K n fr) :=
    ⟨aff_group_action_coord K n fr,
aff_zero_sadd_coord K n fr,
aff_add_sadd_coord K n fr,
aff_group_sub_coord K n fr,
aff_vsub_vadd_coord K n fr,
aff_vadd_vsub_coord K n fr⟩

def pt_plus_vec
    {X : Type u}
    {K : Type v}
    {V : Type w}
    {n : ℕ}
    {ι : Type*}
    [inhabited K]
    [field K]
    [add_comm_group V]
    [module K V]
    [vector_space K V]
    [affine_space V X]
    {fr : affine_coord_frame K n} :
    (aff_coord_pt K n fr) →
    (aff_coord_vec K n fr) →
    (aff_coord_pt K n fr)
| p v := aff_group_action_coord K n fr v p

notation
 pt +ᵥ v := pt_plus_vec pt v

def vec_mul_scalar
    {X : Type u}
    {K : Type v}
    {V : Type w}
    {n : ℕ}
    {ι : Type*}
    [inhabited K]
    [field K]
    [add_comm_group V]
    [module K V]
    [vector_space K V]
    [affine_space V X]
    {fr : affine_coord_frame K n} :
    (aff_coord_vec K n fr) →
    K →
    (aff_coord_vec K n fr)
| v s := s • v

notation
 v • s := vec_mul_scalar v s

def pt_minus_vec
    {X : Type u}
    {K : Type v}
    {V : Type w}
    {n : ℕ}
    {ι : Type*}
    [inhabited K]
    [field K]
    [add_comm_group V]
    [module K V]
    [vector_space K V]
    [affine_space V X]
    {fr : affine_coord_frame K n} :
    (aff_coord_pt K n fr) →
    (aff_coord_vec K n fr) →
    (aff_coord_pt K n fr)
| p v := aff_group_action_coord K n fr (vec_neg_coord K n fr v) p

notation
 pt -ᵥ v := pt_minus_vec pt v

end aff_fr


/-
Copyright (c) 2021 Huub Vromen. All rights reserved.
Author: Huub Vromen
-/

/- outline of Lewis' theory of common knowledge (formalisation according to
the proposal of Jaap van der Does) -/

-- type of individuals in the population P
variable {indiv : Type}
variables {i j: indiv}

-- states of affairs and propositions are of the same type
variables {p ψ χ : Prop}

-- Reason-to-believe is a two-place relation between individuals and propositions
constant R : indiv → Prop → Prop

-- Indication is a three-place relation between individuals and two propositions
constant Ind : indiv → Prop → Prop → Prop

/-- The following axioms represent that state of affairs α is a basis for common
knowledge of proposition φ -/
constants {α φ: Prop}
axiom CK1 : R i α
axiom CK2 : Ind i α (R j α)
axiom CK3 : Ind i α φ

/-- `Indicative modus ponens represents the meaning of indication.
    This axiom corresponds to axiom A1 in Cubitt and Sugden's account. -/
axiom I_MP : Ind i ψ χ → R i ψ → R i χ

/-- `I-introspection` represents that all individuals in P share inductive
    standards and background knowledge. This axiom is a variant of axiom C4
    in Cubitt and Sugden's account -/
axiom I_introspection : Ind i α ψ → Ind i α (Ind j α ψ)

/-- `LR` (Lewis' Rule) is the assumption that the population allows for introspection
    with regard to using I-MP. This axiom is a variant of axiom A6 in
    Cubitt and Sugden's account -/
axiom LR : (Ind i α (R j α) ∧ Ind i α (Ind j α ψ) → Ind i α (R j ψ))

/-- Now we can prove Lewis' theorem. First, we inductively define G (`generated
    by φ), the property of being a proposition `p` for which we have to prove
    that `R i p` holds. -/
inductive G : Prop → Prop
| base                          : G φ
| step (p : Prop) {i : indiv}   : G p → G (R i p)

theorem Lewis (p : Prop) (hG : @G indiv p) : ∀i : indiv, R i p :=
begin
intro i,
have h1 : Ind i α p :=
    begin
    induction hG with u j hu ih,
{   exact CK3   },
{   have h2 : Ind i α (Ind j α u) := I_introspection ih,
    have h3 : Ind i α (R j u) := LR (and.intro CK2 h2),
    assumption }
    end,
exact I_MP h1 CK1
end

/- Both I-MP and LR can be derived as lemmas in the theory of reasoning with
    reasons. -/


/-
Copyright (c) 2018 Chris Hughes. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Chris Hughes, Patrick Stevens

! This file was ported from Lean 3 source module data.nat.choose.sum
! leanprover-community/mathlib commit 4c19a16e4b705bf135cf9a80ac18fcc99c438514
! Please do not edit these lines, except to modify the commit id
! if you have ported upstream changes.
-/
import Mathlib.Data.Nat.Choose.Basic
import Mathlib.Tactic.Linarith
import Mathlib.Algebra.BigOperators.Ring
import Mathlib.Algebra.BigOperators.Intervals
import Mathlib.Algebra.BigOperators.Order
import Mathlib.Algebra.BigOperators.NatAntidiagonal

/-!
# Sums of binomial coefficients

This file includes variants of the binomial theorem and other results on sums of binomial
coefficients. Theorems whose proofs depend on such sums may also go in this file for import
reasons.

-/

open Nat

open Finset

open BigOperators

variable {R : Type _}

namespace Commute

variable [Semiring R] {x y : R}

/-- A version of the **binomial theorem** for commuting elements in noncommutative semirings. -/
theorem add_pow (h : Commute x y) (n : ℕ) :
    (x + y) ^ n = ∑ m in range (n + 1), x ^ m * y ^ (n - m) * choose n m := by
  let t : ℕ → ℕ → R := fun n m ↦ x ^ m * y ^ (n - m) * choose n m
  change (x + y) ^ n = ∑ m in range (n + 1), t n m
  have h_first : ∀ n, t n 0 = y ^ n := fun n ↦ by
    simp only [choose_zero_right, _root_.pow_zero, Nat.cast_one, mul_one, one_mul, tsub_zero]
  have h_last : ∀ n, t n n.succ = 0 := fun n ↦ by
    simp only [ge_iff_le, choose_succ_self, cast_zero, mul_zero]
  have h_middle :
    ∀ n i : ℕ, i ∈ range n.succ → (t n.succ ∘ Nat.succ) i =
      x * t n i + y * t n i.succ := by
    intro n i h_mem
    have h_le : i ≤ n := Nat.le_of_lt_succ (mem_range.mp h_mem)
    dsimp only
    rw [Function.comp_apply, choose_succ_succ, Nat.cast_add, mul_add]
    congr 1
    · rw [pow_succ x, succ_sub_succ, mul_assoc, mul_assoc, mul_assoc]
    · rw [← mul_assoc y, ← mul_assoc y, (h.symm.pow_right i.succ).eq]
      by_cases h_eq : i = n
      · rw [h_eq, choose_succ_self, Nat.cast_zero, mul_zero, mul_zero]
      · rw [succ_sub (lt_of_le_of_ne h_le h_eq)]
        rw [pow_succ y, mul_assoc, mul_assoc, mul_assoc, mul_assoc]
  induction' n with n ih
  · rw [_root_.pow_zero, sum_range_succ, range_zero, sum_empty, zero_add]
    dsimp only
    rw [_root_.pow_zero, _root_.pow_zero, choose_self, Nat.cast_one, mul_one, mul_one]
  · rw [sum_range_succ', h_first]
    erw [sum_congr rfl (h_middle n), sum_add_distrib, add_assoc]
    rw [pow_succ (x + y), ih, add_mul, mul_sum, mul_sum]
    congr 1
    rw [sum_range_succ', sum_range_succ, h_first, h_last, mul_zero, add_zero, _root_.pow_succ]
#align commute.add_pow Commute.add_pow

/-- A version of `Commute.add_pow` that avoids ℕ-subtraction by summing over the antidiagonal and
also with the binomial coefficient applied via scalar action of ℕ. -/
theorem add_pow' (h : Commute x y) (n : ℕ) :
    (x + y) ^ n = ∑ m in Nat.antidiagonal n, choose n m.fst • (x ^ m.fst * y ^ m.snd) := by
  simp_rw [Finset.Nat.sum_antidiagonal_eq_sum_range_succ fun m p ↦ choose n m • (x ^ m * y ^ p),
    _root_.nsmul_eq_mul, cast_comm, h.add_pow]
#align commute.add_pow' Commute.add_pow'

end Commute

/-- The **binomial theorem** -/
theorem add_pow [CommSemiring R] (x y : R) (n : ℕ) :
    (x + y) ^ n = ∑ m in range (n + 1), x ^ m * y ^ (n - m) * choose n m :=
  (Commute.all x y).add_pow n
#align add_pow add_pow

namespace Nat

/-- The sum of entries in a row of Pascal's triangle -/
theorem sum_range_choose (n : ℕ) : (∑ m in range (n + 1), choose n m) = 2 ^ n := by
  have := (add_pow 1 1 n).symm
  simpa [one_add_one_eq_two] using this
#align nat.sum_range_choose Nat.sum_range_choose

theorem sum_range_choose_halfway (m : Nat) : (∑ i in range (m + 1), choose (2 * m + 1) i) = 4 ^ m :=
  have : (∑ i in range (m + 1), choose (2 * m + 1) (2 * m + 1 - i)) =
      ∑ i in range (m + 1), choose (2 * m + 1) i :=
    sum_congr rfl fun i hi ↦ choose_symm <| by linarith [mem_range.1 hi]
  mul_right_injective₀ two_ne_zero <|
    calc
      (2 * ∑ i in range (m + 1), choose (2 * m + 1) i) =
          (∑ i in range (m + 1), choose (2 * m + 1) i) +
            ∑ i in range (m + 1), choose (2 * m + 1) (2 * m + 1 - i) := by rw [two_mul, this]
      _ = (∑ i in range (m + 1), choose (2 * m + 1) i) +
            ∑ i in Ico (m + 1) (2 * m + 2), choose (2 * m + 1) i := by
        { rw [range_eq_Ico, sum_Ico_reflect]
          · congr
            have A : m + 1 ≤ 2 * m + 1 := by linarith
            rw [add_comm, add_tsub_assoc_of_le A, ← add_comm]
            congr
            rw [tsub_eq_iff_eq_add_of_le A]
            ring
          · linarith }
      _ = ∑ i in range (2 * m + 2), choose (2 * m + 1) i := sum_range_add_sum_Ico _ (by linarith)
      _ = 2 ^ (2 * m + 1) := sum_range_choose (2 * m + 1)
      _ = 2 * 4 ^ m := by rw [pow_succ, pow_mul, mul_comm]; rfl
#align nat.sum_range_choose_halfway Nat.sum_range_choose_halfway

theorem choose_middle_le_pow (n : ℕ) : choose (2 * n + 1) n ≤ 4 ^ n := by
  have t : choose (2 * n + 1) n ≤ ∑ i in range (n + 1), choose (2 * n + 1) i :=
    single_le_sum (fun x _ ↦ by linarith) (self_mem_range_succ n)
  simpa [sum_range_choose_halfway n] using t
#align nat.choose_middle_le_pow Nat.choose_middle_le_pow

theorem four_pow_le_two_mul_add_one_mul_central_binom (n : ℕ) :
    4 ^ n ≤ (2 * n + 1) * choose (2 * n) n :=
  calc
    4 ^ n = (1 + 1) ^ (2 * n) := by simp only [pow_mul, one_add_one_eq_two]; rfl
    _ = ∑ m in range (2 * n + 1), choose (2 * n) m := by simp [add_pow]
    _ ≤ ∑ m in range (2 * n + 1), choose (2 * n) (2 * n / 2) :=
      sum_le_sum fun i _ ↦ choose_le_middle i (2 * n)
    _ = (2 * n + 1) * choose (2 * n) n := by simp
#align nat.four_pow_le_two_mul_add_one_mul_central_binom Nat.four_pow_le_two_mul_add_one_mul_central_binom

end Nat

theorem Int.alternating_sum_range_choose {n : ℕ} :
    (∑ m in range (n + 1), ((-1) ^ m * ↑(choose n m) : ℤ)) = if n = 0 then 1 else 0 := by
  cases n; · simp
  case succ n =>
    have h := add_pow (-1 : ℤ) 1 n.succ
    simp only [one_pow, mul_one, add_left_neg] at h
    rw [← h, zero_pow (Nat.succ_pos n), if_neg (Nat.succ_ne_zero n)]
#align int.alternating_sum_range_choose Int.alternating_sum_range_choose

theorem Int.alternating_sum_range_choose_of_ne {n : ℕ} (h0 : n ≠ 0) :
    (∑ m in range (n + 1), ((-1) ^ m * ↑(choose n m) : ℤ)) = 0 := by
  rw [Int.alternating_sum_range_choose, if_neg h0]
#align int.alternating_sum_range_choose_of_ne Int.alternating_sum_range_choose_of_ne

namespace Finset

theorem sum_powerset_apply_card {α β : Type _} [AddCommMonoid α] (f : ℕ → α) {x : Finset β} :
    (∑ m in x.powerset, f m.card) = ∑ m in range (x.card + 1), x.card.choose m • f m := by
  trans ∑ m in range (x.card + 1), ∑ j in x.powerset.filter fun z ↦ z.card = m, f j.card
  · refine' (sum_fiberwise_of_maps_to _ _).symm
    intro y hy
    rw [mem_range, Nat.lt_succ_iff]
    rw [mem_powerset] at hy
    exact card_le_of_subset hy
  · refine' sum_congr rfl fun y _ ↦ _
    rw [← card_powersetLen, ← sum_const]
    refine' sum_congr powersetLen_eq_filter.symm fun z hz ↦ _
    rw [(mem_powersetLen.1 hz).2]
#align finset.sum_powerset_apply_card Finset.sum_powerset_apply_card

theorem sum_powerset_neg_one_pow_card {α : Type _} [DecidableEq α] {x : Finset α} :
    (∑ m in x.powerset, (-1 : ℤ) ^ m.card) = if x = ∅ then 1 else 0 := by
  rw [sum_powerset_apply_card]
  simp only [nsmul_eq_mul', ← card_eq_zero, Int.alternating_sum_range_choose]
#align finset.sum_powerset_neg_one_pow_card Finset.sum_powerset_neg_one_pow_card

theorem sum_powerset_neg_one_pow_card_of_nonempty {α : Type _} {x : Finset α} (h0 : x.Nonempty) :
    (∑ m in x.powerset, (-1 : ℤ) ^ m.card) = 0 := by
  classical
    rw [sum_powerset_neg_one_pow_card, if_neg]
    rw [← Ne.def, ← nonempty_iff_ne_empty]
    apply h0
#align finset.sum_powerset_neg_one_pow_card_of_nonempty Finset.sum_powerset_neg_one_pow_card_of_nonempty

end Finset


import Mathlib.Tactic.Qify

example (a b : ℕ) : (a : ℚ) ≤ b ↔ a ≤ b := by qify
example (a b : ℕ) : (a : ℚ) < b ↔ a < b := by qify
example (a b : ℕ) : (a : ℚ) = b ↔ a = b := by qify
example (a b : ℕ) : (a : ℚ) ≠ b ↔ a ≠ b := by qify

example (a b : ℤ) : (a : ℚ) ≤ b ↔ a ≤ b := by qify
example (a b : ℤ) : (a : ℚ) < b ↔ a < b := by qify
example (a b : ℤ) : (a : ℚ) = b ↔ a = b := by qify
example (a b : ℤ) : (a : ℚ) ≠ b ↔ a ≠ b := by qify

example (a b c : ℕ) (h : a - b = c) (hab : b ≤ a) : a = c + b := by
  qify [hab] at h ⊢ -- `zify` does the same thing here.
  exact sub_eq_iff_eq_add.1 h

example (a b c : ℤ) (h : a / b = c) (hab : b ∣ a) (hb : b ≠ 0) : a = c * b := by
  qify [hab] at h hb ⊢
  exact (div_eq_iff hb).1 h