-
Notifications
You must be signed in to change notification settings - Fork 0
/
runtime.lua
263 lines (227 loc) · 8.49 KB
/
runtime.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
function Array(x1) return { _type = "Array", _args = {x1}, _value = x1} end
function IsString(a) return type(a) == "string" end
function IsNum(a) return type(a) == "number" end
function IsBool(a) return type(a) == "boolean" end
function Either(f1, f2)
return function(a)
if f1(a) then return true end
if f2(a) then return true end
return false
end
end
function Choice(fs)
return function(a)
for _, f in ipairs(fs) do
if f(a) then return true end
end
return false
end
end
function tablelength(T)
local count = 0
for _ in pairs(T) do count = count + 1 end
return count
end
function IsStruct(spec)
local spec = spec.structSpec
return function(struct)
if type(struct) ~= "table" then return false end
if spec == nil then return false end
if tablelength(spec) ~= tablelength(struct) then return false end
for k, v in pairs(struct) do
if spec[k] == nil then return false end
if not spec[k](v) then return false end
end
return true
end
end
function AnyMatching(spec)
local spec = spec.constraintSpec
return function(struct)
if spec == nil then return false end
if next(spec) == nil then return true end
if type(struct) ~= "table" then return false end
for k, f in pairs(spec) do
if struct[k] == nil then return false end
if not f(struct[k]) then return false end
end
return true
end
end
function IsNamedType(spec)
local spec = spec.namedTypeSpec
return function(typ)
if spec == nil then return false end
if type(typ) ~= "table" then return false end
if typ._type ~= spec.name then return false end
if tablelength(spec.args) > tablelength(typ._args) then return false end
for k, v in ipairs(typ._args) do
if spec.args[k] ~= nil then
if not spec.args[k](v) then return false end
end
end
return true
end
end
function getArgs(fun)
local args = {}
local hook = debug.gethook()
local argHook = function( ... )
local info = debug.getinfo(3)
if 'pcall' ~= info.name then return end
for i = 1, math.huge do
local name, value = debug.getlocal(2, i)
if '(*temporary)' == name or name == nil then
debug.sethook(hook)
error('')
return
end
table.insert(args,name)
end
end
debug.sethook(argHook, "c")
pcall(fun)
return args
end
function IsFunction(argNum)
return function(f)
if type(f) ~= "function" then return false end
return tablelength(getArgs(f)) == argNum
end
end
function IsType(val, spec)
if spec == "Num" then return IsNum(val) end
if spec == "Bool" then return IsBool(val) end
if spec == "String" then return IsString(val) end
if type(spec) == "function" then return spec(val) end
if type(spec) == "table" then
if type(val) == "function" and spec.functionSpec ~= nil then return true end
if type(val) == "table" and spec.constraintSpec ~= nil then return AnyMatching(spec)(val) end
if type(val) == "table" and spec.structSpec ~= nil then return IsStruct(spec)(val) end
if type(val) == "table" and val ~= nil and val._type ~= nil and spec.namedTypeSpec ~= nil then return IsNamedType(spec)(val) end
end
return false
end
function newOpenFunction()
local t = {}
function f(t, ...)
for _, pair in pairs(t) do
pred, fn = unpack(pair)
if pred(...) then return fn(...) end
end
return nil
end
setmetatable(t, {__call = f})
return t
end
function newOpenInstance(fTable, pred, body)
table.insert(fTable, {pred, body})
end
eq = newOpenFunction()
newOpenInstance(eq, function(a, b) return IsType(a, IsString) and IsType(b, IsString) end, function(a, b) return a==b end)
newOpenInstance(eq, function(a, b) return IsType(a, IsNum) and IsType(b, IsNum) end, function(a, b) return a==b end)
neq = newOpenFunction()
newOpenInstance(neq, function(a, b) return IsType(a, IsString) and IsType(b, IsString) end, function(a, b) return a~=b end)
newOpenInstance(neq, function(a, b) return IsType(a, IsNum) and IsType(b, IsNum) end, function(a, b) return a~=b end)
gt = newOpenFunction()
newOpenInstance(gt, function(a, b) return IsType(a, IsNum) and IsType(b, IsNum) end, function(a, b) return a>b end)
newOpenInstance(gt, function(a, b) return IsType(a, IsString) and IsType(b, IsString) end, function(a, b) return a>b end)
gte = newOpenFunction()
newOpenInstance(gte, function(a, b) return IsType(a, IsNum) and IsType(b, IsNum) end, function(a, b) return a>=b end)
newOpenInstance(gt, function(a, b) return IsType(a, IsString) and IsType(b, IsString) end, function(a, b) return a>=b end)
lt = newOpenFunction()
newOpenInstance(lt, function(a, b) return IsType(a, IsNum) and IsType(b, IsNum) end, function(a, b) return a<b end)
newOpenInstance(gt, function(a, b) return IsType(a, IsString) and IsType(b, IsString) end, function(a, b) return a<b end)
lte = newOpenFunction()
newOpenInstance(lte, function(a, b) return IsType(a, IsNum) and IsType(b, IsNum) end, function(a, b) return a<=b end)
newOpenInstance(gt, function(a, b) return IsType(a, IsString) and IsType(b, IsString) end, function(a, b) return a<=b end)
add = newOpenFunction()
newOpenInstance(add, function(a, b) return IsType(a, IsNum) and IsType(b, IsNum) end, function(a, b) return a+b end)
newOpenInstance(add, function(a, b) return IsType(a, IsString) and IsType(b, IsString) end, function(a, b) return a..b end)
sub = newOpenFunction()
newOpenInstance(sub, function(a, b) return IsType(a, IsNum) and IsType(b, IsNum) end, function(a, b) return a-b end)
mul = newOpenFunction()
newOpenInstance(mul, function(a, b) return IsType(a, IsNum) and IsType(b, IsNum) end, function(a, b) return a*b end)
div = newOpenFunction()
newOpenInstance(div, function(a, b) return IsType(a, IsNum) and IsType(b, IsNum) end, function(a, b) return a/b end)
mod = newOpenFunction()
newOpenInstance(mod, function(a, b) return IsType(a, IsNum) and IsType(b, IsNum) end, function(a, b) return math.mod(a, b) end)
anded = newOpenFunction()
newOpenInstance(anded, function(a, b) return IsType(a, IsBool) and IsType(b, IsBool) end, function(a, b) return a and b end)
ored = newOpenFunction()
newOpenInstance(ored, function(a, b) return IsType(a, IsBool) and IsType(b, IsBool) end, function(a, b) return a or b end)
getWrappedArray = function(a)
local axx = a._value
local ax
if type(axx) == "table" and axx._wrapped then
ax = axx._wrapped
else
ax = {axx}
end
return ax
end
concat = function(a, b)
local ax, bx = getWrappedArray(a), getWrappedArray(b)
local new_table = duplicate(ax)
for _, v in ipairs(bx) do
table.insert(new_table, v)
end
return Array({_wrapped = new_table})
end
index = function(a, i)
local ax = getWrappedArray(a)
local v = ax[i]
if v == nil then return {} end
return v
end
write = function(a)
if type(a) == "table" and a._type and a._type == "Array" then
io.write("[")
local ax = getWrappedArray(a)
for _, v in ipairs(ax) do
write(v)
io.write(", ")
end
io.write("]")
return
end
if type(a) == "table" then
if a._type ~= nil then
io.write(a._type)
io.write("(")
for _, v in pairs(a._args) do
write(v)
io.write(", ")
end
io.write(")")
return
end
io.write("{")
for k, v in pairs(a) do
io.write(k)
io.write(": ")
write(v)
io.write(", ")
end
io.write("}")
return
end
io.write(tostring(a))
end
function duplicate(obj, seen)
if type(obj) ~= 'table' then return obj end
if seen and seen[obj] then return seen[obj] end
local s = seen or {}
local res = setmetatable({}, getmetatable(obj))
s[obj] = res
for k, v in pairs(obj) do res[duplicate(k, s)] = duplicate(v, s) end
return res
end
println = function(a)
write(a)
io.write("\n")
return a
end
-- print(IsType({a = "A", b = 3}, {constraintSpec = {a = IsString}}))
-- print(IsFunction(3)(function(a, b, c) return 0 end))
-- print(IsType({_type = "Tup", a = "A", b = 3, _args = {"A", 3}}, {namedTypeSpec = {name = "Tup", args = {IsString, Choice({IsNum, IsBool})}}}))