@fraczak/k 8.4.0 → 8.5.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.
Files changed (113) hide show
  1. package/CHANGELOG.md +59 -0
  2. package/DOCS/ARCHITECTURE.md +83 -0
  3. package/DOCS/CODECS.md +93 -204
  4. package/DOCS/DICTIONARY.md +42 -1241
  5. package/DOCS/FILE_FORMATS.md +170 -625
  6. package/DOCS/GETTING_STARTED.md +164 -0
  7. package/DOCS/KIR_V1.md +22 -33
  8. package/DOCS/KVM_EXECUTION_MODEL.md +88 -481
  9. package/DOCS/PATTERNS.md +156 -123
  10. package/DOCS/POLYMORPHIC_IR.md +83 -101
  11. package/DOCS/PROFILING_AND_TRACING.md +52 -166
  12. package/DOCS/README.md +35 -0
  13. package/DOCS/REPL.md +100 -371
  14. package/DOCS/TEXTUAL_VALUES.md +53 -81
  15. package/DOCS/TYPE_DERIVATION.md +95 -234
  16. package/DOCS/book-combined.md +3786 -0
  17. package/Examples/arithmetics.k +45 -59
  18. package/Examples/djson.k +102 -112
  19. package/Examples/karatsuba-mult.k +2 -3
  20. package/Examples/list.k +6 -7
  21. package/Examples/nat.k +6 -6
  22. package/Examples/poly.k +45 -45
  23. package/LICENSE +15 -0
  24. package/README.md +72 -410
  25. package/backends/llvm/README.md +75 -97
  26. package/backends/llvm/src/cli.mjs +0 -31
  27. package/backends/wasm/README.md +88 -66
  28. package/backends/wasm/src/cli.mjs +0 -31
  29. package/canonical-pattern.mjs +69 -35
  30. package/codecs/README.md +23 -261
  31. package/codecs/WIRE_FORMAT.md +132 -0
  32. package/core.k +5 -15
  33. package/index.mjs +5 -1
  34. package/named-patterns.mjs +55 -7
  35. package/object.mjs +28 -6
  36. package/objects/README.md +24 -109
  37. package/package.json +8 -3
  38. package/paper/Makefile +17 -0
  39. package/paper/k-paper.pdf +0 -0
  40. package/paper/k-paper.tex +435 -0
  41. package/parser.jison +0 -10
  42. package/parser.mjs +32 -43
  43. package/repl.mjs +79 -101
  44. package/scripts/benchmark-idempotency.mjs +3 -3
  45. package/scripts/run-tests.mjs +1 -1
  46. package/scripts/test-package.mjs +64 -0
  47. package/tests/README.md +66 -46
  48. package/tests/code-derivation/core.mjs +61 -0
  49. package/tests/helpers/cli.mjs +45 -0
  50. package/tests/integration.sh +2 -3
  51. package/tests/test-arithmetics.mjs +47 -237
  52. package/tests/test-djson-methods.mjs +71 -275
  53. package/tests/test-hash-normalization.mjs +22 -1
  54. package/tests/test-karatsuba.mjs +18 -126
  55. package/tests/test-named-patterns.mjs +174 -606
  56. package/tests/test-paper-examples.mjs +90 -0
  57. package/tests/test-poly.mjs +2 -2
  58. package/tests/test-repl-html.mjs +2 -2
  59. package/tests/test-repl.mjs +89 -38
  60. package/tests/test.mjs +27 -68
  61. package/AGENTS.md +0 -35
  62. package/CLAUDE.md +0 -64
  63. package/DOCS/BIG_PICTURE.md +0 -439
  64. package/DOCS/BINARY_CODEC_STATUS_AND_TODO.md +0 -74
  65. package/DOCS/CANONICAL_PATTERNS.md +0 -349
  66. package/DOCS/CODEC_ARCHITECTURE.md +0 -257
  67. package/DOCS/CONVERGENCE.md +0 -123
  68. package/DOCS/DIVERGENCE_DETECTION.md +0 -23
  69. package/DOCS/JSON_APPROACHES_COMPARISON.md +0 -302
  70. package/DOCS/KIR_SPECIALIZATION_DESIGN.md +0 -472
  71. package/DOCS/Makefile +0 -99
  72. package/DOCS/NAMED_PATTERNS.md +0 -340
  73. package/DOCS/OBJECT_FILE_AND_PATTERN.md +0 -343
  74. package/DOCS/PATTERNS_AND_TYPED_VALUES.md +0 -665
  75. package/DOCS/WEB_REPL_REQUIREMENTS.md +0 -136
  76. package/DOCS/book/01-introduction.md +0 -48
  77. package/DOCS/book/02-syntax-and-values.md +0 -110
  78. package/DOCS/book/03-types-as-finite-automata.md +0 -155
  79. package/DOCS/book/04-partial-functions-and-composition.md +0 -145
  80. package/DOCS/book/05-typing-filters-and-normalization.md +0 -109
  81. package/DOCS/book/06-values-in-memory.md +0 -1085
  82. package/DOCS/book/07-the-partial-function-abi.md +0 -143
  83. package/DOCS/book/08-operational-semantics-and-execution.md +0 -196
  84. package/DOCS/book/09-compiler-architecture.md +0 -160
  85. package/DOCS/book/10-from-ast-to-intermediate-representation.md +0 -166
  86. package/DOCS/book/14-canonical-serialization.md +0 -176
  87. package/DOCS/book/appendix-b-incorporating-external-predefined-types-and-functions.md +0 -156
  88. package/DOCS/book/overview.txt +0 -68
  89. package/DOCS/book.md +0 -50
  90. package/Examples/bnat.k +0 -308
  91. package/Examples/buda.k +0 -66
  92. package/Examples/byte.k +0 -35
  93. package/Examples/code.k +0 -43
  94. package/Examples/vhdl_to_k.k +0 -67
  95. package/JOURNAL.md +0 -288
  96. package/TODO.md +0 -107
  97. package/backends/wasm/DOCS/USE-WEB-ASSEMBLY.md +0 -233
  98. package/backends/wasm/TECHNICAL-DEBT.md +0 -58
  99. package/backends/wasm/demo_ieee.mjs +0 -318
  100. package/browser/vfs-data.mjs +0 -21
  101. package/codecs/BINARY_FORMAT.md +0 -53
  102. package/codecs/POLYMORPHIC_BINARY_FORMAT.md +0 -303
  103. package/codecs/pandoc.css +0 -71
  104. package/tests/code-derivation/blowup.mjs +0 -25
  105. package/tests/code-derivation/bnat.mjs +0 -11
  106. package/tests/code-derivation/dot.mjs +0 -37
  107. package/tests/code-derivation/index.mjs +0 -20
  108. package/tests/code-derivation/loop.mjs +0 -17
  109. package/tests/code-derivation/nat.mjs +0 -10
  110. package/tests/code-derivation/not-not.mjs +0 -14
  111. package/tests/code-derivation/product.mjs +0 -22
  112. package/tests/code-derivation/rlz.mjs +0 -13
  113. package/tests/test-fingerprint.mjs +0 -38
@@ -1,109 +0,0 @@
1
- # Chapter 5 — Schemas, Filters, and Contracts
2
-
3
- ## **5.1 Filters as partial identity relations**
4
-
5
- Filters in `k` are introduced by `?` and appear wherever an expression is expected.
6
- A filter behaves as a partial **identity** relation: it returns its payload unchanged when the payload matches the schema described by the filter, and it is undefined otherwise.
7
- For example, `? < {} true, {} false >` is a partial identity: it returns its payload unchanged when the payload is a boolean variant, and it is undefined otherwise.
8
-
9
- Beyond serving as runtime guards, filters serve as **inductive termination witnesses** that allow contract derivation for recursive relations to converge into finite-state automata.
10
-
11
- ---
12
-
13
- ## **5.2 Filters and Schemas**
14
-
15
- A **filter** is a syntactic form that denotes a *schema graph* — a structural specification of payloads that share a common shape.
16
-
17
- Filter expressions are introduced by `?` and can be:
18
-
19
- - **Product filters** — `? { Filter1 field1, Filter2 field2 }`
20
- - **Union filters** — `? < Filter1 tag1, Filter2 tag2 >`
21
- - **Any-schema filter** — `? ( ... )`
22
- - **Schema variables** — `? X` (local metavariables representing schema graph nodes)
23
- - **Schema bindings** — `? < X f, (...) = Y g, ... > = X`
24
-
25
- Filters may contain `...` to indicate that additional fields or tags are allowed:
26
- `? { Filter1 field1, ... }` matches any product with at least `field1`.
27
-
28
- The filter `? (...)` matches any schema.
29
- The filter `? {...}` matches any product schema.
30
- The filter `? <...>` matches any union schema.
31
- There is no closed unknown filter: use `?{}` for the empty product or `?<>` for the empty union.
32
-
33
- ---
34
-
35
- ## **5.3 Examples of filters**
36
-
37
- - `?(...)` — represents any schema.
38
- - `?< (...) f, (...) g >` — represents all union schemas having exactly two variants `f` and `g`.
39
- - `?{ X f, X g }` — represents all product schemas with two fields `f` and `g`, both of the same schema.
40
-
41
- A filter constrains where a relation is defined; it does not alter the output payload once defined.
42
-
43
- ---
44
-
45
- ## **5.4 Recursive schemas and filters**
46
-
47
- Filters may be recursive.
48
- They can describe families of recursive schemas by defining a metavariable in terms of a filter that references it.
49
-
50
- Example (list definition):
51
-
52
- ```k-lang
53
- ?< {} nil, {X car, Y cdr} cons > = Y
54
- ```
55
-
56
- This filter states that `Y` is a union schema with two variants: `nil` and `cons`.
57
-
58
- - The `nil` variant holds an empty product `{}`.
59
- - The `cons` variant is a product with two fields: `car` of some schema `X`, and `cdr` of schema `Y` itself.
60
-
61
- This recursive structure defines a linked list where each element has a `car` (the payload) and a `cdr` (the rest of the list).
62
- It thus denotes lists of elements of schema `X`.
63
-
64
- ---
65
-
66
- ## **5.5 Schema variables and scope**
67
-
68
- A schema variable (an identifier, typically starting with an uppercase letter) introduced in a filter is visible within the enclosing relation definition.
69
- For example:
70
-
71
- ```k-lang
72
- car = ?< {} nil, {X car, Y cdr} cons > = Y /cons .car ?X;
73
- ```
74
-
75
- Here:
76
-
77
- - `X` and `Y` are schema variables.
78
- - The filter `?<{}nil,{X car,Y cdr}cons>=Y` constrains the relation `car` to be defined only on inputs that match the recursive list structure.
79
- - The expression `/cons` asserts the `cons` variant of the union.
80
- - The expression `.car` accesses the `car` field of a `cons` variant.
81
- - The final filter `?X` asserts that the result of the field access is of schema `X`, the schema of the list's elements.
82
-
83
- ---
84
-
85
- ## **5.6 Contract derivation and normalization**
86
-
87
- Every `k` relation is analyzed by the compiler to derive its **contract**: a connected $(input, output)$ schema graph with shared vertices ($=X$) proving that input substructures are preserved through to the output.
88
-
89
- Contract derivation and normalization proceed as follows:
90
-
91
- 1. Build a schema graph from explicit filters and primitive operations (projections, constructors, products).
92
- 2. Propagate schemas forward and backward through compositions, products, and choices.
93
- 3. Validate inductive convergence for recursive relations using filter witnesses.
94
- 4. Replace filters that match a single, concrete schema with that closed schema, adding any newly discovered schemas to the registry.
95
- 5. Repeat until a fixed point is reached.
96
- 6. Compute canonical forms and content-addressed hashes for all discovered schemas.
97
-
98
- ---
99
-
100
- ## **5.7 Summary**
101
-
102
- - Filter expressions act as partial identity relations defined on payloads conforming to a schema.
103
- - Filters describe schema constraints and inductive termination witnesses for recursive relations.
104
- - Filters may be products, unions, or the unconstrained any-schema form, and can be recursive.
105
- - Variables in filters have definition-level scope.
106
- - Contract derivation computes an interconnected $(input, output)$ schema graph for every relation.
107
- - The canonical form of a schema provides a universal content-addressable identity in the schema registry.
108
-
109
- ---