From f6e26212620de411e082cbd52379075ea5a99032 Mon Sep 17 00:00:00 2001 From: Akshay Nair Date: Sun, 10 Dec 2023 16:15:00 +0530 Subject: init commit rewriting shit --- src/index.ts | 43 +++++++++++++++++++++++++++++++++++++++++++ src/nat.ts | 16 ++++++++++++++++ src/tests.ts | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 98 insertions(+) create mode 100644 src/index.ts create mode 100644 src/nat.ts create mode 100644 src/tests.ts (limited to 'src') diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 0000000..bd93300 --- /dev/null +++ b/src/index.ts @@ -0,0 +1,43 @@ +export type Op = string | { op: string; a: Op; b: Op }; + +export type Rewrite = { + type: 'rewrite'; + left: left; + right: right; +}; +export type RewriteKind = Rewrite; + +export type Flip = Rewrite; + +export type ApplyRewrite = + O extends R['left'] ? R['right'] + : O extends string ? O + : O extends { a: Op, b: Op, op: string } ? ( + ApplyRewrite extends O['a'] + ? Omit & { b: ApplyRewrite } + : Omit & { a: ApplyRewrite } + ) + : never; + +export type Add = { op: '+'; a: a; b: b }; + +export type Identity = Rewrite, A>; +export type Commutativity = Rewrite, Add>; + +// type _x = ApplyRewrite, Identity<'0'>>; + +// type Assoc = + +/* + +Assoc: (a + b) + c = a + (b + c) + +For a = 0, +(0 + b) + c += b + c += 0 + (b + c) + +For a = succ(), + +*/ + diff --git a/src/nat.ts b/src/nat.ts new file mode 100644 index 0000000..86bd318 --- /dev/null +++ b/src/nat.ts @@ -0,0 +1,16 @@ +declare const prev: unique symbol + +export type _nat = number & { [prev]: _nat | null } +export type Succ = number & { [prev]: n } +export type Pred = n extends _0 ? n : n[typeof prev] +export type Nat = _0 | Succ<_nat> + +export type _0 = 0 & { [prev]: null } +export type _1 = Succ<_0> +export type _2 = Succ<_1> +export type _3 = Succ<_2> +export type _4 = Succ<_3> +export type _5 = Succ<_4> +export type _6 = Succ<_5> +export type _7 = Succ<_6> +export type _8 = Succ<_7> diff --git a/src/tests.ts b/src/tests.ts new file mode 100644 index 0000000..8c379e8 --- /dev/null +++ b/src/tests.ts @@ -0,0 +1,39 @@ +import { Add, ApplyRewrite, Commutativity, Flip, Identity } from './index'; + +type Eq = ([a] extends [b] ? ([b] extends [a] ? true : false) : false) & { a: a; b: b }; +type assert = T; + +export type _testCases = { + Identity: [ + assert>, Identity<'B'>>, + Add<'A', 'B'> + >>, + assert, '0'>, Identity>>, + Add<'A', 'B'> + >>, + assert, Flip>>>, + Add, '0'> + >>, + assert>>, Identity<'C'>>, + Add<'A', Add<'B', 'C'>> + >>, + ], + Commutativity: [ + assert, Commutativity<'A', 'B'>>, + Add<'B', 'A'> + >>, + assert, 'C'>, Commutativity<'A', 'B'>>, + Add, 'C'> + >>, + assert>, Commutativity<'B', 'C'>>, + Add<'A', Add<'C', 'B'>> + >>, + ], +}; -- cgit v1.3.1