@fraczak/k 1.2.0 → 1.2.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.
- package/Benchmark/range1000 +1000 -0
- package/Benchmark/range200 +200 -0
- package/README.md +222 -194
- package/package.json +8 -4
- package/repl.coffee +25 -1
- package/repl.js +32 -3
- package/repl.sh +32 -3
- package/run.coffee +44 -44
- package/run.js +77 -74
- package/Examples/bnat.k +0 -121
- package/Examples/code.k +0 -21
- package/Examples/error.k +0 -8
- package/Examples/factorial.k +0 -12
- package/Examples/fib.k +0 -17
- package/Examples/pow.k +0 -7
- package/Examples/rec-code.k +0 -80
- package/Examples/test.k +0 -13
- package/Examples/typed-fib.k +0 -19
package/package.json
CHANGED
|
@@ -1,14 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fraczak/k",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.2",
|
|
4
4
|
"description": "k-language for JSON-like data transformation",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
7
7
|
"prepare": "coffee -c symbol-table.coffee; jison parser.jison",
|
|
8
|
-
|
|
8
|
+
"prepublishOnly": "coffee -c *.coffee; echo '#!/usr/bin/env node' | cat - k.js > k.sh; echo '#!/usr/bin/env node' | cat - repl.js > repl.sh;",
|
|
9
9
|
"all": "coffee -c *.coffee; npm run prepare; npm run prepublishOnly",
|
|
10
10
|
"test": "coffee -c symbol-table.coffee; jison parser.jison; coffee test.coffee"
|
|
11
11
|
},
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "https://github.com/fraczak/k.git"
|
|
15
|
+
},
|
|
12
16
|
"author": "Wojciech Fraczak",
|
|
13
17
|
"license": "ISC",
|
|
14
18
|
"bin": {
|
|
@@ -16,7 +20,7 @@
|
|
|
16
20
|
"k-repl": "./repl.sh"
|
|
17
21
|
},
|
|
18
22
|
"devDependencies": {
|
|
19
|
-
"
|
|
20
|
-
"
|
|
23
|
+
"coffeescript": "^2.7.0",
|
|
24
|
+
"jison": "^0.4.18"
|
|
21
25
|
}
|
|
22
26
|
}
|
package/repl.coffee
CHANGED
|
@@ -1,5 +1,18 @@
|
|
|
1
1
|
#!/usr/bin/env coffee
|
|
2
2
|
k = require "./"
|
|
3
|
+
run = require "./run"
|
|
4
|
+
|
|
5
|
+
console.log "Very! experimental repl shell for 'k-language'..."
|
|
6
|
+
|
|
7
|
+
help = ->
|
|
8
|
+
console.log " --h print help"
|
|
9
|
+
console.log " --a print codes and relations"
|
|
10
|
+
console.log " --c print codes"
|
|
11
|
+
console.log " --l file.k load 'file.k'"
|
|
12
|
+
|
|
13
|
+
help()
|
|
14
|
+
|
|
15
|
+
re = /^[ \n\t]*(?:--l[ ]+)(.+[^ ])[ ]*?$/
|
|
3
16
|
|
|
4
17
|
do (val = {}, buffer = []) ->
|
|
5
18
|
process.stdin.on 'data', (data) ->
|
|
@@ -13,7 +26,18 @@ do (val = {}, buffer = []) ->
|
|
|
13
26
|
for line in todo
|
|
14
27
|
if val is undefined
|
|
15
28
|
val = {}
|
|
16
|
-
if
|
|
29
|
+
if line.match /^[ \n\t]*(?:--h)?$/
|
|
30
|
+
help()
|
|
31
|
+
else if line.match re
|
|
32
|
+
file = line.match(re)[1]
|
|
33
|
+
console.log "-- loading file: #{file} ..."
|
|
34
|
+
val = k.compile_file(file) val
|
|
35
|
+
console.log "=> #{JSON.stringify val}"
|
|
36
|
+
else if line.match /^[ \n\t]*(?:--a)?$/
|
|
37
|
+
console.log JSON.stringify run.defs, " ", 2
|
|
38
|
+
else if line.match /^[ \n\t]*(?:--c)?$/
|
|
39
|
+
console.log JSON.stringify {codes: run.defs?.codes, reps: run.defs?.representatives}, " ", 2
|
|
40
|
+
else if not line.match /^[ \n\t]*(?:#.*)?$/
|
|
17
41
|
try
|
|
18
42
|
val = k.run "#{line} ()", val
|
|
19
43
|
console.log "=> #{JSON.stringify val}"
|
package/repl.js
CHANGED
|
@@ -1,14 +1,29 @@
|
|
|
1
1
|
// Generated by CoffeeScript 2.7.0
|
|
2
2
|
(function() {
|
|
3
3
|
//!/usr/bin/env coffee
|
|
4
|
-
var k,
|
|
4
|
+
var help, k, re, run,
|
|
5
5
|
splice = [].splice;
|
|
6
6
|
|
|
7
7
|
k = require("./");
|
|
8
8
|
|
|
9
|
+
run = require("./run");
|
|
10
|
+
|
|
11
|
+
console.log("Very! experimental repl shell for 'k-language'...");
|
|
12
|
+
|
|
13
|
+
help = function() {
|
|
14
|
+
console.log(" --h print help");
|
|
15
|
+
console.log(" --a print codes and relations");
|
|
16
|
+
console.log(" --c print codes");
|
|
17
|
+
return console.log(" --l file.k load 'file.k'");
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
help();
|
|
21
|
+
|
|
22
|
+
re = /^[ \n\t]*(?:--l[ ]+)(.+[^ ])[ ]*?$/;
|
|
23
|
+
|
|
9
24
|
(function(val, buffer) {
|
|
10
25
|
return process.stdin.on('data', function(data) {
|
|
11
|
-
var e, first, i, last, len, line, ref, rest, results, todo;
|
|
26
|
+
var e, file, first, i, last, len, line, ref, ref1, ref2, rest, results, todo;
|
|
12
27
|
[first, ...rest] = data.toString("utf8").split("\n");
|
|
13
28
|
buffer.push(first);
|
|
14
29
|
if (rest.length > 0) {
|
|
@@ -22,7 +37,21 @@
|
|
|
22
37
|
if (val === void 0) {
|
|
23
38
|
val = {};
|
|
24
39
|
}
|
|
25
|
-
if (
|
|
40
|
+
if (line.match(/^[ \n\t]*(?:--h)?$/)) {
|
|
41
|
+
results.push(help());
|
|
42
|
+
} else if (line.match(re)) {
|
|
43
|
+
file = line.match(re)[1];
|
|
44
|
+
console.log(`-- loading file: ${file} ...`);
|
|
45
|
+
val = k.compile_file(file)(val);
|
|
46
|
+
results.push(console.log(`=> ${JSON.stringify(val)}`));
|
|
47
|
+
} else if (line.match(/^[ \n\t]*(?:--a)?$/)) {
|
|
48
|
+
results.push(console.log(JSON.stringify(run.defs, " ", 2)));
|
|
49
|
+
} else if (line.match(/^[ \n\t]*(?:--c)?$/)) {
|
|
50
|
+
results.push(console.log(JSON.stringify({
|
|
51
|
+
codes: (ref1 = run.defs) != null ? ref1.codes : void 0,
|
|
52
|
+
reps: (ref2 = run.defs) != null ? ref2.representatives : void 0
|
|
53
|
+
}, " ", 2)));
|
|
54
|
+
} else if (!line.match(/^[ \n\t]*(?:#.*)?$/)) {
|
|
26
55
|
try {
|
|
27
56
|
val = k.run(`${line} ()`, val);
|
|
28
57
|
results.push(console.log(`=> ${JSON.stringify(val)}`));
|
package/repl.sh
CHANGED
|
@@ -2,14 +2,29 @@
|
|
|
2
2
|
// Generated by CoffeeScript 2.7.0
|
|
3
3
|
(function() {
|
|
4
4
|
//!/usr/bin/env coffee
|
|
5
|
-
var k,
|
|
5
|
+
var help, k, re, run,
|
|
6
6
|
splice = [].splice;
|
|
7
7
|
|
|
8
8
|
k = require("./");
|
|
9
9
|
|
|
10
|
+
run = require("./run");
|
|
11
|
+
|
|
12
|
+
console.log("Very! experimental repl shell for 'k-language'...");
|
|
13
|
+
|
|
14
|
+
help = function() {
|
|
15
|
+
console.log(" --h print help");
|
|
16
|
+
console.log(" --a print codes and relations");
|
|
17
|
+
console.log(" --c print codes");
|
|
18
|
+
return console.log(" --l file.k load 'file.k'");
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
help();
|
|
22
|
+
|
|
23
|
+
re = /^[ \n\t]*(?:--l[ ]+)(.+[^ ])[ ]*?$/;
|
|
24
|
+
|
|
10
25
|
(function(val, buffer) {
|
|
11
26
|
return process.stdin.on('data', function(data) {
|
|
12
|
-
var e, first, i, last, len, line, ref, rest, results, todo;
|
|
27
|
+
var e, file, first, i, last, len, line, ref, ref1, ref2, rest, results, todo;
|
|
13
28
|
[first, ...rest] = data.toString("utf8").split("\n");
|
|
14
29
|
buffer.push(first);
|
|
15
30
|
if (rest.length > 0) {
|
|
@@ -23,7 +38,21 @@
|
|
|
23
38
|
if (val === void 0) {
|
|
24
39
|
val = {};
|
|
25
40
|
}
|
|
26
|
-
if (
|
|
41
|
+
if (line.match(/^[ \n\t]*(?:--h)?$/)) {
|
|
42
|
+
results.push(help());
|
|
43
|
+
} else if (line.match(re)) {
|
|
44
|
+
file = line.match(re)[1];
|
|
45
|
+
console.log(`-- loading file: ${file} ...`);
|
|
46
|
+
val = k.compile_file(file)(val);
|
|
47
|
+
results.push(console.log(`=> ${JSON.stringify(val)}`));
|
|
48
|
+
} else if (line.match(/^[ \n\t]*(?:--a)?$/)) {
|
|
49
|
+
results.push(console.log(JSON.stringify(run.defs, " ", 2)));
|
|
50
|
+
} else if (line.match(/^[ \n\t]*(?:--c)?$/)) {
|
|
51
|
+
results.push(console.log(JSON.stringify({
|
|
52
|
+
codes: (ref1 = run.defs) != null ? ref1.codes : void 0,
|
|
53
|
+
reps: (ref2 = run.defs) != null ? ref2.representatives : void 0
|
|
54
|
+
}, " ", 2)));
|
|
55
|
+
} else if (!line.match(/^[ \n\t]*(?:#.*)?$/)) {
|
|
27
56
|
try {
|
|
28
57
|
val = k.run(`${line} ()`, val);
|
|
29
58
|
results.push(console.log(`=> ${JSON.stringify(val)}`));
|
package/run.coffee
CHANGED
|
@@ -6,7 +6,7 @@ valid = (x) ->
|
|
|
6
6
|
|
|
7
7
|
builtin =
|
|
8
8
|
"_log!": (arg) ->
|
|
9
|
-
console.
|
|
9
|
+
console.error "_log!: #{JSON.stringify arg}"
|
|
10
10
|
arg
|
|
11
11
|
"fromFILE": (fileName) ->
|
|
12
12
|
fs.readFileSync(fileName).toString()
|
|
@@ -58,16 +58,20 @@ verify = (code, value) ->
|
|
|
58
58
|
# representatives = run.defs.representatives
|
|
59
59
|
# defCodes = JSON.stringify run.defs.codes
|
|
60
60
|
# console.log {code,value, representatives, defCodes}
|
|
61
|
+
return false unless code?
|
|
61
62
|
switch code.code
|
|
62
63
|
when "vector"
|
|
64
|
+
return false unless Array.isArray value
|
|
63
65
|
value.every (x) ->
|
|
64
66
|
verify code.vector, x
|
|
65
67
|
when "product"
|
|
68
|
+
return false unless "object" is typeof value
|
|
66
69
|
do (fields = Object.keys(value)) ->
|
|
67
70
|
return false unless fields.length is Object.keys(code.product).length
|
|
68
71
|
fields.every (label) ->
|
|
69
72
|
verify code.product[label], value[label]
|
|
70
73
|
when "union"
|
|
74
|
+
return false unless "object" is typeof value
|
|
71
75
|
do (fields = Object.keys(value)) ->
|
|
72
76
|
return false unless fields.length is 1
|
|
73
77
|
verify code.union[fields[0]], value[fields[0]]
|
|
@@ -77,50 +81,46 @@ verify = (code, value) ->
|
|
|
77
81
|
codes[code] value
|
|
78
82
|
|
|
79
83
|
run = (exp, value) ->
|
|
84
|
+
"use strict"
|
|
80
85
|
# console.log {exp,value}
|
|
81
86
|
return undefined if value is undefined
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
return undefined
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
result[label] = value
|
|
121
|
-
result
|
|
122
|
-
, {}
|
|
123
|
-
else
|
|
124
|
-
console.log exp.op
|
|
87
|
+
switch exp.op
|
|
88
|
+
when "code"
|
|
89
|
+
return value if verify exp.code, value
|
|
90
|
+
when "identity"
|
|
91
|
+
return value
|
|
92
|
+
when "str", "int"
|
|
93
|
+
return exp[exp.op]
|
|
94
|
+
when "ref"
|
|
95
|
+
defn = run.defs.rels[exp.ref]?[0]
|
|
96
|
+
return run defn, value if defn?
|
|
97
|
+
return builtin[exp.ref] value
|
|
98
|
+
when "dot"
|
|
99
|
+
return value[exp.dot]
|
|
100
|
+
when "comp"
|
|
101
|
+
return exp.comp.reduce (value, exp) ->
|
|
102
|
+
run exp, value unless value is undefined
|
|
103
|
+
, value
|
|
104
|
+
when "union"
|
|
105
|
+
for e in exp.union
|
|
106
|
+
result = run e, value
|
|
107
|
+
return result unless result is undefined
|
|
108
|
+
return undefined
|
|
109
|
+
when "vector"
|
|
110
|
+
result = []
|
|
111
|
+
for e in exp.vector
|
|
112
|
+
r = run e, value
|
|
113
|
+
return if r is undefined
|
|
114
|
+
result.push r
|
|
115
|
+
return result
|
|
116
|
+
when "product"
|
|
117
|
+
result = {}
|
|
118
|
+
for {label,exp} in exp.product
|
|
119
|
+
r = run exp, value
|
|
120
|
+
return if r is undefined
|
|
121
|
+
result[label] = r
|
|
122
|
+
return result
|
|
123
|
+
else
|
|
124
|
+
console.error exp.op
|
|
125
125
|
|
|
126
126
|
module.exports = run
|
package/run.js
CHANGED
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
|
|
17
17
|
builtin = {
|
|
18
18
|
"_log!": function(arg) {
|
|
19
|
-
console.
|
|
19
|
+
console.error(`_log!: ${JSON.stringify(arg)}`);
|
|
20
20
|
return arg;
|
|
21
21
|
},
|
|
22
22
|
"fromFILE": function(fileName) {
|
|
@@ -120,15 +120,24 @@
|
|
|
120
120
|
};
|
|
121
121
|
|
|
122
122
|
verify = function(code, value) {
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
123
|
+
if (code == null) {
|
|
124
|
+
// representatives = run.defs.representatives
|
|
125
|
+
// defCodes = JSON.stringify run.defs.codes
|
|
126
|
+
// console.log {code,value, representatives, defCodes}
|
|
127
|
+
return false;
|
|
128
|
+
}
|
|
126
129
|
switch (code.code) {
|
|
127
130
|
case "vector":
|
|
131
|
+
if (!Array.isArray(value)) {
|
|
132
|
+
return false;
|
|
133
|
+
}
|
|
128
134
|
return value.every(function(x) {
|
|
129
135
|
return verify(code.vector, x);
|
|
130
136
|
});
|
|
131
137
|
case "product":
|
|
138
|
+
if ("object" !== typeof value) {
|
|
139
|
+
return false;
|
|
140
|
+
}
|
|
132
141
|
return (function(fields) {
|
|
133
142
|
if (fields.length !== Object.keys(code.product).length) {
|
|
134
143
|
return false;
|
|
@@ -138,6 +147,9 @@
|
|
|
138
147
|
});
|
|
139
148
|
})(Object.keys(value));
|
|
140
149
|
case "union":
|
|
150
|
+
if ("object" !== typeof value) {
|
|
151
|
+
return false;
|
|
152
|
+
}
|
|
141
153
|
return (function(fields) {
|
|
142
154
|
if (fields.length !== 1) {
|
|
143
155
|
return false;
|
|
@@ -155,83 +167,74 @@
|
|
|
155
167
|
};
|
|
156
168
|
|
|
157
169
|
run = function(exp, value) {
|
|
158
|
-
|
|
170
|
+
"use strict";
|
|
171
|
+
var defn, e, i, j, k, label, len, len1, len2, r, ref, ref1, ref2, ref3, result;
|
|
159
172
|
if (value === void 0) {
|
|
160
173
|
// console.log {exp,value}
|
|
161
174
|
return void 0;
|
|
162
175
|
}
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
if (verify(exp.code, value)) {
|
|
167
|
-
return value;
|
|
168
|
-
}
|
|
169
|
-
break;
|
|
170
|
-
case "identity":
|
|
176
|
+
switch (exp.op) {
|
|
177
|
+
case "code":
|
|
178
|
+
if (verify(exp.code, value)) {
|
|
171
179
|
return value;
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
return exp.vector.map(function(exp) {
|
|
200
|
-
var result;
|
|
201
|
-
result = run(exp, value);
|
|
202
|
-
if (result === void 0) {
|
|
203
|
-
throw new Error("Undefined");
|
|
204
|
-
}
|
|
180
|
+
}
|
|
181
|
+
break;
|
|
182
|
+
case "identity":
|
|
183
|
+
return value;
|
|
184
|
+
case "str":
|
|
185
|
+
case "int":
|
|
186
|
+
return exp[exp.op];
|
|
187
|
+
case "ref":
|
|
188
|
+
defn = (ref = run.defs.rels[exp.ref]) != null ? ref[0] : void 0;
|
|
189
|
+
if (defn != null) {
|
|
190
|
+
return run(defn, value);
|
|
191
|
+
}
|
|
192
|
+
return builtin[exp.ref](value);
|
|
193
|
+
case "dot":
|
|
194
|
+
return value[exp.dot];
|
|
195
|
+
case "comp":
|
|
196
|
+
return exp.comp.reduce(function(value, exp) {
|
|
197
|
+
if (value !== void 0) {
|
|
198
|
+
return run(exp, value);
|
|
199
|
+
}
|
|
200
|
+
}, value);
|
|
201
|
+
case "union":
|
|
202
|
+
ref1 = exp.union;
|
|
203
|
+
for (i = 0, len = ref1.length; i < len; i++) {
|
|
204
|
+
e = ref1[i];
|
|
205
|
+
result = run(e, value);
|
|
206
|
+
if (result !== void 0) {
|
|
205
207
|
return result;
|
|
206
|
-
});
|
|
207
|
-
case "union":
|
|
208
|
-
ref1 = exp.union;
|
|
209
|
-
for (i = 0, len = ref1.length; i < len; i++) {
|
|
210
|
-
e = ref1[i];
|
|
211
|
-
result = (function() {
|
|
212
|
-
try {
|
|
213
|
-
return run(e, value);
|
|
214
|
-
} catch (error) {}
|
|
215
|
-
})();
|
|
216
|
-
if (result !== void 0) {
|
|
217
|
-
return result;
|
|
218
|
-
}
|
|
219
208
|
}
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
209
|
+
}
|
|
210
|
+
return void 0;
|
|
211
|
+
case "vector":
|
|
212
|
+
result = [];
|
|
213
|
+
ref2 = exp.vector;
|
|
214
|
+
for (j = 0, len1 = ref2.length; j < len1; j++) {
|
|
215
|
+
e = ref2[j];
|
|
216
|
+
r = run(e, value);
|
|
217
|
+
if (r === void 0) {
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
result.push(r);
|
|
221
|
+
}
|
|
222
|
+
return result;
|
|
223
|
+
case "product":
|
|
224
|
+
result = {};
|
|
225
|
+
ref3 = exp.product;
|
|
226
|
+
for (k = 0, len2 = ref3.length; k < len2; k++) {
|
|
227
|
+
({label, exp} = ref3[k]);
|
|
228
|
+
r = run(exp, value);
|
|
229
|
+
if (r === void 0) {
|
|
230
|
+
return;
|
|
231
|
+
}
|
|
232
|
+
result[label] = r;
|
|
233
|
+
}
|
|
234
|
+
return result;
|
|
235
|
+
default:
|
|
236
|
+
return console.error(exp.op);
|
|
237
|
+
}
|
|
235
238
|
};
|
|
236
239
|
|
|
237
240
|
module.exports = run;
|
package/Examples/bnat.k
DELETED
|
@@ -1,121 +0,0 @@
|
|
|
1
|
-
-- Standard binary encoding of natural
|
|
2
|
-
-- numbers with addition (+).
|
|
3
|
-
|
|
4
|
-
_ = {{} _};
|
|
5
|
-
_1 = {() "1"};
|
|
6
|
-
_0 = {() "0"};
|
|
7
|
-
|
|
8
|
-
n0 = _ ;
|
|
9
|
-
n1 = _ _1;
|
|
10
|
-
|
|
11
|
-
from_digit = <
|
|
12
|
-
[0, ()] EQ _, -- {_: {}}
|
|
13
|
-
[1, ()] EQ n1, -- {"1": {_: {}}}
|
|
14
|
-
[2, ()] EQ _ _0 _1, -- {"1": {"0": {_: {}}}}
|
|
15
|
-
[3, ()] EQ _ _1 _1, -- {"1": {"1": {_: {}}}}
|
|
16
|
-
[4, ()] EQ _ _0 _0 _1, -- {"1": {"0": {"0": {_: {}}}}}
|
|
17
|
-
[5, ()] EQ _ _1 _0 _1,
|
|
18
|
-
[6, ()] EQ _ _0 _1 _1,
|
|
19
|
-
[7, ()] EQ _ _1 _1 _1,
|
|
20
|
-
[8, ()] EQ _ _0 _0 _0 _1,
|
|
21
|
-
[9, ()] EQ _ _1 _0 _0 _1,
|
|
22
|
-
[10, ()] EQ _ _0 _1 _0 _1
|
|
23
|
-
>;
|
|
24
|
-
|
|
25
|
-
to_digit = <
|
|
26
|
-
._ 0,
|
|
27
|
-
."1"._ 1,
|
|
28
|
-
."1"."0"._ 2,
|
|
29
|
-
."1"."1"._ 3,
|
|
30
|
-
."1"."0"."0"._ 4,
|
|
31
|
-
."1"."0"."1"._ 5,
|
|
32
|
-
."1"."1"."0"._ 6,
|
|
33
|
-
."1"."1"."1"._ 7,
|
|
34
|
-
."1"."0"."0"."0"._ 8,
|
|
35
|
-
."1"."0"."0"."1"._ 9,
|
|
36
|
-
."1"."0"."1"."0"._ 10,
|
|
37
|
-
."1"."0"."1"."1"._ 11,
|
|
38
|
-
."1"."1"."0"."0"._ 12,
|
|
39
|
-
."1"."1"."0"."1"._ 13,
|
|
40
|
-
."1"."1"."1"."0"._ 14,
|
|
41
|
-
."1"."1"."1"."1"._ 15
|
|
42
|
-
> ;
|
|
43
|
-
|
|
44
|
-
remove_leading_zeros =
|
|
45
|
-
< ."0" remove_leading_zeros, () >;
|
|
46
|
-
|
|
47
|
-
_append_zero =
|
|
48
|
-
< ._ _ _0, -- ( _ -> 0 _)
|
|
49
|
-
."1" _append_zero _1, -- ('1 ... -> '1 [$.1 | _append_zero]),
|
|
50
|
-
."0" _append_zero _0 -- ('0 ... -> '0 [$.0 | _append_zero]),
|
|
51
|
-
>
|
|
52
|
-
;
|
|
53
|
-
|
|
54
|
-
_append_one =
|
|
55
|
-
<
|
|
56
|
-
._ _ _1,
|
|
57
|
-
."1" _append_one _1,
|
|
58
|
-
."0" _append_one _0
|
|
59
|
-
>
|
|
60
|
-
;
|
|
61
|
-
|
|
62
|
-
reverse_bnat = {() x, _ y} _reverse_bnat;
|
|
63
|
-
|
|
64
|
-
_reverse_bnat =
|
|
65
|
-
<
|
|
66
|
-
{.x._ x, .y y} .y,
|
|
67
|
-
{.x."0" x, .y _0 y} _reverse_bnat,
|
|
68
|
-
{.x."1" x, .y _1 y} _reverse_bnat
|
|
69
|
-
>;
|
|
70
|
-
|
|
71
|
-
_add2wc =
|
|
72
|
-
-- least significant bit first addition with carry
|
|
73
|
-
--- :{ :bnat x, :bnat y, :[0,1] c}
|
|
74
|
-
-- _log!
|
|
75
|
-
<
|
|
76
|
-
{.x."0" x, .y."0" y, .c."0" c} {.x x, .y y, _ _0 c} _add2wc _0,
|
|
77
|
-
{.x."0" x, .y."0" y, .c."1" c} {.x x, .y y, _ _0 c} _add2wc _1,
|
|
78
|
-
|
|
79
|
-
{.x."0" x, .y."1" y, .c."0" c} {.x x, .y y, _ _0 c} _add2wc _1,
|
|
80
|
-
{.x."0" x, .y."1" y, .c."1" c} {.x x, .y y, _ _1 c} _add2wc _0,
|
|
81
|
-
|
|
82
|
-
{.x."1" x, .y."0" y, .c."0" c} {.x x, .y y, _ _0 c} _add2wc _1,
|
|
83
|
-
{.x."1" x, .y."0" y, .c."1" c} {.x x, .y y, _ _1 c} _add2wc _0,
|
|
84
|
-
|
|
85
|
-
{.x."1" x, .y."1" y, .c."0" c} {.x x, .y y, _ _1 c} _add2wc _0,
|
|
86
|
-
{.x."1" x, .y."1" y, .c."1" c} {.x x, .y y, _ _1 c} _add2wc _1,
|
|
87
|
-
{.x._ x, .c."0" c, .y y} .y,
|
|
88
|
-
{.y._ y, .c."0" c, .x x} .x,
|
|
89
|
-
{.x._ x, .c."1" c, .y._ y} n1,
|
|
90
|
-
{.x._ x, .y."0" y, .c."1" c} .y _1,
|
|
91
|
-
{.x."0" x, .y._ y, .c."1" c} .x _1,
|
|
92
|
-
{.x._ x, .y."1" y, .c."1" c} {_ x, .y y, {} _1 c} _add2wc _0,
|
|
93
|
-
{.x."1" x, .y._ y, .c."1" c} {.x x, _ y, {} _1 c} _add2wc _0
|
|
94
|
-
>;
|
|
95
|
-
|
|
96
|
-
plus = <
|
|
97
|
-
SNOC {
|
|
98
|
-
x: .0 from_digit remove_leading_zeros reverse_bnat,
|
|
99
|
-
y: .1 plus remove_leading_zeros reverse_bnat,
|
|
100
|
-
c: {} _0 } _add2wc reverse_bnat,
|
|
101
|
-
.0 from_digit
|
|
102
|
-
>;
|
|
103
|
-
|
|
104
|
-
to_string = <
|
|
105
|
-
._ "0",
|
|
106
|
-
toString
|
|
107
|
-
>;
|
|
108
|
-
|
|
109
|
-
toString = <
|
|
110
|
-
._ "",
|
|
111
|
-
["0", ."0" toString] CONCAT,
|
|
112
|
-
["1", ."1" toString] CONCAT
|
|
113
|
-
>;
|
|
114
|
-
|
|
115
|
-
[(), plus]
|
|
116
|
-
{
|
|
117
|
-
input: .0,
|
|
118
|
-
sum: {raw: .1, digits: .1 <to_digit,"...too big...">, binary: .1 to_string}
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
-- example: > echo '[9,9,9,8]' | ./k.sh -k ./bnat.k
|
package/Examples/code.k
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
inc = [(),1] PLUS;
|
|
2
|
-
max = <GT .0, .1>; -- select max from vector of two
|
|
3
|
-
-- Finding a max in any vector would be:
|
|
4
|
-
-- max = < SNOC [.0, .1 max] <GT .0, .1>, .0 >;
|
|
5
|
-
$tree = < string leaf, {tree left, tree right} tree>;
|
|
6
|
-
height = $tree <
|
|
7
|
-
.leaf 0,
|
|
8
|
-
.tree [.left height, .right height] max inc
|
|
9
|
-
> $int;
|
|
10
|
-
|
|
11
|
-
<$tree,
|
|
12
|
-
{tree:
|
|
13
|
-
{ left: { leaf: "a"},
|
|
14
|
-
right: { tree:
|
|
15
|
-
{ left: {leaf: "b"},
|
|
16
|
-
right: {leaf: "c"}
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
> _log! height
|
package/Examples/error.k
DELETED
package/Examples/factorial.k
DELETED
package/Examples/fib.k
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
pos? = [(), 0] GT .0;
|
|
2
|
-
dec = [(), -1] PLUS;
|
|
3
|
-
fib_aux =
|
|
4
|
-
<
|
|
5
|
-
{ .n pos? dec n,
|
|
6
|
-
."fib(n),fib(n-1),..." [[.0,.1] PLUS, ()] CONS "fib(n),fib(n-1),..."
|
|
7
|
-
} fib_aux,
|
|
8
|
-
."fib(n),fib(n-1),..."
|
|
9
|
-
>
|
|
10
|
-
;
|
|
11
|
-
|
|
12
|
-
fib = {() n, [1,1] "fib(n),fib(n-1),..."}
|
|
13
|
-
fib_aux _log! .0;
|
|
14
|
-
|
|
15
|
-
----------------------
|
|
16
|
-
|
|
17
|
-
fib
|