@fraczak/k 3.0.2 → 4.0.0
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.
- package/BIG_PICTURE.md +42 -24
- package/Code-derivation-tests/blowup.mjs +27 -0
- package/Code-derivation-tests/bnat.mjs +4 -2
- package/Code-derivation-tests/built-in.mjs +1 -1
- package/Code-derivation-tests/dot.mjs +28 -3
- package/Code-derivation-tests/index.mjs +2 -2
- package/Code-derivation-tests/loop.mjs +1 -1
- package/Code-derivation-tests/nat.mjs +2 -2
- package/Code-derivation-tests/not-not.mjs +2 -2
- package/Code-derivation-tests/product.mjs +9 -5
- package/Code-derivation-tests/rlz.mjs +2 -2
- package/Code-derivation-tests/vector.mjs +7 -16
- package/Filters/bug.k +3 -0
- package/Filters/composing.k +15 -0
- package/Filters/filter.k +9 -0
- package/Graph.mjs +95 -0
- package/README.md +18 -15
- package/TYPING.md +365 -0
- package/codes.mjs +60 -8
- package/hash.mjs +6 -5
- package/index.mjs +11 -41
- package/k.mjs +1 -2
- package/package.json +1 -1
- package/parser.jison +22 -10
- package/parser.mjs +74 -65
- package/patterns.mjs +438 -527
- package/pretty.mjs +100 -23
- package/repl.mjs +124 -86
- package/run.mjs +13 -16
- package/symbol-table.mjs +44 -41
- package/test-filters.mjs +3 -3
- package/typing-test.mjs +40 -0
- package/typing.mjs +689 -0
- package/INTRO.md +0 -88
- package/TODO.md +0 -54
- package/bug.json +0 -419
- package/chatGPT.md +0 -235
- package/chatGPT.md~ +0 -119
- package/code-resolution.md +0 -121
- package/code-resolution.mjs +0 -51
- package/filters.mjs +0 -59
package/chatGPT.md
DELETED
|
@@ -1,235 +0,0 @@
|
|
|
1
|
-
# Key Concepts and Constructs of Kernel/Core
|
|
2
|
-
|
|
3
|
-
1. **Program Structure**:
|
|
4
|
-
- Consists of a possibly empty list of statements, each ending with a semicolon (`;`), followed by an expression.
|
|
5
|
-
- Statements can be:
|
|
6
|
-
- **Code Definitions**: Define data types and start with a dollar sign (`$`).
|
|
7
|
-
- **Relation Definitions**: Define partial functions for data transformations.
|
|
8
|
-
|
|
9
|
-
2. **Code Definitions (Data Types)**:
|
|
10
|
-
- **Union Types**: Defined using angle brackets (`<...>`).
|
|
11
|
-
|
|
12
|
-
```kernelcore
|
|
13
|
-
$ bool = <{} true, {} false>;
|
|
14
|
-
```
|
|
15
|
-
|
|
16
|
-
- **Product Types**: Defined using curly braces (`{...}`).
|
|
17
|
-
|
|
18
|
-
```kernelcore
|
|
19
|
-
$ pair_of_bools = {bool x, bool y};
|
|
20
|
-
```
|
|
21
|
-
|
|
22
|
-
3. **Relation Definitions (Partial Functions)**:
|
|
23
|
-
- **Projection (Field Extraction)**: Partial functions that extract fields from product types or check for specific variants in union types.
|
|
24
|
-
|
|
25
|
-
```kernelcore
|
|
26
|
-
// Extract the `x` field from a pair_of_bools (product type)
|
|
27
|
-
extract_x = $pair_of_bools .x;
|
|
28
|
-
|
|
29
|
-
// Extract the `y` field from a pair_of_bools (product type)
|
|
30
|
-
extract_y = $pair_of_bools .y;
|
|
31
|
-
|
|
32
|
-
// Check if a bool is the `true` variant (union type)
|
|
33
|
-
check_true = $bool .true;
|
|
34
|
-
|
|
35
|
-
// Check if a bool is the `false` variant (union type)
|
|
36
|
-
check_false = $bool .false;
|
|
37
|
-
```
|
|
38
|
-
|
|
39
|
-
- **Sequential (Functional) Composition**: Sequential application of partial functions.
|
|
40
|
-
|
|
41
|
-
```kernelcore
|
|
42
|
-
is_x_true = $pair_of_bools .x .true;
|
|
43
|
-
```
|
|
44
|
-
|
|
45
|
-
- **Parallel (Product) Composition**: Combine multiple partial functions to construct a new product type.
|
|
46
|
-
|
|
47
|
-
```kernelcore
|
|
48
|
-
extract_both = $pair_of_bools {.x x_value, .y y_value};
|
|
49
|
-
```
|
|
50
|
-
|
|
51
|
-
- **Concurrent (Union) Composition**: Try different functions in sequence, returning the first successful result.
|
|
52
|
-
|
|
53
|
-
```kernelcore
|
|
54
|
-
is_any_true = $pair_of_bools <.x .true true_bool, .y .true true_bool, false_bool>;
|
|
55
|
-
```
|
|
56
|
-
|
|
57
|
-
4. **Constants**:
|
|
58
|
-
- Constants are partial functions that ignore their input.
|
|
59
|
-
|
|
60
|
-
```kernelcore
|
|
61
|
-
true_bool = {{} true};
|
|
62
|
-
false_bool = {{} false};
|
|
63
|
-
```
|
|
64
|
-
|
|
65
|
-
5. **Names and Labels**:
|
|
66
|
-
- Names and labels are alphanumerical sequences of ASCII characters, following the convention `[a-zA-Z_][a-zA-Z0-9_]*`.
|
|
67
|
-
|
|
68
|
-
6. **Final Expression**:
|
|
69
|
-
- A final expression evaluates usage examples and collects results in a single product.
|
|
70
|
-
|
|
71
|
-
```kernelcore
|
|
72
|
-
{pair1 is_x_true result1,
|
|
73
|
-
pair2 is_x_true result2,
|
|
74
|
-
pair1 extract_both both_values,
|
|
75
|
-
pair1 is_any_true any_true1,
|
|
76
|
-
pair2 is_any_true any_true2,
|
|
77
|
-
pair3 is_any_true any_true3}
|
|
78
|
-
```
|
|
79
|
-
|
|
80
|
-
## Complete Example Program
|
|
81
|
-
|
|
82
|
-
```kernelcore
|
|
83
|
-
// Define a boolean type as a union of true and false
|
|
84
|
-
$ bool = <{} true, {} false>;
|
|
85
|
-
|
|
86
|
-
// Define a pair of booleans as a product of two booleans
|
|
87
|
-
$ pair_of_bools = {bool x, bool y};
|
|
88
|
-
|
|
89
|
-
// Projection (Field Extraction)
|
|
90
|
-
// Extract the `x` field from a pair_of_bools (product type)
|
|
91
|
-
extract_x = $pair_of_bools .x;
|
|
92
|
-
|
|
93
|
-
// Extract the `y` field from a pair_of_bools (product type)
|
|
94
|
-
extract_y = $pair_of_bools .y;
|
|
95
|
-
|
|
96
|
-
// Check if a bool is the `true` variant (union type)
|
|
97
|
-
check_true = $bool .true;
|
|
98
|
-
|
|
99
|
-
// Check if a bool is the `false` variant (union type)
|
|
100
|
-
check_false = $bool .false;
|
|
101
|
-
|
|
102
|
-
// Check if the `x` field is `true`
|
|
103
|
-
is_x_true = $pair_of_bools .x .true;
|
|
104
|
-
|
|
105
|
-
// Extract both `x` and `y` fields into a new product
|
|
106
|
-
extract_both = $pair_of_bools {.x x_value, .y y_value};
|
|
107
|
-
|
|
108
|
-
// Check if either `x` or `y` is `true`
|
|
109
|
-
is_any_true = $pair_of_bools <.x .true true_bool, .y .true true_bool, false_bool>;
|
|
110
|
-
|
|
111
|
-
// Define constants
|
|
112
|
-
true_bool = {{} true};
|
|
113
|
-
false_bool = {{} false};
|
|
114
|
-
|
|
115
|
-
// Create instances of `pair_of_bools`
|
|
116
|
-
pair1 = {true_bool x, true_bool y};
|
|
117
|
-
pair2 = {true_bool x, false_bool y};
|
|
118
|
-
pair3 = {false_bool x, false_bool y};
|
|
119
|
-
|
|
120
|
-
// Final expression to evaluate usage examples
|
|
121
|
-
{pair1 is_x_true result1,
|
|
122
|
-
pair2 is_x_true result2,
|
|
123
|
-
pair1 extract_both both_values,
|
|
124
|
-
pair1 is_any_true any_true1,
|
|
125
|
-
pair2 is_any_true any_true2,
|
|
126
|
-
pair3 is_any_true any_true3}
|
|
127
|
-
```
|
|
128
|
-
|
|
129
|
-
---
|
|
130
|
-
|
|
131
|
-
## Kernel/Core Programming Language Overview
|
|
132
|
-
|
|
133
|
-
**Goal**: To establish a minimal set of orthogonal constructs for describing data transformations as partial functions.
|
|
134
|
-
|
|
135
|
-
### Basic Concepts
|
|
136
|
-
|
|
137
|
-
1. **Data Types (Codes)**: Defined using tagged unions and product types.
|
|
138
|
-
- Union: `<>` denotes a union.
|
|
139
|
-
- Product: `{}` denotes a product.
|
|
140
|
-
|
|
141
|
-
2. **Partial Functions (Relations)**: Defined for data transformations.
|
|
142
|
-
- Projection: Extract the value of a given field from product types or variant from union types, `.field_name`.
|
|
143
|
-
- Sequential Composition: sequence of partial function applications (`r1 r2 r3 ...`).
|
|
144
|
-
- Parallel Composition: `{r1 f1, r2 f2, ...}`.
|
|
145
|
-
- Concurrent Composition: `<r1, r2, ...>`.
|
|
146
|
-
|
|
147
|
-
### Syntax
|
|
148
|
-
|
|
149
|
-
- **Program Structure**: Consists of code and relation definitions, ending with a semicolon `;`, followed by an expression which defines the relation (i.e., the desired data transformation).
|
|
150
|
-
|
|
151
|
-
- **Code Definition**: Introduces union or product types.
|
|
152
|
-
|
|
153
|
-
```plaintext
|
|
154
|
-
$bool = <{} true, {} false>;
|
|
155
|
-
$pair_of_bools = {bool x, bool y};
|
|
156
|
-
```
|
|
157
|
-
|
|
158
|
-
- **Code annotations**:
|
|
159
|
-
Any code expression (prefixed by `$`) within a relation definition acts as a type annotation and can be seen as a partial identity function that copies its input to the output as long as the input value is of the type defined by the code expression.
|
|
160
|
-
|
|
161
|
-
- **Constants**:
|
|
162
|
-
Constants are partial functions (relations) that ignore their input. E.g., `{}` ignores the input and produces the empty product (unit). Any value (data point) will be represented by an expression defining the constant (i.e., the relation which ignores its input and returns the value). For representing a value of a union type we use the singleton parallel (product) composition with the only field corresponding to the variant. For example, `{{} true}` could represent the `true` variant of the `$bool`. For a product type, we naturally use the parallel (product) composition, e.g., `{{{} true} x, {{} false} y}` could represent a data point of type `$pair_of_bools`.
|
|
163
|
-
|
|
164
|
-
Singleton type definitions, by union or product, should be avoided as they do not bring any additional information and they cannot be distinguished from the variant or field they represent. E.g.:
|
|
165
|
-
|
|
166
|
-
```plaintext
|
|
167
|
-
$unit_by_union = <{} unit>;
|
|
168
|
-
$unit_by_product = {{} unit};
|
|
169
|
-
```
|
|
170
|
-
|
|
171
|
-
are equivalent to the empty product type `{}`.
|
|
172
|
-
|
|
173
|
-
- **Relation Definition**: Defines helper relations.
|
|
174
|
-
|
|
175
|
-
```plaintext
|
|
176
|
-
true_bool = {{} true} $bool;
|
|
177
|
-
false_bool = {{} false} $bool;
|
|
178
|
-
```
|
|
179
|
-
|
|
180
|
-
### Example Program
|
|
181
|
-
|
|
182
|
-
```plaintext
|
|
183
|
-
// Define data types
|
|
184
|
-
$bool = <{} true, {} false>;
|
|
185
|
-
$pair_of_bools = {bool x, bool y};
|
|
186
|
-
|
|
187
|
-
// Define relations
|
|
188
|
-
true_bool = {{} true} $bool;
|
|
189
|
-
false_bool = {{} false} $bool;
|
|
190
|
-
|
|
191
|
-
is_x_true = $pair_of_bools .x .true;
|
|
192
|
-
is_y_true = $pair_of_bools .y .true;
|
|
193
|
-
extract_both = $pair_of_bools {.x x_value, .y y_value};
|
|
194
|
-
|
|
195
|
-
is_any_true = $pair_of_bools <.x .true true_bool, .y .true true_bool, false_bool>;
|
|
196
|
-
|
|
197
|
-
// Create instances of pair_of_bools
|
|
198
|
-
pair1 = {true_bool x, true_bool y};
|
|
199
|
-
pair2 = {true_bool x, false_bool y};
|
|
200
|
-
|
|
201
|
-
// Example usage of the functions
|
|
202
|
-
{
|
|
203
|
-
(pair1 is_x_true) pair1_x_true,
|
|
204
|
-
(pair1 is_y_true) pair1_y_true,
|
|
205
|
-
(pair2 is_x_true) pair2_x_true,
|
|
206
|
-
(pair2 is_y_true) pair2_y_true,
|
|
207
|
-
(pair1 is_any_true) pair1_any_true,
|
|
208
|
-
(pair2 is_any_true) pair2_any_true,
|
|
209
|
-
(pair1 extract_both) extracted1,
|
|
210
|
-
(pair2 extract_both) extracted2
|
|
211
|
-
}
|
|
212
|
-
```
|
|
213
|
-
|
|
214
|
-
## Key Concepts
|
|
215
|
-
|
|
216
|
-
1. **Projections**: Partial functions for field extraction.
|
|
217
|
-
|
|
218
|
-
```plaintext
|
|
219
|
-
.x, .y // For product types
|
|
220
|
-
.true, .false // For union types (act as checks)
|
|
221
|
-
```
|
|
222
|
-
|
|
223
|
-
2. **Constants**: Defined as partial functions that ignore their input.
|
|
224
|
-
|
|
225
|
-
```plaintext
|
|
226
|
-
zero = {{} zero} $nat;
|
|
227
|
-
```
|
|
228
|
-
|
|
229
|
-
3. **Error Handling**: Kernel/core defines partial functions and does not have a notion of errors within the language. Execution errors (e.g., OOM, stack overflow) are handled by the operating system.
|
|
230
|
-
|
|
231
|
-
4. **Global Names**: All names for relations and codes are global. Relation names and code names do not clash.
|
|
232
|
-
|
|
233
|
-
5. **Gradual Typing**: Expressions cannot always be fully type-annotated, e.g., `.true` can be used where the input value has a union or product type with the variant/field 'true'.
|
|
234
|
-
|
|
235
|
-
6. **Performance Considerations**: Partial functions can be optimized into "sequential transducers," "sequential functions," or a dynamic network of these. Full optimization is possible for the first two classes, with heuristics used for general cases.
|
package/chatGPT.md~
DELETED
|
@@ -1,119 +0,0 @@
|
|
|
1
|
-
Thank you for the clarification. Let's revise the explanation to correctly describe the projection from union types as a check.
|
|
2
|
-
|
|
3
|
-
### Key Concepts and Constructs of Kernel/Core
|
|
4
|
-
|
|
5
|
-
1. **Program Structure**:
|
|
6
|
-
- Consists of a possibly empty list of statements, each ending with a semicolon (`;`), followed by an expression.
|
|
7
|
-
- Statements can be:
|
|
8
|
-
- **Code Definitions**: Define data types and start with a dollar sign (`$`).
|
|
9
|
-
- **Relation Definitions**: Define partial functions for data transformations.
|
|
10
|
-
|
|
11
|
-
2. **Code Definitions (Data Types)**:
|
|
12
|
-
- **Union Types**: Defined using angle brackets (`<...>`).
|
|
13
|
-
```kernelcore
|
|
14
|
-
$ bool = <{} true, {} false>;
|
|
15
|
-
```
|
|
16
|
-
- **Product Types**: Defined using curly braces (`{...}`).
|
|
17
|
-
```kernelcore
|
|
18
|
-
$ pair_of_bools = {bool x, bool y};
|
|
19
|
-
```
|
|
20
|
-
|
|
21
|
-
3. **Relation Definitions (Partial Functions)**:
|
|
22
|
-
- **Projection (Field Extraction)**: Partial functions that extract fields from product types or check for specific variants in union types.
|
|
23
|
-
```kernelcore
|
|
24
|
-
// Extract the `x` field from a pair_of_bools (product type)
|
|
25
|
-
extract_x = $pair_of_bools .x;
|
|
26
|
-
|
|
27
|
-
// Extract the `y` field from a pair_of_bools (product type)
|
|
28
|
-
extract_y = $pair_of_bools .y;
|
|
29
|
-
|
|
30
|
-
// Check if a bool is the `true` variant (union type)
|
|
31
|
-
check_true = $bool .true;
|
|
32
|
-
|
|
33
|
-
// Check if a bool is the `false` variant (union type)
|
|
34
|
-
check_false = $bool .false;
|
|
35
|
-
```
|
|
36
|
-
- **Functional Composition**: Sequential application of partial functions.
|
|
37
|
-
```kernelcore
|
|
38
|
-
is_x_true = $pair_of_bools .x .true;
|
|
39
|
-
```
|
|
40
|
-
- **Parallel (Product) Composition**: Combine multiple partial functions to construct a new product type.
|
|
41
|
-
```kernelcore
|
|
42
|
-
extract_both = $pair_of_bools {.x x_value, .y y_value};
|
|
43
|
-
```
|
|
44
|
-
- **Concurrent (Union) Composition**: Try different functions in sequence, returning the first successful result.
|
|
45
|
-
```kernelcore
|
|
46
|
-
is_any_true = $pair_of_bools <.x .true true_bool, .y .true true_bool, false_bool>;
|
|
47
|
-
```
|
|
48
|
-
|
|
49
|
-
4. **Constants**:
|
|
50
|
-
- Constants are partial functions that ignore their input.
|
|
51
|
-
```kernelcore
|
|
52
|
-
true_bool = {{} true};
|
|
53
|
-
false_bool = {{} false};
|
|
54
|
-
```
|
|
55
|
-
|
|
56
|
-
5. **Names and Labels**:
|
|
57
|
-
- Names and labels are alphanumerical sequences of ASCII characters, following the convention `[a-zA-Z_][a-zA-Z0-9_]*`.
|
|
58
|
-
|
|
59
|
-
6. **Final Expression**:
|
|
60
|
-
- A final expression evaluates usage examples and collects results in a single product.
|
|
61
|
-
```kernelcore
|
|
62
|
-
{pair1 is_x_true result1,
|
|
63
|
-
pair2 is_x_true result2,
|
|
64
|
-
pair1 extract_both both_values,
|
|
65
|
-
pair1 is_any_true any_true1,
|
|
66
|
-
pair2 is_any_true any_true2,
|
|
67
|
-
pair3 is_any_true any_true3}
|
|
68
|
-
```
|
|
69
|
-
|
|
70
|
-
### Complete Example Program
|
|
71
|
-
|
|
72
|
-
```kernelcore
|
|
73
|
-
// Define a boolean type as a union of true and false
|
|
74
|
-
$ bool = <{} true, {} false>;
|
|
75
|
-
|
|
76
|
-
// Define a pair of booleans as a product of two booleans
|
|
77
|
-
$ pair_of_bools = {bool x, bool y};
|
|
78
|
-
|
|
79
|
-
// Projection (Field Extraction)
|
|
80
|
-
// Extract the `x` field from a pair_of_bools (product type)
|
|
81
|
-
extract_x = $pair_of_bools .x;
|
|
82
|
-
|
|
83
|
-
// Extract the `y` field from a pair_of_bools (product type)
|
|
84
|
-
extract_y = $pair_of_bools .y;
|
|
85
|
-
|
|
86
|
-
// Check if a bool is the `true` variant (union type)
|
|
87
|
-
check_true = $bool .true;
|
|
88
|
-
|
|
89
|
-
// Check if a bool is the `false` variant (union type)
|
|
90
|
-
check_false = $bool .false;
|
|
91
|
-
|
|
92
|
-
// Check if the `x` field is `true`
|
|
93
|
-
is_x_true = $pair_of_bools .x .true;
|
|
94
|
-
|
|
95
|
-
// Extract both `x` and `y` fields into a new product
|
|
96
|
-
extract_both = $pair_of_bools {.x x_value, .y y_value};
|
|
97
|
-
|
|
98
|
-
// Check if either `x` or `y` is `true`
|
|
99
|
-
is_any_true = $pair_of_bools <.x .true true_bool, .y .true true_bool, false_bool>;
|
|
100
|
-
|
|
101
|
-
// Define constants
|
|
102
|
-
true_bool = {{} true};
|
|
103
|
-
false_bool = {{} false};
|
|
104
|
-
|
|
105
|
-
// Create instances of `pair_of_bools`
|
|
106
|
-
pair1 = {true_bool x, true_bool y};
|
|
107
|
-
pair2 = {true_bool x, false_bool y};
|
|
108
|
-
pair3 = {false_bool x, false_bool y};
|
|
109
|
-
|
|
110
|
-
// Final expression to evaluate usage examples
|
|
111
|
-
{pair1 is_x_true result1,
|
|
112
|
-
pair2 is_x_true result2,
|
|
113
|
-
pair1 extract_both both_values,
|
|
114
|
-
pair1 is_any_true any_true1,
|
|
115
|
-
pair2 is_any_true any_true2,
|
|
116
|
-
pair3 is_any_true any_true3}
|
|
117
|
-
```
|
|
118
|
-
|
|
119
|
-
This updated introduction and complete example program now correctly describe the projection from union types as checks, providing a comprehensive overview of kernel/core. If there are any further questions or a
|
package/code-resolution.md
DELETED
|
@@ -1,121 +0,0 @@
|
|
|
1
|
-
# Pattern and its graph representation
|
|
2
|
-
|
|
3
|
-
Intuitively, a _pattern_ is an adhock description of a set of codes
|
|
4
|
-
by providing some characteristics of the codes. Those characteristics are:
|
|
5
|
-
|
|
6
|
-
1. Is this product vs union?
|
|
7
|
-
2. The code has to have field (label) leading to a type or another parretn
|
|
8
|
-
|
|
9
|
-
Code patterns is a graph with:
|
|
10
|
-
|
|
11
|
-
1. edges labeled by `fields`
|
|
12
|
-
2. nodes labeled by one of the:
|
|
13
|
-
|
|
14
|
-
1. `/*/` - open unkown
|
|
15
|
-
2. `{*}` - open product
|
|
16
|
-
3. `<*>` - open union
|
|
17
|
-
4. `//` - close uknown
|
|
18
|
-
5. `{}` - close product
|
|
19
|
-
6. `<>` - close union
|
|
20
|
-
|
|
21
|
-
A graph without "open" and "unknown" nodes is "code".
|
|
22
|
-
|
|
23
|
-
Actually every node in such a graph corresponds to a code pattern.
|
|
24
|
-
|
|
25
|
-
## Pattern Graph
|
|
26
|
-
|
|
27
|
-
Example:
|
|
28
|
-
|
|
29
|
-
$nat = < {} _, nat 0, nat 1>;
|
|
30
|
-
rlz = $nat <.0._ {{{} _} 0}, .0 rlz, ()>;
|
|
31
|
-
|
|
32
|
-
We get `codes`:
|
|
33
|
-
|
|
34
|
-
id | code
|
|
35
|
-
------|----------------------
|
|
36
|
-
'{}' | {}
|
|
37
|
-
'nat' | <nat 0, nat 1, {} _>
|
|
38
|
-
|
|
39
|
-
and the (singleton) forest of expressons:
|
|
40
|
-
|
|
41
|
-
id | ref-name | rel
|
|
42
|
-
------|----------|--------------
|
|
43
|
-
0 | 'rlz' | ($nat < (.0 ._ {{{} _} 0}), (.0 rlz), ()>)
|
|
44
|
-
|
|
45
|
-
### Initialize pattern graph
|
|
46
|
-
|
|
47
|
-
1. We annotate the expression forest by unique pattern ids, all initialized as `*`, e.g.:
|
|
48
|
-
|
|
49
|
-
id | name | rel*
|
|
50
|
-
---|-------|-----------------
|
|
51
|
-
0 | 'rlz' | ( $nat < ( .0 ._ { { {} _} 0} ) , ( .0 rlz ) , () > )
|
|
52
|
-
| | %0 ( %1 $nat %2 < %3 ( %4 .0 %5 ._ %6 { %7 { %8 {} %9 _} %10 0} %11 ) %12, %13 ( %14 .0 %15 rlz %16 ) %17 , %18 () %19 > %20 ) %21
|
|
53
|
-
|
|
54
|
-
2. The pattern graph (with 22 nodes) has no edges.
|
|
55
|
-
|
|
56
|
-
### Pattern graph folding
|
|
57
|
-
|
|
58
|
-
Given a pattern graph, `(V,E)`, and an expression forest, `F`, we calculate a `folding` of the pattern graph,
|
|
59
|
-
which consists of:
|
|
60
|
-
|
|
61
|
-
1. equivalence classes `eq` over `V + C`
|
|
62
|
-
- `%x $t $y` implies `%x ~ %y ~ $t`
|
|
63
|
-
- `%x ( %y ...)` implies `%x ~ %y`
|
|
64
|
-
- `(... %x ) %y` implies `%x ~ %y`
|
|
65
|
-
- `%x () %y` implies `%x ~ %y`
|
|
66
|
-
- `%x { %y1 ..., %y2 ..., ..., %yn ...}` implies `%x ~ %y1 ~ %y2 ~ ... ~ %yn`
|
|
67
|
-
- `%x < %y1 ..., %y2 ..., ..., %yn ...>` implies `%x ~ %y1 ~ %y2 ~ ... ~ %yn`
|
|
68
|
-
- `<... %y1, ... %y2, ..., ... %yn > %x` implies `%x ~ %y1 ~ %y2 ~ ... ~ %yn`
|
|
69
|
-
- if `%x ~ $t` and `%x .field %y` implies `%y ~ $t.field`
|
|
70
|
-
2. new edges, `E'`
|
|
71
|
-
- `%x .field %y` implies `%x -[field]-> %y`
|
|
72
|
-
- `{..., ... %x field, ...} %y` implies `%y -[field]-> %x`
|
|
73
|
-
3. pattern refinements, `R`
|
|
74
|
-
- `{... field} %x` implies `%x: <*>`
|
|
75
|
-
- `{... field, ...} %y` implies `%y: {}`
|
|
76
|
-
|
|
77
|
-
In our example:
|
|
78
|
-
|
|
79
|
-
`eq: {0,1,nat,2,3,4,11,12,13,14,15,16,17,18,19,20,21}, {5}, {6,7,8}, {9}, {10}`
|
|
80
|
-
|
|
81
|
-
`E': (4 -[0]-> 5), (5 -[_]-> 6), (10 -[_]-> 9), (11 -[0]-> 10), (14 -[0]-> 15)`
|
|
82
|
-
|
|
83
|
-
`R: 9:{}, 10:<*>`
|
|
84
|
-
|
|
85
|
-
By folding the pattern graph with those information we get:
|
|
86
|
-
|
|
87
|
-
%n ( %n $nat %n < %n ( %n .0 %n ._ %{} { %{} { %{} {} %{} _} %n 0} %n ) %n, %n ( %n .0 %n rlz %n ) %n , %n () %n > %n ) %n
|
|
88
|
-
|
|
89
|
-
### Another example
|
|
90
|
-
|
|
91
|
-
dec = [(),-1] PLUS;
|
|
92
|
-
zero? = [(),0] EQ 0;
|
|
93
|
-
factorial = < zero? 1, [dec factorial, ()] TIMES >;
|
|
94
|
-
{ () x, factorial "x!" }
|
|
95
|
-
|
|
96
|
-
name | rel*
|
|
97
|
-
-------|-----------------
|
|
98
|
-
PLUS | %1 $[int] %2 PLUS %3 $int %4
|
|
99
|
-
-1 | %5 {} %6 -1 %7 $int %8
|
|
100
|
-
EQ | %9 $[int] %10
|
|
101
|
-
dec | %11 [ %12 () %13, %14 -1 %15 ] %16 PLUS %17
|
|
102
|
-
zero? | %18 [ %19 () %20, %21 0 %22 ] %23 EQ %24 .0 %25
|
|
103
|
-
|
|
104
|
-
----
|
|
105
|
-
Other observations:
|
|
106
|
-
|
|
107
|
-
Product like {.x ..., .y ...} implies the input is a product (not really valid)
|
|
108
|
-
Pattern `p = <{ p x, ... }>` implies, it is a union!, i.e., `p = < p x, ... >`, e.g, in `< .x, () >`.
|
|
109
|
-
|
|
110
|
-
## To do
|
|
111
|
-
|
|
112
|
-
0. rewrite the algo to be layered:
|
|
113
|
-
1. pure algebraic (no vectors)
|
|
114
|
-
2. with support for homogenous vectors
|
|
115
|
-
3. native support for bytes, integers (`int64` only), booleans, and strings (`UTF8` only)
|
|
116
|
-
1. incremental pattern graph construction
|
|
117
|
-
2. folding pattern graph nodes corresponding to a code (e.g., parts of
|
|
118
|
-
the pattern graph whose nodes are all closed), adding codes into code graph and,
|
|
119
|
-
finally, re-minimizing it.
|
|
120
|
-
3. support for 'built-in' types via plugins.
|
|
121
|
-
4. do not simplify merge for code derivation sake.
|
package/code-resolution.mjs
DELETED
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
function resolveCode(rel, inCode, outCode) {
|
|
2
|
-
const annotatedRel = {inCode, outCode, rel};
|
|
3
|
-
return annotatedRel;
|
|
4
|
-
}
|
|
5
|
-
|
|
6
|
-
// pat: { kind: [null, 'product', 'union'],
|
|
7
|
-
// closed: [true, false],
|
|
8
|
-
// fields: [{f: string, pat: pat}, ...]}
|
|
9
|
-
const emptyPat = { kind: 'union', closed: true, fields: [] };
|
|
10
|
-
const starPat = { kind: null, closed: false, fields: [] };
|
|
11
|
-
function isEmptyPat(pat) {
|
|
12
|
-
const {kind, closed, fields} = pat;
|
|
13
|
-
return kind === 'union' && closed && fields.length === 0;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
function cap(pat1, pat2) {
|
|
17
|
-
if (pat1 === pat2) return pat1;
|
|
18
|
-
const {kind:kind1, closed:closed1, fields:fields1} = pat1;
|
|
19
|
-
const {kind:kind2, closed:closed2, fields:fields2} = pat2;
|
|
20
|
-
let kind, fields;
|
|
21
|
-
if (kind1 === null) {
|
|
22
|
-
kind = kind2;
|
|
23
|
-
} else if (kind2 === null) {
|
|
24
|
-
kind = kind1;
|
|
25
|
-
} else if (kind1 === kind2) {
|
|
26
|
-
kind = kind1;
|
|
27
|
-
} else
|
|
28
|
-
return emptyPat;
|
|
29
|
-
|
|
30
|
-
const map1 = new Map(fields1.map(({f, pat}) => [f, pat]));
|
|
31
|
-
const map2 = new Map(fields2.map(({f, pat}) => [f, pat]));
|
|
32
|
-
const fieldNames = new Set([...map1.keys(), ...map2.keys()]);
|
|
33
|
-
fields = [...fieldNames].map(f => {
|
|
34
|
-
const pat1 = map1.get(f) || starPat;
|
|
35
|
-
const pat2 = map2.get(f) || starPat;
|
|
36
|
-
const pat = cap(pat1, pat2);
|
|
37
|
-
return {f, pat};
|
|
38
|
-
});
|
|
39
|
-
if (fields.some(({pat}) => isEmptyPat(pat))) {
|
|
40
|
-
return emptyPat;
|
|
41
|
-
}
|
|
42
|
-
if ((closed1 && fields1.length !== fields.length) || (closed2 && fields2.length !== fields.length)) {
|
|
43
|
-
return emptyPat;
|
|
44
|
-
}
|
|
45
|
-
return {kind, closed: closed1 || closed2, fields};
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
export default { cap, starPat, emptyPat, isEmptyPat, resolveCode };
|
|
50
|
-
export { cap, starPat, emptyPat, isEmptyPat, resolveCode };
|
|
51
|
-
|
package/filters.mjs
DELETED
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
// filter is of type: [name|code|null|union|product|vector],
|
|
2
|
-
// null|union|product have fields, and can be open
|
|
3
|
-
|
|
4
|
-
function unify_with_code(codes, code, filter, context){
|
|
5
|
-
// console.log({code, filter, context});
|
|
6
|
-
if (context == undefined) return undefined;
|
|
7
|
-
if (code == undefined) return undefined;
|
|
8
|
-
switch (filter.type) {
|
|
9
|
-
case "code": {
|
|
10
|
-
if (filter.code == code)
|
|
11
|
-
return context;
|
|
12
|
-
return undefined;
|
|
13
|
-
}
|
|
14
|
-
case "name": {
|
|
15
|
-
const code_filter = {type: 'code', code: code};
|
|
16
|
-
if (filter.name in context) {
|
|
17
|
-
const new_filter = context[filter.name];
|
|
18
|
-
const new_context = {... context, [filter.name]: code_filter};
|
|
19
|
-
return unify_with_code(codes, code, new_filter, new_context);
|
|
20
|
-
}
|
|
21
|
-
return {...context, [filter.name]: {type: 'code', code: code}}
|
|
22
|
-
}
|
|
23
|
-
case "product":
|
|
24
|
-
case "union":
|
|
25
|
-
case null: {
|
|
26
|
-
const ctype = codes[code]?.code;
|
|
27
|
-
if (ctype != 'product' && ctype != 'union') return undefined;
|
|
28
|
-
const code_fields = codes[code][ctype];
|
|
29
|
-
const new_context = Object.keys(filter.fields).reduce((new_context, field) =>
|
|
30
|
-
unify_with_code(codes, code_fields[field], filter.fields[field], new_context)
|
|
31
|
-
, {...context});
|
|
32
|
-
if (filter.open !== true && Object.keys(code_fields).length != Object.keys(filter.fields).length)
|
|
33
|
-
return undefined;
|
|
34
|
-
return new_context;
|
|
35
|
-
}
|
|
36
|
-
case "vector": {
|
|
37
|
-
const ctype = codes[code]?.code;
|
|
38
|
-
if (ctype != 'vector') return undefined;
|
|
39
|
-
const code_vector = codes[code].vector;
|
|
40
|
-
return unify_with_code(codes, code_vector, filter.vector, context);
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
// we assume the names occurring in both filters are disjoint
|
|
46
|
-
function unify(codes, filter1, filter2, context ) {
|
|
47
|
-
if (context == undefined) return undefined;
|
|
48
|
-
if (filter1 == undefined || filter2 == undefined) return undefined;
|
|
49
|
-
|
|
50
|
-
switch (filter1.type) {
|
|
51
|
-
case "code":
|
|
52
|
-
return unify_with_code(codes, filter1.code, filter2, context);
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
export default { unify_with_code };
|
|
57
|
-
export { unify_with_code };
|
|
58
|
-
|
|
59
|
-
|