@dallaylaen/ski-interpreter 2.4.1 → 2.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,18 +1,19 @@
1
- import { TraverseValue, Dict } from './internal';
1
+ import { z } from 'zod';
2
+ import { TraverseValue } from './internal';
2
3
  /**
3
4
  * @desc Control primitives for fold() and traverse() methods.
4
5
  */
5
6
  export declare const control: {
6
- descend: <T>(value: T) => import("./internal").TraverseControl<T>;
7
- prune: <T>(value: T) => import("./internal").TraverseControl<T>;
8
- redo: <T>(value: T) => import("./internal").TraverseControl<T>;
9
- stop: <T>(value: T) => import("./internal").TraverseControl<T>;
7
+ descend: <T>(value?: T) => import("./internal").TraverseControl<T>;
8
+ prune: <T>(value?: T) => import("./internal").TraverseControl<T>;
9
+ redo: <T>(value?: T) => import("./internal").TraverseControl<T>;
10
+ stop: <T>(value?: T) => import("./internal").TraverseControl<T>;
10
11
  };
11
12
  /**
12
13
  * @desc List of predefined native combinators.
13
14
  * This is required for toSKI() to work, otherwise could as well have been in parser.js.
14
15
  */
15
- export declare const native: Dict<Native>;
16
+ export declare const native: Record<string, Native>;
16
17
  export type TermInfo = {
17
18
  normal: boolean;
18
19
  proper: boolean;
@@ -41,17 +42,18 @@ export type RunOptions = {
41
42
  throw?: boolean;
42
43
  maxSize?: number;
43
44
  };
44
- export type FormatOptions = {
45
- terse?: boolean;
46
- html?: boolean;
47
- brackets?: [string, string];
48
- space?: string;
49
- var?: [string, string];
50
- lambda?: [string, string, string];
51
- around?: [string, string];
52
- redex?: [string, string];
53
- inventory?: Dict<Expr>;
54
- };
45
+ export declare const FormatOptionsSchema: z.ZodObject<{
46
+ terse: z.ZodOptional<z.ZodBoolean>;
47
+ html: z.ZodOptional<z.ZodBoolean>;
48
+ brackets: z.ZodOptional<z.ZodTuple<[z.ZodString, z.ZodString], null>>;
49
+ space: z.ZodOptional<z.ZodString>;
50
+ var: z.ZodOptional<z.ZodTuple<[z.ZodString, z.ZodString], null>>;
51
+ lambda: z.ZodOptional<z.ZodTuple<[z.ZodString, z.ZodString, z.ZodString], null>>;
52
+ around: z.ZodOptional<z.ZodTuple<[z.ZodString, z.ZodString], null>>;
53
+ redex: z.ZodOptional<z.ZodTuple<[z.ZodString, z.ZodString], null>>;
54
+ inventory: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodCustom<Expr, Expr>>>;
55
+ }, z.core.$strip>;
56
+ export type FormatOptions = z.infer<typeof FormatOptionsSchema>;
55
57
  type RefinedFormatOptions = {
56
58
  terse?: boolean;
57
59
  html?: boolean;
@@ -61,7 +63,7 @@ type RefinedFormatOptions = {
61
63
  lambda: [string, string, string];
62
64
  around: [string, string];
63
65
  redex: [string, string];
64
- inventory?: Dict<Expr>;
66
+ inventory?: Record<string, Expr>;
65
67
  };
66
68
  type TraverseOptions = {
67
69
  order?: 'LO' | 'LI' | 'leftmost-outermost' | 'leftmost-innermost';
@@ -69,40 +71,40 @@ type TraverseOptions = {
69
71
  type TraverseCallback = (e: Expr) => TraverseValue<Expr>;
70
72
  export declare class Expr {
71
73
  static control: {
72
- descend: <T>(value: T) => import("./internal").TraverseControl<T>;
73
- prune: <T>(value: T) => import("./internal").TraverseControl<T>;
74
- redo: <T>(value: T) => import("./internal").TraverseControl<T>;
75
- stop: <T>(value: T) => import("./internal").TraverseControl<T>;
74
+ descend: <T>(value?: T) => import("./internal").TraverseControl<T>;
75
+ prune: <T>(value?: T) => import("./internal").TraverseControl<T>;
76
+ redo: <T>(value?: T) => import("./internal").TraverseControl<T>;
77
+ stop: <T>(value?: T) => import("./internal").TraverseControl<T>;
76
78
  };
77
- static native: Dict<Native>;
79
+ static native: Record<string, Native>;
78
80
  /**
79
- * @descr A combinatory logic expression.
81
+ * @desc A combinatory logic expression.
80
82
  *
81
- * Applications, variables, and other terms like combinators per se
82
- * are subclasses of this class.
83
+ * Applications, variables, lambdas, combinators per se,
84
+ * and other expression subtypes all extend this class.
85
+ *
86
+ * Expr itself cannot (or at least should not) be instantiated.
83
87
  *
84
88
  * @abstract
85
- * @property {{
86
- * scope?: any,
87
- * env?: { [key: string]: Expr },
88
- * src?: string,
89
- * parser: object,
90
- * }} [context]
91
- * @property {number} [arity] - number of arguments the term is waiting for (if known)
92
- * @property {string} [note] - a brief description what the term does
93
- * @property {string} [fancyName] - how to display in html mode, e.g. &phi; instead of 'f'
94
- * Typically only applicable to descendants of Named.
95
- * @property {TermInfo} [props] - properties inferred from the term's behavior
96
89
  */
90
+ /** @desc optional context for the term. Is set by the parser with addContext: true */
97
91
  context?: {
98
92
  scope?: object;
99
- env?: Dict<Expr>;
93
+ env?: Record<string, Expr>;
100
94
  src?: string;
101
95
  parser: object;
102
96
  };
97
+ /** @desc number of arguments the term is waiting for (if known) */
103
98
  arity?: number;
99
+ /** @desc a brief description what the term does */
104
100
  note?: string;
101
+ /** @desc the properties of the term, typically inferred from its behavior.
102
+ * This is used internally when declaring Native / Alias terms.
103
+ */
105
104
  props?: TermInfo;
105
+ /** @desc An estimated number of nodes in the expression tree.
106
+ * Used to prevent runaway computations.
107
+ */
106
108
  size?: number;
107
109
  /**
108
110
  *
@@ -262,7 +264,8 @@ export declare class Expr {
262
264
  _infer(options: {
263
265
  max: number;
264
266
  maxArgs: number;
265
- skipNames: Dict<boolean>;
267
+ maxSize: number;
268
+ skipNames: Record<string, boolean>;
266
269
  }, nargs: number): TermInfo;
267
270
  /**
268
271
  * @desc Expand an expression into a list of terms
@@ -477,7 +480,7 @@ export declare class Expr {
477
480
  * @returns {string}
478
481
  * @private
479
482
  */
480
- _format(options: RefinedFormatOptions, nargs: number): string;
483
+ formatImpl(options: RefinedFormatOptions, nargs: number): string;
481
484
  /**
482
485
  * @desc Returns a string representation of the expression tree, with indentation to show structure.
483
486
  *
@@ -499,7 +502,7 @@ export declare class Expr {
499
502
  * FreeVar: x[54]
500
503
  * FreeVar: x[54]
501
504
  */
502
- diag(): string;
505
+ diag(indent?: string): string;
503
506
  /**
504
507
  * @desc Convert the expression to a JSON-serializable format.
505
508
  * @returns {string}
@@ -530,7 +533,8 @@ export declare class App extends Expr {
530
533
  unroll(): Expr[];
531
534
  diff(other: Expr, swap?: boolean): string | null;
532
535
  _braced(first?: boolean): boolean;
533
- _format(options: RefinedFormatOptions, nargs: number): string;
536
+ formatImpl(options: RefinedFormatOptions, nargs: number): string;
537
+ diag(indent?: string): string;
534
538
  _unspaced(arg: Expr): boolean;
535
539
  }
536
540
  export declare class Named extends Expr {
@@ -543,7 +547,7 @@ export declare class Named extends Expr {
543
547
  fancyName?: string;
544
548
  constructor(name: string);
545
549
  _unspaced(arg: Expr): boolean;
546
- _format(options: RefinedFormatOptions, nargs: number): string;
550
+ formatImpl(options: RefinedFormatOptions, nargs: number): string;
547
551
  }
548
552
  export declare class FreeVar extends Named {
549
553
  /**
@@ -568,7 +572,8 @@ export declare class FreeVar extends Named {
568
572
  constructor(name: string, scope?: object);
569
573
  diff(other: Expr, swap?: boolean): string | null;
570
574
  subst(search: Expr, replace: Expr): Expr | null;
571
- _format(options: RefinedFormatOptions, nargs: number): string;
575
+ formatImpl(options: RefinedFormatOptions, nargs: number): string;
576
+ diag(indent?: string): string;
572
577
  static global: string[];
573
578
  }
574
579
  export declare class Native extends Named {
@@ -619,7 +624,8 @@ export declare class Lambda extends Expr {
619
624
  _fold<T>(initial: T, combine: (acc: T, expr: Expr) => TraverseValue<T>): TraverseValue<T>;
620
625
  subst(search: Expr, replace: Expr): Expr | null;
621
626
  diff(other: Expr, swap?: boolean): string | null;
622
- _format(options: RefinedFormatOptions, nargs: number): string;
627
+ formatImpl(options: RefinedFormatOptions, nargs: number): string;
628
+ diag(indent?: string): string;
623
629
  _braced(first: boolean): boolean;
624
630
  }
625
631
  export declare class Church extends Expr {
@@ -632,7 +638,7 @@ export declare class Church extends Expr {
632
638
  constructor(n: number);
633
639
  diff(other: Expr, swap?: boolean): string | null;
634
640
  _unspaced(arg: Expr): boolean;
635
- _format(options: RefinedFormatOptions, nargs: number): string;
641
+ formatImpl(options: RefinedFormatOptions, nargs: number): string;
636
642
  }
637
643
  export declare class Alias extends Named {
638
644
  /**
@@ -660,6 +666,7 @@ export declare class Alias extends Named {
660
666
  maxArgs?: number;
661
667
  note?: string;
662
668
  terminal?: boolean;
669
+ outdated?: boolean;
663
670
  });
664
671
  /**
665
672
  * @property {boolean} [outdated] - whether the alias is outdated
@@ -680,7 +687,8 @@ export declare class Alias extends Named {
680
687
  step(): Step;
681
688
  diff(other: Expr, swap?: boolean): string | null;
682
689
  _braced(first: boolean): boolean;
683
- _format(options: RefinedFormatOptions, nargs: number): string;
690
+ formatImpl(options: RefinedFormatOptions, nargs: number): string;
691
+ diag(indent?: string): string;
684
692
  }
685
693
  export declare const classes: {
686
694
  Expr: typeof Expr;
@@ -3,12 +3,12 @@ import { Parser } from './parser';
3
3
  import { Quest } from './quest';
4
4
  import { toposort } from './toposort';
5
5
  export declare class SKI extends Parser {
6
- static native: import("./internal").Dict<import("./expr").Native>;
6
+ static native: Record<string, import("./expr").Native>;
7
7
  static control: {
8
- descend: <T>(value: T) => import("./internal").TraverseControl<T>;
9
- prune: <T>(value: T) => import("./internal").TraverseControl<T>;
10
- redo: <T>(value: T) => import("./internal").TraverseControl<T>;
11
- stop: <T>(value: T) => import("./internal").TraverseControl<T>;
8
+ descend: <T>(value?: T) => import("./internal").TraverseControl<T>;
9
+ prune: <T>(value?: T) => import("./internal").TraverseControl<T>;
10
+ redo: <T>(value?: T) => import("./internal").TraverseControl<T>;
11
+ stop: <T>(value?: T) => import("./internal").TraverseControl<T>;
12
12
  };
13
13
  static classes: {
14
14
  Expr: typeof import("./expr").Expr;
@@ -20,12 +20,36 @@ export declare class SKI extends Parser {
20
20
  Church: typeof Church;
21
21
  Alias: typeof import("./expr").Alias;
22
22
  };
23
+ static schemas: {
24
+ FormatOptions: import("zod").ZodObject<{
25
+ terse: import("zod").ZodOptional<import("zod").ZodBoolean>;
26
+ html: import("zod").ZodOptional<import("zod").ZodBoolean>;
27
+ brackets: import("zod").ZodOptional<import("zod").ZodTuple<[import("zod").ZodString, import("zod").ZodString], null>>;
28
+ space: import("zod").ZodOptional<import("zod").ZodString>;
29
+ var: import("zod").ZodOptional<import("zod").ZodTuple<[import("zod").ZodString, import("zod").ZodString], null>>;
30
+ lambda: import("zod").ZodOptional<import("zod").ZodTuple<[import("zod").ZodString, import("zod").ZodString, import("zod").ZodString], null>>;
31
+ around: import("zod").ZodOptional<import("zod").ZodTuple<[import("zod").ZodString, import("zod").ZodString], null>>;
32
+ redex: import("zod").ZodOptional<import("zod").ZodTuple<[import("zod").ZodString, import("zod").ZodString], null>>;
33
+ inventory: import("zod").ZodOptional<import("zod").ZodRecord<import("zod").ZodString, import("zod").ZodCustom<import("./expr").Expr, import("./expr").Expr>>>;
34
+ }, import("zod/v4/core").$strip>;
35
+ };
23
36
  static B: import("./expr").Native;
24
37
  static C: import("./expr").Native;
25
38
  static I: import("./expr").Native;
26
39
  static K: import("./expr").Native;
27
40
  static S: import("./expr").Native;
28
41
  static W: import("./expr").Native;
42
+ /**
43
+ * @desc Create a proxy object that generates variables on demand,
44
+ * with names corresponding to the property accessed.
45
+ * Different invocations will return distinct variables,
46
+ * even if with the same name.
47
+ *
48
+ * @example const {x, y, z} = SKI.vars();
49
+ * x.name; // 'x'
50
+ * x instanceof FreeVar; // true
51
+ * x.apply(y).apply(z); // x(y)(z)
52
+ */
29
53
  static vars(scope?: object): {
30
54
  [key: string]: FreeVar;
31
55
  };
@@ -1,6 +1,3 @@
1
- export type Dict<T> = {
2
- [key: string]: T;
3
- };
4
1
  export declare class Tokenizer {
5
2
  /**
6
3
  * @desc Create a tokenizer that splits strings into tokens according to the given terms.
@@ -27,8 +24,8 @@ export declare class Tokenizer {
27
24
  * @returns {Set<string>}
28
25
  */
29
26
  export declare function restrict(set: Set<string>, spec?: string): Set<string>;
30
- export type TraverseDecorator<T> = (value: T) => TraverseControl<T>;
31
- export type TraverseValue<T> = T | TraverseControl<T> | null | undefined;
27
+ export type TraverseDecorator = <T>(value?: T) => TraverseControl<T>;
28
+ export type TraverseValue<T> = TraverseControl<T | null | undefined | void> | T | null | undefined | void;
32
29
  export declare class TraverseControl<T> {
33
30
  /**
34
31
  * @desc A wrapper for values returned by fold/traverse callbacks
@@ -42,9 +39,9 @@ export declare class TraverseControl<T> {
42
39
  * @param {T} value
43
40
  * @param {function(T): TraverseControl<T>} decoration
44
41
  */
45
- value: T;
46
- decoration: TraverseDecorator<T>;
47
- constructor(value: T, decoration: TraverseDecorator<T>);
42
+ value?: T;
43
+ decoration: TraverseDecorator;
44
+ constructor(value: T | undefined, decoration: TraverseDecorator);
48
45
  }
49
46
  /**
50
47
  * @private
@@ -52,7 +49,7 @@ export declare class TraverseControl<T> {
52
49
  * @param {T|TraverseControl<T>|null} value
53
50
  * @returns {[T?, function|undefined]}
54
51
  */
55
- export declare function unwrap<T>(value: TraverseValue<T>): [T?, TraverseDecorator<T>?];
52
+ export declare function unwrap<T>(value: TraverseValue<T>): [T?, TraverseDecorator?];
56
53
  /**
57
54
  * @desc Prepare a self-referencing wrapper function for use as a fold/traverse control decorator.
58
55
  *
@@ -66,4 +63,4 @@ export declare function unwrap<T>(value: TraverseValue<T>): [T?, TraverseDecorat
66
63
  * @param {string} [label]
67
64
  * @returns {function(T): TraverseControl<T>}
68
65
  */
69
- export declare function prepareWrapper(label: string): <T>(value: T) => TraverseControl<T>;
66
+ export declare function prepareWrapper(label?: string): <T>(value?: T) => TraverseControl<T>;
@@ -7,6 +7,7 @@ export type ParserOptions = {
7
7
  [key: string]: Expr | string;
8
8
  } | string[];
9
9
  annotate?: boolean;
10
+ addContext?: boolean;
10
11
  };
11
12
  export type ParseOptions = {
12
13
  env?: {
@@ -34,6 +35,7 @@ export declare class Parser {
34
35
  * annotate?: boolean,
35
36
  * }} [options]
36
37
  */
38
+ addContext: boolean;
37
39
  annotate: boolean;
38
40
  known: {
39
41
  [name: string]: Expr;
@@ -220,7 +220,8 @@ declare class Case {
220
220
  }
221
221
  declare class Subst {
222
222
  /**
223
- * @descr A placeholder object with exactly n free variables to be substituted later.
223
+ * @desc A placeholder object with exactly n free variables to be substituted later.
224
+ * Basically a poor man's lambda.
224
225
  * @param {Expr} expr
225
226
  * @param {FreeVar[]} env
226
227
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dallaylaen/ski-interpreter",
3
- "version": "2.4.1",
3
+ "version": "2.5.1",
4
4
  "description": "Simple Kombinator Interpreter - a combinatory logic & lambda calculus parser and interpreter. Supports SKI, BCKW, Church numerals, and setting up assertions ('quests') involving all of the above.",
5
5
  "keywords": [
6
6
  "combinatory logic",
@@ -64,6 +64,7 @@
64
64
  "typescript": "^5.8.2"
65
65
  },
66
66
  "dependencies": {
67
- "commander": "^14.0.3"
67
+ "commander": "^14.0.3",
68
+ "zod": "^4.3.6"
68
69
  }
69
70
  }