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
264
265
266
|
# File 'lib/stdlib.rb', line 24
def fill_context(globalctx, types)
globalctx["Natural"] = Types::Natural
globalctx["Integer"] = Types::Integer
globalctx["Double"] = Types::Double
globalctx["Bool"] = Types::Bool
globalctx["Text"] = Types::Text
globalctx["Type"] = Types::Type.new
types["Natural"] = Types::Type.new(Types::Natural)
types["Integer"] = Types::Type.new(Types::Integer)
types["Double"] = Types::Type.new(Types::Double)
types["Bool"] = Types::Type.new(Types::Bool)
types["Text"] = Types::Type.new(Types::Text)
types["Type"] = Types::Type.new(Types::Type.new)
globalctx["Natural/show"] = BuiltinFunction.new { |arg| arg.to_s }
types["Natural/show"] = Types::Function.new(Types::Natural, Types::Text)
globalctx["Integer/show"] = BuiltinFunction.new { |arg| arg.to_s }
types["Integer/show"] = Types::Function.new(Types::Integer, Types::Text)
globalctx["Double/show"] = BuiltinFunction.new { |arg| arg.to_s }
types["Double/show"] = Types::Function.new(Types::Double, Types::Text)
globalctx["Text/show"] = BuiltinFunction.new { |arg| arg }
types["Text/show"] = Types::Function.new(Types::Text, Types::Text)
natToInt = BuiltinFunction.new { |arg| arg }
globalctx["Natural/toInteger"] = natToInt
types["Natural/toInteger"] = Types::Function.new(Types::Natural, Types::Integer)
natToDou = BuiltinFunction.new { |arg| arg.to_f }
globalctx["Natural/toDouble"] = natToDou
types["Natural/toDouble"] = Types::Function.new(Types::Natural, Types::Double)
intToNat = BuiltinFunction.new { |arg| arg.abs }
globalctx["Integer/toNatural"] = intToNat
types["Integer/toNatural"] = Types::Function.new(Types::Integer, Types::Natural)
intToDou = BuiltinFunction.new { |arg| arg.to_f }
globalctx["Integer/toDouble"] = intToDou
types["Integer/toDouble"] = Types::Function.new(Types::Integer, Types::Double)
douToNat = BuiltinFunction.new { |arg| arg.round.abs }
globalctx["Double/toNatural"] = douToNat
types["Double/toNatural"] = Types::Function.new(Types::Double, Types::Natural)
douToInt = BuiltinFunction.new { |arg| arg.round }
globalctx["Double/toInteger"] = douToInt
types["Double/toInteger"] = Types::Function.new(Types::Double, Types::Integer)
globalctx["List"] = BuiltinFunction.new { |a| Types::List.new(a) }
types["List"] = make_fn_type([:list_a], Types::Type.new(Types::List.new(Types::Unresolved.new(:list_a))))
list_length_type = make_fn_type([:list_len_a], Types::List.new(Types::Unresolved.new(:list_len_a)), Types::Natural)
globalctx["List/length"] = BuiltinFunction.new{ |a|
BuiltinFunction.new { |list|
list.length
}
}
types["List/length"] = list_length_type
list_fold_type = make_fn_type(
[:list_fold_a],
Types::List.new(Types::Unresolved.new(:list_fold_a)),
[:list_fold_b],
make_fn_type(:list_fold_a, :list_fold_b, :list_fold_b),
:list_fold_b, :list_fold_b)
globalctx["List/fold"] = BuiltinFunction.new { |a|
BuiltinFunction.new { |list|
BuiltinFunction.new { |b|
BuiltinFunction.new { |f|
BuiltinFunction.new { |x|
acc = x
list.reverse_each { |elm| acc = f.call(elm).call(acc) }
acc
}
}
}
}
}
types["List/fold"] = list_fold_type
list_head_type = make_fn_type(
[:list_head_a],
Types::List.new(Types::Unresolved.new(:list_head_a)),
Types::Optional.new(Types::Unresolved.new(:list_head_a)))
globalctx["List/head"] = BuiltinFunction.new { |a|
BuiltinFunction.new { |list|
list.first
}
}
types["List/head"] = list_head_type
list_last_type = make_fn_type(
[:list_last_a],
Types::List.new(Types::Unresolved.new(:list_last_a)),
Types::Optional.new(Types::Unresolved.new(:list_last_a)))
globalctx["List/last"] = BuiltinFunction.new { |a|
BuiltinFunction.new { |list|
list.last
}
}
types["List/last"] = list_last_type
list_tail_type = make_fn_type(
[:list_tail_a],
Types::List.new(Types::Unresolved.new(:list_tail_a)),
Types::List.new(Types::Unresolved.new(:list_tail_a)))
globalctx["List/tail"] = BuiltinFunction.new { |a|
BuiltinFunction.new { |list|
if list.empty?
[]
else
list[1..list.length]
end
}
}
types["List/tail"] = list_tail_type
types["List/reverse"] = make_fn_type(
[:list_reverse_a],
Types::List.new(Types::Unresolved.new(:list_reverse_a)),
Types::List.new(Types::Unresolved.new(:list_reverse_a)))
globalctx["List/reverse"] = BuiltinFunction.new { |a|
BuiltinFunction.new { |list| list.reverse }
}
types["List/build"] = make_fn_type(
[:list_build_a],
make_fn_type(
[:list_build_b],
make_fn_type(:list_build_a, :list_build_b, :list_build_b), :list_build_b, :list_build_b),
Types::List.new(Types::Unresolved.new(:list_build_a)))
globalctx["List/build"] = BuiltinFunction.new { |a|
BuiltinFunction.new { |f|
cons = BuiltinFunction.new { |x|
BuiltinFunction.new { |list| [x] + list }
}
f.call(Types::List.new(a)).call(cons).call([])
}
}
types["List/indexed"] = make_fn_type(
[:list_indexed_a],
Types::List.new(Types::Unresolved.new(:list_indexed_a)),
Types::List.new(Types::Record.new({
"index" => Types::Natural,
"value" => Types::Unresolved.new(:list_indexed_a)})))
globalctx["List/indexed"] = BuiltinFunction.new { |a|
BuiltinFunction.new { |list|
list.map.with_index { |val, idx|
{ "index" => idx, "value" => val }
}
}
}
globalctx["Optional"] = BuiltinFunction.new { |a|
Types::Optional.new(a)
}
types["Optional"] = make_fn_type([:opt_a], Types::Type.new(Types::Optional.new(Types::Unresolved.new(:opt_a))))
optional_fold_type = make_fn_type(
[:opt_fold_a],
Types::Optional.new(Types::Unresolved.new(:opt_fold_a)),
[:opt_fold_b],
make_fn_type(:opt_fold_a, :opt_fold_b),
:opt_fold_b, :opt_fold_b)
globalctx["Optional/fold"] = BuiltinFunction.new { |a|
BuiltinFunction.new { |opt|
BuiltinFunction.new { |b|
BuiltinFunction.new { |f|
BuiltinFunction.new { |x|
if opt.nil?
x
else
f.call(opt)
end
}
}
}
}
}
types["Optional/fold"] = optional_fold_type
types["Optional/build"] = make_fn_type(
[:opt_build_a],
make_fn_type([:opt_build_b],
make_fn_type(:opt_build_a, :opt_build_b), :opt_build_b, :opt_build_b),
Types::Optional.new(Types::Unresolved.new(:opt_build_a)))
globalctx["Optional/build"] = BuiltinFunction.new { |a|
BuiltinFunction.new { |f|
just = BuiltinFunction.new { |x| x }
f.call(Types::Optional.new(a)).call(just).call(nil)
}
}
natural_fold_type = make_fn_type(
Types::Natural,
[:nat_fold_a],
make_fn_type(:nat_fold_a, :nat_fold_a),
:nat_fold_a, :nat_fold_a)
globalctx["Natural/fold"] = BuiltinFunction.new { |n|
BuiltinFunction.new { |a|
BuiltinFunction.new { |succ|
BuiltinFunction.new { |zero|
res = zero
n.times { res = succ.call(res) }
res
}
}
}
}
types["Natural/fold"] = natural_fold_type
types["Natural/even"] = make_fn_type(Types::Natural, Types::Bool)
globalctx["Natural/even"] = BuiltinFunction.new { |n| n % 2 == 0 }
types["Natural/odd"] = make_fn_type(Types::Natural, Types::Bool)
globalctx["Natural/odd"] = BuiltinFunction.new { |n| n % 2 == 1 }
types["Natural/isZero"] = make_fn_type(Types::Natural, Types::Bool)
globalctx["Natural/isZero"] = BuiltinFunction.new { |n| n == 0 }
globalctx["Natural/build"] = BuiltinFunction.new { |f|
zero = 0
succ = BuiltinFunction.new { |n| n + 1 }
f.call(Types::Natural).call(succ).call(zero)
}
types["Natural/build"] = make_fn_type(make_fn_type([:nat_build_a], make_fn_type(:nat_build_a, :nat_build_a), :nat_build_a, :nat_build_a), Types::Natural)
end
|