@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
package/Examples/bnat.k DELETED
@@ -1,308 +0,0 @@
1
- -- Standard binary encoding of natural numbers
2
-
3
- bnat[] = < {} _, B 0, B 1 > = B;
4
- bnat? = ? bnat[];
5
-
6
- bnat_pair[] = { bnat[] x, bnat[] y };
7
- reverse_state[] = { bnat[] x, bnat[] result };
8
- bnat_carry_state[] = { bnat[] x, bnat[] y, < {} 0, {} 1 > c };
9
- times_state[] = { bnat[] result, bnat[] _x, bnat[] y };
10
- align_state[] = { bnat[] _x, bnat[] _y, bnat[] x, bnat[] y };
11
- cmp[] = < {} lt, {} eq, {} gt >;
12
-
13
- _ = {}|_;
14
- _1 = |1;
15
- _0 = |0;
16
-
17
- bnat_0 = _ ;
18
- bnat_1 = _ _1 ;
19
- bnat_2 = _ _0 _1 ;
20
- bnat_3 = _ _1 _1 ;
21
- bnat_4 = _ _0 _0 _1 ;
22
- bnat_5 = _ _1 _0 _1 ;
23
- bnat_6 = _ _0 _1 _1 ;
24
- bnat_7 = _ _1 _1 _1 ;
25
- bnat_8 = _ _0 _0 _0 _1 ;
26
- bnat_9 = _ _1 _0 _0 _1 ;
27
- bnat_10 = _ _0 _1 _0 _1 ;
28
-
29
- remove_leading_zeros = < /0 remove_leading_zeros, () >;
30
-
31
- reverse = {() x, _ result} _reverse;
32
-
33
- _reverse =
34
- ? reverse_state[]
35
- <
36
- {.x/_ if, .result then} .then,
37
- {.x/0 x, .result _0 result} _reverse,
38
- {.x/1 x, .result _1 result} _reverse
39
- >
40
- ;
41
-
42
- append_0 = reverse _0 reverse; -- i.e., shift_left, double, or times2
43
-
44
-
45
- c0 = {} |0 ;
46
- c1 = {} |1 ;
47
-
48
-
49
-
50
- plus =
51
- -- ${bnat x, bnat y} -- $ pair
52
- {
53
- .x remove_leading_zeros reverse x,
54
- .y remove_leading_zeros reverse y,
55
- c0 c
56
- }
57
- _plus
58
- reverse
59
- ;
60
-
61
- _plus =
62
- -- least significant bit first addition with carry
63
- ? bnat_carry_state[]
64
- <
65
- {.x/_ if1, .y/_ if2, <.c/0 bnat_0, bnat_1> then} .then,
66
- {.x/_ if1, .c/0 if2, .y then} .then,
67
-
68
- {.x y, .y/_ _ x, .c c} _plus, -- swap x with y, if y = 0
69
- {<.c/0 bnat_0, bnat_1>
70
- x, .y y, .x/_ c0 c} _plus,
71
-
72
- {.x/0 x, .y/0 y, .c/0 c0 c} _plus _0,
73
- {.x/0 x, .y/0 y, .c/1 c0 c} _plus _1,
74
- {.x/0 x, .y/1 y, .c/0 c0 c} _plus _1,
75
- {.x/0 x, .y/1 y, .c/1 c1 c} _plus _0,
76
-
77
- {.x/1 x, .y/0 y, .c/0 c0 c} _plus _1,
78
- {.x/1 x, .y/0 y, .c/1 c1 c} _plus _0,
79
-
80
- {.x/1 x, .y/1 y, .c/0 c1 c} _plus _0,
81
- {.x/1 x, .y/1 y, .c/1 c1 c} _plus _1
82
- >
83
- ;
84
-
85
- minus =
86
- -- ${bnat x, bnat y}
87
- {
88
- .x remove_leading_zeros reverse x,
89
- .y remove_leading_zeros reverse y,
90
- c0 c
91
- }
92
- _minus
93
- reverse
94
- remove_leading_zeros
95
- ;
96
-
97
- _minus =
98
- -- least significat bit first substraction with carry
99
- ? bnat_carry_state[]
100
- <
101
- {.x x, .y/_ y, .c/0 if} .x,
102
- {.x/1 x, .y/_ y, .c/1 if} .x _0,
103
- {.x/0 x, .y/_ _ y, .c/1 c1 c} _minus _1,
104
- {.x/0 x, .y/0 y, .c/0 c0 c} _minus _0,
105
- {.x/0 x, .y/0 y, .c/1 c1 c} _minus _1,
106
- {.x/0 x, .y/1 y, .c/0 c1 c} _minus _1,
107
- {.x/0 x, .y/1 y, .c/1 c1 c} _minus _0,
108
- {.x/1 x, .y/0 y, .c/0 c0 c} _minus _1,
109
- {.x/1 x, .y/0 y, .c/1 c0 c} _minus _0,
110
- {.x/1 x, .y/1 y, .c/0 c0 c} _minus _0,
111
- {.x/1 x, .y/1 y, .c/1 c1 c} _minus _1
112
- >
113
- ;
114
-
115
- times =
116
- -- ${bnat x, bnat y}
117
- {
118
- bnat_0 result,
119
- .x remove_leading_zeros reverse _x,
120
- .y remove_leading_zeros y
121
- }
122
- _times
123
- reverse
124
- ;
125
-
126
- _times =
127
- ? times_state[]
128
- <
129
- { .y/_ if, .result then } .then,
130
- { .result _0 result, ._x _x, .y/0 y } _times,
131
- { { ._x x, .result _0 y, c0 c } _plus result, ._x _x, .y/1 y } _times
132
- >
133
- ;
134
-
135
- inc = {() x, bnat_1 y} plus;
136
- dec = {() x, bnat_1 y} minus;
137
-
138
- mod =
139
- -- ${bnat x, bnat y}
140
- <
141
- -- if x >= y then (x mod y) = ((x-y mod 2y) mod y)
142
- { { minus x, .y append_0 y } mod x, .y y } mod,
143
- -- else, i.e., if x < y then, (x mod y) = x
144
- .x
145
- >
146
- ;
147
-
148
- align =
149
- -- ${bnat x, bnat y}
150
- {
151
- .x remove_leading_zeros reverse _x,
152
- .y remove_leading_zeros reverse _y,
153
- bnat_0 x,
154
- bnat_0 y
155
- }
156
- _align
157
- ;
158
-
159
- _align =
160
- ? align_state[]
161
- <
162
- {._x/_ if1, ._y/_ if2, {.x x, .y y} then} .then,
163
- {._x </0, /_ _> _x, ._y </0, /_ _> _y, .x _0 x, .y _0 y} _align,
164
- {._x/1 _x, ._y </0, /_ _> _y, .x _1 x, .y _0 y} _align,
165
- {._x </0, /_ _> _x, ._y/1 _y, .x _0 x, .y _1 y} _align,
166
- {._x/1 _x, ._y/1 _y, .x _1 x, .y _1 y} _align
167
- >
168
- ;
169
-
170
-
171
- lt = {} |lt;
172
- eq = {} |eq;
173
- gt = {} |gt;
174
-
175
- compare = align _compare;
176
- _compare =
177
- ? bnat_pair[]
178
- <
179
- {.x/_ if1, .y/_ if2 } eq,
180
- {.x/0 x, .y/0 y} _compare,
181
- {.x/1 x, .y/1 y} _compare,
182
- .x/0 lt,
183
- gt
184
- >
185
-
186
- ;
187
-
188
- zero? = remove_leading_zeros /_ bnat_0;
189
- eq? = {compare /eq if, () then} .then;
190
- gt? = {compare /gt if, () then} .then;
191
- lt? = {compare /lt if, () then} .then;
192
- ge? = < gt?, eq? >;
193
- le? = < lt?, eq? >;
194
-
195
- // gratest common divisor
196
- gcd = <gt?, {.y x,.x y}> _gcd;
197
-
198
- _gcd = <
199
- { .y zero? if, .x then } .then,
200
- { .y x, mod y } _gcd
201
- >;
202
-
203
- log2_floor =
204
- -- ${bnat x, bnat step}
205
- -- finds the largest k, such that 'step * 2^k <= x',
206
- -- returns k+1, i.e., 0 if x < step, and k+1 otherwise.
207
- {.x x, .step step, bnat_0 log} _log2_floor;
208
-
209
- _log2_floor =
210
- <
211
- {{.step x, .x y} gt? if, .log then} .then,
212
- {
213
- .x x,
214
- {.step x, bnat_0 y} gt? .x append_0 step,
215
- .log inc log
216
- } _log2_floor
217
- >
218
- ;
219
-
220
- append_n_times_0 =
221
- -- ${bnat x, bnat n}
222
- <
223
- {.n zero? if, .x then} .then,
224
- {.x append_0 x, .n dec n} append_n_times_0
225
- >
226
- ;
227
-
228
- div =
229
-
230
- {.x x, .y step, bnat_0 res} _div
231
- ;
232
-
233
- _div =
234
- {
235
- .x x,
236
- .step step,
237
- .res res,
238
- {.x x, .step step} log2_floor log
239
- }
240
- <
241
- { .log zero? if, {.res div, .x rem} then } .then,
242
- {
243
- -- {2 x, .log [(),1] MINUS count} rep TIMES factor,
244
- { bnat_1 x, .log dec n} append_n_times_0 factor,
245
- () i
246
- }
247
- {
248
- {.i.x x, {.i.step x, .factor y} times y} minus x,
249
- .i.step step,
250
- {.i.res x, .factor y} plus res
251
- } _div
252
- >
253
- ;
254
-
255
- lcm =
256
-
257
- {times x, gcd y}
258
- div .div
259
- ;
260
-
261
- half =
262
- reverse
263
- <
264
- {/0 reverse div, bnat_0 rem},
265
- {/1 reverse div, bnat_1 rem},
266
- {bnat_0 div, bnat_0 rem}
267
- >
268
- ;
269
-
270
- pow =
271
- -- ${ bnat base, bnat exponent }
272
- <
273
- .exponent zero? bnat_1,
274
- {.base base, .exponent half half}
275
- {
276
- .base base,
277
- .half.rem extra,
278
- {.base base, .half.div exponent} pow {()x, ()y} times pow
279
- }
280
- <
281
- {.extra zero? if, .pow then} .then,
282
- {.base x, .pow y} times
283
- >
284
- >
285
- ;
286
-
287
- sqrt =
288
- <
289
- { () x, {bnat_1 min, () max} range } _sqrt
290
- >
291
- ;
292
-
293
- _sqrt =
294
- { () input, {.range.max x, .range.min y} plus half .div middle }
295
- <
296
- { .input.range.min x, .middle y} eq? .x,
297
- { () aux, {.input.x x, .middle y} div .div middle }
298
- {
299
- .aux.input.x x,
300
- { .aux.middle x, .middle y }
301
- < gt?, {.x y, .y x} > {.y min, .x max} range
302
- } _sqrt
303
- >
304
- ;
305
-
306
- --------------------
307
-
308
- lcm
package/Examples/buda.k DELETED
@@ -1,66 +0,0 @@
1
- -- Examples/buda.k: Relational list algorithms using named patterns
2
- -- Uses arbitrary-precision integers from Examples/arithmetics.k.
3
-
4
- bits[] = < {} _, B 0, B 1 > = B;
5
- int[] = < bits[] "+", bits[] "-" >;
6
-
7
- list[X] = < {} nil, { X car, Y cdr } cons > = Y;
8
- list? = ? list[X];
9
-
10
- nths_args[X] = { int[] n, list[X] list };
11
- reverse_state[X] = { list[X] reversed, list[X] list };
12
- split_state[X] = { list[X] odd, list[X] even, list[X] list };
13
- split_result[X] = { list[X] odd, list[X] even };
14
-
15
- nil = {} | nil list?;
16
- cons = { .car car, .cdr cdr } | cons list?;
17
- car = list? /cons .car;
18
- cdr = list? /cons .cdr;
19
-
20
- nths = ? nths_args[X]
21
- <
22
- { .n pos? dec n, .list cdr list } nths,
23
- { .n zero_int? if, .list car then } .then
24
- >;
25
-
26
- reverse = ? list[X]
27
- { nil reversed, () list } _reverse;
28
-
29
- _reverse = ? reverse_state[X]
30
- <
31
- {
32
- { .list car car, .reversed cdr } cons reversed,
33
- .list cdr list
34
- } _reverse,
35
- .reversed
36
- >
37
- ;
38
-
39
- split = ? list[X]
40
- { nil odd, nil even, reverse list } _split
41
- ;
42
-
43
- _split = ? split_state[X]
44
- <
45
- {
46
- { .list car car, .odd cdr } cons odd,
47
- .even even,
48
- .list cdr list
49
- } split_,
50
-
51
- { .odd odd, .even even }
52
- >
53
- ;
54
-
55
- split_ = ? split_state[X]
56
- <
57
- {
58
- .odd odd,
59
- { .list car car, .even cdr } cons even,
60
- .list cdr list
61
- } _split,
62
- { .odd odd, .even even }
63
- >
64
- ;
65
-
66
- ()
package/Examples/byte.k DELETED
@@ -1,35 +0,0 @@
1
- -- Examples/byte.k: Byte and 7-bit ASCII representation
2
-
3
- 0 = {} | 0;
4
- 1 = {} | 1;
5
-
6
- bit[] = < {} 0, {} 1 >;
7
-
8
- byte[] = {
9
- bit[] 0, bit[] 1, bit[] 2, bit[] 3,
10
- bit[] 4, bit[] 5, bit[] 6, bit[] 7
11
- };
12
-
13
- ascii[] = {
14
- bit[] 0, bit[] 1, bit[] 2, bit[] 3,
15
- bit[] 4, bit[] 5, bit[] 6
16
- };
17
-
18
- _ = { 1 6, 0 5, 1 4, 1 3, 1 2, 1 1, 1 0 };
19
- map = <
20
- /ascii <
21
- { .6/1 6, .5/1 5, .4/1 4, .3/1 3, .2/1 2, .1/1 1, .0/1 0 } _,
22
- { { .6/0 6, .5/1 5 } if, () then } .then,
23
- { .6/1 if, () then } .then
24
- >,
25
- _
26
- > | ascii;
27
-
28
- list[X] = < {} nil, { X car, Y cdr } cons > = Y;
29
-
30
- filter = ? list[X] <
31
- /cons { .car map car, .cdr filter cdr } | cons,
32
- ()
33
- >;
34
-
35
- filter
package/Examples/code.k DELETED
@@ -1,43 +0,0 @@
1
- -- Examples/code.k: Pattern representation and encoding
2
-
3
- chars[] = <
4
- C " ", C "!", C '"', C "#", C "$", C "%", C "&", C "'",
5
- C "(", C ")", C "*", C "+", C ",", C "-", C ".", C "/",
6
- C "0", C "1", C "2", C "3", C "4", C "5", C "6", C "7",
7
- C "8", C "9", C ":", C ";", C "<", C "=", C ">", C "?",
8
- C "@", C "A", C "B", C "C", C "D", C "E", C "F", C "G",
9
- C "H", C "I", C "J", C "K", C "L", C "M", C "N", C "O",
10
- C "P", C "Q", C "R", C "S", C "T", C "U", C "V", C "W",
11
- C "X", C "Y", C "Z", C "[", C "\\", C "]", C "^", C "_",
12
- C "`", C "a", C "b", C "c", C "d", C "e", C "f", C "g",
13
- C "h", C "i", C "j", C "k", C "l", C "m", C "n", C "o",
14
- C "p", C "q", C "r", C "s", C "t", C "u", C "v", C "w",
15
- C "x", C "y", C "z", C "{", C "|", C "}", C "~",
16
- {} end
17
- > = C;
18
-
19
- char[] = <
20
- {} " ", {} "!", {} '"', {} "#", {} "$", {} "%", {} "&", {} "'",
21
- {} "(", {} ")", {} "*", {} "+", {} ",", {} "-", {} ".", {} "/",
22
- {} "0", {} "1", {} "2", {} "3", {} "4", {} "5", {} "6", {} "7",
23
- {} "8", {} "9", {} ":", {} ";", {} "<", {} "=", {} ">", {} "?",
24
- {} "@", {} "A", {} "B", {} "C", {} "D", {} "E", {} "F", {} "G",
25
- {} "H", {} "I", {} "J", {} "K", {} "L", {} "M", {} "N", {} "O",
26
- {} "P", {} "Q", {} "R", {} "S", {} "T", {} "U", {} "V", {} "W",
27
- {} "X", {} "Y", {} "Z", {} "[", {} "\\", {} "]", {} "^", {} "_",
28
- {} "`", {} "a", {} "b", {} "c", {} "d", {} "e", {} "f", {} "g",
29
- {} "h", {} "i", {} "j", {} "k", {} "l", {} "m", {} "n", {} "o",
30
- {} "p", {} "q", {} "r", {} "s", {} "t", {} "u", {} "v", {} "w",
31
- {} "x", {} "y", {} "z", {} "{", {} "|", {} "}", {} "~"
32
- >;
33
-
34
- fields[] = < {} nil, { chars[] car, F cdr } cons > = F;
35
-
36
- code[] = < fields[] product, fields[] union, chars[] ref >;
37
-
38
- nil = {} | nil;
39
- _ = ({} | end) ? chars[];
40
- KL = (_ | L | K) ? chars[];
41
-
42
- bool = { KL car, { KL car, nil cdr } | cons cdr } | cons | union ? code[];
43
- bool
@@ -1,67 +0,0 @@
1
- -- VHDL to K Translation Patterns
2
-
3
- -- VHDL: signal a : std_logic;
4
- -- K: bit[] = < {} 0, {} 1 >;
5
- bit[] = < {} 0, {} 1 >;
6
-
7
- -- VHDL: signal bus : std_logic_vector(7 downto 0);
8
- -- K: byte[] = {
9
- -- bit[] 0, bit[] 1, bit[] 2, bit[] 3,
10
- -- bit[] 4, bit[] 5, bit[] 6, bit[] 7
11
- -- };
12
- byte[] = {
13
- bit[] 0, bit[] 1, bit[] 2, bit[] 3,
14
- bit[] 4, bit[] 5, bit[] 6, bit[] 7
15
- };
16
-
17
- nibble[] = { bit[] 0, bit[] 1, bit[] 2, bit[] 3 };
18
-
19
- full_adder_inputs[] = { bit[] a, bit[] b, bit[] c_in };
20
- full_adder_outputs[] = { bit[] sum, bit[] carry };
21
- nibble_pair[] = { nibble[] a, nibble[] b };
22
- four_bit_adder_result[] = { nibble[] sum, bit[] overflow };
23
-
24
- 0 = {} | 0;
25
- 1 = {} | 1;
26
-
27
- -- VHDL: y <= not a;
28
- not_bit = < /0 1, /1 0 >;
29
-
30
- -- VHDL: y <= a and b;
31
- and_bit = < .x/0 0, .y/0 0, 1 >;
32
-
33
- -- VHDL: y <= a or b;
34
- or_bit = < .x/1 1, .y/1 1, 0 >;
35
-
36
- -- VHDL: y <= a xor b;
37
- xor_bit = < {.x/0 if, .y then} .then, .y not_bit >;
38
- xor_abc = {{.a x, .b y} xor_bit x, .c y} xor_bit;
39
-
40
- -- VHDL: Full adder component
41
- full_adder = ? full_adder_inputs[] {
42
- {.a a, .b b, .c_in c} xor_abc sum,
43
- {
44
- {.a x, .b y} and_bit x,
45
- {{.a x, .b y} xor_bit x, .c_in y} and_bit y
46
- } or_bit carry
47
- };
48
-
49
- -- VHDL: architecture of 4-bit adder
50
- four_bit_adder = ? nibble_pair[] {
51
- .a a, .b b,
52
- {.a.0 a, .b.0 b, 0 c_in} full_adder fa0
53
- } {
54
- .a a, .b b, .fa0 fa0,
55
- {.a.1 a, .b.1 b, .fa0.carry c_in} full_adder fa1
56
- } {
57
- .a a, .b b, .fa0 fa0, .fa1 fa1,
58
- {.a.2 a, .b.2 b, .fa1.carry c_in} full_adder fa2
59
- } {
60
- .fa0 fa0, .fa1 fa1, .fa2 fa2,
61
- {.a.3 a, .b.3 b, .fa2.carry c_in} full_adder fa3
62
- } {
63
- {.fa0.sum 0, .fa1.sum 1, .fa2.sum 2, .fa3.sum 3} sum,
64
- .fa3.carry overflow
65
- };
66
-
67
- ()