File size: 29,833 Bytes
4a56cd6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/-
Copyright 2026 The Formal Conjectures Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
This file has been modified.
-/
import Mathlib.Algebra.TrivSqZeroExt.Basic
import Mathlib.LinearAlgebra.CliffordAlgebra.Conjugation
import Mathlib.LinearAlgebra.ExteriorPower.Basic
import Mathlib.RingTheory.Kaehler.Basic

/-! # Basic -/

namespace OAI.Algebra.DeRham.Basic

noncomputable section

open _root_.Algebra

/-!
# Algebraic de Rham forms

Relative differential forms of degree `p` are the exterior powers `⋀[A]^p Ω[A⁄R]` of the module
of relative Kähler differentials, built from Mathlib's `exteriorPower` and `KaehlerDifferential`.
Since `⋀[A]^p M` is a submodule of `ExteriorAlgebra A M`, all degrees live inside the single
algebra `ExtAlg R A = ⋀ Ω[A⁄R]`, and the exterior derivative is constructed once and for all on
that algebra.

## The exterior derivative

The exterior derivative is the odd degree-one `R`-derivation `d` of `⋀ Ω[A⁄R]` extending the
universal derivation `A → Ω[A⁄R]`. It is obtained from a universal property: an odd derivation
is encoded by an algebra map `x ↦ (x, d x)` into an auxiliary square-zero ring `Sq R A` with
additive group `⋀ Ω[A⁄R] × ⋀ Ω[A⁄R]`. Its multiplication `(x, u) * (y, v) = (x * y, involute x *
v + u * y)` encodes the graded Leibniz rule. Here `involute` is the grade involution, so
`involute x = (-1) ^ p * x` on a form of degree `p`, and the rule read off from that product is
the usual `d (x * y) = d x * y + (-1) ^ p * x * d y`.

Giving `Sq R A` the twisted `A`-algebra structure `a ↦ (a, d a)` turns that algebra map into
an `A`-algebra map, so it is produced in two steps:

* the universal property of `Ω[A⁄R]` applied to the derivation `a ↦ (d a, 0)` gives an
 `A`-linear `phi: Ω[A⁄R] → Sq R A`, of the form `ω ↦ (ω, d ω)`;
* the universal property of the exterior algebra applied to `phi` gives `deRhamHom`, and `d` is
 its second component `extDeriv`.

The identities `d ∘ d = 0`, the graded Leibniz rule, and the fact that `d` raises the degree by
one then follow by induction on the exterior algebra.

## Main definitions

* `Algebra.DeRham.Form R A p`: differential forms of degree `p`, that is `⋀[A]^p Ω[A⁄R]`;
* `Algebra.DeRham.differential`: the exterior derivative `Form R A p →ₗ[R] Form R A (p + 1)`;
* `Algebra.DeRham.mk R A p a₀ v`: the form `a₀ * d v₀ ∧... ∧ d vₚ₋₁`; these span `Form R A p`
 over `R`, by `Algebra.DeRham.span_mk`;
* `Algebra.DeRham.map`: functoriality along an `R`-algebra homomorphism.
-/

universe u

open CliffordAlgebra (involute involute_ι)
open TrivSqZeroExt (fst snd)

variable (R A : Type u) [CommRing R] [CommRing A] [Algebra R A]

/-- Let `R` be a commutative ring and `A` a commutative `R`-algebra. This is the exterior algebra
`⋀_A Ω_{A/R}` of the module of Kähler differentials. Its degree-`p` part is the module of
relative differential `p`-forms, generated by expressions `a₀ da₁ ∧ ⋯ ∧ daₚ`. -/
abbrev ExtAlg : Type u := ExteriorAlgebra A Ω[A⁄R]

variable {R A}

/-- A single differential anticommutes with every homogeneous factor, which globally reads as
commuting past the grade involution. -/

private lemma ι_mul_eq_involute_mul_ι (m : Ω[A⁄R]) (y : ExtAlg R A) :
    ExteriorAlgebra.ι A m * y = involute y * ExteriorAlgebra.ι A m := by

  induction y using ExteriorAlgebra.induction with
  | algebraMap a => rw [AlgHom.commutes, Algebra.commutes]
  | ι n =>
      rw [involute_ι, neg_mul, eq_neg_iff_add_eq_zero]
      exact ExteriorAlgebra.ι_add_mul_swap m n
  | mul y z hy hz => rw [← mul_assoc, hy, mul_assoc, hz, ← mul_assoc, map_mul]
  | add y z hy hz => rw [mul_add, hy, hz, map_add, add_mul]

variable (R A)

/-- The square-zero extension of the exterior algebra. Its multiplication is
`(x, u) * (y, v) = (x * y, involute x * v + u * y)`, encoding the graded Leibniz rule.
The synonym unfolds in implicit arguments so that Mathlib's projections apply directly, but stays
opaque to typeclass synthesis to keep the twisted ring and scalar actions separate. -/
@[implicit_reducible] def Sq : Type u := TrivSqZeroExt (ExtAlg R A) (ExtAlg R A)

namespace Sq

instance instRing : Ring (Sq R A) := by
  -- Only the left action is twisted; these instances stay local to the ring construction.
  letI : SMul (ExtAlg R A) (ExtAlg R A) := ⟨fun x v => involute x * v⟩
  letI : Module (ExtAlg R A) (ExtAlg R A) :=
    Module.compHom (ExtAlg R A) (involute (R := A) (M := Ω[A⁄R])).toRingHom
  letI : SMulCommClass (ExtAlg R A) (ExtAlg R A)ᵐᵒᵖ (ExtAlg R A) :=
    ⟨fun x y u => (mul_assoc (involute x) u y.unop).symm⟩
  exact inferInstanceAs (Ring (TrivSqZeroExt (ExtAlg R A) (ExtAlg R A)))

variable {R A}

@[simp] lemma fst_add (s t : Sq R A) : fst (s + t) = fst s + fst t := rfl

@[simp] lemma snd_add (s t : Sq R A) : snd (s + t) = snd s + snd t := rfl

@[simp] lemma fst_mul (s t : Sq R A) : fst (s * t) = fst s * fst t := rfl

@[simp] lemma fst_one : fst (1 : Sq R A) = 1 := rfl

@[simp] lemma snd_one : snd (1 : Sq R A) = 0 := rfl

@[simp] lemma snd_mul (s t : Sq R A) :
    snd (s * t) = involute (fst s) * snd t + snd s * fst t := rfl

end Sq

/-- For a commutative `R`-algebra `A`, let `E = ⋀_A Ω_{A/R}` be the exterior algebra of Kähler
differentials. This ring homomorphism `A → Sq R A = E × E` sends `a` to `(a,da)`, with `a` in
degree zero and its differential in degree one. It defines the `A`-algebra structure on `Sq`. -/
def twist : A →+* Sq R A where
  toFun a := (algebraMap A (ExtAlg R A) a,
    ExteriorAlgebra.ι A (KaehlerDifferential.D R A a))
  map_one' := by apply TrivSqZeroExt.ext <;> simp
  map_mul' a b := by
    apply TrivSqZeroExt.ext
    · exact map_mul (algebraMap A (ExtAlg R A)) a b
    · change ExteriorAlgebra.ι A (KaehlerDifferential.D R A (a * b)) =
        involute (algebraMap A (ExtAlg R A) a) *
          ExteriorAlgebra.ι A (KaehlerDifferential.D R A b) +
        ExteriorAlgebra.ι A (KaehlerDifferential.D R A a) * algebraMap A (ExtAlg R A) b
      simp [Algebra.smul_def, Algebra.commutes (R := A) b (ExteriorAlgebra.ι A _)]
  map_zero' := by apply TrivSqZeroExt.ext <;> simp
  map_add' a b := by
    apply TrivSqZeroExt.ext
    · exact map_add (algebraMap A (ExtAlg R A)) a b
    · exact map_add ((ExteriorAlgebra.ι A).compDer (KaehlerDifferential.D R A)) a b

@[simp] lemma fst_twist (a : A) :
    fst (twist R A a) = algebraMap A (ExtAlg R A) a := rfl

@[simp] lemma snd_twist (a : A) :
    snd (twist R A a) = ExteriorAlgebra.ι A (KaehlerDifferential.D R A a) := rfl

lemma twist_commutes (a : A) (s : Sq R A) : twist R A a * s = s * twist R A a := by
  apply TrivSqZeroExt.ext
  · simpa using Algebra.commutes a (fst s)
  · simp only [Sq.snd_mul, fst_twist, snd_twist, AlgHom.commutes,
      ι_mul_eq_involute_mul_ι (KaehlerDifferential.D R A a) (fst s)]
    rw [Algebra.commutes a (snd s)]
    abel

-- This scalar action includes `d a`, so it is not the componentwise action on `TrivSqZeroExt`.
instance : Algebra A (Sq R A) := (twist R A).toAlgebra' (twist_commutes R A)

instance : Algebra R (Sq R A) :=
  ((twist R A).comp (algebraMap R A)).toAlgebra' fun _ s => twist_commutes R A _ s

instance : IsScalarTower R A (Sq R A) := .of_algebraMap_eq fun _ => rfl

namespace Sq
section

variable {R A}

@[simp] lemma fst_algebraMap (a : A) :
    fst (algebraMap A (Sq R A) a) = algebraMap A (ExtAlg R A) a := rfl

@[simp] lemma snd_algebraMap (a : A) :
    snd (algebraMap A (Sq R A) a) = ExteriorAlgebra.ι A (KaehlerDifferential.D R A a) := rfl

@[simp] lemma fst_smul (a : A) (s : Sq R A) : fst (a • s) = a • fst s := rfl

lemma snd_smul (a : A) (s : Sq R A) :
    snd (a • s) = a • snd s +
      ExteriorAlgebra.ι A (KaehlerDifferential.D R A a) * fst s := by
  simp [Algebra.smul_def, snd_mul, fst_algebraMap, snd_algebraMap, AlgHom.commutes]

@[simp] lemma fst_smul_base (r : R) (s : Sq R A) : fst (r • s) = r • fst s := rfl

@[simp] lemma snd_smul_base (r : R) (s : Sq R A) : snd (r • s) = r • snd s := by
  rw [← algebraMap_smul A r s, snd_smul, algebraMap_smul]
  simp

end

/-- For a commutative `R`-algebra `A`, let `E = ⋀_A Ω_{A/R}` be the exterior algebra of Kähler
differentials. For the `A`-algebra structure `a ↦ (a,da)` on `Sq R A = E × E`, this is the first
projection `(x,u) ↦ x` as an `A`-algebra homomorphism. -/
def fstAlgHom : Sq R A →ₐ[A] ExtAlg R A where
  toFun := fst
  map_one' := rfl
  map_mul' _ _ := rfl
  map_zero' := rfl
  map_add' _ _ := rfl
  commutes' _ := rfl

@[simp] lemma fstAlgHom_apply (s : Sq R A) : fstAlgHom R A s = fst s := rfl

/-- For a commutative `R`-algebra `A`, let `E = ⋀_A Ω_{A/R}` be the exterior algebra of Kähler
differentials. This is the second projection `(x,u) ↦ u` from `Sq R A = E × E` as an `R`-linear
map. -/
def sndLinear : Sq R A →ₗ[R] ExtAlg R A where
  toFun := snd
  map_add' _ _ := rfl
  map_smul' := snd_smul_base

@[simp] lemma sndLinear_apply (s : Sq R A) : sndLinear R A s = snd s := rfl

end Sq

/-- For a commutative `R`-algebra `A`, let `E = ⋀_A Ω_{A/R}` be the exterior algebra of Kähler
differentials. This `R`-derivation `A → Sq R A = E × E` sends `a` to `(da,0)`. The target uses
the `A`-action defined by `a ↦ (a,da)`. -/
def epsDerivation : Derivation R A (Sq R A) where
  toFun a := TrivSqZeroExt.inl (ExteriorAlgebra.ι A (KaehlerDifferential.D R A a))
  map_add' a b := by apply TrivSqZeroExt.ext <;> simp
  map_smul' r a := by apply TrivSqZeroExt.ext <;> simp
  map_one_eq_zero' := by apply TrivSqZeroExt.ext <;> simp
  leibniz' a b := by
    apply TrivSqZeroExt.ext
    · simp
    · simp [Sq.snd_smul, ExteriorAlgebra.ι_add_mul_swap]

/-- For a commutative `R`-algebra `A`, let `E = ⋀_A Ω_{A/R}` be the exterior algebra of Kähler
differentials. This `A`-linear map `Ω_{A/R} → Sq R A = E × E` sends `da` to `(da,0)`, using the
target action `a ↦ (a,da)`. In particular, it sends `a db` to `(a db,da ∧ db)`. -/
def phi : Ω[A⁄R] →ₗ[A] Sq R A := (epsDerivation R A).liftKaehlerDifferential

@[simp] lemma phi_D (a : A) :
    phi R A (KaehlerDifferential.D R A a) =
      TrivSqZeroExt.inl (ExteriorAlgebra.ι A (KaehlerDifferential.D R A a)) :=
  (epsDerivation R A).liftKaehlerDifferential_comp_D a

/-- To prove a statement about every Kähler differential it suffices to treat the exact ones
`d a` and to check closure under zero, sums, and scalars. -/
@[elab_as_elim]
lemma D_induction {motive : Ω[A⁄R] → Prop} (ω : Ω[A⁄R])
    (D : ∀ a : A, motive (KaehlerDifferential.D R A a)) (zero : motive 0)
    (add : ∀ x y, motive x → motive y → motive (x + y))
    (smul : ∀ (a : A) x, motive x → motive (a • x)) : motive ω := by
  have hω : ω ∈ Submodule.span A (Set.range (KaehlerDifferential.D R A)) := by
    rw [KaehlerDifferential.span_range_derivation]; trivial
  induction hω using Submodule.span_induction with
  | mem _ hx => obtain ⟨a, rfl⟩ := hx; exact D a
  | zero => exact zero
  | add x y _ _ hx hy => exact add x y hx hy
  | smul a x _ hx => exact smul a x hx

@[simp] lemma fst_phi (ω : Ω[A⁄R]) : fst (phi R A ω) = ExteriorAlgebra.ι A ω := by
  have h : (Sq.fstAlgHom R A).toLinearMap.comp (phi R A) = ExteriorAlgebra.ι A := by
    refine LinearMap.ext_on (KaehlerDifferential.span_range_derivation R A) ?_
    rintro _ ⟨a, rfl⟩
    simp
  exact congrArg (fun f => f ω) h

lemma snd_phi_smul (a : A) (ω : Ω[A⁄R]) :
    snd (phi R A (a • ω)) =
      a • snd (phi R A ω) +
        ExteriorAlgebra.ι A (KaehlerDifferential.D R A a) * ExteriorAlgebra.ι A ω := by
  rw [map_smul, Sq.snd_smul, fst_phi]

/-- The exterior derivative of a differential is an even element, so it commutes with everything
of degree one. -/
lemma involute_snd_phi (ω : Ω[A⁄R]) :
    involute (snd (phi R A ω)) = snd (phi R A ω) := by
  induction ω using D_induction with
  | D a => simp
  | zero => simp
  | add x y hx hy => simp [hx, hy]
  | smul a x hx =>
      rw [snd_phi_smul, map_add, map_smul, hx, map_mul, involute_ι, involute_ι, neg_mul_neg]

lemma phi_mul_self (ω : Ω[A⁄R]) : phi R A ω * phi R A ω = 0 := by
  apply TrivSqZeroExt.ext
  · simp
  · rw [Sq.snd_mul, fst_phi, involute_ι, neg_mul, TrivSqZeroExt.snd_zero,
      ι_mul_eq_involute_mul_ι, involute_snd_phi, neg_add_cancel]

/-- For a commutative `R`-algebra `A`, let `E = ⋀_A Ω_{A/R}` be the exterior algebra of Kähler
differentials. With `A` acting on `Sq R A = E × E` by `a ↦ (a,da)`, this `A`-algebra
homomorphism sends `da` to `(da,0)`. On an arbitrary form `ω` its value is `(ω,dω)`; the second
component defines the exterior derivative. -/
def deRhamHom : ExtAlg R A →ₐ[A] Sq R A :=
  ExteriorAlgebra.lift A ⟨phi R A, phi_mul_self R A⟩

@[simp] lemma deRhamHom_ι (ω : Ω[A⁄R]) :
    deRhamHom R A (ExteriorAlgebra.ι A ω) = phi R A ω :=
  ExteriorAlgebra.lift_ι_apply A _ _ ω

@[simp] lemma fst_deRhamHom (x : ExtAlg R A) : fst (deRhamHom R A x) = x := by
  have h : (Sq.fstAlgHom R A).comp (deRhamHom R A) = AlgHom.id A (ExtAlg R A) := by
    apply ExteriorAlgebra.hom_ext
    ext ω
    simp
  exact AlgHom.congr_fun h x

/-- Let `R` be a commutative ring and `A` a commutative `R`-algebra. Write `E = ⋀_A Ω_{A/R}` for the
exterior algebra of Kähler differentials. The exterior derivative is the `R`-linear map `d: E →
E` determined by `d(a) = da`, `d(da) = 0`, and `d(ωη) = dω η + (-1)^p ω dη` for `ω` of degree
`p`. It raises degree by one. -/
def extDeriv : ExtAlg R A →ₗ[R] ExtAlg R A :=
  (Sq.sndLinear R A).comp ((deRhamHom R A).toLinearMap.restrictScalars R)

@[simp] lemma extDeriv_ι (ω : Ω[A⁄R]) :
    extDeriv R A (ExteriorAlgebra.ι A ω) = snd (phi R A ω) := by
  simp [extDeriv]

@[simp] lemma extDeriv_algebraMap (a : A) :
    extDeriv R A (algebraMap A (ExtAlg R A) a) =
      ExteriorAlgebra.ι A (KaehlerDifferential.D R A a) := by
  show snd (deRhamHom R A (algebraMap A (ExtAlg R A) a)) = _
  rw [AlgHom.commutes]
  rfl

/-- The exterior derivative is an odd derivation of the exterior algebra. -/
lemma extDeriv_mul (x y : ExtAlg R A) :
    extDeriv R A (x * y) =
      involute x * extDeriv R A y + extDeriv R A x * y := by
  show snd (deRhamHom R A (x * y)) = _
  simp only [map_mul, Sq.snd_mul, fst_deRhamHom]
  rfl

/-- Multiplying forms adds their degrees. -/
private lemma mul_mem_exteriorPower {m n : ℕ} {x y : ExtAlg R A}
    (hx : x ∈ ⋀[A]^m Ω[A⁄R]) (hy : y ∈ ⋀[A]^n Ω[A⁄R]) :
    x * y ∈ ⋀[A]^(m + n) Ω[A⁄R] := by
  rw [ExteriorAlgebra.exteriorPower, pow_add]
  exact Submodule.mul_mem_mul hx hy

private lemma ι_mem_exteriorPower (ω : Ω[A⁄R]) :
    ExteriorAlgebra.ι A ω ∈ ⋀[A]^1 Ω[A⁄R] := by simp

private lemma snd_phi_mem (ω : Ω[A⁄R]) : snd (phi R A ω) ∈ ⋀[A]^2 Ω[A⁄R] := by

  induction ω using D_induction with
  | D a => simp
  | zero => simp
  | add x y hx hy => rw [map_add, TrivSqZeroExt.snd_add]; exact Submodule.add_mem _ hx hy
  | smul a x hx =>
      rw [snd_phi_smul]
      exact Submodule.add_mem _ (Submodule.smul_mem _ _ hx)
        (mul_mem_exteriorPower R A (ι_mem_exteriorPower R A _) (ι_mem_exteriorPower R A _))

/-- The exterior derivative raises the degree of a form by one. -/
lemma extDeriv_mem (p : ℕ) {x : ExtAlg R A} (hx : x ∈ ⋀[A]^p Ω[A⁄R]) :
    extDeriv R A x ∈ ⋀[A]^(p + 1) Ω[A⁄R] := by
  induction p generalizing x with
  | zero =>
      rw [ExteriorAlgebra.exteriorPower, pow_zero, Submodule.one_eq_range] at hx
      obtain ⟨a, rfl⟩ := hx
      simp
  | succ p ih =>
      rw [ExteriorAlgebra.exteriorPower, pow_succ'] at hx
      refine Submodule.mul_induction_on hx ?_ ?_
      · rintro m hm n hn
        obtain ⟨ω, rfl⟩ := hm
        rw [extDeriv_mul, involute_ι, extDeriv_ι]
        refine Submodule.add_mem _ ?_ ?_
        · rw [neg_mul]
          have := mul_mem_exteriorPower R A (ι_mem_exteriorPower R A ω) (ih hn)
          rw [show 1 + (p + 1) = p + 1 + 1 by omega] at this
          exact Submodule.neg_mem _ this
        · have := mul_mem_exteriorPower R A (snd_phi_mem R A ω) hn
          rwa [show 2 + p = p + 1 + 1 by omega] at this
      · intro x y hx hy
        rw [map_add]
        exact Submodule.add_mem _ hx hy

/-! ### Differential forms -/

/-- Let `R` be a commutative ring and `A` a commutative `R`-algebra. The relative differential
`p`-forms form the module `⋀^p_A Ω_{A/R}`, the `p`-th exterior power of the module of Kähler
differentials. Its elements are sums of expressions `a₀ da₁ ∧ ⋯ ∧ daₚ`. -/
abbrev Form (p : ℕ) : Type u := ↥(⋀[A]^p Ω[A⁄R])

/-- Let `R` be a commutative ring and `A` a commutative `R`-algebra. This `R`-linear exterior
derivative maps relative differential `p`-forms to `(p+1)`-forms. On generators it sends `a₀ da₁
∧ ⋯ ∧ daₚ` to `da₀ ∧ da₁ ∧ ⋯ ∧ daₚ`. -/
def differential (p : ℕ) : Form R A p →ₗ[R] Form R A (p + 1) where
  toFun x := ⟨extDeriv R A x, extDeriv_mem R A p x.2⟩
  map_add' _ _ := Subtype.ext (map_add (extDeriv R A) _ _)
  map_smul' r _ := Subtype.ext (map_smul (extDeriv R A) r _)

@[simp] lemma coe_differential (p : ℕ) (x : Form R A p) :
    (differential R A p x : ExtAlg R A) = extDeriv R A x := rfl

private lemma extDeriv_smul (a : A) (x : ExtAlg R A) :
    extDeriv R A (a • x) =
      a • extDeriv R A x +
        ExteriorAlgebra.ι A (KaehlerDifferential.D R A a) * x := by
  simp [Algebra.smul_def, extDeriv_mul, AlgHom.commutes, extDeriv_algebraMap]

/-- Let `R` be a commutative ring and `A` a commutative `R`-algebra. For a family `v₀, …, v_{p-1}`
in `A`, this is the relative differential form `dv₀ ∧ ⋯ ∧ dv_{p-1}`. The empty wedge, for `p =
0`, is the constant zero-form `1`. -/
def exact (p : ℕ) (v : Fin p → A) : Form R A p :=
  exteriorPower.ιMulti A p fun i => KaehlerDifferential.D R A (v i)

@[simp] lemma coe_exact (p : ℕ) (v : Fin p → A) :
    (DeRham.Basic.exact R A p v : ExtAlg R A) =
      ExteriorAlgebra.ιMulti A p fun i => KaehlerDifferential.D R A (v i) := rfl

/-- Wedges of exact forms are closed. -/
private lemma extDeriv_exact (p : ℕ) (v : Fin p → A) :
    extDeriv R A (DeRham.Basic.exact R A p v : ExtAlg R A) = 0 := by
  induction p with
  | zero =>
      rw [coe_exact, ExteriorAlgebra.ιMulti_zero_apply,
        ← map_one (algebraMap A (ExtAlg R A)), extDeriv_algebraMap]
      simp
  | succ p ih =>
      rw [coe_exact, ExteriorAlgebra.ιMulti_succ_apply, extDeriv_mul, involute_ι,
        extDeriv_ι, phi_D, TrivSqZeroExt.snd_inl, zero_mul, add_zero]
      have : (Matrix.vecTail fun i => KaehlerDifferential.D R A (v i)) =
          fun i => KaehlerDifferential.D R A (Matrix.vecTail v i) := rfl
      rw [this, ← coe_exact R A p (Matrix.vecTail v), ih (Matrix.vecTail v), mul_zero]

/-- Let `R` be a commutative ring and `A` a commutative `R`-algebra. For `a₀ ∈ A` and a family `v₀,
…, v_{p-1}` in `A`, this constructs the relative differential `p`-form `a₀ dv₀ ∧ ⋯ ∧ dv_{p-1}`
in `⋀^p_A Ω_{A/R}`. -/
def mk (p : ℕ) (a₀ : A) (v : Fin p → A) : Form R A p := a₀ • DeRham.Basic.exact R A p v

@[simp] lemma coe_mk (p : ℕ) (a₀ : A) (v : Fin p → A) :
    (DeRham.Basic.mk R A p a₀ v : ExtAlg R A) =
      a₀ • ExteriorAlgebra.ιMulti A p fun i => KaehlerDifferential.D R A (v i) := rfl

@[simp] lemma differential_mk (p : ℕ) (a₀ : A) (v : Fin p → A) :
    differential R A p (DeRham.Basic.mk R A p a₀ v) = DeRham.Basic.mk R A (p + 1) 1 (Fin.cons a₀ v) := by
  refine Subtype.ext ?_
  rw [coe_differential, coe_mk, coe_mk, extDeriv_smul, ← coe_exact, extDeriv_exact,
    smul_zero, zero_add, one_smul, ExteriorAlgebra.ιMulti_succ_apply]
  rfl

/-- Let `R` be a commutative ring and `A` a commutative `R`-algebra. This `R`-linear map sends `a ∈
A` to the corresponding degree-zero relative differential form in `⋀^0_A Ω_{A/R}`. -/
def ofFunction : A →ₗ[R] Form R A 0 where
  toFun a := DeRham.Basic.mk R A 0 a Fin.elim0
  map_add' a b := by simp [DeRham.Basic.mk, add_smul]
  map_smul' r a := by simp [DeRham.Basic.mk, smul_assoc]

lemma ofFunction_apply (a : A) : ofFunction R A a = DeRham.Basic.mk R A 0 a Fin.elim0 := rfl

/-- Let `R` be a commutative ring and `A` a commutative `R`-algebra. This `R`-linear map sends `r ∈
R` to the zero-form given by its image in `A` under the algebra structure map. -/
def ofConstant : R →ₗ[R] Form R A 0 :=
  (ofFunction R A).comp (Algebra.linearMap R A)

lemma ofConstant_apply (r : R) :
    ofConstant R A r = ofFunction R A (algebraMap R A r) := rfl

@[simp] lemma differential_ofFunction_algebraMap (r : R) :
    differential R A 0 (ofFunction R A (algebraMap R A r)) = 0 := by
  refine Subtype.ext ?_
  rw [ofFunction_apply, coe_differential, coe_mk, extDeriv_smul, ← coe_exact,
    extDeriv_exact, smul_zero, zero_add]
  simp

@[simp] lemma differential_ofConstant (r : R) :
    differential R A 0 (ofConstant R A r) = 0 := by
  rw [ofConstant_apply, differential_ofFunction_algebraMap]

/-! ### Generators -/

/-- Exact forms span the differential forms of degree `p` over `A`. -/
private lemma span_exact (p : ℕ) :
    Submodule.span A (Set.range fun v : Fin p → A => DeRham.Basic.exact R A p v) = ⊤ := by
  rw [← top_le_iff, ← exteriorPower.ιMulti_span_of_span A p Ω[A⁄R]
    (KaehlerDifferential.span_range_derivation R A), Submodule.span_le]
  rintro _ ⟨w, hw, rfl⟩
  choose v hv using fun i => hw ⟨i, rfl⟩
  exact Submodule.subset_span ⟨v, congrArg _ (funext hv)⟩

/-- Forms `a₀ * d a₁ ∧ ⋯ ∧ d aₚ` span the differential forms of degree `p` over the base ring. -/
private lemma span_mk (p : ℕ) :
    Submodule.span R (Set.range fun av : A × (Fin p → A) => DeRham.Basic.mk R A p av.1 av.2) = ⊤ := by
  have key : ∀ x : Form R A p,
      x ∈ Submodule.span A (Set.range fun v : Fin p → A => DeRham.Basic.exact R A p v) → ∀ a : A,
      a • x ∈ Submodule.span R (Set.range fun av : A × (Fin p → A) => DeRham.Basic.mk R A p av.1 av.2) := by
    intro x hx
    induction hx using Submodule.span_induction with
    | mem y hy => obtain ⟨v, rfl⟩ := hy; exact fun a => Submodule.subset_span ⟨(a, v), rfl⟩
    | zero => simp
    | add y z _ _ hy hz =>
        exact fun a => by rw [smul_add]; exact Submodule.add_mem _ (hy a) (hz a)
    | smul b y _ hy => exact fun a => by rw [smul_smul]; exact hy _
  exact top_unique fun x _ => by simpa using key x (by rw [span_exact]; trivial) 1

/-- Two `R`-linear maps out of degree `p` forms agreeing on the generators are equal. -/
private lemma linearMap_ext {M : Type*} [AddCommGroup M] [Module R M] {p : ℕ}
    {F G : Form R A p →ₗ[R] M} (h : ∀ a₀ v, F (DeRham.Basic.mk R A p a₀ v) = G (DeRham.Basic.mk R A p a₀ v)) : F = G :=
  LinearMap.ext_on (span_mk R A p) (by rintro _ ⟨⟨a₀, v⟩, rfl⟩; exact h a₀ v)

@[simp] lemma mk_smul_coeff (p : ℕ) (r : R) (a₀ : A) (v : Fin p → A) :
    DeRham.Basic.mk R A p (r • a₀) v = r • DeRham.Basic.mk R A p a₀ v :=
  smul_assoc r a₀ _

@[simp] lemma mk_zero_coeff (p : ℕ) (v : Fin p → A) : DeRham.Basic.mk R A p 0 v = 0 :=
  zero_smul A _

/-- To prove a statement about every differential form of degree `p` it suffices to treat the
generators `a₀ * d v₀ ∧... ∧ d vₚ₋₁` and to check closure under zero, sums, and scalars. -/
@[elab_as_elim]
lemma mk_induction {p : ℕ} {motive : Form R A p → Prop} (x : Form R A p)
    (mk : ∀ (a₀ : A) (v : Fin p → A), motive (DeRham.Basic.mk R A p a₀ v))
    (zero : motive 0) (add : ∀ x y, motive x → motive y → motive (x + y))
    (smul : ∀ (r : R) x, motive x → motive (r • x)) : motive x := by
  have hx : x ∈ Submodule.span R
      (Set.range fun av : A × (Fin p → A) => DeRham.Basic.mk R A p av.1 av.2) := by
    rw [span_mk]; trivial
  induction hx using Submodule.span_induction with
  | mem y hy => obtain ⟨⟨a₀, v⟩, rfl⟩ := hy; exact mk a₀ v
  | zero => exact zero
  | add y z _ _ hy hz => exact add y z hy hz
  | smul r y _ hy => exact smul r y hy

/-! ### Functoriality -/

section Tower

variable (B : Type u) [CommRing B] [Algebra R B] [Algebra A B] [IsScalarTower R A B]

/-- Let `R → A → B` be a tower of commutative algebras. This `A`-algebra homomorphism `⋀_A Ω_{A/R} →
⋀_B Ω_{B/R}` sends `a` to its image in `B` and `da` to the differential of that image. -/
def extAlgMap : ExtAlg R A →ₐ[A] ExtAlg R B :=
  ExteriorAlgebra.lift A
    ⟨((ExteriorAlgebra.ι B).restrictScalars A).comp (KaehlerDifferential.map R R A B),
      fun _ => ExteriorAlgebra.ι_sq_zero _⟩

@[simp] lemma extAlgMap_ι (ω : Ω[A⁄R]) :
    extAlgMap R A B (ExteriorAlgebra.ι A ω) =
      ExteriorAlgebra.ι B (KaehlerDifferential.map R R A B ω) :=
  ExteriorAlgebra.lift_ι_apply A _ _ ω

@[simp] lemma extAlgMap_algebraMap (a : A) :
    extAlgMap R A B (algebraMap A (ExtAlg R A) a) =
      algebraMap B (ExtAlg R B) (algebraMap A B a) := by aesop

@[simp] lemma extAlgMap_algebraMap_base (r : R) :
    extAlgMap R A B (algebraMap R (ExtAlg R A) r) = algebraMap R (ExtAlg R B) r := by simp

private lemma extAlgMap_ιMulti (p : ℕ) (w : Fin p → Ω[A⁄R]) :

    extAlgMap R A B (ExteriorAlgebra.ιMulti A p w) =
      ExteriorAlgebra.ιMulti B p fun i => KaehlerDifferential.map R R A B (w i) := by
  induction p with
  | zero => simp [ExteriorAlgebra.ιMulti_zero_apply]
  | succ p ih =>
      rw [ExteriorAlgebra.ιMulti_succ_apply, map_mul, extAlgMap_ι,
        ExteriorAlgebra.ιMulti_succ_apply, ih (Matrix.vecTail w)]
      rfl

lemma extAlgMap_mem (p : ℕ) {x : ExtAlg R A} (hx : x ∈ ⋀[A]^p Ω[A⁄R]) :
    extAlgMap R A B x ∈ ⋀[B]^p Ω[B⁄R] := by
  induction p generalizing x with
  | zero =>
      rw [ExteriorAlgebra.exteriorPower, pow_zero, Submodule.one_eq_range] at hx
      obtain ⟨a, rfl⟩ := hx
      rw [ExteriorAlgebra.exteriorPower, pow_zero, Submodule.one_eq_range]
      exact ⟨algebraMap A B a, (extAlgMap_algebraMap R A B a).symm⟩
  | succ p ih =>
      rw [ExteriorAlgebra.exteriorPower, pow_succ'] at hx
      refine Submodule.mul_induction_on hx ?_ ?_
      · rintro _ ⟨ω, rfl⟩ n hn
        rw [map_mul, extAlgMap_ι]
        have := mul_mem_exteriorPower R B
          (ι_mem_exteriorPower R B (KaehlerDifferential.map R R A B ω)) (ih hn)
        rwa [show 1 + p = p + 1 by omega] at this
      · intro x y hx hy
        rw [map_add]
        exact Submodule.add_mem _ hx hy

/-- Let `R → A → B` be a tower of commutative algebras. For a natural number `p`, this `R`-linear
map from relative `p`-forms of `A` to those of `B` applies `A → B` to each coefficient and each
function appearing in `a₀ da₁ ∧ ⋯ ∧ daₚ`. -/
def mapTower (p : ℕ) : Form R A p →ₗ[R] Form R B p where
  toFun x := ⟨extAlgMap R A B x, extAlgMap_mem R A B p x.2⟩
  map_add' x y := Subtype.ext (map_add (extAlgMap R A B) _ _)
  map_smul' r x := Subtype.ext (by
    show extAlgMap R A B (r • (x : ExtAlg R A)) = r • extAlgMap R A B (x : ExtAlg R A)
    rw [Algebra.smul_def, map_mul, extAlgMap_algebraMap_base, Algebra.smul_def])

@[simp] lemma coe_mapTower (p : ℕ) (x : Form R A p) :
    (mapTower R A B p x : ExtAlg R B) = extAlgMap R A B x := rfl

end Tower

variable {A}
variable {B C : Type u} [CommRing B] [CommRing C] [Algebra R B] [Algebra R C]

/-- Let `A` and `B` be commutative algebras over a commutative ring `R`, and let `f: A → B` be an
`R`-algebra homomorphism. This induced map on relative `p`-forms sends `a₀ da₁ ∧ ⋯ ∧ daₚ` to
`f(a₀) d(f(a₁)) ∧ ⋯ ∧ d(f(aₚ))`. -/
def map (f : A →ₐ[R] B) (p : ℕ) : Form R A p →ₗ[R] Form R B p :=
  letI := f.toAlgebra
  haveI : IsScalarTower R A B := IsScalarTower.of_algebraMap_eq fun r => (f.commutes r).symm
  mapTower R A B p

@[simp] lemma map_mk (f : A →ₐ[R] B) (p : ℕ) (a₀ : A) (v : Fin p → A) :
    DeRham.Basic.map R f p (DeRham.Basic.mk R A p a₀ v) = DeRham.Basic.mk R B p (f a₀) fun i => f (v i) := by
  let _ := f.toAlgebra
  refine Subtype.ext ?_
  show extAlgMap R A B (DeRham.Basic.mk R A p a₀ v : ExtAlg R A) =
    ((DeRham.Basic.mk R B p (f a₀) fun i => f (v i) : Form R B p) : ExtAlg R B)
  rw [coe_mk, coe_mk, map_smul, extAlgMap_ιMulti]
  have hfam : (fun i => KaehlerDifferential.map R R A B (KaehlerDifferential.D R A (v i))) =
      fun i => KaehlerDifferential.D R B (f (v i)) := by
    funext i
    exact KaehlerDifferential.map_D R R A B (v i)
  rw [hfam]
  exact (algebraMap_smul (R := A) B a₀ _).symm

@[simp] lemma map_id (p : ℕ) : DeRham.Basic.map R (AlgHom.id R A) p = LinearMap.id :=
  linearMap_ext R A fun a₀ v => by simp

@[simp] lemma map_comp (f : A →ₐ[R] B) (g : B →ₐ[R] C) (p : ℕ) :
    DeRham.Basic.map R (g.comp f) p = (DeRham.Basic.map R g p).comp (DeRham.Basic.map R f p) :=
  linearMap_ext R A fun a₀ v => by simp

@[simp] lemma map_ofFunction (f : A →ₐ[R] B) (a : A) :
    DeRham.Basic.map R f 0 (ofFunction R A a) = ofFunction R B (f a) := by
  rw [ofFunction_apply, map_mk, ofFunction_apply]
  congr 1
  exact funext fun i : Fin 0 => i.elim0

@[simp] lemma map_ofConstant (f : A →ₐ[R] B) (r : R) :
    DeRham.Basic.map R f 0 (ofConstant R A r) = ofConstant R B r := by
  rw [ofConstant_apply, map_ofFunction, f.commutes, ofConstant_apply]

end

end OAI.Algebra.DeRham.Basic