Datasets:

Modalities:
Text
Languages:
English
Libraries:
Datasets
License:
File size: 37,785 Bytes
4365a98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fc5e983
 
 
4365a98
 
 
 
fc5e983
 
 
 
4365a98
fc5e983
 
 
 
 
 
 
 
4365a98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fc5e983
 
 
 
 
 
4365a98
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
/-
Copyright (c) 2020 Anne Baanen. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Nathaniel Thomas, Jeremy Avigad, Johannes HΓΆlzl, Mario Carneiro, Anne Baanen,
  FrΓ©dΓ©ric Dupuis, Heather Macbeth
-/
import algebra.hom.group
import algebra.hom.group_action
import algebra.module.basic
import algebra.module.pi
import algebra.ring.comp_typeclasses
import algebra.star.basic

/-!
# (Semi)linear maps

In this file we define

* `linear_map Οƒ M Mβ‚‚`, `M β†’β‚›β‚—[Οƒ] Mβ‚‚` : a semilinear map between two `module`s. Here,
  `Οƒ` is a `ring_hom` from `R` to `Rβ‚‚` and an `f : M β†’β‚›β‚—[Οƒ] Mβ‚‚` satisfies
  `f (c β€’ x) = (Οƒ c) β€’ (f x)`. We recover plain linear maps by choosing `Οƒ` to be `ring_hom.id R`.
  This is denoted by `M β†’β‚—[R] Mβ‚‚`. We also add the notation `M →ₗ⋆[R] Mβ‚‚` for star-linear maps.

* `is_linear_map R f` : predicate saying that `f : M β†’ Mβ‚‚` is a linear map. (Note that this
  was not generalized to semilinear maps.)

We then provide `linear_map` with the following instances:

* `linear_map.add_comm_monoid` and `linear_map.add_comm_group`: the elementwise addition structures
  corresponding to addition in the codomain
* `linear_map.distrib_mul_action` and `linear_map.module`: the elementwise scalar action structures
  corresponding to applying the action in the codomain.
* `module.End.semiring` and `module.End.ring`: the (semi)ring of endomorphisms formed by taking the
  additive structure above with composition as multiplication.

## Implementation notes

To ensure that composition works smoothly for semilinear maps, we use the typeclasses
`ring_hom_comp_triple`, `ring_hom_inv_pair` and `ring_hom_surjective` from
`algebra/ring/comp_typeclasses`.

## Notation

* Throughout the file, we denote regular linear maps by `fβ‚—`, `gβ‚—`, etc, and semilinear maps
  by `f`, `g`, etc.

## TODO

* Parts of this file have not yet been generalized to semilinear maps (i.e. `compatible_smul`)

## Tags

linear map
-/

open function
open_locale big_operators

universes u u' v w x y z
variables {R : Type*} {R₁ : Type*} {Rβ‚‚ : Type*} {R₃ : Type*}
variables {k : Type*} {S : Type*} {S₃ : Type*} {T : Type*}
variables {M : Type*} {M₁ : Type*} {Mβ‚‚ : Type*} {M₃ : Type*}
variables {N₁ : Type*} {Nβ‚‚ : Type*} {N₃ : Type*} {ΞΉ : Type*}

/-- A map `f` between modules over a semiring is linear if it satisfies the two properties
`f (x + y) = f x + f y` and `f (c β€’ x) = c β€’ f x`. The predicate `is_linear_map R f` asserts this
property. A bundled version is available with `linear_map`, and should be favored over
`is_linear_map` most of the time. -/
structure is_linear_map (R : Type u) {M : Type v} {Mβ‚‚ : Type w}
  [semiring R] [add_comm_monoid M] [add_comm_monoid Mβ‚‚] [module R M] [module R Mβ‚‚]
  (f : M β†’ Mβ‚‚) : Prop :=
(map_add : βˆ€ x y, f (x + y) = f x + f y)
(map_smul : βˆ€ (c : R) x, f (c β€’ x) = c β€’ f x)

section

set_option old_structure_cmd true

/-- A map `f` between an `R`-module and an `S`-module over a ring homomorphism `Οƒ : R β†’+* S`
is semilinear if it satisfies the two properties `f (x + y) = f x + f y` and
`f (c β€’ x) = (Οƒ c) β€’ f x`. Elements of `linear_map Οƒ M Mβ‚‚` (available under the notation
`M β†’β‚›β‚—[Οƒ] Mβ‚‚`) are bundled versions of such maps. For plain linear maps (i.e. for which
`Οƒ = ring_hom.id R`), the notation `M β†’β‚—[R] Mβ‚‚` is available. An unbundled version of plain linear
maps is available with the predicate `is_linear_map`, but it should be avoided most of the time. -/
structure linear_map {R : Type*} {S : Type*} [semiring R] [semiring S] (Οƒ : R β†’+* S)
  (M : Type*) (Mβ‚‚ : Type*)
  [add_comm_monoid M] [add_comm_monoid Mβ‚‚] [module R M] [module S Mβ‚‚]
  extends add_hom M Mβ‚‚ :=
(map_smul' : βˆ€ (r : R) (x : M), to_fun (r β€’ x) = (Οƒ r) β€’ to_fun x)

/-- The `add_hom` underlying a `linear_map`. -/
add_decl_doc linear_map.to_add_hom

notation M ` β†’β‚›β‚—[`:25 Οƒ:25 `] `:0 Mβ‚‚:0 := linear_map Οƒ M Mβ‚‚
notation M ` β†’β‚—[`:25 R:25 `] `:0 Mβ‚‚:0 := linear_map (ring_hom.id R) M Mβ‚‚
notation M ` →ₗ⋆[`:25 R:25 `] `:0 Mβ‚‚:0 := linear_map (star_ring_end R) M Mβ‚‚

/-- `semilinear_map_class F Οƒ M Mβ‚‚` asserts `F` is a type of bundled `Οƒ`-semilinear maps `M β†’ Mβ‚‚`.

See also `linear_map_class F R M Mβ‚‚` for the case where `Οƒ` is the identity map on `R`.

A map `f` between an `R`-module and an `S`-module over a ring homomorphism `Οƒ : R β†’+* S`
is semilinear if it satisfies the two properties `f (x + y) = f x + f y` and
`f (c β€’ x) = (Οƒ c) β€’ f x`. -/
class semilinear_map_class (F : Type*) {R S : out_param Type*} [semiring R] [semiring S]
  (Οƒ : out_param $ R β†’+* S) (M Mβ‚‚ : out_param Type*)
  [add_comm_monoid M] [add_comm_monoid Mβ‚‚] [module R M] [module S Mβ‚‚]
  extends add_hom_class F M Mβ‚‚ :=
(map_smulβ‚›β‚— : βˆ€ (f : F) (r : R) (x : M), f (r β€’ x) = (Οƒ r) β€’ f x)

end

-- `Οƒ` becomes a metavariable but that's fine because it's an `out_param`
attribute [nolint dangerous_instance] semilinear_map_class.to_add_hom_class

export semilinear_map_class (map_smulβ‚›β‚—)
attribute [simp] map_smulβ‚›β‚—

/-- `linear_map_class F R M Mβ‚‚` asserts `F` is a type of bundled `R`-linear maps `M β†’ Mβ‚‚`.

This is an abbreviation for `semilinear_map_class F (ring_hom.id R) M Mβ‚‚`.
-/
abbreviation linear_map_class (F : Type*) (R M Mβ‚‚ : out_param Type*)
  [semiring R] [add_comm_monoid M] [add_comm_monoid Mβ‚‚] [module R M] [module R Mβ‚‚] :=
semilinear_map_class F (ring_hom.id R) M Mβ‚‚

namespace semilinear_map_class

variables (F : Type*)
variables [semiring R] [semiring S]
variables [add_comm_monoid M] [add_comm_monoid M₁] [add_comm_monoid Mβ‚‚] [add_comm_monoid M₃]
variables [add_comm_monoid N₁] [add_comm_monoid Nβ‚‚] [add_comm_monoid N₃]
variables [module R M] [module R Mβ‚‚] [module S M₃]
variables {Οƒ : R β†’+* S}

@[priority 100, nolint dangerous_instance] -- `Οƒ` is an `out_param` so it's not dangerous
instance [semilinear_map_class F Οƒ M M₃] : add_monoid_hom_class F M M₃ :=
{ coe := Ξ» f, (f : M β†’ M₃),
  map_zero := Ξ» f, show f 0 = 0, by { rw [← zero_smul R (0 : M), map_smulβ‚›β‚—], simp },
  .. semilinear_map_class.to_add_hom_class F Οƒ M M₃ }

@[priority 100, nolint dangerous_instance] -- `R` is an `out_param` so it's not dangerous
instance [linear_map_class F R M Mβ‚‚] : distrib_mul_action_hom_class F R M Mβ‚‚ :=
{ coe := Ξ» f, (f : M β†’ Mβ‚‚),
  map_smul := Ξ» f c x, by rw [map_smulβ‚›β‚—, ring_hom.id_apply],
  .. semilinear_map_class.add_monoid_hom_class F }

variables {F} (f : F) [i : semilinear_map_class F Οƒ M M₃]
include i

lemma map_smul_inv {Οƒ' : S β†’+* R} [ring_hom_inv_pair Οƒ Οƒ'] (c : S) (x : M) :
  c β€’ f x = f (Οƒ' c β€’ x) :=
by simp

end semilinear_map_class

namespace linear_map

section add_comm_monoid

variables [semiring R] [semiring S]

section
variables [add_comm_monoid M] [add_comm_monoid M₁] [add_comm_monoid Mβ‚‚] [add_comm_monoid M₃]
variables [add_comm_monoid N₁] [add_comm_monoid Nβ‚‚] [add_comm_monoid N₃]
variables [module R M] [module R Mβ‚‚] [module S M₃]
variables {Οƒ : R β†’+* S}

instance : semilinear_map_class (M β†’β‚›β‚—[Οƒ] M₃) Οƒ M M₃ :=
{ coe := linear_map.to_fun,
  coe_injective' := Ξ» f g h, by cases f; cases g; congr',
  map_add := linear_map.map_add',
  map_smulβ‚›β‚— := linear_map.map_smul' }

/-- Helper instance for when there's too many metavariables to apply `fun_like.has_coe_to_fun`
directly.
-/
instance : has_coe_to_fun (M β†’β‚›β‚—[Οƒ] M₃) (Ξ» _, M β†’ M₃) := ⟨λ f, f⟩

/-- The `distrib_mul_action_hom` underlying a `linear_map`. -/
def to_distrib_mul_action_hom (f : M β†’β‚—[R] Mβ‚‚) : distrib_mul_action_hom R M Mβ‚‚ :=
{ map_zero' := show f 0 = 0, from map_zero f, ..f }

@[simp] lemma to_fun_eq_coe {f : M β†’β‚›β‚—[Οƒ] M₃} : f.to_fun = (f : M β†’ M₃) := rfl

@[ext] theorem ext {f g : M β†’β‚›β‚—[Οƒ] M₃} (h : βˆ€ x, f x = g x) : f = g := fun_like.ext f g h

/-- Copy of a `linear_map` with a new `to_fun` equal to the old one. Useful to fix definitional
equalities. -/
protected def copy (f : M β†’β‚›β‚—[Οƒ] M₃) (f' : M β†’ M₃) (h : f' = ⇑f) : M β†’β‚›β‚—[Οƒ] M₃ :=
{ to_fun := f',
  map_add' := h.symm β–Έ f.map_add',
  map_smul' := h.symm β–Έ f.map_smul' }

/-- See Note [custom simps projection]. -/
protected def simps.apply {R S : Type*} [semiring R] [semiring S] (Οƒ : R β†’+* S)
  (M M₃ : Type*) [add_comm_monoid M] [add_comm_monoid M₃] [module R M] [module S M₃]
  (f : M β†’β‚›β‚—[Οƒ] M₃) : M β†’ M₃ := f

initialize_simps_projections linear_map (to_fun β†’ apply)

@[simp] lemma coe_mk {Οƒ : R β†’+* S} (f : M β†’ M₃) (h₁ hβ‚‚) :
  ((linear_map.mk f h₁ hβ‚‚ : M β†’β‚›β‚—[Οƒ] M₃) : M β†’ M₃) = f := rfl

/-- Identity map as a `linear_map` -/
def id : M β†’β‚—[R] M :=
{ to_fun := id, ..distrib_mul_action_hom.id R }

lemma id_apply (x : M) :
  @id R M _ _ _ x = x := rfl

@[simp, norm_cast] lemma id_coe : ((linear_map.id : M β†’β‚—[R] M) : M β†’ M) = _root_.id := rfl

end

section
variables [add_comm_monoid M] [add_comm_monoid M₁] [add_comm_monoid Mβ‚‚] [add_comm_monoid M₃]
variables [add_comm_monoid N₁] [add_comm_monoid Nβ‚‚] [add_comm_monoid N₃]
variables [module R M] [module R Mβ‚‚] [module S M₃]
variables (Οƒ : R β†’+* S)
variables (fβ‚— gβ‚— : M β†’β‚—[R] Mβ‚‚) (f g : M β†’β‚›β‚—[Οƒ] M₃)

theorem is_linear : is_linear_map R fβ‚— := ⟨fβ‚—.map_add', fβ‚—.map_smul'⟩

variables {fβ‚— gβ‚— f g Οƒ}

theorem coe_injective : @injective (M β†’β‚›β‚—[Οƒ] M₃) (M β†’ M₃) coe_fn :=
fun_like.coe_injective

protected lemma congr_arg {x x' : M} : x = x' β†’ f x = f x' :=
fun_like.congr_arg f

/-- If two linear maps are equal, they are equal at each point. -/
protected lemma congr_fun (h : f = g) (x : M) : f x = g x :=
fun_like.congr_fun h x

theorem ext_iff : f = g ↔ βˆ€ x, f x = g x :=
fun_like.ext_iff

@[simp] lemma mk_coe (f : M β†’β‚›β‚—[Οƒ] M₃) (h₁ hβ‚‚) :
  (linear_map.mk f h₁ hβ‚‚ : M β†’β‚›β‚—[Οƒ] M₃) = f := ext $ Ξ» _, rfl

variables (fβ‚— gβ‚— f g)

protected lemma map_add (x y : M) : f (x + y) = f x + f y := map_add f x y
protected lemma map_zero : f 0 = 0 := map_zero f
-- TODO: `simp` isn't picking up `map_smulβ‚›β‚—` for `linear_map`s without specifying `map_smulβ‚›β‚— f`
@[simp] protected lemma map_smulβ‚›β‚— (c : R) (x : M) : f (c β€’ x) = (Οƒ c) β€’ f x := map_smulβ‚›β‚— f c x
protected lemma map_smul (c : R) (x : M) : fβ‚— (c β€’ x) = c β€’ fβ‚— x := map_smul fβ‚— c x
protected lemma map_smul_inv {Οƒ' : S β†’+* R} [ring_hom_inv_pair Οƒ Οƒ'] (c : S) (x : M) :
  c β€’ f x = f (Οƒ' c β€’ x) :=
by simp

-- TODO: generalize to `zero_hom_class`
@[simp] lemma map_eq_zero_iff (h : function.injective f) {x : M} : f x = 0 ↔ x = 0 :=
⟨λ w, by { apply h, simp [w], }, λ w, by { subst w, simp, }⟩

section pointwise
open_locale pointwise

variables (M M₃ Οƒ) {F : Type*} (h : F)

@[simp] lemma _root_.image_smul_setβ‚›β‚— [semilinear_map_class F Οƒ M M₃] (c : R) (s : set M) :
  h '' (c β€’ s) = (Οƒ c) β€’ h '' s :=
begin
  apply set.subset.antisymm,
  { rintros x ⟨y, ⟨z, zs, rfl⟩, rfl⟩,
    exact ⟨h z, set.mem_image_of_mem _ zs, (map_smulβ‚›β‚— _ _ _).symm ⟩ },
  { rintros x ⟨y, ⟨z, hz, rfl⟩, rfl⟩,
    exact (set.mem_image _ _ _).2 ⟨c β€’ z, set.smul_mem_smul_set hz, map_smulβ‚›β‚— _ _ _⟩ }
end

lemma _root_.preimage_smul_setβ‚›β‚— [semilinear_map_class F Οƒ M M₃] {c : R} (hc : is_unit c)
  (s : set M₃) : h ⁻¹' (Οƒ c β€’ s) = c β€’ h ⁻¹' s :=
begin
  apply set.subset.antisymm,
  { rintros x ⟨y, ys, hy⟩,
    refine ⟨(hc.unit.inv : R) β€’ x, _, _⟩,
    { simp only [←hy, smul_smul, set.mem_preimage, units.inv_eq_coe_inv, map_smulβ‚›β‚— h, ← map_mul,
        is_unit.coe_inv_mul, one_smul, map_one, ys] },
    { simp only [smul_smul, is_unit.mul_coe_inv, one_smul, units.inv_eq_coe_inv] } },
  { rintros x ⟨y, hy, rfl⟩,
    refine ⟨h y, hy, by simp only [ring_hom.id_apply, map_smulβ‚›β‚— h]⟩ }
end

variables (R Mβ‚‚)

lemma _root_.image_smul_set [linear_map_class F R M Mβ‚‚] (c : R) (s : set M) :
  h '' (c β€’ s) = c β€’ h '' s :=
image_smul_setβ‚›β‚— _ _ _ h c s

lemma _root_.preimage_smul_set [linear_map_class F R M Mβ‚‚] {c : R} (hc : is_unit c) (s : set Mβ‚‚) :
  h ⁻¹' (c β€’ s) = c β€’ h ⁻¹' s :=
preimage_smul_setβ‚›β‚— _ _ _ h hc s

end pointwise

variables (M Mβ‚‚)
/--
A typeclass for `has_smul` structures which can be moved through a `linear_map`.
This typeclass is generated automatically from a `is_scalar_tower` instance, but exists so that
we can also add an instance for `add_comm_group.int_module`, allowing `z β€’` to be moved even if
`R` does not support negation.
-/
class compatible_smul (R S : Type*) [semiring S] [has_smul R M]
  [module S M] [has_smul R Mβ‚‚] [module S Mβ‚‚] :=
(map_smul : βˆ€ (fβ‚— : M β†’β‚—[S] Mβ‚‚) (c : R) (x : M), fβ‚— (c β€’ x) = c β€’ fβ‚— x)
variables {M Mβ‚‚}

@[priority 100]
instance is_scalar_tower.compatible_smul
  {R S : Type*} [semiring S] [has_smul R S]
  [has_smul R M] [module S M] [is_scalar_tower R S M]
  [has_smul R Mβ‚‚] [module S Mβ‚‚] [is_scalar_tower R S Mβ‚‚] : compatible_smul M Mβ‚‚ R S :=
⟨λ fβ‚— c x, by rw [← smul_one_smul S c x, ← smul_one_smul S c (fβ‚— x), map_smul]⟩

@[simp, priority 900]
lemma map_smul_of_tower {R S : Type*} [semiring S] [has_smul R M]
  [module S M] [has_smul R Mβ‚‚] [module S Mβ‚‚]
  [compatible_smul M Mβ‚‚ R S] (fβ‚— : M β†’β‚—[S] Mβ‚‚) (c : R) (x : M) :
  fβ‚— (c β€’ x) = c β€’ fβ‚— x :=
compatible_smul.map_smul fβ‚— c x

/-- convert a linear map to an additive map -/
def to_add_monoid_hom : M β†’+ M₃ :=
{ to_fun := f,
  map_zero' := f.map_zero,
  map_add' := f.map_add }

@[simp] lemma to_add_monoid_hom_coe : ⇑f.to_add_monoid_hom = f := rfl

section restrict_scalars

variables (R) [module S M] [module S Mβ‚‚] [compatible_smul M Mβ‚‚ R S]

/-- If `M` and `Mβ‚‚` are both `R`-modules and `S`-modules and `R`-module structures
are defined by an action of `R` on `S` (formally, we have two scalar towers), then any `S`-linear
map from `M` to `Mβ‚‚` is `R`-linear.

See also `linear_map.map_smul_of_tower`. -/
def restrict_scalars (fβ‚— : M β†’β‚—[S] Mβ‚‚) : M β†’β‚—[R] Mβ‚‚ :=
{ to_fun := fβ‚—,
  map_add' := fβ‚—.map_add,
  map_smul' := fβ‚—.map_smul_of_tower }

@[simp] lemma coe_restrict_scalars (fβ‚— : M β†’β‚—[S] Mβ‚‚) : ⇑(restrict_scalars R fβ‚—) = fβ‚— :=
rfl

lemma restrict_scalars_apply (fβ‚— : M β†’β‚—[S] Mβ‚‚) (x) : restrict_scalars R fβ‚— x = fβ‚— x :=
rfl

lemma restrict_scalars_injective :
  function.injective (restrict_scalars R : (M β†’β‚—[S] Mβ‚‚) β†’ (M β†’β‚—[R] Mβ‚‚)) :=
Ξ» fβ‚— gβ‚— h, ext (linear_map.congr_fun h : _)

@[simp]
lemma restrict_scalars_inj (fβ‚— gβ‚— : M β†’β‚—[S] Mβ‚‚) :
  fβ‚—.restrict_scalars R = gβ‚—.restrict_scalars R ↔ fβ‚— = gβ‚— :=
(restrict_scalars_injective R).eq_iff

end restrict_scalars

variable {R}

@[simp] lemma map_sum {ΞΉ} {t : finset ΞΉ} {g : ΞΉ β†’ M} :
  f (βˆ‘ i in t, g i) = (βˆ‘ i in t, f (g i)) :=
f.to_add_monoid_hom.map_sum _ _

theorem to_add_monoid_hom_injective :
  function.injective (to_add_monoid_hom : (M β†’β‚›β‚—[Οƒ] M₃) β†’ (M β†’+ M₃)) :=
Ξ» f g h, ext $ add_monoid_hom.congr_fun h

/-- If two `Οƒ`-linear maps from `R` are equal on `1`, then they are equal. -/
@[ext] theorem ext_ring {f g : R β†’β‚›β‚—[Οƒ] M₃} (h : f 1 = g 1) : f = g :=
ext $ Ξ» x, by rw [← mul_one x, ← smul_eq_mul, f.map_smulβ‚›β‚—, g.map_smulβ‚›β‚—, h]

theorem ext_ring_iff {Οƒ : R β†’+* R} {f g : R β†’β‚›β‚—[Οƒ] M} : f = g ↔ f 1 = g 1 :=
⟨λ h, h β–Έ rfl, ext_ring⟩

@[ext] theorem ext_ring_op {Οƒ : Rᡐᡒᡖ β†’+* S} {f g : R β†’β‚›β‚—[Οƒ] M₃} (h : f 1 = g 1) : f = g :=
ext $ Ξ» x, by rw [← one_mul x, ← op_smul_eq_mul, f.map_smulβ‚›β‚—, g.map_smulβ‚›β‚—, h]

end

/-- Interpret a `ring_hom` `f` as an `f`-semilinear map. -/
@[simps]
def _root_.ring_hom.to_semilinear_map (f : R β†’+* S) : R β†’β‚›β‚—[f] S :=
{ to_fun := f,
  map_smul' := f.map_mul,
  .. f}

section

variables [semiring R₁] [semiring Rβ‚‚] [semiring R₃]
variables [add_comm_monoid M] [add_comm_monoid M₁] [add_comm_monoid Mβ‚‚] [add_comm_monoid M₃]
variables {module_M₁ : module R₁ M₁} {module_Mβ‚‚ : module Rβ‚‚ Mβ‚‚} {module_M₃ : module R₃ M₃}
variables {σ₁₂ : R₁ β†’+* Rβ‚‚} {σ₂₃ : Rβ‚‚ β†’+* R₃} {σ₁₃ : R₁ β†’+* R₃}
variables [ring_hom_comp_triple σ₁₂ σ₂₃ σ₁₃]
variables (f : Mβ‚‚ β†’β‚›β‚—[σ₂₃] M₃) (g : M₁ β†’β‚›β‚—[σ₁₂] Mβ‚‚)

include module_M₁ module_Mβ‚‚ module_M₃
/-- Composition of two linear maps is a linear map -/
def comp : M₁ β†’β‚›β‚—[σ₁₃] M₃ :=
{ to_fun := f ∘ g,
  map_add' := by simp only [map_add, forall_const, eq_self_iff_true, comp_app],
  map_smul' := Ξ» r x, by rw [comp_app, map_smulβ‚›β‚—, map_smulβ‚›β‚—, ring_hom_comp_triple.comp_apply] }
omit module_M₁ module_Mβ‚‚ module_M₃

infixr ` βˆ˜β‚— `:80 := @linear_map.comp _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
  (ring_hom.id _) (ring_hom.id _) (ring_hom.id _) ring_hom_comp_triple.ids

include σ₁₃
lemma comp_apply (x : M₁) : f.comp g x = f (g x) := rfl
omit σ₁₃

include σ₁₃
@[simp, norm_cast] lemma coe_comp : (f.comp g : M₁ β†’ M₃) = f ∘ g := rfl
omit σ₁₃

@[simp] theorem comp_id : f.comp id = f :=
linear_map.ext $ Ξ» x, rfl

@[simp] theorem id_comp : id.comp f = f :=
linear_map.ext $ Ξ» x, rfl

variables {f g} {f' : Mβ‚‚ β†’β‚›β‚—[σ₂₃] M₃} {g' : M₁ β†’β‚›β‚—[σ₁₂] Mβ‚‚}

include σ₁₃
theorem cancel_right (hg : function.surjective g) :
  f.comp g = f'.comp g ↔ f = f' :=
⟨λ h, ext $ hg.forall.2 (ext_iff.1 h), Ξ» h, h β–Έ rfl⟩

theorem cancel_left (hf : function.injective f) :
  f.comp g = f.comp g' ↔ g = g' :=
⟨λ h, ext $ Ξ» x, hf $ by rw [← comp_apply, h, comp_apply], Ξ» h, h β–Έ rfl⟩
omit σ₁₃

end

variables [add_comm_monoid M] [add_comm_monoid M₁] [add_comm_monoid Mβ‚‚] [add_comm_monoid M₃]

/-- If a function `g` is a left and right inverse of a linear map `f`, then `g` is linear itself. -/
def inverse [module R M] [module S Mβ‚‚] {Οƒ : R β†’+* S} {Οƒ' : S β†’+* R} [ring_hom_inv_pair Οƒ Οƒ']
  (f : M β†’β‚›β‚—[Οƒ] Mβ‚‚) (g : Mβ‚‚ β†’ M) (h₁ : left_inverse g f) (hβ‚‚ : right_inverse g f) :
  Mβ‚‚ β†’β‚›β‚—[Οƒ'] M :=
by dsimp [left_inverse, function.right_inverse] at h₁ hβ‚‚; exact
  { to_fun := g,
    map_add' := Ξ» x y, by { rw [← h₁ (g (x + y)), ← h₁ (g x + g y)]; simp [hβ‚‚] },
    map_smul' := Ξ» a b, by { rw [← h₁ (g (a β€’ b)), ← h₁ ((Οƒ' a) β€’ g b)], simp [hβ‚‚] } }

end add_comm_monoid

section add_comm_group

variables [semiring R] [semiring S] [add_comm_group M] [add_comm_group Mβ‚‚]
variables {module_M : module R M} {module_Mβ‚‚ : module S Mβ‚‚} {Οƒ : R β†’+* S}
variables (f : M β†’β‚›β‚—[Οƒ] Mβ‚‚)

protected lemma map_neg (x : M) : f (- x) = - f x := map_neg f x

protected lemma map_sub (x y : M) : f (x - y) = f x - f y := map_sub f x y

instance compatible_smul.int_module
  {S : Type*} [semiring S] [module S M] [module S Mβ‚‚] : compatible_smul M Mβ‚‚ β„€ S :=
⟨λ fβ‚— c x, begin
  induction c using int.induction_on,
  case hz : { simp },
  case hp : n ih { simp [add_smul, ih] },
  case hn : n ih { simp [sub_smul, ih] }
end⟩

instance compatible_smul.units {R S : Type*}
  [monoid R] [mul_action R M] [mul_action R Mβ‚‚] [semiring S] [module S M] [module S Mβ‚‚]
  [compatible_smul M Mβ‚‚ R S] :
  compatible_smul M Mβ‚‚ RΛ£ S :=
⟨λ fβ‚— c x, (compatible_smul.map_smul fβ‚— (c : R) x : _)⟩

end add_comm_group

end linear_map

namespace module

/-- `g : R β†’+* S` is `R`-linear when the module structure on `S` is `module.comp_hom S g` . -/
@[simps]
def comp_hom.to_linear_map {R S : Type*} [semiring R] [semiring S] (g : R β†’+* S) :
  (by haveI := comp_hom S g; exact (R β†’β‚—[R] S)) :=
by exact
{ to_fun := (g : R β†’ S),
  map_add' := g.map_add,
  map_smul' := g.map_mul }

end module

namespace distrib_mul_action_hom

variables [semiring R] [add_comm_monoid M] [add_comm_monoid Mβ‚‚] [module R M] [module R Mβ‚‚]

/-- A `distrib_mul_action_hom` between two modules is a linear map. -/
def to_linear_map (fβ‚— : M β†’+[R] Mβ‚‚) : M β†’β‚—[R] Mβ‚‚ := { ..fβ‚— }

instance : has_coe (M β†’+[R] Mβ‚‚) (M β†’β‚—[R] Mβ‚‚) := ⟨to_linear_map⟩

@[simp] lemma to_linear_map_eq_coe (f : M β†’+[R] Mβ‚‚) :
  f.to_linear_map = ↑f :=
rfl

@[simp, norm_cast] lemma coe_to_linear_map (f : M β†’+[R] Mβ‚‚) :
  ((f : M β†’β‚—[R] Mβ‚‚) : M β†’ Mβ‚‚) = f :=
rfl

lemma to_linear_map_injective {f g : M β†’+[R] Mβ‚‚} (h : (f : M β†’β‚—[R] Mβ‚‚) = (g : M β†’β‚—[R] Mβ‚‚)) :
  f = g :=
by { ext m, exact linear_map.congr_fun h m, }

end distrib_mul_action_hom

namespace is_linear_map

section add_comm_monoid
variables [semiring R] [add_comm_monoid M] [add_comm_monoid Mβ‚‚]
variables [module R M] [module R Mβ‚‚]
include R

/-- Convert an `is_linear_map` predicate to a `linear_map` -/
def mk' (f : M β†’ Mβ‚‚) (H : is_linear_map R f) : M β†’β‚—[R] Mβ‚‚ :=
{ to_fun := f, map_add' := H.1, map_smul' := H.2 }

@[simp] theorem mk'_apply {f : M β†’ Mβ‚‚} (H : is_linear_map R f) (x : M) :
  mk' f H x = f x := rfl

lemma is_linear_map_smul {R M : Type*} [comm_semiring R] [add_comm_monoid M] [module R M]
  (c : R) :
  is_linear_map R (Ξ» (z : M), c β€’ z) :=
begin
  refine is_linear_map.mk (smul_add c) _,
  intros _ _,
  simp only [smul_smul, mul_comm]
end

lemma is_linear_map_smul' {R M : Type*} [semiring R] [add_comm_monoid M] [module R M] (a : M) :
  is_linear_map R (Ξ» (c : R), c β€’ a) :=
is_linear_map.mk (Ξ» x y, add_smul x y a) (Ξ» x y, mul_smul x y a)

variables {f : M β†’ Mβ‚‚} (lin : is_linear_map R f)
include M Mβ‚‚ lin

lemma map_zero : f (0 : M) = (0 : Mβ‚‚) := (lin.mk' f).map_zero

end add_comm_monoid

section add_comm_group

variables [semiring R] [add_comm_group M] [add_comm_group Mβ‚‚]
variables [module R M] [module R Mβ‚‚]
include R

lemma is_linear_map_neg :
  is_linear_map R (Ξ» (z : M), -z) :=
is_linear_map.mk neg_add (Ξ» x y, (smul_neg x y).symm)

variables {f : M β†’ Mβ‚‚} (lin : is_linear_map R f)
include M Mβ‚‚ lin

lemma map_neg (x : M) : f (- x) = - f x := (lin.mk' f).map_neg x

lemma map_sub (x y) : f (x - y) = f x - f y := (lin.mk' f).map_sub x y

end add_comm_group

end is_linear_map

/-- Linear endomorphisms of a module, with associated ring structure
`module.End.semiring` and algebra structure `module.End.algebra`. -/
abbreviation module.End (R : Type u) (M : Type v)
  [semiring R] [add_comm_monoid M] [module R M] := M β†’β‚—[R] M

/-- Reinterpret an additive homomorphism as a `β„•`-linear map. -/
def add_monoid_hom.to_nat_linear_map [add_comm_monoid M] [add_comm_monoid Mβ‚‚] (f : M β†’+ Mβ‚‚) :
  M β†’β‚—[β„•] Mβ‚‚ :=
{ to_fun := f, map_add' := f.map_add, map_smul' := map_nsmul f }

lemma add_monoid_hom.to_nat_linear_map_injective [add_comm_monoid M] [add_comm_monoid Mβ‚‚] :
  function.injective (@add_monoid_hom.to_nat_linear_map M Mβ‚‚ _ _) :=
by { intros f g h, ext, exact linear_map.congr_fun h x }

/-- Reinterpret an additive homomorphism as a `β„€`-linear map. -/
def add_monoid_hom.to_int_linear_map [add_comm_group M] [add_comm_group Mβ‚‚] (f : M β†’+ Mβ‚‚) :
  M β†’β‚—[β„€] Mβ‚‚ :=
{ to_fun := f, map_add' := f.map_add, map_smul' := map_zsmul f }

lemma add_monoid_hom.to_int_linear_map_injective [add_comm_group M] [add_comm_group Mβ‚‚] :
  function.injective (@add_monoid_hom.to_int_linear_map M Mβ‚‚ _ _) :=
by { intros f g h, ext, exact linear_map.congr_fun h x }

@[simp] lemma add_monoid_hom.coe_to_int_linear_map [add_comm_group M] [add_comm_group Mβ‚‚]
  (f : M β†’+ Mβ‚‚) :
  ⇑f.to_int_linear_map = f := rfl

/-- Reinterpret an additive homomorphism as a `β„š`-linear map. -/
def add_monoid_hom.to_rat_linear_map [add_comm_group M] [module β„š M]
  [add_comm_group Mβ‚‚] [module β„š Mβ‚‚] (f : M β†’+ Mβ‚‚) :
  M β†’β‚—[β„š] Mβ‚‚ :=
{ map_smul' := map_rat_smul f, ..f }

lemma add_monoid_hom.to_rat_linear_map_injective
  [add_comm_group M] [module β„š M] [add_comm_group Mβ‚‚] [module β„š Mβ‚‚] :
  function.injective (@add_monoid_hom.to_rat_linear_map M Mβ‚‚ _ _ _ _) :=
by { intros f g h, ext, exact linear_map.congr_fun h x }

@[simp] lemma add_monoid_hom.coe_to_rat_linear_map [add_comm_group M] [module β„š M]
  [add_comm_group Mβ‚‚] [module β„š Mβ‚‚] (f : M β†’+ Mβ‚‚) :
  ⇑f.to_rat_linear_map = f := rfl

namespace linear_map

section has_smul

variables [semiring R] [semiring Rβ‚‚] [semiring R₃]
variables [add_comm_monoid M] [add_comm_monoid Mβ‚‚] [add_comm_monoid M₃]
variables [module R M] [module Rβ‚‚ Mβ‚‚] [module R₃ M₃]
variables {σ₁₂ : R β†’+* Rβ‚‚} {σ₂₃ : Rβ‚‚ β†’+* R₃} {σ₁₃ : R β†’+* R₃} [ring_hom_comp_triple σ₁₂ σ₂₃ σ₁₃]
variables [monoid S] [distrib_mul_action S Mβ‚‚] [smul_comm_class Rβ‚‚ S Mβ‚‚]
variables [monoid S₃] [distrib_mul_action S₃ M₃] [smul_comm_class R₃ S₃ M₃]
variables [monoid T] [distrib_mul_action T Mβ‚‚] [smul_comm_class Rβ‚‚ T Mβ‚‚]

instance : has_smul S (M β†’β‚›β‚—[σ₁₂] Mβ‚‚) :=
⟨λ a f, { to_fun := a β€’ f,
          map_add' := Ξ» x y, by simp only [pi.smul_apply, f.map_add, smul_add],
          map_smul' := Ξ» c x, by simp [pi.smul_apply, smul_comm (σ₁₂ c)] }⟩

@[simp] lemma smul_apply (a : S) (f : M β†’β‚›β‚—[σ₁₂] Mβ‚‚) (x : M) : (a β€’ f) x = a β€’ f x := rfl

lemma coe_smul (a : S) (f : M β†’β‚›β‚—[σ₁₂] Mβ‚‚) : ⇑(a β€’ f) = a β€’ f := rfl

instance [smul_comm_class S T Mβ‚‚] : smul_comm_class S T (M β†’β‚›β‚—[σ₁₂] Mβ‚‚) :=
⟨λ a b f, ext $ λ x, smul_comm _ _ _⟩

-- example application of this instance: if S -> T -> R are homomorphisms of commutative rings and
-- M and Mβ‚‚ are R-modules then the S-module and T-module structures on Hom_R(M,Mβ‚‚) are compatible.
instance [has_smul S T] [is_scalar_tower S T Mβ‚‚] : is_scalar_tower S T (M β†’β‚›β‚—[σ₁₂] Mβ‚‚) :=
{ smul_assoc := Ξ» _ _ _, ext $ Ξ» _, smul_assoc _ _ _ }

instance [distrib_mul_action Sᡐᡒᡖ Mβ‚‚] [smul_comm_class Rβ‚‚ Sᡐᡒᡖ Mβ‚‚] [is_central_scalar S Mβ‚‚] :
  is_central_scalar S (M β†’β‚›β‚—[σ₁₂] Mβ‚‚) :=
{ op_smul_eq_smul := Ξ» a b, ext $ Ξ» x, op_smul_eq_smul _ _ }

end has_smul

/-! ### Arithmetic on the codomain -/
section arithmetic

variables [semiring R₁] [semiring Rβ‚‚] [semiring R₃]
variables [add_comm_monoid M] [add_comm_monoid Mβ‚‚] [add_comm_monoid M₃]
variables [add_comm_group N₁] [add_comm_group Nβ‚‚] [add_comm_group N₃]
variables [module R₁ M] [module Rβ‚‚ Mβ‚‚] [module R₃ M₃]
variables [module R₁ N₁] [module Rβ‚‚ Nβ‚‚] [module R₃ N₃]
variables {σ₁₂ : R₁ β†’+* Rβ‚‚} {σ₂₃ : Rβ‚‚ β†’+* R₃} {σ₁₃ : R₁ β†’+* R₃} [ring_hom_comp_triple σ₁₂ σ₂₃ σ₁₃]

/-- The constant 0 map is linear. -/
instance : has_zero (M β†’β‚›β‚—[σ₁₂] Mβ‚‚) :=
⟨{ to_fun := 0, map_add' := by simp, map_smul' := by simp }⟩

@[simp] lemma zero_apply (x : M) : (0 : M β†’β‚›β‚—[σ₁₂] Mβ‚‚) x = 0 := rfl

@[simp] theorem comp_zero (g : Mβ‚‚ β†’β‚›β‚—[σ₂₃] M₃) : (g.comp (0 : M β†’β‚›β‚—[σ₁₂] Mβ‚‚) : M β†’β‚›β‚—[σ₁₃] M₃) = 0 :=
ext $ assume c, by rw [comp_apply, zero_apply, zero_apply, g.map_zero]

@[simp] theorem zero_comp (f : M β†’β‚›β‚—[σ₁₂] Mβ‚‚) : ((0 : Mβ‚‚ β†’β‚›β‚—[σ₂₃] M₃).comp f : M β†’β‚›β‚—[σ₁₃] M₃) = 0 :=
rfl

instance : inhabited (M β†’β‚›β‚—[σ₁₂] Mβ‚‚) := ⟨0⟩

@[simp] lemma default_def : (default : (M β†’β‚›β‚—[σ₁₂] Mβ‚‚)) = 0 := rfl

/-- The sum of two linear maps is linear. -/
instance : has_add (M β†’β‚›β‚—[σ₁₂] Mβ‚‚) :=
⟨λ f g, { to_fun := f + g,
          map_add' := by simp [add_comm, add_left_comm],
          map_smul' := by simp [smul_add] }⟩

@[simp] lemma add_apply (f g : M β†’β‚›β‚—[σ₁₂] Mβ‚‚) (x : M) : (f + g) x = f x + g x := rfl

lemma add_comp (f : M β†’β‚›β‚—[σ₁₂] Mβ‚‚) (g h : Mβ‚‚ β†’β‚›β‚—[σ₂₃] M₃) :
  ((h + g).comp f : M β†’β‚›β‚—[σ₁₃] M₃) = h.comp f + g.comp f := rfl

lemma comp_add (f g : M β†’β‚›β‚—[σ₁₂] Mβ‚‚) (h : Mβ‚‚ β†’β‚›β‚—[σ₂₃] M₃) :
  (h.comp (f + g) : M β†’β‚›β‚—[σ₁₃] M₃) = h.comp f + h.comp g :=
ext $ Ξ» _, h.map_add _ _

/-- The type of linear maps is an additive monoid. -/
instance : add_comm_monoid (M β†’β‚›β‚—[σ₁₂] Mβ‚‚) :=
fun_like.coe_injective.add_comm_monoid _ rfl (Ξ» _ _, rfl) (Ξ» _ _, rfl)

/-- The negation of a linear map is linear. -/
instance : has_neg (M β†’β‚›β‚—[σ₁₂] Nβ‚‚) :=
⟨λ f, { to_fun := -f, map_add' := by simp [add_comm], map_smul' := by simp }⟩

@[simp] lemma neg_apply (f : M β†’β‚›β‚—[σ₁₂] Nβ‚‚) (x : M) : (- f) x = - f x := rfl

include σ₁₃
@[simp] lemma neg_comp (f : M β†’β‚›β‚—[σ₁₂] Mβ‚‚) (g : Mβ‚‚ β†’β‚›β‚—[σ₂₃] N₃) : (- g).comp f = - g.comp f := rfl

@[simp] lemma comp_neg (f : M β†’β‚›β‚—[σ₁₂] Nβ‚‚) (g : Nβ‚‚ β†’β‚›β‚—[σ₂₃] N₃) : g.comp (- f) = - g.comp f :=
ext $ Ξ» _, g.map_neg _
omit σ₁₃

/-- The negation of a linear map is linear. -/
instance : has_sub (M β†’β‚›β‚—[σ₁₂] Nβ‚‚) :=
⟨λ f g, { to_fun := f - g,
          map_add' := Ξ» x y, by simp only [pi.sub_apply, map_add, add_sub_add_comm],
          map_smul' := λ r x, by simp [pi.sub_apply, map_smul, smul_sub] }⟩

@[simp] lemma sub_apply (f g : M β†’β‚›β‚—[σ₁₂] Nβ‚‚) (x : M) : (f - g) x = f x - g x := rfl

include σ₁₃
lemma sub_comp (f : M β†’β‚›β‚—[σ₁₂] Mβ‚‚) (g h : Mβ‚‚ β†’β‚›β‚—[σ₂₃] N₃) :
  (g - h).comp f = g.comp f - h.comp f := rfl

lemma comp_sub (f g : M β†’β‚›β‚—[σ₁₂] Nβ‚‚) (h : Nβ‚‚ β†’β‚›β‚—[σ₂₃] N₃) :
  h.comp (g - f) = h.comp g - h.comp f :=
ext $ Ξ» _, h.map_sub _ _
omit σ₁₃

/-- The type of linear maps is an additive group. -/
instance : add_comm_group (M β†’β‚›β‚—[σ₁₂] Nβ‚‚) :=
fun_like.coe_injective.add_comm_group _
  rfl (Ξ» _ _, rfl) (Ξ» _, rfl) (Ξ» _ _, rfl) (Ξ» _ _, rfl)  (Ξ» _ _, rfl)

end arithmetic

section actions

variables [semiring R] [semiring Rβ‚‚] [semiring R₃]
variables [add_comm_monoid M] [add_comm_monoid Mβ‚‚] [add_comm_monoid M₃]
variables [module R M] [module Rβ‚‚ Mβ‚‚] [module R₃ M₃]
variables {σ₁₂ : R β†’+* Rβ‚‚} {σ₂₃ : Rβ‚‚ β†’+* R₃} {σ₁₃ : R β†’+* R₃} [ring_hom_comp_triple σ₁₂ σ₂₃ σ₁₃]

section has_smul
variables [monoid S] [distrib_mul_action S Mβ‚‚] [smul_comm_class Rβ‚‚ S Mβ‚‚]
variables [monoid S₃] [distrib_mul_action S₃ M₃] [smul_comm_class R₃ S₃ M₃]
variables [monoid T] [distrib_mul_action T Mβ‚‚] [smul_comm_class Rβ‚‚ T Mβ‚‚]

instance : distrib_mul_action S (M β†’β‚›β‚—[σ₁₂] Mβ‚‚) :=
{ one_smul := Ξ» f, ext $ Ξ» _, one_smul _ _,
  mul_smul := Ξ» c c' f, ext $ Ξ» _, mul_smul _ _ _,
  smul_add := Ξ» c f g, ext $ Ξ» x, smul_add _ _ _,
  smul_zero := Ξ» c, ext $ Ξ» x, smul_zero _ }

include σ₁₃
theorem smul_comp (a : S₃) (g : Mβ‚‚ β†’β‚›β‚—[σ₂₃] M₃) (f : M β†’β‚›β‚—[σ₁₂] Mβ‚‚) :
  (a β€’ g).comp f = a β€’ (g.comp f) := rfl
omit σ₁₃

-- TODO: generalize this to semilinear maps
theorem comp_smul [module R Mβ‚‚] [module R M₃] [smul_comm_class R S Mβ‚‚] [distrib_mul_action S M₃]
  [smul_comm_class R S M₃] [compatible_smul M₃ Mβ‚‚ S R]
  (g : M₃ β†’β‚—[R] Mβ‚‚) (a : S) (f : M β†’β‚—[R] M₃) : g.comp (a β€’ f) = a β€’ (g.comp f) :=
ext $ Ξ» x, g.map_smul_of_tower _ _

end has_smul

section module
variables [semiring S] [module S Mβ‚‚] [smul_comm_class Rβ‚‚ S Mβ‚‚]

instance : module S (M β†’β‚›β‚—[σ₁₂] Mβ‚‚) :=
{ add_smul := Ξ» a b f, ext $ Ξ» x, add_smul _ _ _,
  zero_smul := Ξ» f, ext $ Ξ» x, zero_smul _ _ }

instance [no_zero_smul_divisors S Mβ‚‚] : no_zero_smul_divisors S (M β†’β‚›β‚—[σ₁₂] Mβ‚‚) :=
coe_injective.no_zero_smul_divisors _ rfl coe_smul

end module

end actions

/-!
### Monoid structure of endomorphisms

Lemmas about `pow` such as `linear_map.pow_apply` appear in later files.
-/
section endomorphisms

variables [semiring R] [add_comm_monoid M] [add_comm_group N₁] [module R M] [module R N₁]

instance : has_one (module.End R M) := ⟨linear_map.id⟩
instance : has_mul (module.End R M) := ⟨linear_map.comp⟩

lemma one_eq_id : (1 : module.End R M) = id := rfl
lemma mul_eq_comp (f g : module.End R M) : f * g = f.comp g := rfl

@[simp] lemma one_apply (x : M) : (1 : module.End R M) x = x := rfl
@[simp] lemma mul_apply (f g : module.End R M) (x : M) : (f * g) x = f (g x) := rfl

lemma coe_one : ⇑(1 : module.End R M) = _root_.id := rfl
lemma coe_mul (f g : module.End R M) : ⇑(f * g) = f ∘ g := rfl

instance _root_.module.End.monoid : monoid (module.End R M) :=
{ mul := (*),
  one := (1 : M β†’β‚—[R] M),
  mul_assoc := Ξ» f g h, linear_map.ext $ Ξ» x, rfl,
  mul_one := comp_id,
  one_mul := id_comp }

instance _root_.module.End.semiring : semiring (module.End R M) :=
{ mul := (*),
  one := (1 : M β†’β‚—[R] M),
  zero := 0,
  add := (+),
  mul_zero := comp_zero,
  zero_mul := zero_comp,
  left_distrib := Ξ» f g h, comp_add _ _ _,
  right_distrib := Ξ» f g h, add_comp _ _ _,
  nat_cast := Ξ» n, n β€’ 1,
  nat_cast_zero := add_monoid.nsmul_zero' _,
  nat_cast_succ := Ξ» n, (add_monoid.nsmul_succ' n 1).trans (add_comm _ _),
  .. add_monoid_with_one.unary,
  .. _root_.module.End.monoid,
  .. linear_map.add_comm_monoid }

/-- See also `module.End.nat_cast_def`. -/
@[simp] lemma _root_.module.End.nat_cast_apply (n : β„•) (m : M) :
  (↑n : module.End R M) m = n β€’ m := rfl

instance _root_.module.End.ring : ring (module.End R N₁) :=
{ int_cast := Ξ» z, z β€’ 1,
  int_cast_of_nat := of_nat_zsmul _,
  int_cast_neg_succ_of_nat := zsmul_neg_succ_of_nat _,
  ..module.End.semiring, ..linear_map.add_comm_group }

/-- See also `module.End.int_cast_def`. -/
@[simp] lemma _root_.module.End.int_cast_apply (z : β„€) (m : N₁) :
  (↑z : module.End R N₁) m = z β€’ m := rfl

section
variables [monoid S] [distrib_mul_action S M] [smul_comm_class R S M]

instance _root_.module.End.is_scalar_tower :
  is_scalar_tower S (module.End R M) (module.End R M) := ⟨smul_comp⟩

instance _root_.module.End.smul_comm_class [has_smul S R] [is_scalar_tower S R M] :
  smul_comm_class S (module.End R M) (module.End R M) :=
⟨λ s _ _, (comp_smul _ s _).symm⟩

instance _root_.module.End.smul_comm_class' [has_smul S R] [is_scalar_tower S R M] :
  smul_comm_class (module.End R M) S (module.End R M) :=
smul_comm_class.symm _ _ _

end

/-! ### Action by a module endomorphism. -/

/-- The tautological action by `module.End R M` (aka `M β†’β‚—[R] M`) on `M`.

This generalizes `function.End.apply_mul_action`. -/
instance apply_module : module (module.End R M) M :=
{ smul := ($),
  smul_zero := linear_map.map_zero,
  smul_add := linear_map.map_add,
  add_smul := linear_map.add_apply,
  zero_smul := (linear_map.zero_apply : βˆ€ m, (0 : M β†’β‚—[R] M) m = 0),
  one_smul := Ξ» _, rfl,
  mul_smul := Ξ» _ _ _, rfl }

@[simp] protected lemma smul_def (f : module.End R M) (a : M) : f β€’ a = f a := rfl

/-- `linear_map.apply_module` is faithful. -/
instance apply_has_faithful_smul : has_faithful_smul (module.End R M) M :=
⟨λ _ _, linear_map.ext⟩

instance apply_smul_comm_class : smul_comm_class R (module.End R M) M :=
{ smul_comm := Ξ» r e m, (e.map_smul r m).symm }

instance apply_smul_comm_class' : smul_comm_class (module.End R M) R M :=
{ smul_comm := linear_map.map_smul }

instance apply_is_scalar_tower {R M : Type*} [comm_semiring R] [add_comm_monoid M] [module R M] :
  is_scalar_tower R (module.End R M) M :=
⟨λ t f m, rfl⟩

end endomorphisms

end linear_map

/-! ### Actions as module endomorphisms -/

namespace distrib_mul_action

variables (R M) [semiring R] [add_comm_monoid M] [module R M]
variables [monoid S] [distrib_mul_action S M] [smul_comm_class S R M]

/-- Each element of the monoid defines a linear map.

This is a stronger version of `distrib_mul_action.to_add_monoid_hom`. -/
@[simps]
def to_linear_map (s : S) : M β†’β‚—[R] M :=
{ to_fun := has_smul.smul s,
  map_add' := smul_add s,
  map_smul' := Ξ» a b, smul_comm _ _ _ }

/-- Each element of the monoid defines a module endomorphism.

This is a stronger version of `distrib_mul_action.to_add_monoid_End`. -/
@[simps]
def to_module_End : S β†’* module.End R M :=
{ to_fun := to_linear_map R M,
  map_one' := linear_map.ext $ one_smul _,
  map_mul' := Ξ» a b, linear_map.ext $ mul_smul _ _ }

end distrib_mul_action

namespace module

variables (R M) [semiring R] [add_comm_monoid M] [module R M]
variables [semiring S] [module S M] [smul_comm_class S R M]

/-- Each element of the semiring defines a module endomorphism.

This is a stronger version of `distrib_mul_action.to_module_End`. -/
@[simps]
def to_module_End : S β†’+* module.End R M :=
{ to_fun := distrib_mul_action.to_linear_map R M,
  map_zero' := linear_map.ext $ zero_smul _,
  map_add' := Ξ» f g, linear_map.ext $ add_smul _ _,
  ..distrib_mul_action.to_module_End R M }

/-- The canonical (semi)ring isomorphism from `Rᡐᡒᡖ` to `module.End R R` induced by the right
multiplication. -/
@[simps]
def module_End_self : Rᡐᡒᡖ ≃+* module.End R R :=
{ to_fun := distrib_mul_action.to_linear_map R R,
  inv_fun := Ξ» f, mul_opposite.op (f 1),
  left_inv := mul_one,
  right_inv := Ξ» f, linear_map.ext_ring $ one_mul _,
  ..module.to_module_End R R }

/-- The canonical (semi)ring isomorphism from `R` to `module.End Rᡐᡒᡖ R` induced by the left
multiplication. -/
@[simps]
def module_End_self_op : R ≃+* module.End Rᡐᡒᡖ R :=
{ to_fun := distrib_mul_action.to_linear_map _ _,
  inv_fun := Ξ» f, f 1,
  left_inv := mul_one,
  right_inv := Ξ» f, linear_map.ext_ring_op $ mul_one _,
  ..module.to_module_End _ _ }

lemma End.nat_cast_def (n : β„•) [add_comm_monoid N₁] [module R N₁] :
  (↑n : module.End R N₁) = module.to_module_End R N₁ n := rfl

lemma End.int_cast_def (z : β„€) [add_comm_group N₁] [module R N₁] :
  (↑z : module.End R N₁) = module.to_module_End R N₁ z := rfl

end module