-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.d.ts
219 lines (192 loc) · 5.4 KB
/
index.d.ts
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
// Natural
export type Peano = "S"[];
export type Succ<T extends "S"[]> = [...T, "S"];
export type Pred<T extends "S"[]> = T extends Z
? Z
: T extends [infer _, ...infer Tail]
? Tail
: never;
type Z = [];
type One = Succ<Z>;
export type ToPeano<
N extends number,
Result extends Peano = Z
> = Result["length"] extends N ? Result : ToPeano<N, Succ<Result>>;
export type FromPeano<N extends Peano> = N["length"];
export type AddPeano<A extends Peano, B extends Peano> = [...A, ...B];
export type SubtractPeano<A extends Peano, B extends Peano> = B extends Z
? A extends Z
? Z
: A
: SubtractPeano<Pred<A>, Pred<B>>;
export type MultiplyPeano<
A extends Peano,
B extends Peano,
Result extends Peano = A
> = A extends Z
? Z
: B extends Z
? Z
: B extends One
? Result
: MultiplyPeano<A, Pred<B>, AddPeano<A, Result>>;
export type LessThanPeano<A extends Peano, B extends Peano> = A extends B
? false
: SubtractPeano<A, B> extends Z
? true
: false;
export type DivPeano<
A extends Peano,
B extends Peano,
Result extends Peano = Z
> = A extends Z
? Result
: B extends Z
? never
: LessThanPeano<A, B> extends true
? Result
: DivPeano<SubtractPeano<A, B>, B, Succ<Result>>;
// Integer
export type Pos<N extends Peano> = ["Pos", N];
export type Neg<N extends Peano> = ["Neg", N];
export type Int = Pos<Peano> | Neg<Peano>;
type ToNumber<
T extends string,
R extends true[] = []
> = T extends `${R["length"]}` ? R["length"] : ToNumber<T, [true, ...R]>;
export type ToInt<N extends number> = `${N}` extends `-${infer N_}`
? Neg<ToPeano<ToNumber<N_>>>
: Pos<ToPeano<N>>;
// Limitation: Can not convert negative integer
export type FromInt<N extends Int> = N extends ["Pos", infer N_]
? FromPeano<N_>
: N extends ["Neg", infer N_]
? ["Neg", FromPeano<N_>]
: never;
type AddPosNeg<P extends Peano, N extends Peano> = LessThanPeano<
N,
P
> extends true
? Pos<SubtractPeano<P, N>>
: Neg<SubtractPeano<N, P>>;
export type AddInt<A extends Int, B extends Int> = A extends ["Pos", infer A_]
? B extends ["Pos", infer B_]
? Pos<AddPeano<A_, B_>>
: B extends ["Neg", infer B_]
? AddPosNeg<A_, B_>
: never
: A extends ["Neg", infer A_]
? B extends ["Pos", infer B_]
? AddPosNeg<B_, A_>
: B extends ["Neg", infer B_]
? Neg<AddPeano<A_, B_>>
: never
: never;
export type SubtractInt<A extends Int, B extends Int> = B extends [
"Pos",
infer B_
]
? AddInt<A, Neg<B_>>
: B extends ["Neg", infer B_]
? AddInt<A, Pos<B_>>
: never;
export type MultiplyInt<A extends Int, B extends Int> = A extends [
"Pos",
infer A_
]
? B extends ["Pos", infer B_]
? Pos<MultiplyPeano<A_, B_>>
: B extends ["Neg", infer B_]
? Neg<MultiplyPeano<A_, B_>>
: never
: A extends ["Neg", infer A_]
? B extends ["Pos", infer B_]
? Neg<MultiplyPeano<A_, B_>>
: B extends ["Neg", infer B_]
? Pos<MultiplyPeano<A_, B_>>
: never
: never;
export type DivInt<A extends Int, B extends Int> = A extends ["Pos", infer A_]
? B extends ["Pos", infer B_]
? Pos<DivPeano<A_, B_>>
: B extends ["Neg", infer B_]
? Neg<DivPeano<A_, B_>>
: never
: A extends ["Neg", infer A_]
? B extends ["Pos", infer B_]
? Neg<DivPeano<A_, B_>>
: B extends ["Neg", infer B_]
? Pos<DivPeano<A_, B_>>
: never
: never;
export type LessThanInt<A extends Int, B extends Int> = A extends [
"Pos",
infer A_
]
? B extends ["Pos", infer B_]
? LessThanPeano<A_, B_>
: B extends ["Neg", infer B_]
? false
: never
: A extends ["Neg", infer A_]
? B extends ["Pos", infer B_]
? true
: B extends ["Neg", infer B_]
? LessThanPeano<B_, A_>
: never
: never;
export type Add<A extends number, B extends number> = FromInt<
AddInt<ToInt<A>, ToInt<B>>
>;
export function add<A extends number, B extends number>(a: A, b: B): Add<A, B>;
export type Subtract<A extends number, B extends number> = FromInt<
SubtractInt<ToInt<A>, ToInt<B>>
>;
export function subtract<A extends number, B extends number>(
a: A,
b: B
): Subtract<A, B>;
export type Multiply<A extends number, B extends number> = FromInt<
MultiplyInt<ToInt<A>, ToInt<B>>
>;
export function multiply<A extends number, B extends number>(
a: A,
b: B
): Multiply<A, B>;
export type Div<A extends number, B extends number> = FromInt<
DivInt<ToInt<A>, ToInt<B>>
>;
export function div<A extends number, B extends number>(a: A, b: B): Div<A, B>;
export type Equal<A, B> = A extends B ? true : false;
export function equal<A extends number, B extends number>(
a: A,
b: B
): Equal<A, B>;
export type LessThan<A extends number, B extends number> = A extends B
? false
: LessThanInt<ToInt<A>, ToInt<B>>;
export function lessThan<A extends number, B extends number>(
a: A,
b: B
): LessThan<A, B>;
export type GreaterThan<A extends number, B extends number> = A extends B
? false
: LessThanInt<ToInt<B>, ToInt<A>>;
export function greaterThan<A extends number, B extends number>(
a: A,
b: B
): GreaterThan<A, B>;
export type PeanoRange<
Result extends Peano[],
Length extends number
> = Result["length"] extends Length
? Result
: Result extends [...infer _, infer L]
? PeanoRange<[...Result, Succ<L>], Length>
: never;
export type FromPeanoRange<R extends Peano[]> = {
[I in keyof R]: FromPeano<R[I]>;
};
export type Range<Bottom extends number, Top extends number> = FromPeanoRange<
PeanoRange<[ToPeano<Bottom>], Subtract<Top, Bottom>>
>;