From fd50076850ee1fa94198ba9ecb388202cbb307e6 Mon Sep 17 00:00:00 2001 From: Kenneth Date: Mon, 13 May 2024 23:13:37 +0100 Subject: [PATCH] initial commit --- LICENSE | 8 ++++++++ README.md | 12 ++++++++++++ arithmetics.ts | 31 +++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+) create mode 100644 LICENSE create mode 100644 README.md create mode 100644 arithmetics.ts diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..9871934 --- /dev/null +++ b/LICENSE @@ -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. + diff --git a/README.md b/README.md new file mode 100644 index 0000000..5f0e545 --- /dev/null +++ b/README.md @@ -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. + diff --git a/arithmetics.ts b/arithmetics.ts new file mode 100644 index 0000000..350ae0e --- /dev/null +++ b/arithmetics.ts @@ -0,0 +1,31 @@ +type Num = { prev?: Num; zero: boolean }; +type Succ = { prev: N; zero: false }; +type Pred = 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 _0 + ? B + : Succ, B>>; +type Sub = B extends _0 + ? A + : Pred>>; +type Multiply = B extends _0 + ? _0 + : Add>>; + +type Fib = N extends _0 + ? N + : N extends _1 + ? N + : Add>, Fib>>;