@fraczak/k 6.0.1 → 6.0.2

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 (38) hide show
  1. package/AGENTS.md +35 -0
  2. package/CHANGELOG.md +48 -0
  3. package/CLAUDE.md +64 -0
  4. package/DOCS/BIG_PICTURE.md +4 -8
  5. package/DOCS/BINARY_CODEC_STATUS_AND_TODO.md +74 -0
  6. package/DOCS/CONVERGENCE.md +92 -0
  7. package/DOCS/DIVERGENCE_DETECTION.md +24 -0
  8. package/DOCS/Makefile +99 -0
  9. package/DOCS/OBJECT_FILE_AND_PATTERN.md +217 -0
  10. package/DOCS/PATTERNS.md +114 -0
  11. package/DOCS/PATTERNS_AND_TYPED_VALUES.md +665 -0
  12. package/DOCS/REPL.md +125 -125
  13. package/DOCS/TEXTUAL_VALUES.md +108 -0
  14. package/DOCS/TYPE_DERIVATION.md +230 -0
  15. package/DOCS/book/06-values-in-memory.md +1085 -0
  16. package/DOCS/book/07-the-partial-function-abi.md +143 -0
  17. package/DOCS/book/08-operational-semantics-and-execution.md +201 -0
  18. package/DOCS/book/09-compiler-architecture.md +160 -0
  19. package/DOCS/book/10-from-ast-to-intermediate-representation.md +167 -0
  20. package/DOCS/book/14-canonical-serialization.md +176 -0
  21. package/DOCS/book/appendix-b-incorporating-external-predefined-types-and-functions.md +155 -0
  22. package/DOCS/book/overview.txt +68 -0
  23. package/README.md +71 -27
  24. package/codecs/README.md +8 -0
  25. package/codecs/pandoc.css +71 -0
  26. package/codecs/runtime/test-codec.mjs +89 -0
  27. package/package.json +2 -2
  28. package/{codecs/k-pattern.mjs → patterns/from-k.mjs} +3 -3
  29. package/repl2.mjs +259 -50
  30. package/scripts/generate-dependency-graph.mjs +121 -0
  31. package/test-repl2.mjs +55 -8
  32. package/DOCS/book-combined.html +0 -3798
  33. package/DOCS/book-combined.md +0 -3745
  34. package/DOCS/book-combined.pdf +0 -0
  35. package/DOCS/book-combined.tex +0 -5682
  36. package/DOCS/book.html +0 -65
  37. package/DOCS/book.pdf +0 -0
  38. package/DOCS/book.tex +0 -150
@@ -0,0 +1,114 @@
1
+ # Patterns
2
+
3
+ A pattern describes the shape of values that may flow through a k expression.
4
+ Patterns are used by:
5
+
6
+ - the type-derivation engine
7
+ - the binary codec
8
+ - runtime values carried by the codec pipeline
9
+ - filter expressions
10
+
11
+ ## Pattern Kinds
12
+
13
+ The exported canonical pattern form is a property-list array of nodes.
14
+
15
+ Each node has the form:
16
+
17
+ ```text
18
+ [kind, edges]
19
+ ```
20
+
21
+ where `kind` is one of:
22
+
23
+ - `"any"`
24
+ - `"open-product"`
25
+ - `"open-union"`
26
+ - `"closed-product"`
27
+ - `"closed-union"`
28
+
29
+ and `edges` is a list of:
30
+
31
+ ```text
32
+ [label, targetNodeId]
33
+ ```
34
+
35
+ Example:
36
+
37
+ ```json
38
+ [
39
+ ["closed-union", [["nil", 1], ["cons", 2]]],
40
+ ["closed-product", []],
41
+ ["closed-product", [["car", 3], ["cdr", 0]]],
42
+ ["closed-union", [["_", 1], ["0", 3], ["1", 3]]]
43
+ ]
44
+ ```
45
+
46
+ This is not the wire format. It is the canonical readable export form.
47
+
48
+ ## Filters and Type Names
49
+
50
+ A k script can describe a pattern through:
51
+
52
+ - a type name in terminal position
53
+ - a filter expression in terminal position
54
+
55
+ Examples:
56
+
57
+ ```k
58
+ $ bits = < {} _, bits 0, bits 1 >;
59
+ $ list = < {} nil, { bits car, list cdr } cons >;
60
+ list
61
+ ```
62
+
63
+ ```k
64
+ ?< {} nil, { $bits car, X cdr } cons > = X
65
+ ```
66
+
67
+ In both cases, the compiler derives a root pattern from `__main__`.
68
+
69
+ ## `k-pattern`
70
+
71
+ `k-pattern` extracts that root pattern from a k script.
72
+
73
+ It:
74
+
75
+ 1. reads a k script from stdin or a file argument
76
+ 2. runs `k.annotate(...)`
77
+ 3. resolves `__main__`
78
+ 4. requires `__main__` to be either:
79
+ - a filter expression
80
+ - a type name
81
+ 5. exports the corresponding root pattern
82
+ 6. prints it as the canonical property-list array
83
+
84
+ Example:
85
+
86
+ ```bash
87
+ echo '?< {} nil, { $bits car, X cdr } cons > = X' | k-pattern
88
+ ```
89
+
90
+ Typical output:
91
+
92
+ ```json
93
+ [["closed-union",[["nil",1],["cons",2]]],["closed-product",[]],["closed-product",[["car",3],["cdr",0]]],["closed-union",[["_",1],["0",3],["1",3]]]]
94
+ ```
95
+
96
+ ## Patterns in the Codec
97
+
98
+ The active wire format is:
99
+
100
+ ```text
101
+ encode($pattern_value : $pattern) encode(value : decoded_pattern)
102
+ ```
103
+
104
+ So the property-list array printed by `k-pattern` is a readable externalization
105
+ of the same pattern information carried in the wire stream.
106
+
107
+ `k-parse --input-pattern ...` and `k-parse --input-type ...` both ultimately
108
+ produce the same kind of root pattern used to interpret textual values.
109
+
110
+ ## See Also
111
+
112
+ - [DOCS/TEXTUAL_VALUES.md](./TEXTUAL_VALUES.md)
113
+ - [codecs/README.md](../codecs/README.md)
114
+ - [DOCS/OBJECT_FILE_AND_PATTERN.md](./OBJECT_FILE_AND_PATTERN.md)