Skip to content

Commit

Permalink
update linter, only emit types, refactor to cons
Browse files Browse the repository at this point in the history
  • Loading branch information
maxdeliso committed Jul 14, 2024
1 parent 910be87 commit 82b1780
Show file tree
Hide file tree
Showing 35 changed files with 1,971 additions and 1,960 deletions.
104 changes: 53 additions & 51 deletions bin/ski.ts
Original file line number Diff line number Diff line change
@@ -1,104 +1,106 @@
#!/usr/bin/env node
import { stepOnceSKI } from "../lib/evaluator/skiEvaluator.js";
import { generateExpr, SKIExpression } from "../lib/ski/expression.js";
import { SKITerminalSymbol } from "../lib/ski/terminal.js";
import { hrtime } from "process";

import * as terminalKit from 'terminal-kit'
import { hrtime } from 'process'
import { create } from 'random-seed'
import { Terminal } from 'terminal-kit'
import { SKIExpression, generate } from '../lib/ski/expression'
import { SKITerminalSymbol } from '../lib/ski/terminal'
import { stepOnceSKI } from '../lib'
import rsexport from 'random-seed';
const { create } = rsexport;

function colorizeSymbol (sym: SKITerminalSymbol): string {
import tkexport from 'terminal-kit';
import Terminal from 'terminal-kit/Terminal.js';
const { terminal } = tkexport;

function colorizeSymbol(sym: SKITerminalSymbol): string {
switch (sym) {
case SKITerminalSymbol.S:
return ' ^[red]S^ '
return ' ^[red]S^ ';
case SKITerminalSymbol.K:
return ' ^[green]K^ '
return ' ^[green]K^ ';
case SKITerminalSymbol.I:
return ' ^[blue]I^ '
return ' ^[blue]I^ ';
default:
return '?'
return '?';
}
}

function colorizeExpression (expr: SKIExpression): string {
function colorizeExpression(expr: SKIExpression): string {
switch (expr.kind) {
case 'terminal':
return colorizeSymbol(expr.sym)
return colorizeSymbol(expr.sym);
case 'non-terminal': {
return [
'(',
colorizeExpression(expr.lft),
colorizeExpression(expr.rgt),
')'
].join('')
].join('');
}
}
}

function formatted (expr: SKIExpression): string {
return '> ' + colorizeExpression(expr) + '\n'
function formatted(expr: SKIExpression): string {
return '> ' + colorizeExpression(expr) + '\n';
}

function runTUI (): number {
const term : Terminal = terminalKit.terminal
const seed = hrtime.bigint()
const randomSeed = create(seed.toString())
const N = 32
const MAX_ITER = 100
function runTUI(): number {
const term: Terminal = terminal;
const seed = hrtime.bigint();
const rs = create(seed.toString());
const N = 32;
const MAX_ITER = 100;

let expression = generate(randomSeed, N)
let expression = generateExpr(rs, N);

term.cyan('Control C or q or Q exits. ' +
's steps once. ' +
'm steps many. ' +
'g regenerates a new expression. \n')
's steps once. ' +
'm steps many. ' +
'g regenerates a new expression. \n');

term.grabInput({})
term.grabInput({});
term.on('key', (keyName: string) => {
switch (keyName) {
case 'CTRL_C':
case 'q':
case 'Q':
term.grabInput(false)
break
term.grabInput(false);
break;
case 's': {
const stepResult = stepOnceSKI(expression)
expression = stepResult.expr
term(formatted(expression))
break
const stepResult = stepOnceSKI(expression);
expression = stepResult.expr;
term(formatted(expression));
break;
}
case 'm': {
let loop = true
let iterations = 0
let loop = true;
let iterations = 0;

while (loop && iterations < MAX_ITER) {
const stepResult = stepOnceSKI(expression)
expression = stepResult.expr
const stepResult = stepOnceSKI(expression);
expression = stepResult.expr;
if (stepResult.altered) {
term(formatted(expression))
term(formatted(expression));
}
loop = stepResult.altered
iterations = iterations + 1
loop = stepResult.altered;
iterations = iterations + 1;
}

if (iterations === MAX_ITER) {
term.red(`stopped evaluating after ${iterations.toString()} iterations. \n`)
term.red(`stopped evaluating after ${iterations.toString()} iterations. \n`);
}

break
break;
}
case 'g': {
expression = generate(randomSeed, N)
term(formatted(expression))
break
expression = generateExpr(rs, N);
term(formatted(expression));
break;
}
default:
term.red('unrecognized command key: ' + keyName + '\n')
term.red('unrecognized command key: ' + keyName + '\n');
}
})
});

return 0
return 0;
}

process.exitCode = runTUI()
process.exitCode = runTUI();
36 changes: 21 additions & 15 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -1,32 +1,38 @@
import eslint from '@eslint/js'
import tseslint from 'typescript-eslint'
import path from "path"
import url from "url"

const __meta_url = new url.URL(import.meta.url)
const __filename = url.fileURLToPath(__meta_url)
const __dirname = path.dirname(__filename)
import eslint from '@eslint/js';
import tseslint from 'typescript-eslint';
import stylisticTs from '@stylistic/eslint-plugin-ts';

export default tseslint.config(
{
files: ["**/*.{ts,mjs}"],
files: ['**/*.ts']
},
{
ignores: [
"**/node_modules/**",
"build/**",
],
ignores: ['**/build/**', '**/node_modules/**'],
},
eslint.configs.recommended,
...tseslint.configs.strictTypeChecked,
...tseslint.configs.stylisticTypeChecked,
...tseslint.configs.recommendedTypeChecked,
{
plugins: {
'@typescript-eslint': tseslint.plugin,
'@stylistic/ts': stylisticTs
},
languageOptions: {
parser: tseslint.parser,
parserOptions: {
project: true,
tsconfigRootDir: __dirname,
},
},
},
rules: {
'@typescript-eslint/no-unsafe-argument': 'error',
'@typescript-eslint/no-unsafe-assignment': 'error',
'@typescript-eslint/no-unsafe-call': 'error',
'@typescript-eslint/no-unsafe-member-access': 'error',
'@typescript-eslint/no-unsafe-return': 'error',
'@stylistic/ts/semi': 'error',
'@stylistic/ts/indent': ['error', 2],
'@stylistic/ts/quotes': ['error', 'single']
},
}
);
6 changes: 3 additions & 3 deletions lib/nonterminal.ts → lib/cons.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export interface NonTerminal <E> {
export interface ConsCell<E> {
kind: 'non-terminal'
lft: E
rgt: E
Expand All @@ -9,8 +9,8 @@ export interface NonTerminal <E> {
* @param rgt the right subtree.
* @returns a new non-terminal node, with E as the type of each branch.
*/
export const nt = <E, >(lft: E, rgt: E): NonTerminal<E> => ({
export const cons = <E,>(lft: E, rgt: E): ConsCell<E> => ({
kind: 'non-terminal',
lft,
rgt
})
});
53 changes: 26 additions & 27 deletions lib/consts/combinators.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { convertLambda } from '../conversion/converter'
import { apply } from '../ski/expression'
import { predLambda } from './lambdas'
import { parseSKI } from '../parser/ski'

import { S, K, I } from '../ski/terminal'
import { convertLambda } from '../conversion/converter.ts';
import { parseSKI } from '../parser/ski.ts';
import { apply } from '../ski/expression.ts';
import { K, I, S } from '../ski/terminal.ts';
import { predLambda } from './lambdas.ts';

/*
* Zero. apply a function to its arguments zero times.
Expand All @@ -16,8 +15,8 @@ import { S, K, I } from '../ski/terminal'
*
* λfx.x ≡ KI
*/
export const Zero = apply(K, I)
export const Snd = Zero
export const Zero = apply(K, I);
export const Snd = Zero;

/*
* false is the second alternative of two arguments
Expand All @@ -26,7 +25,7 @@ export const Snd = Zero
*
* λab.b ≡ KI ≡ False
*/
export const False = Zero
export const False = Zero;

/*
* true is the first alternative of two arguments
Expand All @@ -35,8 +34,8 @@ export const False = Zero
*
* λab.a ≡ K ≡ True
*/
export const True = K
export const Fst = True
export const True = K;
export const Fst = True;

/*
* One. apply a function to its arguments once.
Expand All @@ -48,7 +47,7 @@ export const Fst = True
*
* λx.x ≡ I
*/
export const One = I
export const One = I;

/*
* Composition function
Expand All @@ -63,7 +62,7 @@ export const One = I
*
* λnfx.n(fx) ≡ B
*/
export const B = parseSKI('S(KS)K')
export const B = parseSKI('S(KS)K');

/*
* Successor function
Expand All @@ -86,7 +85,7 @@ export const B = parseSKI('S(KS)K')
*
* this acts as the successor function for Church numerals
*/
export const Succ = apply(S, B)
export const Succ = apply(S, B);

/*
* Binary addition
Expand All @@ -102,7 +101,7 @@ export const Succ = apply(S, B)
*
* λmnfx.mf((nf)x) ≡ BS(BB) ≡ Plus
*/
export const Plus = apply(B, S, apply(B, B))
export const Plus = apply(B, S, apply(B, B));

/*
* Cardinal
Expand All @@ -120,7 +119,7 @@ export const Plus = apply(B, S, apply(B, B))
*
* λxyz.xzy ≡ S(BBS)(KK)
*/
export const C = apply(S, apply(B, B, S), apply(K, K))
export const C = apply(S, apply(B, B, S), apply(K, K));

/*
* Thrush
Expand All @@ -135,7 +134,7 @@ export const C = apply(S, apply(B, B, S), apply(K, K))
*
* λxy.yx ≡ CI ≡ flip
*/
export const T = apply(C, I)
export const T = apply(C, I);

/*
* Vireo
Expand All @@ -155,7 +154,7 @@ export const T = apply(C, I)
*
* Sometimes called a cons cell.
*/
export const V = apply(B, C, T)
export const V = apply(B, C, T);

/*
* Mockingbird
Expand All @@ -169,7 +168,7 @@ export const V = apply(B, C, T)
*
* λa.aa ≡ M
*/
export const M = parseSKI('SII')
export const M = parseSKI('SII');

/*
* Retrieve the first element in a Cons cell.
Expand All @@ -183,7 +182,7 @@ export const M = parseSKI('SII')
* p K
* a
*/
export const Car = apply(T, Fst)
export const Car = apply(T, Fst);

/*
* Retrieve the second element in a Cons cell.
Expand All @@ -197,7 +196,7 @@ export const Car = apply(T, Fst)
* p KI
* b
*/
export const Cdr = apply(T, Snd)
export const Cdr = apply(T, Snd);

/*
* Duplicate the second argument of a function.
Expand All @@ -212,19 +211,19 @@ export const Cdr = apply(T, Snd)
*
* λxy.xyy ≡ W
*/
export const W = parseSKI('SS(SK)')
export const W = parseSKI('SS(SK)');

// λabcd.a(bcd)
export const Blk = apply(B, B, B)
export const Blk = apply(B, B, B);

// λabcde.ab(cde)
export const E = apply(B, apply(B, B, B))
export const E = apply(B, apply(B, B, B));

// λabc.cba
export const F = apply(E, T, T, E, T)
export const F = apply(E, T, T, E, T);

// λf.(λx.f(x x))(λx.f(x x))
export const Y = parseSKI('S(K(SII))(S(S(KS)K)(K(SII)))')
export const Y = parseSKI('S(K(SII))(S(S(KS)K)(K(SII)))');

// note: this is a crossover
export const pred = convertLambda(predLambda)
export const pred = convertLambda(predLambda);
4 changes: 2 additions & 2 deletions lib/consts/lambdas.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { parseLambda } from '../parser/untyped'
import { parseLambda } from '../parser/untyped.ts';

export const [, predLambda] = parseLambda('λn.λf.λx.n(λg.λh.h(gf))(λu.x)(λu.u)')
export const [, predLambda] = parseLambda('λn.λf.λx.n(λg.λh.h(gf))(λu.x)(λu.u)');
Loading

0 comments on commit 82b1780

Please sign in to comment.