From 4a0fb237cac8796e45d7b217db0d808452454820 Mon Sep 17 00:00:00 2001 From: Akshay Nair Date: Sat, 16 Dec 2023 17:31:12 +0530 Subject: feat: lambda calculus --- src/lambda-calculus.ts | 114 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 src/lambda-calculus.ts (limited to 'src/lambda-calculus.ts') diff --git a/src/lambda-calculus.ts b/src/lambda-calculus.ts new file mode 100644 index 0000000..bbd2d1e --- /dev/null +++ b/src/lambda-calculus.ts @@ -0,0 +1,114 @@ +import { Op, Rewrite, ChainRewrites, Sym, Equation, Refl, VerifyEquation, assert, Subst, ApplyRewrite, Evaluate, OpToStr, SubstEq } from './utils/theorem'; + +export type Abs = { op: '->', a: x, b: o }; // \x. o +export type Ap = { op: '$', a: F, b: x }; // F(x) +export type Comp = { op: '.', a: F, b: G }; // F . G +export type Id = Abs // \x. x +export type Const = Abs> // \x. \y. x +export type Inv = { op: 'inv', a: F } + +export namespace composition { + export type Abstraction = Rewrite, v>, y>; + + export type Composition = Rewrite, x>, Ap>>; + + // Identity + export type Identity = Rewrite, x>; + export namespace proof { + export interface Identity_Proof { + type: 'rewrite'; + left: Equation, x>, x>; // id(x) = x + right: ChainRewrites<[ + Abstraction, // x = x + Refl, + ], this['left']>; + } + + export interface Identity_Composition_Proof { + type: 'rewrite'; + left: Equation, x>, Ap>; // (f . id)(x) = f(x) + right: ChainRewrites<[ + Composition, // f(id(x)) = f(x) + Identity, // f(x) = f(x) + Refl, + ], this['left']>; + } + + export type identity = [ + assert>>, + assert>>, + ] + } + + // Associativity + export type Associativity = Rewrite< + Comp, H>, + Comp> + > + export namespace proof { + export interface Assoc_Proof { + type: 'rewrite'; + left: Equation, H>, x>, Ap>, x>>; // ((f . g) . h)(x) = (f . (g . h))(x) + right: ChainRewrites<[ + Composition, H, x>, // (f . g)(h(x)) = (f . (g . h))(x) + Composition>, // f(g(h(x))) = (f . (g . h))(x) + Composition, x>, // f(g(h(x))) = f((g . h)(x)) + Composition, // f(g(h(x))) = f(g(h(x))) + Refl, + ], this['left']>; + } + export type associativity = [ + assert>>, + ] + } + + // Identity + export type Transitivity = Rewrite, Abs>, Abs>; + export namespace proof { + export interface Transitivity_Proof { + type: 'rewrite'; + left: Equation, Abs>, x>, Ap, x>>; // ((\y -> z) . (\x -> y))(x) = (\x -> z)(x) + right: ChainRewrites<[ + Composition, Abs, x>, // (\y -> z) $ (\x -> y)(x) = (\x -> z)(x) + Abstraction, // (\y -> z) $ y = (\x -> z)(x) + Abstraction, // z = (\x -> z)(x) + Abstraction, // z = z + Refl, + ], this['left']>; + } + export type transitivity = [ + // OpToStr>>, + assert>>, + ] + } + + // Inverse functions + export type Inverse = Rewrite>, Abs>; + export namespace proof { + export interface Composition_Inverse_Proof { + type: 'rewrite', + known: { + 'f': Equation>, + 'g': Equation>, + }, + left: Equation>, Comp, Inv>>, // inv(f . g) = inv(g) . inv(f) + right: ChainRewrites<[ + SubstEq, + SubstEq, // inv((\y -> z) . g) = inv(g) . inv(\y -> z) + SubstEq, + SubstEq, // inv((\y -> z) . (\x -> y)) = inv(\x -> y) . inv(\y -> z) + Inverse<'x', 'y'>, + Inverse<'y', 'z'>, // inv((\x -> y) . (\y -> z)) = (\z -> y) . (\y -> x) + Transitivity<'x', 'y', 'z'>, // inv(\x -> z) = (\z -> y) . (\y -> x) + Transitivity<'z', 'y', 'x'>, // inv(\x -> z) = \z -> x + Inverse<'x', 'z'>, // \z -> x = \z -> x + Refl + ], this['left']>, + } + export type inverse = [ + // OpToStr>>, + assert>>, + ] + } +} + -- cgit v1.3.1