@fraczak/k 8.4.1 → 8.5.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.
Files changed (116) hide show
  1. package/CHANGELOG.md +32 -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 +155 -138
  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 -377
  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/kvm2llvm.mjs +5 -4
  27. package/backends/llvm/src/llvm.mjs +2 -1
  28. package/backends/llvm/tests/integration.sh +7 -0
  29. package/backends/llvm/tests/test-llvm.mjs +20 -0
  30. package/backends/wasm/README.md +88 -66
  31. package/backends/wasm/src/kvm2wasm.mjs +64 -18
  32. package/backends/wasm/tests/integration.sh +5 -0
  33. package/backends/wasm/tests/test-wasm.mjs +86 -2
  34. package/canonical-pattern.mjs +7 -3
  35. package/codecs/README.md +23 -261
  36. package/codecs/WIRE_FORMAT.md +132 -0
  37. package/core.k +5 -15
  38. package/named-patterns.mjs +19 -7
  39. package/objects/README.md +24 -109
  40. package/package.json +8 -3
  41. package/paper/Makefile +17 -0
  42. package/paper/k-paper.pdf +0 -0
  43. package/paper/k-paper.tex +435 -0
  44. package/parser.jison +0 -10
  45. package/parser.mjs +32 -43
  46. package/repl.mjs +74 -29
  47. package/scripts/benchmark-idempotency.mjs +3 -3
  48. package/scripts/run-tests.mjs +1 -1
  49. package/scripts/test-package.mjs +64 -0
  50. package/tests/README.md +66 -46
  51. package/tests/code-derivation/core.mjs +61 -0
  52. package/tests/helpers/cli.mjs +45 -0
  53. package/tests/integration.sh +2 -3
  54. package/tests/test-arithmetics.mjs +47 -237
  55. package/tests/test-djson-methods.mjs +71 -275
  56. package/tests/test-hash-normalization.mjs +22 -1
  57. package/tests/test-karatsuba.mjs +18 -126
  58. package/tests/test-named-patterns.mjs +174 -621
  59. package/tests/test-paper-examples.mjs +90 -0
  60. package/tests/test-poly.mjs +2 -2
  61. package/tests/test-repl-html.mjs +22 -10
  62. package/tests/test-repl.mjs +49 -41
  63. package/tests/test.mjs +27 -68
  64. package/AGENTS.md +0 -35
  65. package/CLAUDE.md +0 -64
  66. package/DOCS/BIG_PICTURE.md +0 -439
  67. package/DOCS/BINARY_CODEC_STATUS_AND_TODO.md +0 -74
  68. package/DOCS/CANONICAL_PATTERNS.md +0 -362
  69. package/DOCS/CODEC_ARCHITECTURE.md +0 -257
  70. package/DOCS/CONVERGENCE.md +0 -123
  71. package/DOCS/DIVERGENCE_DETECTION.md +0 -23
  72. package/DOCS/JSON_APPROACHES_COMPARISON.md +0 -302
  73. package/DOCS/KIR_SPECIALIZATION_DESIGN.md +0 -472
  74. package/DOCS/Makefile +0 -99
  75. package/DOCS/NAMED_PATTERNS.md +0 -361
  76. package/DOCS/OBJECT_FILE_AND_PATTERN.md +0 -343
  77. package/DOCS/PATTERNS_AND_TYPED_VALUES.md +0 -665
  78. package/DOCS/WEB_REPL_REQUIREMENTS.md +0 -134
  79. package/DOCS/book/01-introduction.md +0 -48
  80. package/DOCS/book/02-syntax-and-values.md +0 -110
  81. package/DOCS/book/03-types-as-finite-automata.md +0 -155
  82. package/DOCS/book/04-partial-functions-and-composition.md +0 -145
  83. package/DOCS/book/05-typing-filters-and-normalization.md +0 -109
  84. package/DOCS/book/06-values-in-memory.md +0 -1085
  85. package/DOCS/book/07-the-partial-function-abi.md +0 -143
  86. package/DOCS/book/08-operational-semantics-and-execution.md +0 -196
  87. package/DOCS/book/09-compiler-architecture.md +0 -160
  88. package/DOCS/book/10-from-ast-to-intermediate-representation.md +0 -166
  89. package/DOCS/book/14-canonical-serialization.md +0 -176
  90. package/DOCS/book/appendix-b-incorporating-external-predefined-types-and-functions.md +0 -156
  91. package/DOCS/book/overview.txt +0 -68
  92. package/DOCS/book.md +0 -50
  93. package/Examples/bnat.k +0 -308
  94. package/Examples/buda.k +0 -66
  95. package/Examples/byte.k +0 -35
  96. package/Examples/code.k +0 -43
  97. package/Examples/vhdl_to_k.k +0 -67
  98. package/JOURNAL.md +0 -288
  99. package/TODO.md +0 -107
  100. package/backends/wasm/DOCS/USE-WEB-ASSEMBLY.md +0 -233
  101. package/backends/wasm/TECHNICAL-DEBT.md +0 -58
  102. package/backends/wasm/demo_ieee.mjs +0 -318
  103. package/browser/vfs-data.mjs +0 -21
  104. package/codecs/BINARY_FORMAT.md +0 -53
  105. package/codecs/POLYMORPHIC_BINARY_FORMAT.md +0 -303
  106. package/codecs/pandoc.css +0 -71
  107. package/tests/code-derivation/blowup.mjs +0 -25
  108. package/tests/code-derivation/bnat.mjs +0 -11
  109. package/tests/code-derivation/dot.mjs +0 -37
  110. package/tests/code-derivation/index.mjs +0 -20
  111. package/tests/code-derivation/loop.mjs +0 -17
  112. package/tests/code-derivation/nat.mjs +0 -10
  113. package/tests/code-derivation/not-not.mjs +0 -14
  114. package/tests/code-derivation/product.mjs +0 -22
  115. package/tests/code-derivation/rlz.mjs +0 -13
  116. 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
- ---