miscelleaneous

Random Lean experiments

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
  52. 52
  53. 53
  54. 54
  55. 55
  56. 56
  57. 57
  58. 58
  59. 59
  60. 60
  61. 61
  62. 62
  63. 63
  64. 64
  65. 65
  66. 66
  67. 67
  68. 68
  69. 69
  70. 70
  71. 71
  72. 72
  73. 73
  74. 74
  75. 75
  76. 76
  77. 77
  78. 78
  79. 79
  80. 80
  81. 81
  82. 82
  83. 83
  84. 84
  85. 85
  86. 86
  87. 87
  88. 88
  89. 89
  90. 90
  91. 91
  92. 92
  93. 93
  94. 94
  95. 95
  96. 96
  97. 97
  98. 98
  99. 99
  100. 100
  101. 101
  102. 102
  103. 103
  104. 104
  105. 105
  106. 106
  107. 107
  108. 108
  109. 109
  110. 110
  111. 111
  112. 112
  113. 113
  114. 114
  115. 115
  116. 116
  117. 117
  118. 118
  119. 119
  120. 120
  121. 121
  122. 122
  123. 123
  124. 124
  125. 125
  126. 126
  127. 127
  128. 128
  129. 129
  130. 130
  131. 131
  132. 132
  133. 133
  134. 134
  135. 135
  136. 136
  137. 137
  138. 138
  139. 139
  140. 140
  141. 141
  142. 142
  143. 143
  144. 144
  145. 145
  146. 146
  147. 147
  148. 148
  149. 149
  150. 150
  151. 151
  152. 152
  153. 153
  154. 154
  155. 155
  156. 156
  157. 157
  158. 158
  159. 159
  160. 160
  161. 161
  162. 162
  163. 163
  164. 164
  165. 165
  166. 166
  167. 167
  168. 168
  169. 169
  170. 170
  171. 171
  172. 172
  173. 173
  174. 174
  175. 175
  176. 176
  177. 177
  178. 178
  179. 179
  180. 180
  181. 181
  182. 182
  183. 183
  184. 184
  185. 185
  186. 186
  187. 187
  188. 188
  189. 189
  190. 190
  191. 191
  192. 192
  193. 193
  194. 194
  195. 195
  196. 196
  197. 197
  198. 198
  199. 199
  200. 200
  201. 201
  202. 202
  203. 203
  204. 204
  205. 205
  206. 206
  207. 207
  208. 208
  209. 209
  210. 210
  211. 211
  212. 212
  213. 213
  214. 214
  215. 215
  216. 216
  217. 217
  218. 218
  219. 219
  220. 220
  221. 221
  222. 222
  223. 223
  224. 224
  225. 225
  226. 226
  227. 227
  228. 228
  229. 229
  230. 230
  231. 231
  232. 232
  233. 233
  234. 234
  235. 235
  236. 236
  237. 237
  238. 238
  239. 239
  240. 240
  241. 241
  242. 242
  243. 243
  244. 244
  245. 245
  246. 246
  247. 247
  248. 248
  249. 249
  250. 250
  251. 251
  252. 252
  253. 253
  254. 254
  255. 255
  256. 256
  257. 257
  258. 258
  259. 259
  260. 260
  261. 261
  262. 262
  263. 263
  264. 264
  265. 265
  266. 266
  267. 267
  268. 268
  269. 269
  270. 270
  271. 271
  272. 272
  273. 273
  274. 274
  275. 275
  276. 276
  277. 277
  278. 278
  279. 279
  280. 280
  281. 281
  282. 282
  283. 283
  284. 284
  285. 285
  286. 286
  287. 287
  288. 288
  289. 289
  290. 290
  291. 291
  292. 292
  293. 293
  294. 294
  295. 295
  296. 296
  297. 297
  298. 298
  299. 299
  300. 300
  301. 301
  302. 302
  303. 303
  304. 304
  305. 305
  306. 306
  307. 307
  308. 308
  309. 309
  310. 310
  311. 311
  312. 312
  313. 313
  314. 314
  315. 315
  316. 316
  317. 317
  318. 318
  319. 319
  320. 320
  321. 321
  322. 322
  323. 323
  324. 324
  325. 325
  326. 326
  327. 327
  328. 328
  329. 329
  330. 330
  331. 331
  332. 332
  333. 333
  334. 334
  335. 335
  336. 336
  337. 337
  338. 338
  339. 339
  340. 340
  341. 341
  342. 342
  343. 343
  344. 344
  345. 345
  346. 346
  347. 347
  348. 348
  349. 349
  350. 350
  351. 351
  352. 352
  353. 353
  354. 354
  355. 355
  356. 356
  357. 357
  358. 358
  359. 359
  360. 360
  361. 361
  362. 362
  363. 363
  364. 364
  365. 365
  366. 366
  367. 367
  368. 368
  369. 369
  370. 370
  371. 371
  372. 372
  373. 373
  374. 374
  375. 375
  376. 376
  377. 377
  378. 378
  379. 379
  380. 380
  381. 381
  382. 382
  383. 383
  384. 384
  385. 385
  386. 386
  387. 387
  388. 388
  389. 389
  390. 390
  391. 391
  392. 392
  393. 393
  394. 394
  395. 395
  396. 396
  397. 397
  398. 398
  399. 399
  400. 400
  401. 401
  402. 402
  403. 403
  404. 404
  405. 405
  406. 406
  407. 407
  408. 408
  409. 409
  410. 410
  411. 411
  412. 412
  413. 413
  414. 414
  415. 415
  416. 416
  417. 417
  418. 418
  419. 419
  420. 420
  421. 421
  422. 422
  423. 423
  424. 424
  425. 425
  426. 426
  427. 427
  428. 428
  429. 429
  430. 430
  431. 431
  432. 432
  433. 433
  434. 434
  435. 435
  436. 436
  437. 437
  438. 438
  439. 439
  440. 440
  441. 441
  442. 442
  443. 443
  444. 444
  445. 445
  446. 446
  447. 447
  448. 448
  449. 449
  450. 450
  451. 451
  452. 452
  453. 453
  454. 454
  455. 455
  456. 456
  457. 457
  458. 458
  459. 459
  460. 460
  461. 461
  462. 462
  463. 463
  464. 464
  465. 465
  466. 466
  467. 467
  468. 468
  469. 469
  470. 470
  471. 471
  472. 472
  473. 473
  474. 474
  475. 475
  476. 476
  477. 477
  478. 478
  479. 479
  480. 480
  481. 481
  482. 482
  483. 483
  484. 484
  485. 485
  486. 486
  487. 487
  488. 488
  489. 489
  490. 490
  491. 491
  492. 492
  493. 493
  494. 494
  495. 495
  496. 496
  497. 497
  498. 498
  499. 499
  500. 500
  501. 501
  502. 502
  503. 503
  504. 504
  505. 505
  506. 506
  507. 507
  508. 508
  509. 509
  510. 510
  511. 511
  512. 512
  513. 513
  514. 514
  515. 515
  516. 516
  517. 517
  518. 518
  519. 519
  520. 520
  521. 521
  522. 522
  523. 523
  524. 524
  525. 525
  526. 526
  527. 527
  528. 528
  529. 529
  530. 530
  531. 531
  532. 532
  533. 533
  534. 534
  535. 535
  536. 536
  537. 537
  538. 538
  539. 539
  540. 540
  541. 541
  542. 542
  543. 543
  544. 544
  545. 545
  546. 546
  547. 547
  548. 548
  549. 549
  550. 550
  551. 551
  552. 552
  553. 553
  554. 554
  555. 555
  556. 556
  557. 557
  558. 558
  559. 559
  560. 560
  561. 561
  562. 562
  563. 563
  564. 564
  565. 565
import Mathlib

/-
Lean, the category!
Objects: Types
Morphisms: (Total) Functions

Resources:
https://raw.githubusercontent.com/BartoszMilewski/DaoFP/refs/heads/master/DaoFP.pdf
https://math.andrej.com/2016/08/06/hask-is-not-a-category/
https://www.mit.edu/~xy/lean/
https://tannerduve.github.io/files/monads.pdf
-/

-- Initial object
#check Empty

-- Morphism from initial object
#check Empty.elim

-- Terminal object
#check PUnit

-- Identity morphism
#check id

-- Composition of morphism is associative
#check Function.comp_assoc

-- Equality of morphisms
#check funext

-- Sums (coproducts)
#check Sum

-- Products
#check Prod

-- Exponentials
#check (·  ·)

-- Lean is a bicartesian closed category, which means it has an initial object, terminal object, sums, products, exponentials, and sums distribute over products.


/-
Endofunctors in Lean!

f maps objects
f.map or `<$>` maps morphisms
`(α → β) → f α → f β` is the same thing as `(α → β) → (f α → f β)`
-/
#check Functor
#check LawfulFunctor

#synth Functor List

#synth Functor Option

#synth Functor Tree

#synth Functor (Except String)

instance : LawfulFunctor List where
  map_const := by solve_by_elim
  id_map xs := by simp
  comp_map := by simp


/--
Contravariant functors (normal functors are "covariant" functors) are functors from Colean to Lean

`map` turns a "producer of α" into a "producer of β"
`comap` turns a "consumer of α" into a "consumer of β"
-/
class Cofunctor (f : Type u  Type v) where
  comap : (β  α)  f α  f β
  id_comap (x : f α) : comap id x = x
  comp_comap (g : β  α) (h : γ  β) (x : f α) : comap (g  h) x = comap h (comap g x)

/-- This is not standard notation but I just made something up -/
infixr:100 " <¥> " => Cofunctor.comap

theorem Cofunctor.id_comap' [Cofunctor f] : Cofunctor.comap (f := f) (@id α) = id := by
  ext x
  exact Cofunctor.id_comap (f := f) x

theorem Cofunctor.comap_comp_comap [Cofunctor f] (g : α  β) (h : β  γ) :
    ((g <¥> ·)  (h <¥> ·) : f γ  f α) = Cofunctor.comap (f := f) (h  g) :=
  funext fun _  (comp_comap _ _ _).symm


/-- Hom-functor in enriched category -/
@[simp]
instance (α : Type u) : Functor (α  ·) where
  map f g := f  g

instance (α : Type u) : LawfulFunctor (α  ·) where
  map_const := by solve_by_elim
  id_map := by simp
  comp_map := by simp [Function.comp_assoc]

@[simp]
instance (α : Type u) : Cofunctor (·  α) where
  comap f g := g  f
  id_comap := by simp
  comp_comap := by simp [Function.comp_assoc]

-- Most examples of cofunctors in Lean are these function object things

/-
A function type is covariant if the free param is in an even depth and contravariant otherwise.

α → · is co
· → α is contra
(· → α) → β is co
((· → α) → β) → γ is contra
and so on
-/


/-- Composition of two functors of same variance is a functor -/
@[simp]
instance [Functor f] [Functor g] : Functor (f  g) where
  map h x := Functor.map (f := f) (h <$> ·) x

instance [Functor f] [LawfulFunctor f] [Functor g] [LawfulFunctor g] : LawfulFunctor (f  g) where
  map_const := by solve_by_elim
  id_map := by simp
  comp_map h h' x := by simp; rfl

@[simp]
instance [Cofunctor f] [Cofunctor g] : Functor (f  g) where
  map h x := Cofunctor.comap (f := f) (Cofunctor.comap h) x

instance [Cofunctor f] [Cofunctor g] : LawfulFunctor (f  g) where
  map_const := by solve_by_elim
  id_map := by simp [Cofunctor.id_comap']
  comp_map h h' x := by simp [ Cofunctor.comap_comp_comap]

-- If functors are sort of like "containers" for data, then functor composition is "nesting" two containers
#synth LawfulFunctor (List  Option)


/-- Composition of functors of opposite variance is a contravariant functor -/
@[simp]
instance [Functor f] [LawfulFunctor f] [Cofunctor g] : Cofunctor (f  g) where
  comap h x := Functor.map (f := f) (h <¥> ·) x
  id_comap := by simp [Cofunctor.id_comap (f := g)]
  comp_comap := by simp [Cofunctor.comp_comap]

@[simp]
instance [Cofunctor f] [Functor g] [LawfulFunctor g] : Cofunctor (f  g) where
  comap h x := Cofunctor.comap (f := f) (h <$> ·) x
  id_comap := by
    simp only [Function.comp_apply, id_map]
    exact Cofunctor.id_comap (f := f)
  comp_comap h h' x := by
    simp only [ Functor.map_comp_map h' h, Cofunctor.comp_comap (f := f)]


-- Bifunctors map Lean × Lean to Lean
#check Bifunctor
#check LawfulBifunctor

-- `Sum` and `Prod` are bifunctors
#synth LawfulBifunctor Sum
#synth LawfulBifunctor Prod


-- Multivariate functors
#check MvFunctor
#check LawfulMvFunctor


/-- Profunctors are useful for lenses -/
class Profunctor (p : Type u  Type v  Type*) where
  dimap : (s  a)  (b  t)  (p a b  p s t)
  id_dimap (x : p α β) : dimap id id x = x
  dimap_dimap (f : α₁  α₀) (f' : α₂  α₁) (g : β₀  β₁) (g' : β₁  β₂) (x : p α₀ β₀) :
    dimap f' g' (dimap f g x) = dimap (f  f') (g'  g) x

/-- Exponentials are profunctors -/
instance : Profunctor (·  ·) where
  dimap f g h := g  h  f
  id_dimap := by simp
  dimap_dimap := by simp [Function.comp_assoc]

inductive Procompose p q [Profunctor p] [Profunctor q] a b
  | mk : q a x  p x b  Procompose p q a b

def mapOut [Profunctor p] [Profunctor q] (pc : Procompose p q a b) (f : {x : Type u}  q a x  p x b  c) :=
  match pc with
  | qax, pxb => f qax pxb

instance [Profunctor p] [Profunctor q] : Profunctor (Procompose p q) where
  dimap l r
    | qax, pxb => Profunctor.dimap l id qax, Profunctor.dimap id r pxb
  id_dimap := by simp [Profunctor.id_dimap]
  dimap_dimap := by simp [Profunctor.dimap_dimap]

abbrev End p [Profunctor p] :=  x, p x x

abbrev Coend p [Profunctor p] := Σ x, p x x

abbrev ProPair q p [Profunctor p] [Profunctor q] a b x y :=
  q a y × p x b

instance [Profunctor p] [Profunctor q] : Profunctor (ProPair q p a b) where
  dimap l r
    | qax, pxb => Profunctor.dimap id r qax, Profunctor.dimap l id pxb
  id_dimap := by simp [Profunctor.id_dimap]
  dimap_dimap := by simp [Profunctor.dimap_dimap]

abbrev CoendCompose p q [Profunctor p] [Profunctor q] a b :=
  Coend (ProPair q p a b)

instance [Profunctor p] [Profunctor q] : Profunctor (CoendCompose p q) where
  dimap l r
    | x, (qay, pxb) => x, (Profunctor.dimap l id qay, Profunctor.dimap id r pxb)
  id_dimap := by simp [Profunctor.id_dimap]
  dimap_dimap := by simp [Profunctor.dimap_dimap]


/--
Type of a natural transformation (without the naturality condition)

Intuitively, it represents moving data from one "container" to another
-/
abbrev NaturalType.{u} (f : Type u  Type v) (g : Type u  Type v) :=
  {α : Type u}  f α  g α

/--
Naturality is automatically guarenteed for parametrically polymorphic functions (where the implementation is the same for each type), AKA "theorems for free". This is not guarenteed in general though since we could have a function which inspects the input type and does something crazy.

TODO: Would it be more convenient to make this a subtype?
-/
class Natural f [Functor f] [LawfulFunctor f] g [Functor g] [LawfulFunctor g] (η : NaturalType f g) where
  naturality (h : α  β) (x : f α) : h <$> (η x) = η (h <$> x)

instance : Natural List Option List.head? :=
  by simp

def OptionToList : Option α  List α
  | some a => [a]
  | none => []

instance : Natural Option List OptionToList :=
  by simp [OptionToList]; grind

class Conatural f [Cofunctor f] g [Cofunctor g] (η : NaturalType f g) where
  naturality (h : β  α) (x : f α) : h <¥> (η x) = η (h <¥> x)

/--
Vertical composition of natural transformations

Intuitively this is like doing two data moves.
-/
instance [Functor f] [LawfulFunctor f] [Functor g] [LawfulFunctor g] [Functor h] [LawfulFunctor h] [M : Natural f g η] [N : Natural g h μ] :
    Natural f h (fun {α : Type u}  @μ α  @η α) :=
  by simp [N.naturality, M.naturality]

/--
Horizontal composition of natural transformations

Intuitively this is like repackaging data in nested "containers"
-/
instance (η : NaturalType f f') (μ : NaturalType g g') [Functor f] [LawfulFunctor f] [Functor f'] [LawfulFunctor f'] [Functor g] [LawfulFunctor g] [Functor g'] [LawfulFunctor g'] [M : Natural f f' η] [N : Natural g g' μ] :
    Natural (g  f) (g'  f') (μ  (Functor.map (f := g) η ·)) :=
  by simp [N.naturality, M.naturality]

/-- Alternatively we do `μ` first and then the map second -/
instance (η : NaturalType f f') (μ : NaturalType g g') [Functor f] [LawfulFunctor f] [Functor f'] [LawfulFunctor f'] [Functor g] [LawfulFunctor g] [Functor g'] [LawfulFunctor g'] [M : Natural f f' η] [N : Natural g g' μ] :
    Natural (g  f) (g'  f') ((Functor.map (f := g') η ·)  μ) :=
  by simp [N.naturality, M.naturality]

/-- The two orderings are equivalent, which only requires the outer transformation to be natural -/
lemma horizontal_comp_equiv (η : NaturalType f f') (μ : NaturalType g g') [Functor f] [LawfulFunctor f] [Functor f'] [LawfulFunctor f'] [Functor g] [LawfulFunctor g] [Functor g'] [LawfulFunctor g'] [N : Natural g g' μ] :
    (μ  (Functor.map (f := g) η ·)) x = ((Functor.map (f := g') η ·)  μ) x := by
  simp [N.naturality]


/-- Yoneda forward map (g is not necessarily natural) -/
def yoneda (g : NaturalType (α  ·) f) [Functor f] [LawfulFunctor f] : f α := g id

/-- Yoneda reverse map -/
def yoneda' [Functor f] [LawfulFunctor f] (y : f α) : NaturalType (α  ·) f := (· <$> y)

/-- Reverse map always produces a natural transformation -/
instance [Functor f] [LawfulFunctor f] : Natural (α  ·) f (yoneda' y) :=
  fun h x  by simp [yoneda']; rfl

/--
Mapping and unmapping a natural transformation returns the itself

Note that this does not work for an arbitrary function between the hom-functor and `f` because we use the naturality condition.
-/
theorem yoneda_lemma (g : NaturalType (α  ·) f) [Functor f] [LawfulFunctor f] [N : Natural (α  ·) f g] : yoneda' (yoneda g) x = g x := by
  simp [yoneda, yoneda', N.naturality]

/-- Mapping and unmapping an element `f α` returns itself -/
theorem yoneda_lemma' (y : f α) [Functor f] [LawfulFunctor f] : yoneda (yoneda' y) = y := by
  simp [yoneda, yoneda']

/-- Coyoneda forward map -/
def coyoneda (g : NaturalType (·  α) f) [Cofunctor f] : f α := g id

/-- Coyoneda reverse map -/
def coyoneda' [Cofunctor f] (y : f α) : NaturalType (·  α) f := (· <¥> y)

/-- Reverse map always produces a natural transformation -/
instance [Cofunctor f] : Conatural (·  α) f (coyoneda' y) :=
  fun h x  by simp [coyoneda', Cofunctor.comp_comap]

/-- Same but for Coyoneda -/
theorem coyoneda_lemma (g : NaturalType (·  α) f) [Cofunctor f] [N : Conatural (·  α) f g] : coyoneda' (coyoneda g) x = g x := by
  simp [coyoneda, coyoneda', N.naturality]

/-- Same but for Coyoneda -/
theorem coyoneda_lemma' (y : f α) [Cofunctor f] : coyoneda (coyoneda' y) = y := by
  simp [coyoneda, coyoneda', Cofunctor.id_comap]


-- Applicative functors
#check Applicative
#check LawfulApplicative

-- Motivation: mapping multi-arg functions
#simp (some 3).map (· * ·)
#eval (· * ·) <$> (some 3) <*> (some 4)

/-- Composition of two applicatives is an applicative -/
@[simp]
instance [Applicative f] [Applicative g] : Applicative (f  g) where
  pure x := pure (f := f) (pure x)
  seq h x := Seq.seq (f := f) ((· <*> ·) <$> h) x

instance [Applicative f] [LawfulApplicative f] [Applicative g] [LawfulApplicative g] : LawfulApplicative (f  g) where
  seqLeft_eq := by simp
  seqRight_eq := by simp
  pure_seq := by simp [pure_seq]
  map_pure := by simp
  seq_pure := by simp
  seq_assoc x h h' := by
    simp [seq_assoc, seq_map_assoc, map_seq]
    congr
    ext
    simp [seq_assoc]

-- TODO: Lax monoidal functors


-- Monads, "warm fuzzy things"
#check Monad
#check LawfulMonad

/-
Functors let us apply `α → β` to `f α`
Applicatives let us apply `f (a → β)` to `f α`
But what about applying an "effectful function" `α → f β` to `f α`?
Another use case is to compose `α → f β` and `β → f γ`.

Kleisli category: Any monad `m` creates a category where the objects are still types but the morphisms are `α → β` for every `α → f β` in Lean. Then composition of effectful functions becomes composition of morphisms.

This construction also motivates the monad laws.

In fact, using `>>=` and `pure` we can implement `<$>` and `<*>` so every monad is also a functor and applicative.

Exercise: Find an example of a functor which is not applicative and an applicative which is not a monad.
-/

#synth Monad Option

#synth Monad IO

#synth Monad (StateM )

#synth Monad (Writer )

#synth Monad (ST )

#synth Monad (Except String)

#synth Monad (Sum )

instance : LawfulMonad Option :=
  LawfulMonad.mk' Option
    (id_map := by simp)
    (pure_bind := by simp [Option.bind])
    (bind_assoc := by simp; grind)
    (bind_pure_comp := by simp [Option.map]; grind)

#synth Monad List

#synth LawfulMonad List


/--
This function looks ugly, but we can simplify it with `do` notation, which is syntactic sugar that lets us unwrap monadic values and automatically inserts `>>=` when we use the unwrapped values

https://slightknack.dev/blog/do-notation/
-/
def option_div (x_wrapped : Option ) (y_wrapped : Option ) : Option  :=
  y_wrapped >>= fun y 
    if y = 0 then
      none
    else
      x_wrapped >>= fun x  some <| x / y

#eval option_div (some 3) (some 0)

def option_div' (x_wrapped : Option ) (y_wrapped : Option ) : Option  := do
  let x  x_wrapped
  let y  y_wrapped
  if y = 0 then none else some <| x / y

/-- Even the identity monad is powerful! -/
def Array.insSort [LinearOrder α] (A : Array α) := Id.run do
  let N := A.size
  let mut A := A.toVector
  for hi : i in [:N] do
    for hj : j in [:i] do
      have := Membership.get_elem_helper hi rfl
      if A[i - j] < A[i - j - 1] then
        A := A.swap (i - j - 1) (i - j)
      else
        break
  return A.toArray

/-- List monad demo -/
def UpToN (xs : List ) : List  := do
  let x  xs
  let y  List.range x
  return y

#eval UpToN [1, 2, 3]

/-
Sadly, in general monads do not compose 😿
https://carlo-hamalainen.net/2014/01/02/applicatives-compose-monads-do-not/

However, in some cases we can use monad transformers to compose them.
-/


-- Equivalent definition using "fish"
#check Bind.kleisliRight
-- Equivalent definition using "join"
#check joinM
-- Exercise: Implement bind using fish

/-
"A monad is just a monoid in the category of endofunctors"

In fact, there is a bijection between the two!

https://old.reddit.com/r/math/comments/ap25mr/a_monad_is_a_monoid_in_the_category_of/

The category of Lean endofunctors
Objects: Endofunctors
Morphisms: Natural transformations (we showed earlier that vertical composition produces another natural transformation)
-/

/-- Every object has an identity morphism -/
instance [Functor f] [LawfulFunctor f] : Natural f f id :=
  by simp

/-- Vertical composition is associative -/
lemma nat_trans_comp_assoc (η : NaturalType f g) (μ : NaturalType g h) (ν : NaturalType h i) [Functor f] [LawfulFunctor f] [Functor g] [LawfulFunctor g] [Functor h] [LawfulFunctor h] [Functor i] [LawfulFunctor i] :
    ((ν  μ)  η) x = (ν  μ  η) x := by
  simp only [Function.comp_assoc]

#check Monoid

/-
A monoidal category is a category C equipped with a tensor product ⨂ from C × C to C and an identity object I with certain properties.

For the category of Lean endofunctors, let ⨂ be functor composition and I be the identity functor `Id`.
-/

/-- ⨂ is obviously associative -/
lemma functor_comp_assoc [Functor f] [LawfulFunctor f] [Functor g] [LawfulFunctor g] [Functor h] [LawfulFunctor h] : (f  g)  h = f  g  h := by
  apply Function.comp_assoc

/-- `Id` is an identity for ⨂ -/
lemma functor_left_id [Functor f] [LawfulFunctor f] : id  f = f := by
  simp

/-- `Id` is an identity for ⨂ -/
lemma functor_right_id [Functor f] [LawfulFunctor f] : f  id = f := by
  simp

-- The coherence conditions (insert scary pentagon diagram here) are automatically satisfied because the associator and unitor natural isomorphisms are equalities.

/-
A monoidal object is an object M in (C, ⨂, I) with an arrow μ from M ⨂ M to M and η from I to M such that μ is associative and η is an identity with respect to μ.

A monoidal object in the category of Lean endofunctors is a functor with natural transformations `join` (corresponding to μ) and `pure` (η) with the following properties:
-/

class EndofunctorMonoid m extends Functor m, LawfulFunctor m where
  join : NaturalType (m  m) m
  pure : NaturalType Id m
  join_pure : (join  pure) x = x
  join_map_pure : (join  (Functor.map (f := m) pure ·)) x = x
  join_join : (join  (Functor.map (f := m) join ·)) x = (join  join) x

@[simp]
def bindFromJoin [EndofunctorMonoid m] (join : NaturalType (m  m) m) (x : m α) (f : α  m β) :=
  join (Functor.map (f := m) f x)

@[simp]
instance [EndofunctorMonoid m] : Monad m where
  pure := EndofunctorMonoid.pure
  bind := bindFromJoin EndofunctorMonoid.join

/-- A monoid in the category of endofunctors is a monad -/
instance [EndofunctorMonoid m] [J : Natural (m  m) m EndofunctorMonoid.join] [P : Natural Id m EndofunctorMonoid.pure] : LawfulMonad m :=
  LawfulMonad.mk' m id_map
    (pure_bind := fun x f  by
      simpa [P.naturality, Functor.map] using EndofunctorMonoid.join_pure)
    (bind_assoc := fun x f g  by
      have := EndofunctorMonoid.join_join (x := (fun a  Functor.map (f := m) g (f a)) <$> x)
      simp at this
      simp [J.naturality,  this])
    (map_const := by simp [map_const])
    (bind_pure_comp := fun f x  by
      have := EndofunctorMonoid.join_map_pure (x := f <$> x)
      simpa using this)

@[simp]
def joinFromBind [Monad m] (bind : {α β : Type u}  m α  (α  m β)  m β) (x : m (m α)) :=
  bind x id

/-- A monad is a monoid in the category of endofunctors -/
@[simp]
instance [Monad m] [LawfulMonad m] : EndofunctorMonoid m where
  pure := pure
  join := joinFromBind bind
  join_pure := by simp
  join_map_pure := by simp
  join_join := by simp

instance [Monad m] [LawfulMonad m] : Natural (m  m) m EndofunctorMonoid.join :=
  by simp

instance [Monad m] [LawfulMonad m] : Natural Id m EndofunctorMonoid.pure :=
  by simp [Functor.map]

/-- `bindFromJoin` and `joinFromBind` form a bijection -/
theorem bind_join_equiv [Monad m] [LawfulMonad m] : (bindFromJoin (m := m) (joinFromBind bind)) x f = bind x f := by
  simp

theorem bind_join_equiv' [E : EndofunctorMonoid m] : joinFromBind (bindFromJoin E.join) x = E.join x := by
  simp

-- TODO: Monad transformers

-- TODO: Enrichment

-- Unlike in Haskell, Lean is powerful enough that we can also use it for doing category theory in any category, not just the category Lean
#check CategoryTheory.Category
#check CategoryTheory.Functor
#check CategoryTheory.yoneda
#check CategoryTheory.Monad
#check CategoryTheory.Monad.monadMonEquiv