summaryrefslogtreecommitdiffstats
path: root/site/posts/AlgebraicDatatypes.v
blob: 1b2452081835131cd9d08e7e5712a1ba74466f6b (plain)
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
(** * Proving Algebraic Datatypes are “Algebraic” *)

(** Several programming languages allow programmers to define (potentially
    recursive) custom types, by composing together existing ones. For instance,
    in OCaml, one can define lists as follows:

<<
type 'a list =
| Cons of 'a * 'a list
| Nil
>>

    This translates in Haskell as

<<
data List a =
  Cons a (List a)
| Nil
>>

    In Rust:

<<
enum List<A> {
  Cons(A, Box< List<a> >),
  Nil,
}
>>

    In Coq:

<<
Inductive list a :=
| cons : a -> list a -> list a
| nil
>>

    And so forth.

    Each language will have its own specific constructions, and the type systems
    of OCaml, Haskell, Rust and Coq —to only cite them— are far from being
    equivalent. That being said, they often share a common “base formalism,”
    usually (and sometimes abusively) referred to as _algebraic datatypes_. This
    expression is used because under the hood any datatype can be encoded as a
    composition of types using two operators: sum ([+]) and product ([*]) for
    types.

      - [a + b] is the disjoint union of types [a] and [b]. Any term of [a]
        can be injected into [a + b], and the same goes for [b]. Conversely,
        a term of [a + b] can be projected into either [a] or [b].
      - [a * b] is the Cartesian product of types [a] and [b]. Any term of [a *
        b] is made of one term of [a] and one term of [b] (remember tuples?).

    For an algebraic datatype, one constructor allows for defining “named
    tuples”, that is ad-hoc product types. Besides, constructors are mutually
    exclusive: you cannot define the same term using two different constructors.
    Therefore, a datatype with several constructors is reminescent of a disjoint
    union.  Coming back to the [list] type, under the syntactic sugar of
    algebraic datatypes, the [list α] type is equivalent to [unit + α * list α],
    where [unit] models the [nil] case, and [α * list α] models the [cons] case.

    The set of types which can be defined in a language together with [+] and
    [*] form an “algebraic structure” in the mathematical sense, hence the
    name. It means the definitions of [+] and [*] have to satisfy properties
    such as commutativity or the existence of neutral elements. In this article,
    we will prove some of them in Coq. More precisely,

      - [+] is commutative, that is #<span class="imath">#\forall (x, y),\ x + y
        = y + x#</span>#
      - [+] is associative, that is #<span class="imath">#\forall (x, y, z),\ (x
        + y) + z = x + (y + z)#</span>#
      - [+] has a neutral element, that is #<span class="imath">#\exists e_s,
        \ \forall x,\ x + e_s = x#</span>#
      - [*] is commutative, that is #<span class="imath">#\forall (x, y),\ x * y
        = y * x#</span>#
      - [*] is associative, that is #<span class="imath">#\forall (x, y, z),\ (x
        * y) * z = x * (y * z)#</span>#
      - [*] has a neutral element, that is #<span class="imath">#\exists e_p,
        \ \forall x,\ x * e_p = x#</span>#
      - The distributivity of [+] and [*], that is #<span class="imath">#\forall
        (x, y, z),\ x * (y + z) = x * y + x * z#</span>#
      - [*] has an absorbing element, that is #<span class="imath">#\exists e_a,
        \ \forall x, \ x * e_a = e_a#</span>#

    For the record, the [sum] and [prod] types are defined in Coq as follows:

<<
Inductive sum (A B : Type) : Type :=
| inl : A -> sum A B
| inr : B -> sum A B

Inductive prod (A B : Type) : Type :=
| pair : A -> B -> prod A B
>>

    #<div id="generate-toc"></div>#

    #<div id="history">site/posts/AlgebraicDatatypes.v</div># *)

From Coq Require Import
     Basics Setoid Equivalence Morphisms
     List FunctionalExtensionality.
Import ListNotations.

Set Implicit Arguments.

(** ** An Equivalence for [Type] *)

(** Algebraic structures come with _equations_ expected to be true.  This means
    there is an implicit dependency which is —to my opinion— too easily
    overlooked: the definition of [=]. In Coq, [=] is a built-in relation that
    states that two terms are “equal” if they can be reduced to the same
    “hierarchy” of constructors. This is too strong in the general case, and in
    particular for our study of algebraic structures of [Type]. It is clear
    that, to Coq’s opinion, [α + β] is not structurally _equal_ to [β + α], yet
    we will have to prove they are “equivalent.” *)

(** *** Introducing [type_equiv] *)

(** Since [=] for [Type] is not suitable for reasoning about algebraic
    datatypes, we introduce our own equivalence relation, denoted [==].  We say
    two types [α] and [β] are equivalent up to an isomorphism (denoted by [α ==
    β]) when for any term of type [α], there exists a counter-part term of type
    [β] and vice versa. In other words, [α] and [β] are equivalent if we can
    exhibit two functions [f] and [g] such that:

    #<span class="dmath">#\forall (x : α),\ x = g(f(x))#</span>#

    #<span class="dmath">#\forall (y : β),\ y = f(g(y))#</span>#

    In Coq, this translates into the following inductive types. *)

Reserved Notation "x == y" (at level 72).

Inductive type_equiv (α β : Type) : Prop :=
| mk_type_equiv (f : α -> β) (g : β -> α)
                (equ1 : forall (x : α), x = g (f x))
                (equ2 : forall (y : β), y = f (g y))
  : α == β
where "x == y" := (type_equiv x y).

(** As mentioned earlier, we prove two types are equivalent by exhibiting
    two functions, and proving these functions satisfy two properties. We
    introduce a <<Ltac>> notation to that end. *)

Tactic Notation "equiv" "with" uconstr(f) "and" uconstr(g)
  := apply (mk_type_equiv f g).

(** The tactic [equiv with f and g] will turn a goal of the form [α == β] into
    two subgoals to prove [f] and [g] form an isomorphism. *)

(** *** [type_equiv] is an Equivalence *)

(** [type_equiv] is an equivalence, and we can prove it by demonstrating it is
    (1) reflexive, (2) symmetric, and (3) transitive.

    [type_equiv] is reflexive. *)

Lemma type_equiv_refl (α : Type) : α == α.

(** This proof is straightforward. A type [α] is equivalent to itself because:

    #<span class="imath">#\forall (x : α),\ x = id(id(x))#</span># *)

Proof.
  now equiv with (@id α) and (@id α).
Qed.

(** [type_equiv] is symmetric. *)

Lemma type_equiv_sym {α β} (equ : α == β) : β == α.

(** If [α == β], then we know there exists two functions [f] and [g] which
    satisfy the expected properties. We can “swap” them to prove that [β == α].
    *)

Proof.
  destruct equ as [f g equ1 equ2].
  now equiv with g and f.
Qed.

(** [type_equiv] is transitive *)

Lemma type_equiv_trans {α β γ} (equ1 : α == β) (equ2 : β == γ)
  : α == γ.

(** If [α == β], we know there exists two functions [fα] and [gβ] which satisfy
    the expected properties of [type_equiv]. Similarly, because [β == γ], we
    know there exists two additional functions [fβ] and [gγ]. We can compose
    these functions together to prove [α == γ].

    As a reminder, composing two functions [f] and [g] (denoted by [f >>> g]
    thereafter) consists in using the result of [f] as the input of [g]: *)

Infix ">>>" := (fun f g x => g (f x)) (at level 70).

(** Then comes the proof. *)

Proof.
  destruct equ1 as [  equαβ equβα],
           equ2 as [  equβγ equγβ].
  equiv with ( >>> ) and ( >>> ).
  + intros x.
    rewrite <- equβγ.
    now rewrite <- equαβ.
  + intros x.
    rewrite <- equβα.
    now rewrite <- equγβ.
Qed.

(** The Coq standard library introduces the [Equivalence] type class. We can
    provide an instance of this type class for [type_equiv], using the three
    lemmas we have proven in this section. *)

#[refine]
Instance type_equiv_Equivalence : Equivalence type_equiv :=
  {}.

Proof.
  + intros x.
    apply type_equiv_refl.
  + intros x y.
    apply type_equiv_sym.