@fraczak/k 6.1.2 → 6.1.3
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/DOCS/BIG_PICTURE.md +10 -10
- package/DOCS/DIVERGENCE_DETECTION.md +1 -1
- package/DOCS/TYPE_DERIVATION.md +12 -12
- package/DOCS/book/01-introduction.md +2 -2
- package/DOCS/book/02-syntax-and-values.md +8 -8
- package/DOCS/book/03-types-as-finite-automata.md +2 -2
- package/DOCS/book/04-partial-functions-and-composition.md +2 -2
- package/DOCS/book/05-typing-filters-and-normalization.md +3 -3
- package/DOCS/book/06-values-in-memory.md +3 -3
- package/DOCS/book/07-the-partial-function-abi.md +1 -1
- package/DOCS/book/08-operational-semantics-and-execution.md +1 -1
- package/DOCS/book/09-compiler-architecture.md +2 -2
- package/DOCS/book/appendix-b-incorporating-external-predefined-types-and-functions.md +1 -0
- package/README.md +7 -8
- package/a.k +138 -0
- package/a.klib +1 -0
- package/codecs/BINARY_FORMAT.md +1 -1
- package/codecs/POLYMORPHIC_BINARY_FORMAT.md +1 -1
- package/codes.mjs +14 -33
- package/export.mjs +3 -3
- package/ieee.klib +1 -1
- package/nak.klib +1 -0
- package/nak2.klib +1 -0
- package/nat.k +10 -0
- package/nat2.k +9 -0
- package/object.mjs +20 -7
- package/package.json +2 -1
- package/scripts/benchmark-idempotency.mjs +72 -0
- package/test-k-object.mjs +19 -0
package/DOCS/BIG_PICTURE.md
CHANGED
|
@@ -12,7 +12,7 @@ with that tree.
|
|
|
12
12
|
|
|
13
13
|
### Types (codes)
|
|
14
14
|
|
|
15
|
-
Formally, a _type_ is a
|
|
15
|
+
Formally, a _type_ is a finite tree automaton. The type defines a set of values,
|
|
16
16
|
which corresponds to the set of trees accepted by the automaton.
|
|
17
17
|
Therefore, every type admits a unique normal form in terms of its minimal deterministic tree automaton.
|
|
18
18
|
|
|
@@ -38,7 +38,7 @@ Given a finite (possibly empty) set of types `t1, t2, ..., tn` and a set of pair
|
|
|
38
38
|
Each projection function maps a value of type `{t1 l1, t2 l2, ..., tn ln}`
|
|
39
39
|
into a value of type `t1`, `t2`, `...`, `tn`, respectively.
|
|
40
40
|
- by `< t1 l1, t2 l2, ..., tn ln >`, we define a _union type_ with exactly `n` projection functions `/l1`, `/l2`, `...`, `/ln`,
|
|
41
|
-
|
|
41
|
+
mapping a value of type `<t1 l1, t2 l2, ..., tn ln>` into a value of type `t1`, `t2`, `...`, `tn`, respectively.
|
|
42
42
|
|
|
43
43
|
Intuitively, the projection functions on a product type are all total (meaning, they are defined for every value of the
|
|
44
44
|
input product type), whereas in the case of a union type, exactly one projection function is defined
|
|
@@ -68,7 +68,7 @@ Two (possibly recursive) codes are equivalent iff they are bisimilar over their
|
|
|
68
68
|
|
|
69
69
|
### Filters (patterns)
|
|
70
70
|
|
|
71
|
-
A _filter_ is a way of representing a _pattern_, i.e
|
|
71
|
+
A _filter_ is a way of representing a _pattern_, i.e., a set of types sharing some structure.
|
|
72
72
|
For example, if I wanted to refer to all product types that have a field `f` of type `T`,
|
|
73
73
|
I would write: `?{ T f, ... }`. In general, a filter is defined by:
|
|
74
74
|
|
|
@@ -158,14 +158,14 @@ registry of types (schemas) and functions.
|
|
|
158
158
|
|
|
159
159
|
## Typing and Polymorphism (codes and patterns)
|
|
160
160
|
|
|
161
|
-
|
|
162
|
-
a dictionary
|
|
161
|
+
The abstract syntax tree of a program consists of a dictionary of _type definitions_,
|
|
162
|
+
a dictionary of _function definitions_, and the final _main expression_.
|
|
163
163
|
|
|
164
164
|
In principle, the initial form of the Abstract Syntax Tree, _raw_ AST, is enough to evaluate
|
|
165
165
|
the program on an input.
|
|
166
166
|
|
|
167
|
-
Our objective however is to compute canonical representations for types and normalized
|
|
168
|
-
representations for functions
|
|
167
|
+
Our objective, however, is to compute canonical representations for types and normalized
|
|
168
|
+
representations for functions so they can be reused.
|
|
169
169
|
|
|
170
170
|
The normalization steps are:
|
|
171
171
|
|
|
@@ -225,7 +225,7 @@ Functions `inc` and `inc3` are (non-polymorphic) functions of types
|
|
|
225
225
|
`${ bit overflown, byte byte } -> ${ bit overflown, byte byte }`, respectively.
|
|
226
226
|
|
|
227
227
|
Function `inc_o` is a polymorphic function of type patterns: `?X -> ${ byte byte, bit overflown }`
|
|
228
|
-
with following constraints:
|
|
228
|
+
with the following constraints:
|
|
229
229
|
|
|
230
230
|
- `?X` is a product type with at least two fields: `byte` and `overflown`, denoted by:
|
|
231
231
|
> `?{ $byte byte, Z overflown, ...}`;
|
|
@@ -269,7 +269,7 @@ The registry will be a key-value store, where the key is the hash of the normali
|
|
|
269
269
|
and the value is the normalized type itself.
|
|
270
270
|
|
|
271
271
|
All functions (non-polymorphic and polymorphic) will be renamed by the hash of their normalized
|
|
272
|
-
definition and stored in
|
|
272
|
+
definition and stored in a similar key-value store.
|
|
273
273
|
|
|
274
274
|
Non-polymorphic functions can be easily indexed by the hashes of their input and output types
|
|
275
275
|
so that we can quickly find the function we need.
|
|
@@ -280,7 +280,7 @@ Indexing polymorphic functions seems complicated (TODO).
|
|
|
280
280
|
The language is designed to transform _codes_, i.e., serialized typed values, i.e., trees
|
|
281
281
|
accepted by a tree automaton, into other _codes_.
|
|
282
282
|
The transformation is done by partial functions as defined in the language.
|
|
283
|
-
The non-recursive and tail-recursive (and even some non-tail
|
|
283
|
+
The non-recursive and tail-recursive (and even some non-tail-recursive) functions can
|
|
284
284
|
be compiled into deterministic finite (pushdown) transducers.
|
|
285
285
|
|
|
286
286
|
### AND-OR graphs for encoding and decoding codes
|
|
@@ -19,6 +19,6 @@ HEAD(?F E) = HEAD(E)
|
|
|
19
19
|
HEAD($T E) = HEAD(E)
|
|
20
20
|
HEAD((E1 E2 ...)) = HEAD(E1)
|
|
21
21
|
HEAD({ E1 f1, E2 f2, ... }) = HEAD(E1) union HEAD(E2) union ...
|
|
22
|
-
HEAD(<E1, E2, ...>) = {} --
|
|
22
|
+
HEAD(<E1, E2, ...>) = {} -- the head of a union is the empty set
|
|
23
23
|
HEAD(/ tag) = HEAD(. lab) = HEAD(| tag) = {}
|
|
24
24
|
```
|
package/DOCS/TYPE_DERIVATION.md
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
This algorithm infers type constraints for programs consisting of mutually recursive partial functions over algebraic data types (products and unions).
|
|
6
6
|
|
|
7
|
-
**Input:** AST of a `k` program, consisting of function definitions (expressions)
|
|
7
|
+
**Input:** AST of a `k` program, consisting of function definitions (expressions), which can be seen as a set of recursive equations.
|
|
8
8
|
**Output:** Annotated AST with type patterns (also called filters)
|
|
9
9
|
|
|
10
10
|
It is assumed that we have access (through APIs) to a universal "type registry", which provides us with unique (canonical) names for each type.
|
|
@@ -22,12 +22,12 @@ A **pattern** represents a set of types:
|
|
|
22
22
|
| `<>` | Closed union | Exactly specified |
|
|
23
23
|
| `T` | Named type | From type definition |
|
|
24
24
|
|
|
25
|
-
**Fields:** Each pattern has associated field labels. For open patterns, additional fields may exist. For closed patterns, the field set is exact.
|
|
25
|
+
**Fields:** Each pattern has associated field labels. For open patterns, additional fields may exist. For closed patterns, the field set is exact.
|
|
26
26
|
Unknown-kind patterns have only the open form `(...)`; a closed filter must choose product `{...}`/`{}` or union `<...>`/`<>`.
|
|
27
27
|
|
|
28
28
|
A type pattern is a singleton pattern, i.e., only one type is captured by such a pattern.
|
|
29
29
|
|
|
30
|
-
## 3. Pattern Graph -
|
|
30
|
+
## 3. Pattern Graph - A Data Structure Used During Type Derivation
|
|
31
31
|
|
|
32
32
|
**Nodes:** Patterns organized in a "reps" (representatives) forest, similar to union-find. However, a union of two (or more) different trees always produces a new "rep" (root) node, joining the trees.
|
|
33
33
|
**Edges:** Labeled by field names, pointing to other patterns
|
|
@@ -42,13 +42,13 @@ A type pattern is a singleton pattern, i.e., only one type is captured by such a
|
|
|
42
42
|
|
|
43
43
|
## 4. Unification
|
|
44
44
|
|
|
45
|
-
`unify(p₁, ..., pₙ)` computes the least upper bound (most specific common pattern); it may add a new node (rep) into the reps forest
|
|
45
|
+
`unify(p₁, ..., pₙ)` computes the least upper bound (most specific common pattern); it may add a new node (rep) into the reps forest, but it does not modify any existing pattern node.
|
|
46
46
|
|
|
47
47
|
**Rules:**
|
|
48
48
|
|
|
49
49
|
- **Open + Open:** Merge field sets, stay open
|
|
50
|
-
- **Open + Closed:** Check
|
|
51
|
-
- **Closed + Closed:** Check
|
|
50
|
+
- **Open + Closed:** Check field inclusion, then become closed
|
|
51
|
+
- **Closed + Closed:** Check field equality, then stay closed
|
|
52
52
|
- **Product ⊥ Union:** Error (incompatible)
|
|
53
53
|
- **Type T:** Unique (canonical name)
|
|
54
54
|
|
|
@@ -75,7 +75,7 @@ out(eᵢ) = in(eᵢ₊₁) for i = 1..n-1
|
|
|
75
75
|
out((e₁ e₂ ... eₙ)) = out(eₙ)
|
|
76
76
|
```
|
|
77
77
|
|
|
78
|
-
|
|
78
|
+
When `n=0`, the composition is the identity, `()`.
|
|
79
79
|
|
|
80
80
|
```text
|
|
81
81
|
in(()) = out(())
|
|
@@ -136,7 +136,7 @@ in(T) = out(T) = pattern(T)
|
|
|
136
136
|
see patterns.filterToPattern(F)
|
|
137
137
|
```
|
|
138
138
|
|
|
139
|
-
|
|
139
|
+
A filter expression is syntax for describing pattern graphs.
|
|
140
140
|
|
|
141
141
|
## 6. Global Algorithm
|
|
142
142
|
|
|
@@ -166,13 +166,13 @@ For each SCC (in topological order):
|
|
|
166
166
|
For each function `f` in SCC:
|
|
167
167
|
1. Compact `f`'s pattern graph
|
|
168
168
|
2. For each occurrence `o` of `g` in `f`, clone(in(g), out(g)) and unify input and output patterns of `o` with the corresponding cloned patterns
|
|
169
|
-
|
|
169
|
+
|
|
170
170
|
If pattern graphs unchanged: break
|
|
171
171
|
```
|
|
172
172
|
|
|
173
173
|
## 7. Compaction
|
|
174
174
|
|
|
175
|
-
**Purpose:** Replace all pattern nodes
|
|
175
|
+
**Purpose:** Replace all pattern nodes with their reps and replace all singleton patterns with types.
|
|
176
176
|
|
|
177
177
|
**Algorithm:**
|
|
178
178
|
|
|
@@ -186,7 +186,7 @@ For each SCC (in topological order):
|
|
|
186
186
|
|
|
187
187
|
**Criterion:** Pattern graph structure unchanged between iterations
|
|
188
188
|
|
|
189
|
-
**Measured by:**
|
|
189
|
+
**Measured by:**
|
|
190
190
|
1. Representative forest structure (parent pointers)
|
|
191
191
|
2. Edge sets for each representative
|
|
192
192
|
3. Pattern descriptors for each representative
|
|
@@ -206,7 +206,7 @@ Converged = (current_state == previous_state)
|
|
|
206
206
|
|
|
207
207
|
## 9. Error Handling
|
|
208
208
|
|
|
209
|
-
**Unification errors:**
|
|
209
|
+
**Unification errors include:**
|
|
210
210
|
|
|
211
211
|
- Product vs Union
|
|
212
212
|
- Closed pattern field mismatch
|
|
@@ -44,8 +44,8 @@ computation in a concise and understandable way.
|
|
|
44
44
|
|
|
45
45
|
There is another concept in the language, called **filters**.
|
|
46
46
|
Filters play a role in the process of reasoning about types of partial functions.
|
|
47
|
-
Filters have been added to the syntax of the language
|
|
48
|
-
of "polymorphic" expressions, i.e., expressions
|
|
47
|
+
Filters have been added to the syntax of the language because they allow explicit annotations
|
|
48
|
+
of "polymorphic" expressions, i.e., expressions that make sense in different contexts.
|
|
49
49
|
Filters are similar to *type classes* in other languages.
|
|
50
50
|
|
|
51
51
|
---
|
|
@@ -10,7 +10,7 @@ $ type_name = ...type_expression... ;
|
|
|
10
10
|
function_name = ...expression... ;
|
|
11
11
|
```
|
|
12
12
|
|
|
13
|
-
The definitions are followed in the file by the *main expression
|
|
13
|
+
The definitions are followed in the file by the *main expression*, which represents the partial function defined by the program as a whole.
|
|
14
14
|
|
|
15
15
|
There is no syntax for values.
|
|
16
16
|
Every syntactic form in an expression describes a **function** -- possibly a constant function that always returns the same value.
|
|
@@ -48,22 +48,22 @@ Every type denotes a finite tree automaton whose accepted trees are the possible
|
|
|
48
48
|
|
|
49
49
|
## **2.4 Projections**
|
|
50
50
|
|
|
51
|
-
**Dot** notation, e.g.,
|
|
51
|
+
**Dot** notation, e.g.,
|
|
52
52
|
|
|
53
53
|
```k
|
|
54
54
|
. field_name
|
|
55
55
|
```
|
|
56
56
|
|
|
57
57
|
is used for extracting the value of a field from a record (product type value).
|
|
58
|
-
**Div** notation, e.g.,
|
|
58
|
+
**Div** notation, e.g.,
|
|
59
59
|
|
|
60
60
|
```k
|
|
61
61
|
/ variant_name
|
|
62
62
|
```
|
|
63
63
|
|
|
64
64
|
is used for asserting a particular variant of a union and extracting its value.
|
|
65
|
-
|
|
66
|
-
and union type `< T₁ l₁, T₂ l₂, … , Tₙ lₙ >` naturally defines `n` partial functions `/l₁, /l₂, … , /lₙ`.
|
|
65
|
+
A product type `{ T₁ l₁, T₂ l₂, … , Tₙ lₙ }` naturally defines `n` partial functions: `.l₁, .l₂, … , .lₙ`,
|
|
66
|
+
and a union type `< T₁ l₁, T₂ l₂, … , Tₙ lₙ >` naturally defines `n` partial functions `/l₁, /l₂, … , /lₙ`.
|
|
67
67
|
Those partial functions are called **projections** and they constitute the basis for the definition
|
|
68
68
|
of all other partial functions in the `k` language.
|
|
69
69
|
|
|
@@ -81,10 +81,10 @@ Expressions combine partial functions using the following operators:
|
|
|
81
81
|
Parentheses around a composition may be omitted except for the empty composition `()` — the identity function.
|
|
82
82
|
Empty product, `{ }`, defines a constant function (ignoring its input) returning *unit*.
|
|
83
83
|
|
|
84
|
-
It is important to notice that after the three markers
|
|
84
|
+
It is important to notice that after the three markers `dot (.)`, `div (/)`, or `pipe (|)`, there must be a constant label literal. For that reason, it is possible and recommended not to leave any blanks after the marker, e.g.:
|
|
85
85
|
|
|
86
86
|
```k-lang
|
|
87
|
-
.field /'a-tag' |"strange and long tag name ✅"
|
|
87
|
+
.field /'a-tag' |"strange and long tag name ✅"
|
|
88
88
|
```
|
|
89
89
|
|
|
90
90
|
---
|
|
@@ -105,7 +105,7 @@ This defines a "two-variant" union type and three functions: two constants and o
|
|
|
105
105
|
## **2.7 Summary**
|
|
106
106
|
|
|
107
107
|
* All types describe tree shapes.
|
|
108
|
-
* As type expression, `{}` is the empty product type (with one value, called *unit*).
|
|
108
|
+
* As a type expression, `{}` is the empty product type (with one value, called *unit*).
|
|
109
109
|
* Functions, not values, are the only expressions in `k`.
|
|
110
110
|
|
|
111
111
|
---
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
## **3.1 Motivation**
|
|
4
4
|
|
|
5
|
-
A type in `k` describes a set of finite labeled trees, which can be recognized by a finite tree automaton (FTA). Equivalently, a type definition can be seen as a
|
|
5
|
+
A type in `k` describes a set of finite labeled trees, which can be recognized by a finite tree automaton (FTA). Equivalently, a type definition can be seen as a context-free grammar (CFG), where the values of the type are the derivation trees of the grammar.
|
|
6
6
|
|
|
7
7
|
For example, the recursive type definition of a `list`:
|
|
8
8
|
|
|
@@ -31,7 +31,7 @@ A value of type `list` is a derivation tree of this grammar. For instance, a lis
|
|
|
31
31
|
-- (rule 1) --> {} |true
|
|
32
32
|
```
|
|
33
33
|
|
|
34
|
-
This derivation tree corresponds to the value generated by constant function
|
|
34
|
+
This derivation tree corresponds to the value generated by the constant function `{ {} |true head, {} |nil tail } |cons`.
|
|
35
35
|
|
|
36
36
|
Representing types as automata (or CFGs) provides each type with a well-defined structure, independent of the names used in the source code. This allows for canonical normal forms and for comparing types for equality by structure alone.
|
|
37
37
|
|
|
@@ -91,7 +91,7 @@ Here, `true_bool` and `false_bool` are constant functions. They are also **total
|
|
|
91
91
|
|
|
92
92
|
---
|
|
93
93
|
|
|
94
|
-
## **4.6 Projection and
|
|
94
|
+
## **4.6 Projection and variant value constructors**
|
|
95
95
|
|
|
96
96
|
Projection selects a field or a variant from a product or union.
|
|
97
97
|
It is written with a leading dot `.` or a leading slash `/`, respectively:
|
|
@@ -141,6 +141,6 @@ The empty product `{}` is the constant function returning the unit value.
|
|
|
141
141
|
* **Product composition** `{...}` runs functions in parallel and gathers their results.
|
|
142
142
|
* **Union composition** `<...>` tries functions in order, returning the first successful result.
|
|
143
143
|
* **Projections** (`.x`, `/x`) are fundamental building blocks for accessing data structures.
|
|
144
|
-
* **Variant
|
|
144
|
+
* **Variant constructors** (`|tag`) allow lifting a value into a variant type value.
|
|
145
145
|
|
|
146
146
|
---
|
|
@@ -87,13 +87,13 @@ Here:
|
|
|
87
87
|
|
|
88
88
|
## **5.6 Type inference and normalization**
|
|
89
89
|
|
|
90
|
-
Every `k` program can be analyzed to assign an input and an output filter to every sub-expression. This process is called type inference
|
|
90
|
+
Every `k` program can be analyzed to assign an input and an output filter to every sub-expression. This process is called type inference and normalization.
|
|
91
91
|
|
|
92
|
-
Type inference and normalization
|
|
92
|
+
Type inference and normalization proceed as follows:
|
|
93
93
|
|
|
94
94
|
1. Build a graph of all type references appearing explicitly and implicitly in the program.
|
|
95
95
|
2. Annotate each expression node with a pair of (input filter, output filter).
|
|
96
|
-
3. Replace filters that match a single, concrete type with that type expression, adding any newly
|
|
96
|
+
3. Replace filters that match a single, concrete type with that type expression, adding any newly discovered types to the graph.
|
|
97
97
|
4. Repeat until a fixed point is reached and no more changes occur.
|
|
98
98
|
5. Compute canonical forms for all newly introduced types.
|
|
99
99
|
|
|
@@ -138,7 +138,7 @@ The value `{}|_|0|1` or in JSON-like notation `{1:{0:{_:{}}}}` (binary 10₂ = d
|
|
|
138
138
|
01 00 10
|
|
139
139
|
```
|
|
140
140
|
|
|
141
|
-
This represents the
|
|
141
|
+
This represents the leftmost derivation of `{}|_|0|1` from `C0`.
|
|
142
142
|
|
|
143
143
|
#### **Prefix-free property**
|
|
144
144
|
|
|
@@ -304,7 +304,7 @@ Without DAG: Each "Engineering" string encoded separately
|
|
|
304
304
|
- 1000 records × 30 bytes/record = 30KB
|
|
305
305
|
|
|
306
306
|
With DAG: "Engineering" string shared via back-references
|
|
307
|
-
- 1 × "Engineering" (11 bytes) + 800 × back-reference (3 bytes) = 2.
|
|
307
|
+
- 1 × "Engineering" (11 bytes) + 800 × back-reference (3 bytes) = 2.4 KB
|
|
308
308
|
- Compression ratio: ~12.5×
|
|
309
309
|
```
|
|
310
310
|
|
|
@@ -1061,7 +1061,7 @@ The final encoding strategy balances theoretical optimality with practical hardw
|
|
|
1061
1061
|
- **Decode performance**: 5-10× faster than bit-level encoding
|
|
1062
1062
|
- **Cache behavior**: Excellent (word-aligned, sequential access)
|
|
1063
1063
|
- **SIMD compatibility**: Yes (bulk discriminator processing)
|
|
1064
|
-
- **Architecture
|
|
1064
|
+
- **Architecture-agnostic**: Optimized for x86-64 and ARM64
|
|
1065
1065
|
|
|
1066
1066
|
**Performance characteristics**:
|
|
1067
1067
|
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
## **7.1 Purpose**
|
|
4
4
|
|
|
5
5
|
The **application binary interface (ABI)** defines how partial functions in `k` are represented and invoked at runtime.
|
|
6
|
-
Its goal is to make all functions
|
|
6
|
+
Its goal is to make all functions, whether user-defined or compiled, compatible with the same calling convention and data layout.
|
|
7
7
|
|
|
8
8
|
Every compiled `k` function receives a single argument (a runtime value) and may either produce a result or remain undefined.
|
|
9
9
|
The ABI provides a uniform way to express both outcomes.
|
|
@@ -133,7 +133,7 @@ otherwise it is an identity
|
|
|
133
133
|
⟨ ?F , v ⟩ ⇓ v
|
|
134
134
|
```
|
|
135
135
|
|
|
136
|
-
Filters therefore act as compile
|
|
136
|
+
Filters therefore act as compile-time annotations for type checking only.
|
|
137
137
|
As operations they are identities at run time, unless they lead to fully typed
|
|
138
138
|
expressions (i.e., can be replaced by a type). Their pattern information may
|
|
139
139
|
still be present on the runtime value because values can carry codec patterns.
|
|
@@ -44,7 +44,7 @@ Each stage transforms one representation of the program into another, simpler on
|
|
|
44
44
|
|
|
45
45
|
A minimal compiler can be viewed as three connected modules:
|
|
46
46
|
|
|
47
|
-
* **Front end** — reads source code, builds the AST, performs type analysis.
|
|
47
|
+
* **Front end** — reads source code, builds the AST, and performs type analysis.
|
|
48
48
|
* **Middle** — resolves all references, normalizes types, and simplifies expressions.
|
|
49
49
|
* **Back end** — produces the executable form.
|
|
50
50
|
|
|
@@ -107,7 +107,7 @@ For `k`, the IR mirrors the structure of partial functions:
|
|
|
107
107
|
|
|
108
108
|
| Concept in k | IR operation |
|
|
109
109
|
| --------------------- | -------------------------------- |
|
|
110
|
-
| Projection `.x`
|
|
110
|
+
| Projection `.x` `/x` | `PROJECT label_id` |
|
|
111
111
|
| Composition `(f g)` | `CALL f; CALL g` |
|
|
112
112
|
| Union `<f,g>` | `TRY f; IF undefined THEN TRY g` |
|
|
113
113
|
| Product `{.x f, .y g}` | `PRODUCT_CALL (f, g); COMBINE` |
|
package/README.md
CHANGED
|
@@ -40,13 +40,12 @@ numbers are either `0` or one more than another natural number:
|
|
|
40
40
|
|
|
41
41
|
```k
|
|
42
42
|
$ nat = < {} 0, nat +1 >;
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
add
|
|
48
|
-
|
|
49
|
-
.y
|
|
43
|
+
0 = {} | 0 $ nat ;
|
|
44
|
+
inc = | +1 $ nat ;
|
|
45
|
+
dec = $ nat / +1 ; # undefined for '0'
|
|
46
|
+
add = $ { nat x, nat y } <
|
|
47
|
+
{ . x dec x, . y inc y } add, # defined if 'x > 0'
|
|
48
|
+
. y # else, return 'y'
|
|
50
49
|
>;
|
|
51
50
|
```
|
|
52
51
|
|
|
@@ -240,7 +239,7 @@ Still evolving:
|
|
|
240
239
|
- optimization and backend experiments
|
|
241
240
|
- larger real-world examples
|
|
242
241
|
|
|
243
|
-
## Good Areas
|
|
242
|
+
## Good Areas for Contributors
|
|
244
243
|
|
|
245
244
|
k is small enough to study, but there are several useful directions:
|
|
246
245
|
|
package/a.k
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
----- codes -----
|
|
2
|
+
$ 2aks = {VtPH "factor", Envk "i"};
|
|
3
|
+
$ 2xPW = {PHbG "r", VtPH "x"};
|
|
4
|
+
$ 33dY = {VtPH "_x", VtPH "result", VtPH "y"};
|
|
5
|
+
$ 6d2A = {NiDZ "if", nh8T "then"};
|
|
6
|
+
$ 8MHR = {VtPH "if", 9bLM "then"};
|
|
7
|
+
$ 9bLM = < NiDZ "no", NiDZ "yes" >;
|
|
8
|
+
$ CuVs = {VtPH "k", VtPH "x"};
|
|
9
|
+
$ ERx5 = {Mjus "c", VtPH "x", VtPH "y"};
|
|
10
|
+
$ Envk = {VtPH "log", Nws3 "res", VtPH "step", VtPH "x"};
|
|
11
|
+
$ EvuD = {VtPH "if", VtPH "then"};
|
|
12
|
+
$ F7RN = < NiDZ "+", NiDZ "-" >;
|
|
13
|
+
$ FL4i = {nh8T "res_wo_sign", F7RN "sign"};
|
|
14
|
+
$ H24y = {VtPH "if", Nws3 "then"};
|
|
15
|
+
$ Hj9J = {NiDZ "if", Nws3 "then"};
|
|
16
|
+
$ KxW7 = {VtPH "log", VtPH "step", VtPH "x"};
|
|
17
|
+
$ Ljf9 = {VtPH "car", PHbG "cdr"};
|
|
18
|
+
$ Mjus = < NiDZ "0", NiDZ "1" >;
|
|
19
|
+
$ NiDZ = {};
|
|
20
|
+
$ Nws3 = < VtPH "+", VtPH "-" >;
|
|
21
|
+
$ PHbG = < Ljf9 "cons", NiDZ "nil" >;
|
|
22
|
+
$ R7RD = {VtPH "x", VtPH "y"};
|
|
23
|
+
$ RLhu = {NiDZ "if", R7RD "then"};
|
|
24
|
+
$ RknV = {VtPH "if", PHbG "then"};
|
|
25
|
+
$ S5nE = {Nws3 "int", muqD "rat"};
|
|
26
|
+
$ TcsE = {muqD "x", muqD "y"};
|
|
27
|
+
$ Thzc = {VtPH "r", VtPH "x"};
|
|
28
|
+
$ UFVU = {VtPH "_x", Nws3 "k"};
|
|
29
|
+
$ Vc85 = {Nws3 "x", Nws3 "y"};
|
|
30
|
+
$ VtPH = < VtPH "0", VtPH "1", NiDZ "_" >;
|
|
31
|
+
$ WWuP = {muqD "x", Nws3 "y"};
|
|
32
|
+
$ YeUu = {VtPH "and", VtPH "if"};
|
|
33
|
+
$ YfKH = {VtPH "d", VtPH "n"};
|
|
34
|
+
$ ZLDk = {VtPH "step", VtPH "x"};
|
|
35
|
+
$ bSFt = {NiDZ "and", VtPH "if", VtPH "then"};
|
|
36
|
+
$ dXdV = {9bLM "inOrder", VtPH "x", VtPH "y"};
|
|
37
|
+
$ eYsq = {NiDZ "if", VtPH "then"};
|
|
38
|
+
$ hJHA = {R7RD "if", VtPH "then"};
|
|
39
|
+
$ hPpx = {Nws3 "if", nh8T "then"};
|
|
40
|
+
$ muqD = {Nws3 "d", Nws3 "n"};
|
|
41
|
+
$ nPgb = {Nws3 "lcm", muqD "x", muqD "y"};
|
|
42
|
+
$ nh8T = {Nws3 "div", VtPH "rem"};
|
|
43
|
+
$ nhfK = {VtPH "and", VtPH "if", VtPH "then"};
|
|
44
|
+
$ prGg = {Vc85 "fib(n),fib(n.1)", VtPH "n"};
|
|
45
|
+
$ s7Sd = {Nws3 "res", VtPH "step", VtPH "x"};
|
|
46
|
+
$ sqr8 = {VtPH "if", F7RN "then"};
|
|
47
|
+
$ uMrJ = {VtPH "if", muqD "then"};
|
|
48
|
+
$ yYPm = {VtPH "result", F7RN "sign"};
|
|
49
|
+
$ zLbj = {Nws3 "d", nh8T "div", Nws3 "n"};
|
|
50
|
+
----- rels -----
|
|
51
|
+
8jvS = ?(...) 4Uqf mx7B ?<<$NiDZ _, ...> 0, ...>;
|
|
52
|
+
TLyi = ?(...) 8jvS 7GtG $VtPH;
|
|
53
|
+
2ezj = ?(...) TLyi 7GtG $VtPH;
|
|
54
|
+
BHYs = ?(...) 2ezj 7GtG $VtPH;
|
|
55
|
+
XeYK = ?(...) BHYs 7GtG $VtPH;
|
|
56
|
+
ycSP = ?(...) XeYK 7GtG $VtPH;
|
|
57
|
+
c3fq = ?(...) ycSP 7GtG $VtPH;
|
|
58
|
+
ANDv = ?(...) c3fq 7GtG $VtPH;
|
|
59
|
+
BASZ = ?(...) ANDv 7GtG $VtPH;
|
|
60
|
+
Je6N = ?(...) BASZ 7GtG $VtPH;
|
|
61
|
+
PaZn = ?(...) Je6N 7GtG $VtPH;
|
|
62
|
+
8Wzq = ?(...) PaZn 7GtG $VtPH;
|
|
63
|
+
7gmR = ?(...) 8Wzq 7GtG $VtPH;
|
|
64
|
+
SCqj = ?<$NiDZ no, $NiDZ yes, ...> </yes |no, /no |yes> $9bLM;
|
|
65
|
+
KWT2 = ?(...) {} |yes $9bLM;
|
|
66
|
+
7RTv = ?(...) KWT2 SCqj $9bLM;
|
|
67
|
+
L8iN = ?<{(...) car, X0 cdr} cons, $NiDZ nil>=X0 () ?<{(...) car, X0 cdr} cons, $NiDZ nil>=X0;
|
|
68
|
+
tyxZ = ?(...) {} |nil ?<{(...) car, X0 cdr} cons, $NiDZ nil>=X0;
|
|
69
|
+
C9mM = ?(...) {() car, tyxZ cdr} |cons ?<{(...) car, X0 cdr} cons, $NiDZ nil>=X0;
|
|
70
|
+
w6w6 = ?{X0 car, <{X0 car, X1 cdr} cons, $NiDZ nil>=X1 cdr, ...} {.car car, .cdr cdr} |cons ?<{(...) car, X0 cdr} cons, $NiDZ nil>=X0;
|
|
71
|
+
jhbb = ?<{(...) car, X0 cdr} cons, $NiDZ nil>=X0 /nil tyxZ ?<{(...) car, X0 cdr} cons, $NiDZ nil>=X0;
|
|
72
|
+
UY5b = ?<{(...) car, X0 cdr} cons, $NiDZ nil>=X0 /cons .car ?(...);
|
|
73
|
+
S7xP = ?<{(...) car, X0 cdr} cons, $NiDZ nil>=X0 /cons .cdr ?<{(...) car, X0 cdr} cons, $NiDZ nil>=X0;
|
|
74
|
+
4Uqf = ?(...) {} |_ ?<$NiDZ _, ...>;
|
|
75
|
+
J6xt = ?(...) |1 ?<(...) 1, ...>;
|
|
76
|
+
mx7B = ?(...) |0 ?<(...) 0, ...>;
|
|
77
|
+
7GtG = $VtPH {4YaF x, 4Uqf y} JfKb $VtPH;
|
|
78
|
+
ZTFs = $VtPH </_ KWT2, 7RTv> $9bLM;
|
|
79
|
+
AkEL = $VtPH {ZTFs /yes if, () then} .then $VtPH;
|
|
80
|
+
fKyL = $R7RD {.x 4YaF x, .y y} N8w3 $VtPH;
|
|
81
|
+
N8w3 = $R7RD <{.x /0 x, .y mx7B y} N8w3, {.x /1 x, .y J6xt y} N8w3, .y> $VtPH;
|
|
82
|
+
4YaF = $VtPH {() x, 4Uqf r} NbnA $VtPH;
|
|
83
|
+
NbnA = $Thzc <{.x /0 x, .r mx7B r} NbnA, {.x /1 x, .r J6xt r} NbnA, .r> $VtPH;
|
|
84
|
+
vbhA = $R7RD {.x 4YaF x, .y 4YaF y, KWT2 inOrder} CaAi $9bLM;
|
|
85
|
+
CaAi = $dXdV <{.x /0 x, .y /0 y, .inOrder inOrder} CaAi, {.x /1 x, .y /1 y, .inOrder inOrder} CaAi, {.x /0 x, .y /1 y, .inOrder inOrder} CaAi, {.x /1 y, .y /0 x, .inOrder SCqj inOrder} CaAi, {.x </0, /1> if, .inOrder SCqj then} .then, .inOrder> $9bLM;
|
|
86
|
+
6rrB = $R7RD {vbhA /no if, () then} .then $R7RD;
|
|
87
|
+
y3WV = $R7RD 6rrB {.x y, .y x} 6rrB $R7RD;
|
|
88
|
+
fSRE = $VtPH {4YaF x, tyxZ r} 9hTs $PHbG;
|
|
89
|
+
9hTs = ?{$PHbG r, $VtPH x, ...} <{.x AkEL if, .r then} .then, {.x /0 x, {8jvS car, .r cdr} w6w6 r} 9hTs, {.x /1 x, {TLyi car, .r cdr} w6w6 r} 9hTs> $PHbG;
|
|
90
|
+
Ywaf = ?<{<(...) 0, (...) 1, ...> car, X0 cdr} cons, $NiDZ nil>=X0 <S7xP <{UY5b /0 if, S7xP Ywaf mx7B then} .then, {UY5b /1 if, S7xP Ywaf J6xt then} .then>, 4Uqf> $VtPH;
|
|
91
|
+
Prn6 = ?<X0 0, ...>=X0 </0 Prn6, ()> ?<X0 0, ...>=X0;
|
|
92
|
+
xX22 = $VtPH Prn6 <AkEL 8jvS, ()> $VtPH;
|
|
93
|
+
Phke = $VtPH xX22 |+ $Nws3;
|
|
94
|
+
xdAf = $Nws3 </+, /-> $VtPH;
|
|
95
|
+
JyBD = $Nws3 {7sp2 /+ if, () then} .then $Nws3;
|
|
96
|
+
BzLM = $Nws3 {/- if, () then} .then $Nws3;
|
|
97
|
+
NxZn = $Nws3 {/+ /0 AkEL if, () then} .then $Nws3;
|
|
98
|
+
NKnM = $Nws3 {/- /1 AkEL if, () then} .then $Nws3;
|
|
99
|
+
6VKZ = $Nws3 </+ {4YaF x, 4Uqf y} JfKb |+, NKnM 8jvS Phke, /- {4YaF x, 4Uqf y} 9Cyq |-> $Nws3;
|
|
100
|
+
JfKb = $R7RD <{.x /0 x, .y J6xt y} N8w3, {.x /1 x, .y mx7B y} JfKb, .y J6xt> $VtPH;
|
|
101
|
+
7sp2 = $Nws3 <NxZn TLyi |-, /+ {4YaF x, 4Uqf y} 9Cyq |+, /- {4YaF x, 4Uqf y} JfKb |-> $Nws3;
|
|
102
|
+
9Cyq = ?{$VtPH x, $VtPH y, ...} <{.x /1 x, .y mx7B y} N8w3, {.x /0 x, .y J6xt y} 9Cyq> $VtPH;
|
|
103
|
+
vvzA = ?(...) {} |0 $Mjus;
|
|
104
|
+
nXAf = ?(...) {} |1 $Mjus;
|
|
105
|
+
APnt = $Vc85 <{.x /+ 4YaF x, .y /+ 4YaF y, vvzA c} 73G3 4YaF xX22 |+, {.x /- 4YaF x, .y /- 4YaF y, vvzA c} 73G3 4YaF xX22 |-, {.y /- x, .x /+ y} 6rrB {.x 4YaF x, .y 4YaF y, vvzA c} 3kn7 4YaF xX22 |-, {.x /+ 4YaF x, .y /- 4YaF y, vvzA c} 3kn7 4YaF xX22 |+, {.x /- x, .y /+ y} 6rrB {.x 4YaF x, .y 4YaF y, vvzA c} 3kn7 4YaF xX22 |-, {.y /+ 4YaF x, .x /- 4YaF y, vvzA c} 3kn7 4YaF xX22 |+> $Nws3;
|
|
106
|
+
4ZfX = $Nws3 <NxZn, /+ |-, /- |+> $Nws3;
|
|
107
|
+
kBAy = $Vc85 {.x x, .y 4ZfX y} APnt $Nws3;
|
|
108
|
+
73G3 = $ERx5 <{.x AkEL if, .y AkEL and, <.c /0 8jvS, TLyi> then} .then, {.x AkEL if, .c /0 and, .y then} .then, {.x y, .y AkEL x, .c c} 73G3, {<.c /0 8jvS, TLyi> x, .y y, .x AkEL vvzA c} 73G3, {.x /0 x, .y /0 y, .c /0 vvzA c} 73G3 mx7B, {.x /0 x, .y /0 y, .c /1 vvzA c} 73G3 J6xt, {.x /0 x, .y /1 y, .c /0 vvzA c} 73G3 J6xt, {.x /0 x, .y /1 y, .c /1 nXAf c} 73G3 mx7B, {.x /1 x, .y /0 y, .c /0 vvzA c} 73G3 J6xt, {.x /1 x, .y /0 y, .c /1 nXAf c} 73G3 mx7B, {.x /1 x, .y /1 y, .c /0 nXAf c} 73G3 mx7B, {.x /1 x, .y /1 y, .c /1 nXAf c} 73G3 J6xt> $VtPH;
|
|
109
|
+
3kn7 = $ERx5 <{.x then, .y AkEL if, .c /0 and} .then, {.x /1 then, .y AkEL if, .c /1 and} .then mx7B, {.x /0 x, .y AkEL y, .c /1 nXAf c} 3kn7 J6xt, {.x /0 x, .y /0 y, .c /0 vvzA c} 3kn7 mx7B, {.x /0 x, .y /0 y, .c /1 nXAf c} 3kn7 J6xt, {.x /0 x, .y /1 y, .c /0 nXAf c} 3kn7 J6xt, {.x /0 x, .y /1 y, .c /1 nXAf c} 3kn7 mx7B, {.x /1 x, .y /0 y, .c /0 vvzA c} 3kn7 J6xt, {.x /1 x, .y /0 y, .c /1 vvzA c} 3kn7 mx7B, {.x /1 x, .y /1 y, .c /0 vvzA c} 3kn7 mx7B, {.x /1 x, .y /1 y, .c /1 nXAf c} 3kn7 J6xt> $VtPH;
|
|
110
|
+
NiRD = ?(...) {} |+ $F7RN;
|
|
111
|
+
u9uY = ?(...) {} |- $F7RN;
|
|
112
|
+
JEEj = $Nws3 </+ NiRD, /- u9uY> $F7RN;
|
|
113
|
+
3PYm = $Vc85 {<{.x /+ if, .y /+ and} NiRD, {.x /- if, .y /- and} NiRD, u9uY> sign, {8jvS result, .x xdAf 4YaF _x, .y xdAf y} 9bpS result} <{.sign /+ if, .result 4YaF Phke then} .then, {.sign /- if, .result 4YaF Phke 4ZfX then} .then> $Nws3;
|
|
114
|
+
9bpS = $33dY <{.y AkEL if, .result then} .then, {.result mx7B result, ._x _x, .y /0 y} 9bpS, {{._x x, .result mx7B y, vvzA c} 73G3 result, ._x _x, .y /1 y} 9bpS> $VtPH;
|
|
115
|
+
TPzm = $R7RD _log! <{.y x, .x y} 6rrB .y, {{{.x Phke x, .y Phke y} kBAy /+ x, .y 4YaF mx7B 4YaF y} TPzm x, .y y} TPzm> $VtPH;
|
|
116
|
+
Vs6b = $R7RD <{.y x, .x y} 6rrB, ()> Ah93 $VtPH;
|
|
117
|
+
Ah93 = $R7RD <{.y /0 if, .x then} .then, {.y x, TPzm y} Ah93> $VtPH;
|
|
118
|
+
nB9M = $ZLDk {.x x, .step step, 8jvS log} 5ivr $VtPH;
|
|
119
|
+
5ivr = ?{$VtPH log, $VtPH step, $VtPH x, ...} <{{.step x, .x y} 6rrB if, .log then} .then, {.x x, {.step x, 8jvS y} 6rrB {.x x, 2ezj y} {.x Phke x, .y Phke y} 3PYm xdAf step, .log Phke 6VKZ xdAf log} 5ivr> $VtPH;
|
|
120
|
+
MTKW = $CuVs {.x 4YaF _x, .k Phke k} au4T 4YaF $VtPH;
|
|
121
|
+
au4T = ?{<X0 0, ...>=X0 _x, $Nws3 k, ...} <{.k NxZn if, ._x then} .then, {._x mx7B _x, .k 7sp2 k} au4T> ?<X0 0, ...>=X0;
|
|
122
|
+
Bs2e = $Vc85 {<{.x /+ if, .y JEEj then} .then, {.x /- if, .y 4ZfX JEEj then} .then> sign, {.x xdAf x, .y xdAf step, 8jvS Phke res} 8Dwz res_wo_sign} <{.sign /+ if, {.res_wo_sign .div div, .res_wo_sign .rem rem} then} .then, {.res_wo_sign .div 4ZfX div, .res_wo_sign .rem rem}> $nh8T;
|
|
123
|
+
8Dwz = ?{$Nws3 res, $VtPH step, $VtPH x, ...} {.x x, .step step, .res res, {.x x, .step step} nB9M log} <{.log Phke NxZn if, {.res div, .x rem} then} .then, {{TLyi x, .log Phke 7sp2 xdAf k} MTKW factor, () i} {{.i .x Phke x, {.i .step Phke x, .factor Phke y} 3PYm y} kBAy xdAf x, .i .step step, {.i .res x, .factor Phke y} APnt res} 8Dwz> $nh8T;
|
|
124
|
+
GSLa = $R7RD {{.x Phke x, .y Phke y} 3PYm x, Vs6b Phke y} Bs2e .div xdAf $VtPH;
|
|
125
|
+
FaWv = $Nws3 <NxZn TLyi Phke, /+ Phke {7sp2 FaWv x, () y} 3PYm> $Nws3;
|
|
126
|
+
HZYv = ?{$Vc85"fib(n),fib(n.1)", $VtPH n, ...} <{.n Phke 7sp2 /+ n, ."fib(n),fib(n.1)" {APnt x, .x y}"fib(n),fib(n.1)"} HZYv, ."fib(n),fib(n.1)"> $Vc85;
|
|
127
|
+
aUJp = $VtPH {() n, {TLyi Phke x, 8jvS Phke y}"fib(n),fib(n.1)"} HZYv .x $Nws3;
|
|
128
|
+
sghj = $YfKH {.n Phke n, .d Phke d} 2zFi $muqD;
|
|
129
|
+
2zFi = $muqD <{.d /- if, {.n 4ZfX n, .d 4ZfX d} then} .then, ()> {() x, {.n xdAf x, .d xdAf y} Vs6b Phke y} {{.x .n x, .y y} Bs2e .div n, {.x .d x, .y y} Bs2e .div d} $muqD;
|
|
130
|
+
Jn92 = $TcsE {.x x, .y y, {.x .d xdAf x, .y .d xdAf y} GSLa Phke lcm} {{{.x .n x, {.lcm x, .x .d y} Bs2e .div y} 3PYm x, {.y .n x, {.lcm x, .y .d y} Bs2e .div y} 3PYm y} APnt n, .lcm d} 2zFi $muqD;
|
|
131
|
+
2Uyd = $TcsE {{.x .n x, .y .n y} 3PYm n, {.x .d x, .y .d y} 3PYm d} 2zFi $muqD;
|
|
132
|
+
uRTk = $TcsE {.x x, .y {.n 4ZfX n, .d d} y} Jn92 $muqD;
|
|
133
|
+
FtdC = $muqD {.d n, .n d} 2zFi $muqD;
|
|
134
|
+
MH4q = $TcsE {.x x, .y FtdC y} 2Uyd $muqD;
|
|
135
|
+
8jRf = $muqD {.n n, .d d, {.n x, .d y} Bs2e div} {.div .div int, {.div .rem Phke n, .d d} rat} $S5nE;
|
|
136
|
+
|
|
137
|
+
()
|
|
138
|
+
|