From 999c2314c0d649646a53e7cc440ae3f4aa1a3d61 Mon Sep 17 00:00:00 2001 From: Akshay Nair Date: Tue, 12 Dec 2023 18:47:06 +0530 Subject: feat: commutativity + identity for multiplication --- src/index.ts | 52 +++++++++++++++++++++++++++++++-- src/nat.ts | 1 + src/tests.ts | 94 +++++++++++++++++++++++++++++------------------------------- src/util.ts | 4 ++- 4 files changed, 100 insertions(+), 51 deletions(-) diff --git a/src/index.ts b/src/index.ts index a161134..4c3f114 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,5 @@ -import { Add, Multiply, Succ, _0 } from './nat'; -import { Op, Rewrite, ChainRewrites, Flip, Equation, Refl } from './util'; +import { Add, Multiply, Succ, _0, _1, _3 } from './nat'; +import { Op, Rewrite, ChainRewrites, Flip, Equation, Refl, VerifyEquation, assert } from './util'; export namespace addition { export type Identity = Rewrite, A>; @@ -53,5 +53,53 @@ export namespace addition { Refl>>>, // true ], this['left']>; }; + + export namespace spec { + export type commutativity = [ + assert>>, + assert>>, + ] + + export type associativity = [ + assert>>, + assert>>, + ] + } +} + +export namespace multiplication { + export type Identity = Rewrite, A>; + export type IdentityR = Rewrite, A>; + + export type Mul_Add = Rewrite, B>, Add>>; + export type Mul_AddR = Rewrite>, Add>>; + + export type Commutativity = Rewrite, Multiply>; + export interface Comm_Base { + type: 'rewrite', + left: Equation, Multiply>; // 1 * b = b * 1 + right: ChainRewrites<[ + IdentityR, // b = b + 0 + Identity, // b = b + Refl, // true + ], this['left']>; + }; + export interface Comm_Inductive { + type: 'rewrite', + left: Equation, B>, Multiply>>; // succ(a)*b = b*succ(a) + right: ChainRewrites<[ + Mul_Add, // a + a*b = b*succ(a) + Mul_AddR, // a + a*b = a + b*a + Commutativity, // a + a*b = a + a*b + Refl>>, // true + ], this['left']>; + }; + + export namespace spec { + export type commutativity = [ + assert>>, + assert>>, + ] + } } diff --git a/src/nat.ts b/src/nat.ts index 124c2c6..5c52bdf 100644 --- a/src/nat.ts +++ b/src/nat.ts @@ -1,6 +1,7 @@ import { Op } from "./util"; export type Add = { op: '+'; a: A; b: B }; +export type Multiply = { op: '*'; a: A; b: B }; export type Succ = Add<'1', A>; export type _0 = '0' diff --git a/src/tests.ts b/src/tests.ts index f4530b4..5c066dc 100644 --- a/src/tests.ts +++ b/src/tests.ts @@ -1,47 +1,6 @@ -import { addition } from './index'; -import { Add } from './nat'; -import { ApplyRewrite, ChainRewrites, Flip, Eq, VerifyEquation } from './util'; - -type assert = T; - -export type specIdentity = [ - assert>, addition.Identity<'B'>>, - Add<'A', 'B'> - >>, - assert, '0'>, addition.Identity>>, - Add<'A', 'B'> - >>, - assert, Flip>>>, - Add, '0'> - >>, - assert>>, addition.Identity<'C'>>, - Add<'A', Add<'B', 'C'>> - >>, -] - -export type specCommutativity = [ - assert, addition.Commutativity<'A', 'B'>>, - Add<'B', 'A'> - >>, - assert, 'C'>, addition.Commutativity<'A', 'B'>>, - Add, 'C'> - >>, - assert>, addition.Commutativity<'B', 'C'>>, - Add<'A', Add<'C', 'B'>> - >>, -] - -export type specCommutativityInduction = [ - assert>>, - assert>>, -] +import { addition, multiplication } from './index'; +import { Add, Multiply, Succ } from './nat'; +import { ApplyRewrite, ChainRewrites, Flip, Eq, VerifyEquation, Equation, assert } from './util'; export type specChainRewrites = [ assert>, ] -export type specAssociativityInduction = [ - assert>>, - assert>>, -] +export namespace spec_addition { + export type specIdentity = [ + assert>, addition.Identity<'B'>>, + Add<'A', 'B'> + >>, + assert, '0'>, addition.Identity>>, + Add<'A', 'B'> + >>, + assert, Flip>>>, + Add, '0'> + >>, + assert>>, addition.Identity<'C'>>, + Add<'A', Add<'B', 'C'>> + >>, + ] + + export type specCommutativity = [ + assert, addition.Commutativity<'A', 'B'>>, + Add<'B', 'A'> + >>, + assert, 'C'>, addition.Commutativity<'A', 'B'>>, + Add, 'C'> + >>, + assert>, addition.Commutativity<'B', 'C'>>, + Add<'A', Add<'C', 'B'>> + >>, + ] +} +export namespace multiplication_spec { + export type specCommutativity = [ + assert, 'C'>, multiplication.Commutativity<'A', 'B'>>, + Multiply, 'C'> + >>, + ] +} diff --git a/src/util.ts b/src/util.ts index f6067ae..97e9335 100644 --- a/src/util.ts +++ b/src/util.ts @@ -13,7 +13,8 @@ export type ApplyRewrite> = : O extends string ? O : O extends { a: Op, b: Op, op: string } ? ( ApplyRewrite extends O['a'] - ? Omit & { b: ApplyRewrite } + ? (ApplyRewrite extends O['b'] ? O + : Omit & { b: ApplyRewrite }) : Omit & { a: ApplyRewrite } ) : never; @@ -33,6 +34,7 @@ export type ChainRewrites[], O extends Op> = export type VerifyEquation> = Eq['right'] extends 'true' ? true : false & Eq['right']; +export type assert = T; export type Eq = ([a] extends [b] ? ([b] extends [a] ? true : false & { lhs: a; rhs: b }) : false & { lhs: a; rhs: b }); export type Equation = { op: '=', a: A, b: B }; -- cgit v1.3.1