From a393765df40744aae68dab943e25bad8175bd5c0 Mon Sep 17 00:00:00 2001 From: Akshay Nair Date: Tue, 12 Dec 2023 20:21:13 +0530 Subject: feat: associativity + distributivity for multiplication --- src/index.ts | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++---------- src/tests.ts | 14 +++++++++- src/util.ts | 2 ++ 3 files changed, 90 insertions(+), 16 deletions(-) diff --git a/src/index.ts b/src/index.ts index 4c3f114..2b6f59a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -8,7 +8,6 @@ export namespace addition { export type SumSucc1 = Rewrite, B>, Succ>> export type SumSucc2 = Rewrite>, Succ>> - /// Sum commutativity export type Commutativity = Rewrite, Add>; export interface Comm_Base { type: 'rewrite', @@ -29,8 +28,14 @@ export namespace addition { Refl>>, // true ], this['left']>; }; + export namespace spec { + export type commutativity = [ + assert>>, + assert>>, + ] + } + - // Sum associativity export type Associativity = Rewrite, C>, Add>>; export interface Assoc_Base { type: 'rewrite', @@ -55,11 +60,6 @@ export namespace addition { }; export namespace spec { - export type commutativity = [ - assert>>, - assert>>, - ] - export type associativity = [ assert>>, assert>>, @@ -71,15 +71,15 @@ 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 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 + IdentityR, // b = b * 1 Identity, // b = b Refl, // true ], this['left']>; @@ -88,18 +88,78 @@ export namespace multiplication { 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 + Mul_Add, // b + a*b = b*succ(a) + Mul_AddR, // b + a*b = b + b*a + Commutativity, // b + a*b = b + a*b + Refl>>, // true ], this['left']>; }; - export namespace spec { export type commutativity = [ assert>>, assert>>, ] } + + export type Distributivity = + Rewrite, C>, Add, Multiply>>; + export interface Dist_Base { + type: 'rewrite', + left: Equation, C>, Add, Multiply>>; // (1 + b)*c = 1*c + b*c + right: ChainRewrites<[ + IdentityR, // (1 + b)*c = c + b*c + addition.SumSucc1<_0, B>, // succ(0 + b)*c = c + b*c + addition.IdentityR, // succ(b)*c = c + b*c + Mul_Add, // c + b*c = c + b*c + Refl>>, // true + ], this['left']>; + }; + export interface Dist_Inductive { + type: 'rewrite', + left: Equation, B>, C>, Add, C>, Multiply>>; // (succ(a) + b)*c = succ(a)*c + b*c + right: ChainRewrites<[ + addition.SumSucc1, // succ(a + b)*c = succ(a)*c + b*c + Mul_Add, C>, // c + (a + b)*c = succ(a)*c + b*c + Mul_Add, // c + (a + b)*c = (c + a*c) + b*c + Distributivity, // c + (a*c + b*c) = (c + a*c) + b*c + addition.Associativity, Multiply>, // (c + a*c) + b*c = (c + a*c) + b*c + Refl, Multiply>>>, // true + ], this['left']>; + }; + export namespace spec { + export type distributivity = [ + assert>>, + assert>>, + ] + } + + export type Associativity = + Rewrite, C>, Multiply>>; + export interface Assoc_Base { + type: 'rewrite', + left: Equation, C>, Multiply<_1, Multiply>>; // (1*b)*c = 1*(b*c) + right: ChainRewrites<[ + IdentityR, // b*c = 1*(b*c) + IdentityR>, // b*c = b*c + Refl>, // true + ], this['left']>; + }; + export interface Assoc_Inductive { + type: 'rewrite', + left: Equation, B>, C>, Multiply, Multiply>>; // (succ(a)*b)*c = succ(a)*(b*c) + right: ChainRewrites<[ + Mul_Add, // (b + a*b)*c = succ(a) * (b*c) + Distributivity, C>, // b*c + (a*b)*c = succ(a) * (b*c) + Mul_Add>, // b*c + (a*b)*c = b*c + a*(b*c) + Associativity, // b*c + a*(b*c) = b*c + a*(b*c) + Refl, Multiply>>>, // true + ], this['left']>; + }; + export namespace spec { + export type associativity = [ + assert>>, + assert>>, + ] + } } diff --git a/src/tests.ts b/src/tests.ts index 5c066dc..04a009a 100644 --- a/src/tests.ts +++ b/src/tests.ts @@ -1,6 +1,6 @@ import { addition, multiplication } from './index'; import { Add, Multiply, Succ } from './nat'; -import { ApplyRewrite, ChainRewrites, Flip, Eq, VerifyEquation, Equation, assert } from './util'; +import { ApplyRewrite, ChainRewrites, Flip, Eq, VerifyEquation, Equation, assert, Evaluate } from './util'; export type specChainRewrites = [ assert, 'C'> >>, ] + export type specDistributivity = [ + assert>, + Add, Multiply<'B', 'C'>> + >>, + ] + export type specAssociativity = [ + assert>, + Multiply<'A', Multiply<'B', 'C'>> + >>, + ] } diff --git a/src/util.ts b/src/util.ts index 97e9335..88e3749 100644 --- a/src/util.ts +++ b/src/util.ts @@ -34,6 +34,8 @@ export type ChainRewrites[], O extends Op> = export type VerifyEquation> = Eq['right'] extends 'true' ? true : false & Eq['right']; +export type Evaluate> = ApplyRewrite; + export type assert = T; export type Eq = ([a] extends [b] ? ([b] extends [a] ? true : false & { lhs: a; rhs: b }) : false & { lhs: a; rhs: b }); -- cgit v1.3.1