@fraczak/k 5.1.2 → 6.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/Code-derivation-tests/blowup.mjs +3 -3
- package/Code-derivation-tests/bnat.mjs +2 -2
- package/Code-derivation-tests/codec-witness-pattern.mjs +50 -0
- package/Code-derivation-tests/convergence-strategy.mjs +24 -0
- package/Code-derivation-tests/dot.mjs +12 -9
- package/Code-derivation-tests/filter-syntax.mjs +21 -0
- package/Code-derivation-tests/loop.mjs +8 -2
- package/Code-derivation-tests/nat.mjs +1 -1
- package/Code-derivation-tests/not-not.mjs +3 -3
- package/Code-derivation-tests/pattern-envelope.mjs +68 -0
- package/Code-derivation-tests/rlz.mjs +2 -2
- package/{BIG_PICTURE.md → DOCS/BIG_PICTURE.md} +103 -50
- package/DOCS/REPL.md +191 -0
- package/DOCS/book/01-introduction.md +51 -0
- package/DOCS/book/02-syntax-and-values.md +111 -0
- package/DOCS/book/03-types-as-finite-automata.md +156 -0
- package/DOCS/book/04-partial-functions-and-composition.md +146 -0
- package/DOCS/book/05-typing-filters-and-normalization.md +111 -0
- package/DOCS/book.md +54 -0
- package/README.md +204 -508
- package/TypeGraph.mjs +43 -0
- package/TypePatternGraph.mjs +251 -0
- package/Value.mjs +192 -0
- package/augmentation.mjs +198 -0
- package/codecs/BINARY_FORMAT.md +53 -0
- package/codecs/POLYMORPHIC_BINARY_FORMAT.md +303 -0
- package/codecs/README.md +215 -0
- package/codecs/example-pipeline.mjs +149 -0
- package/codecs/ieee.mjs +140 -0
- package/codecs/int.mjs +104 -0
- package/codecs/json-codec.mjs +215 -0
- package/codecs/json.mjs +40 -0
- package/codecs/k-parse.mjs +90 -0
- package/codecs/k-pattern.mjs +75 -0
- package/codecs/k-print.mjs +46 -0
- package/codecs/runtime/codec.mjs +768 -0
- package/codecs/runtime/ieee-pattern.mjs +30 -0
- package/codecs/runtime/pattern-json.mjs +96 -0
- package/codecs/runtime/prefix-codec.mjs +425 -0
- package/codecs/runtime/show-value.mjs +68 -0
- package/codecs/runtime/typeFromValue.mjs +34 -0
- package/codecs/runtime/unicode-string.mjs +220 -0
- package/codecs/show.mjs +41 -0
- package/codecs/string-codec.mjs +80 -0
- package/codecs/unit.mjs +51 -0
- package/codecs/utf16.mjs +66 -0
- package/codecs/utf8.mjs +37 -0
- package/codes.mjs +146 -44
- package/compiler.mjs +111 -0
- package/convergence.mjs +197 -0
- package/core.k +197 -0
- package/export.mjs +22 -32
- package/hash.mjs +29 -31
- package/index.mjs +41 -15
- package/k-vscode/.vscode/launch.json +14 -0
- package/k-vscode/README.md +65 -0
- package/k-vscode/language-configuration.json +42 -0
- package/k-vscode/package.json +57 -0
- package/k-vscode/snippets/k.json +92 -0
- package/k-vscode/syntaxes/k.tmLanguage.json +243 -0
- package/k.mjs +56 -54
- package/object.mjs +517 -0
- package/objects/README.md +38 -0
- package/objects/compile-lib.mjs +42 -0
- package/objects/compile.mjs +42 -0
- package/objects/decompile.mjs +32 -0
- package/package.json +18 -8
- package/parser.jison +27 -46
- package/parser.mjs +68 -100
- package/pretty.mjs +21 -40
- package/repl2.mjs +652 -0
- package/run.mjs +264 -231
- package/symbol-table.mjs +3 -17
- package/test-fingerprint.mjs +7 -7
- package/test-hash-fuzz.mjs +107 -0
- package/test-hash-normalization.mjs +134 -0
- package/test-k-object.mjs +74 -0
- package/test-repl2.mjs +95 -0
- package/test.mjs +45 -70
- package/tests.sh +10 -1
- package/typing.mjs +129 -676
- package/unification.mjs +157 -0
- package/valueIO.mjs +54 -0
- package/valueParser.jison +143 -0
- package/valueParser.mjs +713 -0
- package/Code-derivation-tests/built-in.mjs +0 -19
- package/Code-derivation-tests/vector.mjs +0 -19
- package/Filters/bug.k +0 -3
- package/Filters/composing.k +0 -15
- package/Filters/filter.k +0 -9
- package/TODO.md +0 -53
- package/TYPING.md +0 -365
- package/k-repl.cast +0 -1913
- package/k.cast +0 -618
- package/my-objects.json +0 -6
- package/patterns.mjs +0 -543
- package/repl.mjs +0 -213
- package/test-filters.mjs +0 -45
- package/typing-test.mjs +0 -40
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
# Chapter 5 — Typing, Filters, and Normalization
|
|
2
|
+
|
|
3
|
+
## **5.1 Types as functions**
|
|
4
|
+
|
|
5
|
+
A type expression in `k` can appear wherever a function is expected.
|
|
6
|
+
When used this way, it is prefixed by `$`.
|
|
7
|
+
It then behaves as a partial **identity** function that is defined only for values of that type.
|
|
8
|
+
For example, `$ bool` is a partial identity: it returns its argument unchanged when the argument is of type `bool`, and it is undefined otherwise.
|
|
9
|
+
|
|
10
|
+
This convention eliminates any special syntax for annotating sub-expressions with types.
|
|
11
|
+
An expression may be *restricted* to a type simply by composing it with the corresponding type expression.
|
|
12
|
+
|
|
13
|
+
---
|
|
14
|
+
|
|
15
|
+
## **5.2 Filters**
|
|
16
|
+
|
|
17
|
+
A **filter** is a syntactic form that denotes a *class of types* — a set of types that share a common structure.
|
|
18
|
+
|
|
19
|
+
Filter expressions are introduced by `?` and can be:
|
|
20
|
+
|
|
21
|
+
- **Type expressions** — `? $bool`, `? $< {} x, bool y >`
|
|
22
|
+
- **Product filters** — `? { Filter1 field1, Filter2 field2 }`
|
|
23
|
+
- **Union filters** — `? < Filter1 tag1, Filter2 tag2 >`
|
|
24
|
+
- **Any-type filter** — `? ( ... )`
|
|
25
|
+
- **Filter variables** — `? X` (metavariables representing unknown types)
|
|
26
|
+
- **Filter bindings** — `? < X f, (...) = Y g, ... > = X`
|
|
27
|
+
|
|
28
|
+
Filters may contain `...` to indicate that additional fields or tags are allowed:
|
|
29
|
+
`? { Filter1 field1, ... }` matches any product with at least `field1`.
|
|
30
|
+
|
|
31
|
+
The filter `? (...)` matches any type.
|
|
32
|
+
The filter `? {...}` matches any product type.
|
|
33
|
+
The filter `? <...>` matches any union type.
|
|
34
|
+
There is no closed unknown filter: use `?{}` for the empty product or `?<>` for the empty union.
|
|
35
|
+
|
|
36
|
+
---
|
|
37
|
+
|
|
38
|
+
## **5.3 Examples of filters**
|
|
39
|
+
|
|
40
|
+
- `?(...)` — represents any type.
|
|
41
|
+
- `?< (...) f, (...) g >` — represents all union types having exactly two variants `f` and `g`.
|
|
42
|
+
- `?{ X f, X g }` — represents all product types with two fields `f` and `g`, both of the same type.
|
|
43
|
+
|
|
44
|
+
A filter constrains where a partial function is defined; it does not affect the operational behavior of the function once defined.
|
|
45
|
+
|
|
46
|
+
---
|
|
47
|
+
|
|
48
|
+
## **5.4 Recursive filters**
|
|
49
|
+
|
|
50
|
+
Filters may be recursive.
|
|
51
|
+
They can describe families of recursive types by defining a metavariable in terms of a filter that references it.
|
|
52
|
+
|
|
53
|
+
Example (list definition):
|
|
54
|
+
|
|
55
|
+
```k-lang
|
|
56
|
+
?< {} nil, {X car, Y cdr} cons > = Y
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
This filter states that `Y` is a union type with two variants: `nil` and `cons`.
|
|
60
|
+
|
|
61
|
+
- The `nil` variant holds an empty product `{}`.
|
|
62
|
+
- The `cons` variant is a product with two fields: `car` of some type `X`, and `cdr` of type `Y` itself.
|
|
63
|
+
|
|
64
|
+
This recursive structure defines a linked list where each element has a `car` (the value) and a `cdr` (the rest of the list).
|
|
65
|
+
It thus denotes lists of elements of type `X`.
|
|
66
|
+
|
|
67
|
+
---
|
|
68
|
+
|
|
69
|
+
## **5.5 Type variables and scope**
|
|
70
|
+
|
|
71
|
+
A type variable (an identifier, typically starting with an uppercase letter) introduced in a filter is visible within the enclosing function definition.
|
|
72
|
+
For example:
|
|
73
|
+
|
|
74
|
+
```k-lang
|
|
75
|
+
car = ?< {} nil, {X car, Y cdr} cons > = Y /cons .car ?X;
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
Here:
|
|
79
|
+
|
|
80
|
+
- `X` and `Y` are type variables.
|
|
81
|
+
- The filter `?<{}nil,{X car,Y cdr}cons>=Y` constrains the function `car` to be defined only on types that match the recursive list structure.
|
|
82
|
+
- The expression `/cons` selects the `cons` variant of the union.
|
|
83
|
+
- The expression `.car` accesses the `car` field of a `cons` variant.
|
|
84
|
+
- The final filter `?X` asserts that the result of the field access is of type `X`, the type of the list's elements.
|
|
85
|
+
|
|
86
|
+
---
|
|
87
|
+
|
|
88
|
+
## **5.6 Type inference and normalization**
|
|
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 abd normalization.
|
|
91
|
+
|
|
92
|
+
Type inference and normalization proceeds as follows:
|
|
93
|
+
|
|
94
|
+
1. Build a graph of all type references appearing explicitly and implicitly in the program.
|
|
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-discovered types to the graph.
|
|
97
|
+
4. Repeat until a fixed point is reached and no more changes occur.
|
|
98
|
+
5. Compute canonical forms for all newly introduced types.
|
|
99
|
+
|
|
100
|
+
---
|
|
101
|
+
|
|
102
|
+
## **5.7 Summary**
|
|
103
|
+
|
|
104
|
+
- Type expressions act as identity functions defined on values of that type.
|
|
105
|
+
- Filters describe sets of types and can constrain where a function is defined.
|
|
106
|
+
- Filters may be products, unions, or the unconstrained any-type form, and can be recursive.
|
|
107
|
+
- Variables in filters have definition-level scope.
|
|
108
|
+
- Normalization computes explicit input/output filters for every expression.
|
|
109
|
+
- Filters are used in the normalization and type checking process. They are ignored at runtime.
|
|
110
|
+
|
|
111
|
+
---
|
package/DOCS/book.md
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "k-language"
|
|
3
|
+
author: "W. Fraczak"
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
<div align="center">
|
|
7
|
+
|
|
8
|
+
```
|
|
9
|
+
{ } { }
|
|
10
|
+
/ \ {.lang name} |
|
|
11
|
+
lang by --> name
|
|
12
|
+
| | |
|
|
13
|
+
'k' 'wojtek' 'k'
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
</div>
|
|
17
|
+
|
|
18
|
+
# The k-language Book
|
|
19
|
+
|
|
20
|
+
This book describes the k programming language - a minimal functional language for transforming tree-structured data using partial functions and algebraic data types.
|
|
21
|
+
|
|
22
|
+
## Table of Contents
|
|
23
|
+
|
|
24
|
+
1. [Introduction](book/01-introduction.md)
|
|
25
|
+
2. [Syntax and Values](book/02-syntax-and-values.md)
|
|
26
|
+
3. [Types as Finite Automata](book/03-types-as-finite-automata.md)
|
|
27
|
+
4. [Partial Functions and Composition](book/04-partial-functions-and-composition.md)
|
|
28
|
+
5. [Typing, Filters, and Normalization](book/05-typing-filters-and-normalization.md)
|
|
29
|
+
6. [Values in Memory](book/06-values-in-memory.md)
|
|
30
|
+
7. [The Partial Function ABI](book/07-the-partial-function-abi.md)
|
|
31
|
+
8. [Operational Semantics and Execution](book/08-operational-semantics-and-execution.md)
|
|
32
|
+
9. [Compiler Architecture](book/09-compiler-architecture.md)
|
|
33
|
+
10. [From AST to Intermediate Representation](book/10-from-ast-to-intermediate-representation.md)
|
|
34
|
+
11. [LLVM Basics](book/11-llvm-basics.md)
|
|
35
|
+
12. [Code Generation](book/12-code-generation.md)
|
|
36
|
+
13. [Linking and Execution](book/13-linking-and-execution.md)
|
|
37
|
+
14. [Canonical Serialization](book/14-canonical-serialization.md)
|
|
38
|
+
15. [Optimization and Folding](book/15-optimization-and-folding.md)
|
|
39
|
+
16. [Toward a Universal Schema Registry](book/16-toward-a-universal-schema-registry.md)
|
|
40
|
+
|
|
41
|
+
## Appendices
|
|
42
|
+
|
|
43
|
+
- [Appendix A: Implementing a Prototype Compiler in Python](book/appendix-a-implementing-a-prototype-compiler-in-python.md)
|
|
44
|
+
- [Appendix B: Incorporating External Predefined Types and Functions](book/appendix-b-incorporating-external-predefined-types-and-functions.md)
|
|
45
|
+
|
|
46
|
+
---
|
|
47
|
+
|
|
48
|
+
*Note: This is the master file. Individual chapters are stored in the `book/` directory for easier editing and maintenance. Each chapter can be edited independently and the changes will be reflected when the individual files are viewed.*
|
|
49
|
+
|
|
50
|
+
## Usage
|
|
51
|
+
|
|
52
|
+
- **For reading**: Click on any chapter link above to read individual chapters
|
|
53
|
+
- **For editing**: Edit the individual chapter files in the `book/` directory
|
|
54
|
+
- **For publishing**: Combine chapters as needed for different output formats
|