aboutsummaryrefslogtreecommitdiff
path: root/src/natural-numbers.ts
blob: fa8a3ce99fdf300619bf94648976ed257d26b942 (plain) (blame)
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
import { Add, Multiply, Succ, _0, _1, _3 } from './utils/nat';
import { Op, Rewrite, ChainRewrites, Sym, Equation, Refl, VerifyEquation, assert, Subst, ApplyRewrite, Evaluate, OpToStr } from './utils/theorem';

export namespace addition {
  // Identity
  export type Identity<A extends Op> = Rewrite<Add<A, _0>, A>;
  export type IdentityR<A extends Op> = Rewrite<Add<_0, A>, A>;

  export type SuccLemma<A extends Op, B extends Op> = Rewrite<Add<Succ<A>, B>, Succ<Add<A, B>>>
  export type SuccLemmaR<A extends Op, B extends Op> = Rewrite<Add<A, Succ<B>>, Succ<Add<A, B>>>

  // Commutativity
  export type Commutativity<A extends Op, B extends Op> = Rewrite<Add<A, B>, Add<B, A>>;
  export interface Comm_Base_Proof<B extends Op> {
    type: 'rewrite',
    left: Equation<Add<_0, B>, Add<B, _0>>; // 0 + b = b + 0
    right: ChainRewrites<[
      IdentityR<B>,           // b = b + 0
      Sym<Identity<B>>,       // b + 0 = b + 0
      Refl,
    ], this['left']>;
  };
  export interface Comm_Inductive_Proof<A extends Op, B extends Op> {
    type: 'rewrite',
    left: Equation<Add<Succ<A>, B>, Add<B, Succ<A>>>;  // succ(a) + b = b + succ(a)
    right: ChainRewrites<[
      SuccLemma<A, B>,          // succ(a + b) = b + succ(a)
      Commutativity<A, B>,      // succ(b + a) = b + succ(a)
      SuccLemmaR<B, A>,         // succ(b + a) = succ(b + a)
      Refl,
    ], this['left']>;
  };
  export namespace spec {
    export type commutativity = [
      assert<VerifyEquation<Comm_Base_Proof<'B'>>>,
      assert<VerifyEquation<Comm_Inductive_Proof<'A', 'B'>>>,
    ]
  }

  // Associativity
  export type Associativity<A extends Op, B extends Op, C extends Op> = Rewrite<Add<Add<A, B>, C>, Add<A, Add<B, C>>>;
  export interface Assoc_Base_Proof<B extends Op, C extends Op> {
    type: 'rewrite',
    left: Equation<Add<Add<_0, B>, C>, Add<_0, Add<B, C>>>; // (0 + b) + c = 0 + (b + c)
    right: ChainRewrites<[
      Commutativity<_0, B>,      // (b + 0) + c = 0 + (b + c)
      Identity<B>,               // b + c = 0 + (b + c)
      IdentityR<Add<B, C>>,      // b + c = b + c
      Refl,
    ], this['left']>;
  };
  export interface Assoc_Inductive_Proof<A extends Op, B extends Op, C extends Op> {
    type: 'rewrite',
    left: Equation<Add<Add<Succ<A>, B>, C>, Add<Succ<A>, Add<B, C>>>; // (succ(a) + b) + c = succ(a) + (b + c)
    right: ChainRewrites<[
      SuccLemma<A, B>,               // succ(a + b) + c = succ(a) + (b + c)
      SuccLemma<A, Add<B, C>>,       // succ(a + b) + c = succ(a + (b + c))
      SuccLemma<Add<A, B>, C>,       // succ((a + b) + c) = succ(a + (b + c))
      Associativity<A, B, C>,        // succ(a + (b + c)) = succ(a + (b + c))
      Refl,
    ], this['left']>;
  };

  export namespace spec {
    export type associativity = [
      assert<VerifyEquation<Assoc_Base_Proof<'A', 'B'>>>,
      assert<VerifyEquation<Assoc_Inductive_Proof<'A', 'B', 'C'>>>,
    ]
  }

  // Rearrange
  export interface Rearrange_Proof<A extends Op, B extends Op, C extends Op, D extends Op> {
    type: 'rewrite',
    left: Equation<Add<Add<A, B>, Add<C, D>>, Add<A, Add<Add<B, C>, D>>>; // (a + b) + (c + d) = a + ((b + c) + d)
    right: ChainRewrites<[
      Associativity<A, B, Add<C, D>>,   // a + (b + (c + d)) = a + ((b + c) + d)
      Associativity<B, C, D>,           // a + (b + (c + d)) = a + (b + (c + d))
      Refl,
    ], this['left']>;
  };
  export namespace spec {
    export type rearrange = [
      assert<VerifyEquation<Rearrange_Proof<'A', 'B', 'C', 'D'>>>,
    ]
  }
}

export namespace multiplication {
  // Identity
  export type Identity<A extends Op> = Rewrite<Multiply<A, _1>, A>;
  export type IdentityR<A extends Op> = Rewrite<Multiply<_1, A>, A>;

  export type SuccLemma<A extends Op, B extends Op> = Rewrite<Multiply<Succ<A>, B>, Add<B, Multiply<A, B>>>;
  export type SuccLemmaR<A extends Op, B extends Op> = Rewrite<Multiply<A, Succ<B>>, Add<A, Multiply<A, B>>>;

  // Commutativity
  export type Commutativity<A extends Op, B extends Op> = Rewrite<Multiply<A, B>, Multiply<B, A>>;
  export interface Comm_Base_Proof<B extends Op> {
    type: 'rewrite',
    left: Equation<Multiply<_1, B>, Multiply<B, _1>>; // 1 * b = b * 1
    right: ChainRewrites<[
      IdentityR<B>,        // b = b * 1
      Identity<B>,         // b = b
      Refl,
    ], this['left']>;
  };
  export interface Comm_Inductive_Proof<A extends Op, B extends Op> {
    type: 'rewrite',
    left: Equation<Multiply<Succ<A>, B>, Multiply<B, Succ<A>>>; // succ(a)*b = b*succ(a)
    right: ChainRewrites<[
      SuccLemma<A, B>,             // b + a*b = b*succ(a)
      SuccLemmaR<B, A>,            // b + a*b = b + b*a
      Commutativity<B, A>,         // b + a*b = b + a*b
      Refl,
    ], this['left']>;
  };
  export namespace spec {
    export type commutativity = [
      assert<VerifyEquation<Comm_Base_Proof<'B'>>>,
      assert<VerifyEquation<Comm_Inductive_Proof<'A', 'B'>>>,
    ]
  }

  // Distributivity
  export type Distributivity<A extends Op, B extends Op, C extends Op> =
    Rewrite<Multiply<Add<A, B>, C>, Add<Multiply<A, C>, Multiply<B, C>>>;
  export interface Dist_Base_Proof<B extends Op, C extends Op> {
    type: 'rewrite',
    left: Equation<Multiply<Add<_1, B>, C>, Add<Multiply<_1, C>, Multiply<B, C>>>; // (1 + b)*c = 1*c + b*c
    right: ChainRewrites<[
      IdentityR<C>,                    // (1 + b)*c = c + b*c
      addition.SuccLemma<_0, B>,       // succ(0 + b)*c = c + b*c
      addition.IdentityR<B>,           // succ(b)*c = c + b*c
      SuccLemma<B, C>,                 // c + b*c = c + b*c
      Refl,
    ], this['left']>;
  };
  export interface Dist_Inductive_Proof<A extends Op, B extends Op, C extends Op> {
    type: 'rewrite',
    left: Equation<Multiply<Add<Succ<A>, B>, C>, Add<Multiply<Succ<A>, C>, Multiply<B, C>>>; // (succ(a) + b)*c = succ(a)*c + b*c
    right: ChainRewrites<[
      addition.SuccLemma<A, B>,                                     // succ(a + b)*c = succ(a)*c + b*c
      SuccLemma<Add<A, B>, C>,                                      // c + (a + b)*c = succ(a)*c + b*c
      SuccLemma<A, C>,                                              // c + (a + b)*c = (c + a*c) + b*c
      Distributivity<A, B, C>,                                      // c + (a*c + b*c) = (c + a*c) + b*c
      addition.Associativity<C, Multiply<A, C>, Multiply<B, C>>,    // (c + a*c) + b*c = (c + a*c) + b*c
      Refl,
    ], this['left']>;
  };
  export namespace spec {
    export type distributivity = [
      assert<VerifyEquation<Dist_Base_Proof<'B', 'C'>>>,
      assert<VerifyEquation<Dist_Inductive_Proof<'A', 'B', 'C'>>>,
    ]
  }

  // Associativity
  export type Associativity<A extends Op, B extends Op, C extends Op> =
    Rewrite<Multiply<Multiply<A, B>, C>, Multiply<A, Multiply<B, C>>>;
  export interface Assoc_Base_Proof<B extends Op, C extends Op> {
    type: 'rewrite',
    left: Equation<Multiply<Multiply<_1, B>, C>, Multiply<_1, Multiply<B, C>>>; // (1*b)*c = 1*(b*c)
    right: ChainRewrites<[
      IdentityR<B>,                     // b*c = 1*(b*c)
      IdentityR<Multiply<B, C>>,        // b*c = b*c
      Refl,
    ], this['left']>;
  };
  export interface Assoc_Inductive_Proof<A extends Op, B extends Op, C extends Op> {
    type: 'rewrite',
    left: Equation<Multiply<Multiply<Succ<A>, B>, C>, Multiply<Succ<A>, Multiply<B, C>>>; // (succ(a)*b)*c = succ(a)*(b*c)
    right: ChainRewrites<[
      SuccLemma<A, B>,                         // (b + a*b)*c = succ(a) * (b*c)
      Distributivity<B, Multiply<A, B>, C>,    // b*c + (a*b)*c = succ(a) * (b*c)
      SuccLemma<A, Multiply<B, C>>,            // b*c + (a*b)*c = b*c + a*(b*c)
      Associativity<A, B, C>,                  // b*c + a*(b*c) = b*c + a*(b*c)
      Refl,
    ], this['left']>;
  };
  export namespace spec {
    export type associativity = [
      assert<VerifyEquation<Assoc_Base_Proof<'B', 'C'>>>,
      assert<VerifyEquation<Assoc_Inductive_Proof<'A', 'B', 'C'>>>,
    ]
  }
}