initial commit

This commit is contained in:
2024-05-13 23:13:37 +01:00
commit fd50076850
3 changed files with 51 additions and 0 deletions

8
LICENSE Normal file
View File

@@ -0,0 +1,8 @@
Copyright 2024 kennethnym
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

12
README.md Normal file
View File

@@ -0,0 +1,12 @@
this repo contains implementation of addition, subtraction, and multiplication using only typescript types, results of which are obtained by inferring the type.
[link to my blog](https://kennethnym.com/blog/arithmetics-with-typescript-types)
## why?
i am bored
## future plan
porting doom to the typescript type system, obviously.

31
arithmetics.ts Normal file
View File

@@ -0,0 +1,31 @@
type Num = { prev?: Num; zero: boolean };
type Succ<N extends Num> = { prev: N; zero: false };
type Pred<N extends Num> = N["prev"] extends Num ? N["prev"] : _0;
type _0 = { zero: true };
type _1 = Succ<_0>;
type _2 = Succ<_1>;
type _3 = Succ<_2>;
type _4 = Succ<_3>;
type _5 = Succ<_4>;
type _6 = Succ<_5>;
type _7 = Succ<_6>;
type _8 = Succ<_7>;
type _9 = Succ<_8>;
type _10 = Succ<_9>;
type Add<A extends Num, B extends Num> = A extends _0
? B
: Succ<Add<Pred<A>, B>>;
type Sub<A extends Num, B extends Num> = B extends _0
? A
: Pred<Sub<A, Pred<B>>>;
type Multiply<A extends Num, B extends Num> = B extends _0
? _0
: Add<A, Multiply<A, Pred<B>>>;
type Fib<N extends Num> = N extends _0
? N
: N extends _1
? N
: Add<Fib<Pred<N>>, Fib<Sub<N, _2>>>;