@dallaylaen/ski-interpreter 2.7.0 → 2.8.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.
@@ -50,7 +50,7 @@ export type FormatOptions = {
50
50
  lambda?: [string, string, string];
51
51
  around?: [string, string];
52
52
  redex?: [string, string];
53
- inventory?: Record<string, Expr>;
53
+ inventory?: Record<string, Named>;
54
54
  };
55
55
  /**
56
56
  * A version of FormatOptions with defaults plugged in,
@@ -71,6 +71,10 @@ export type TraverseOptions = {
71
71
  order?: 'LO' | 'LI' | 'leftmost-outermost' | 'leftmost-innermost';
72
72
  };
73
73
  export type TraverseCallback = (e: Expr) => TraverseValue<Expr>;
74
+ export type ToposortResult = {
75
+ list: Expr[];
76
+ env: Record<string, Named>;
77
+ };
74
78
  /**
75
79
  * A combinatory logic expression.
76
80
  *
@@ -102,10 +106,13 @@ export declare abstract class Expr {
102
106
  */
103
107
  size?: number;
104
108
  /**
109
+ * Add metadata based on user-supplied values and/or the properties of the term itself.
105
110
  *
106
- * Define properties of the term based on user supplied options and/or inference results.
107
- * Typically useful for declaring Native and Alias terms.
108
- * @protected
111
+ * Typically applied to named terms shortly after instantiation.
112
+ *
113
+ * Experimental. Name and meaning may change in the future.
114
+ *
115
+ * @experimental
109
116
  * @param {Object} options
110
117
  * @param {string} [options.note] - a brief description what the term does
111
118
  * @param {number} [options.arity] - number of arguments the term is waiting for (if known)
@@ -115,7 +122,7 @@ export declare abstract class Expr {
115
122
  * @param {number} [options.maxArgs] - maximum number of arguments for inference, if canonize is true
116
123
  * @return {this}
117
124
  */
118
- _setup(options?: {
125
+ annotate(options?: {
119
126
  note?: string;
120
127
  arity?: number;
121
128
  fancy?: string;
@@ -553,12 +560,14 @@ export declare abstract class Expr {
553
560
  * FreeVar: x[54]
554
561
  */
555
562
  diag(indent?: string): string;
563
+ declare(options?: FormatOptions & {
564
+ declaration?: [string, string, string];
565
+ }): string;
556
566
  /**
557
567
  * Convert the expression to a JSON-serializable format.
558
568
  * Sadly the format is not yet finalized and may change in the future.
559
569
  *
560
570
  * @experimental
561
- * @returns {string}
562
571
  * @sealed
563
572
  */
564
573
  toJSON(): string | object;
@@ -763,6 +772,24 @@ export declare class Alias extends Named {
763
772
  formatImpl(options: RefinedFormatOptions, nargs: number): string;
764
773
  diag(indent?: string): string;
765
774
  }
775
+ /**
776
+ * Topologically sort a list of terms, extending it with any missing dependency terms,
777
+ * if necessary. The output list is guaranteed to contain at least the input terms.
778
+ *
779
+ * @param options
780
+ * @param [options.list] - a single expression or a list of expressions to sort.
781
+ * @param [options.env] - a set of terms assumed to be known (and thus not required in the output list).
782
+ * @param [options.allow] - a set of terms that are allowed in the returned list (aside from the input list).
783
+ *
784
+ * @example
785
+ * const expr = ski.parse(src);
786
+ * toposort([expr], ski.getTerms()); // returns all terms appearing in Expr in correct order
787
+ */
788
+ export declare function toposort(options: {
789
+ list?: Expr | Expr[];
790
+ env?: Record<string, Named>;
791
+ allow?: Record<string, Named>;
792
+ }): ToposortResult;
766
793
  export declare const classes: {
767
794
  Expr: typeof Expr;
768
795
  App: typeof App;
@@ -1,5 +1,4 @@
1
- import { Expr, Named, FormatOptions, TermInfo } from './expr';
2
- import { toposort } from './toposort';
1
+ import { Expr, Named, FormatOptions, TermInfo, toposort } from './expr';
3
2
  /**
4
3
  * Extra utilities that do not belong in the core.
5
4
  */
@@ -52,48 +51,67 @@ export type SearchResult = {
52
51
  gen: number;
53
52
  cache?: Expr[][];
54
53
  };
55
- declare function search(seed: Expr[], options: SearchOptions, predicate: SearchCallback): SearchResult;
54
+ export type EquivResult = {
55
+ steps: number;
56
+ equal: boolean;
57
+ normal: boolean;
58
+ canonical: [Expr | null, Expr | null];
59
+ };
56
60
  /**
57
- * Recursively replace all instances of Expr in a data structure with
58
- * respective string representation using the format() options.
59
- * Objects of other types and primitive values are eft as is.
61
+ * Converts an unknown object into a FormatOptions, or returns an error it it is not valid.
62
+ * A null/undefined counts as an empty options object (and is thus valid).
63
+ */
64
+ declare function checkFormatOptions(raw: unknown): {
65
+ value: FormatOptions;
66
+ } | {
67
+ error: Record<string, string>;
68
+ };
69
+ /**
70
+ * Find out if two expressions are computationally equivalent.
60
71
  *
61
- * May be useful for debugging or diagnostic output.
72
+ * Unlike equals(), this function will attempt to normalize both expressions
73
+ * before comparing.
62
74
  *
63
75
  * @experimental
76
+ * @param e1
77
+ * @param e2
78
+ * @param options
64
79
  */
65
- declare function deepFormat(obj: any, options?: FormatOptions): any;
80
+ declare function equiv(e1: Expr, e2: Expr, options?: {}): EquivResult;
66
81
  /**
67
82
  * Given an expression and a hash of named terms,
68
- * return a semicolon-separated string that declares said expression
69
- * unambiguously.
83
+ * return a semicolon-separated string that declares said expression
84
+ * unambiguously.
70
85
  *
71
86
  * @example
72
87
  * var expr = ski.parse("T=CI; V=BCT; V x y");
73
88
  * SKI.extras.declare(expr, expr.context.env);
74
89
  * // 'B; C; I; T=CI; V=BC(T); x=; y=; Vx y'
75
90
  *
76
- * @param {Expr} expr
77
- * @param {{[s: string]: Named}} [env]
78
- * @returns {string}
79
91
  */
80
- declare function declare(expr: Expr, env?: {
81
- [s: string]: Named;
82
- }): string;
92
+ declare function declare(expr: Expr, env?: Record<string, Named>): string;
83
93
  /**
84
- * Converts an unknown object into a FormatOptions, or returns an error it it is not valid.
85
- * A null/undefined counts as an empty options object (and is thus valid).
94
+ * Recursively replace all instances of Expr in a data structure with
95
+ * respective string representation using the format() options.
96
+ * Objects of other types and primitive values are eft as is.
97
+ *
98
+ * May be useful for debugging or diagnostic output.
99
+ *
100
+ * @experimental
86
101
  */
87
- declare function checkFormatOptions(raw: unknown): {
88
- value: FormatOptions;
89
- } | {
90
- error: Record<string, string>;
91
- };
102
+ declare function deepFormat(obj: any, options?: FormatOptions): any;
103
+ /**
104
+ * @experimental
105
+ * Look for an expression that matches the predicate,
106
+ * starting with the seed and applying the terms to one another.
107
+ */
108
+ declare function search(seed: Expr[], options: SearchOptions, predicate: SearchCallback): SearchResult;
92
109
  export declare const extras: {
93
110
  search: typeof search;
94
111
  deepFormat: typeof deepFormat;
95
112
  declare: typeof declare;
96
113
  toposort: typeof toposort;
97
114
  checkFormatOptions: typeof checkFormatOptions;
115
+ equiv: typeof equiv;
98
116
  };
99
117
  export {};
@@ -1,7 +1,6 @@
1
- import { FreeVar, Church } from './expr';
1
+ import { FreeVar, Church, toposort } from './expr';
2
2
  import { Parser } from './parser';
3
3
  import { Quest } from './quest';
4
- import { toposort } from './toposort';
5
4
  export declare class SKI extends Parser {
6
5
  static native: Record<string, import("./expr").Native>;
7
6
  static control: {
@@ -44,15 +43,14 @@ export declare class SKI extends Parser {
44
43
  static extras: {
45
44
  search: (seed: import("./expr").Expr[], options: import("./extras").SearchOptions, predicate: import("./extras").SearchCallback) => import("./extras").SearchResult;
46
45
  deepFormat: (obj: any, options?: import("./expr").FormatOptions) => any;
47
- declare: (expr: import("./expr").Expr, env?: {
48
- [s: string]: import("./expr").Named;
49
- }) => string;
46
+ declare: (expr: import("./expr").Expr, env?: Record<string, import("./expr").Named>) => string;
50
47
  toposort: typeof toposort;
51
48
  checkFormatOptions: (raw: unknown) => {
52
49
  value: import("./expr").FormatOptions;
53
50
  } | {
54
51
  error: Record<string, string>;
55
52
  };
53
+ equiv: (e1: import("./expr").Expr, e2: import("./expr").Expr, options?: {}) => import("./extras").EquivResult;
56
54
  };
57
55
  static Quest: typeof Quest;
58
56
  }
@@ -1,4 +1,4 @@
1
- import { Expr, Alias, Native, Invocation } from './expr';
1
+ import { Expr, Alias, Native, Named, Invocation } from './expr';
2
2
  export type ParserOptions = {
3
3
  allow?: string;
4
4
  numbers?: boolean;
@@ -17,6 +17,7 @@ export type ParseOptions = {
17
17
  numbers?: boolean;
18
18
  lambdas?: boolean;
19
19
  allow?: string;
20
+ canonize?: boolean;
20
21
  };
21
22
  export type AddOptions = {
22
23
  note?: string;
@@ -37,9 +38,7 @@ export declare class Parser {
37
38
  */
38
39
  addContext: boolean;
39
40
  annotate: boolean;
40
- known: {
41
- [name: string]: Expr;
42
- };
41
+ known: Record<string, Named>;
43
42
  allow: Set<string>;
44
43
  hasNumbers: boolean;
45
44
  hasLambdas: boolean;
@@ -122,9 +121,7 @@ export declare class Parser {
122
121
  *
123
122
  * @return {{[key:string]: Native|Alias}}
124
123
  */
125
- getTerms(): {
126
- [key: string]: Expr;
127
- };
124
+ getTerms(): Record<string, Named>;
128
125
  /**
129
126
  * Export term declarations for use in bulkAdd().
130
127
  * Currently only Alias terms are serialized.
@@ -132,14 +129,15 @@ export declare class Parser {
132
129
  */
133
130
  declare(): string[];
134
131
  /**
135
- * @template T
136
132
  * @param {string} source
137
133
  * @param {Object} [options]
138
- * @param {{[keys: string]: Expr}} [options.env]
139
- * @param {T} [options.scope]
140
- * @param {boolean} [options.numbers]
141
- * @param {boolean} [options.lambdas]
142
- * @param {string} [options.allow]
134
+ * @param [options.env] - additional
135
+ * @param [options.scope] - assign this scope to unknown free variables
136
+ * @param {boolean} [options.numbers] - whether numbers are allowed
137
+ * @param {boolean} [options.lambdas] - whether lambdas are allowed
138
+ * @param {string} [options.allow] - restrict known terms
139
+ * @param [options.canonize] - whether to calculate canonical form, arity, and properties
140
+ * of intermediate aliases
143
141
  * @return {Expr}
144
142
  */
145
143
  parse(source: string, options?: ParseOptions): Expr;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dallaylaen/ski-interpreter",
3
- "version": "2.7.0",
3
+ "version": "2.8.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",