-
Notifications
You must be signed in to change notification settings - Fork 1
/
prelude.sky
289 lines (170 loc) · 5.43 KB
/
prelude.sky
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
% prelude.sky
%
% Predefined Husky functions by Robert A. van Engelen
pi := 3.141592653589793.
% the 'let' construct expands into nested 'where'
1102 prefix (let).
1104 infixr (in).
(let def; defs in val) == (let def in let defs in val).
(let def in val) == (val where def).
% the 'case' construct expands into a lambda application
960 prefix (case).
962 infix (of).
case a of b == b:a.
% the 'if-then-else' construct expands into 'case'
962 prefix (if).
964 infixl (then).
964 infixl (else).
a then if b then c else d == a then (if b then c else d).
a else if b then c else d == a else (if b then c else d).
if a then b else c == case a of (true -> b; false -> c).
% list comprehensions expand into map, filter, and concat
1050 infix (<-).
[ f | x <- xs; p; y <- ys; r ] == concat(map(x -> [ f | y <- ys; r ], filter(x -> p, xs))).
[ f | x <- xs; p; y <- ys ] == concat(map(x -> [ f | y <- ys ], filter(x -> p, xs))).
[ f | x <- xs; y <- ys; r ] == concat(map(x -> [ f | y <- ys; r ], xs)).
[ f | x <- xs; y <- ys ] == concat(map(x -> [ f | y <- ys ], xs)).
[ f | x <- xs; p; q; r ] == [ f | x <- xs; p /\ q; r ].
[ f | x <- xs; p; q ] == [ f | x <- xs; p /\ q ].
[ x | x <- xs; p ] == filter(x -> p, xs).
[ f | x <- xs; p ] == map(x -> f, filter(x -> p, xs)).
[ x | x <- xs ] == xs.
[ f | x <- xs ] == map(x -> f, xs).
% list length
110 prefix (#).
# [] := 0;
# x.xs := 1 + # xs.
% list element access by index from 1
110 infixl (?).
x.xs ? 1 := x;
x.xs ? n := xs ? (n-1).
% list membership
700 infix (is_in).
x is_in [] := false;
x is_in y.ys := if x = y then true else x is_in ys.
700 infix (not_in).
x not_in [] := true;
x not_in y.ys := if x = y then false else x not_in ys.
% init of a list
init([]) := nil;
init(x.xs) := if xs = [] then [] else x.init(xs).
% last of a list
last([]) := nil;
last(x.xs) := if xs = [] then x else last(xs).
% list concatenation
950 infixr (++).
[] ++ ys := ys;
x.xs ++ ys := x.(xs ++ ys).
% list subtraction
950 infix (\\).
[] \\ ys := [];
x.xs \\ ys := if x is_in ys then xs \\ ys else x.(xs \\ ys).
% first and second tuple elements
fst((x, y)) := x.
snd((x, y)) := y.
% even and odd
even(x) := x mod 2 = 0.
odd(x) := x mod 2 <> 0.
% GCD
gcd(0, b) := b;
gcd(a, b) := gcd(b mod a, a).
% LCM
lcm(a, b) := a * b / gcd(a, b).
% factorial
fac(0) := 1;
fac(n) := n * fac(n - 1).
% Fibonacci
fib1(1, a, b) := a + b;
fib1(n, a, b) := fib1(n - 1, b, a + b).
fib(0) := 0;
fib(1) := 1;
fib(n) := fib1(n - 1, 0, 1).
% map
map(f, []) := [];
map(f, x.xs) := f(x).map(f, xs).
% filter
filter(p, []) := [];
filter(p, x.xs) := if p(x) then x.filter(p, xs) else filter(p, xs).
% left and right fold
foldl((** -> * -> **), **, [*]) :: ** .
foldl(f, a, []) := a;
foldl(f, a, x.xs) := foldl(f, f(a, x), xs).
foldr((* -> ** -> **), **, [*]) :: ** .
foldr(f, a, []) := a;
foldr(f, a, x.xs) := f(x, foldr(f, a, xs)).
foldl1((* -> * -> *), [*]) :: * .
foldl1(f, []) := nil;
foldl1(f, x.xs) := foldl(f, x, xs).
foldr1((* -> * -> *), [*]) :: * .
foldr1(f, []) := nil;
foldr1(f, x.xs) := if xs = [] then x else f(x, foldr1(f, xs)).
% left and right scan
scanl((** -> * -> **), **, [*]) :: [**].
scanl(f, a, []) := [a];
scanl(f, a, x.xs) := a.scanl(f, f(a, x), xs).
scanr((* -> ** -> **), **, [*]) :: [**].
scanr(f, a, []) := [a];
scanr(f, a, x.xs) := f(x, hd(ys)).ys where ys := scanr(f, a, xs).
% all and any
all(_, []) := true;
all(p, x.xs) := if p(x) then all(p, xs) else false.
any(_, []) := false;
any(p, x.xs) := if p(x) then true else any(p, xs).
% concat list of lists
concat := foldr(++, []).
% zip two lists
zip([], ys) := [];
zip(x.xs, []) := [];
zip(x.xs, y.ys) := (x,y).zip(xs, ys).
% unzip tuple list
unzip([]) := ([],[]);
unzip(p.xys) := (x.xs,y.ys) where (x,y) := p where (xs,ys) := unzip(xys).
% zip with operator
zipwith(f, [], ys) := [];
zipwith(f, x.xs, []) := [];
zipwith(f, x.xs, y.ys) := f(x, y).zipwith(f, xs, ys).
% until applies f to x until p(x) holds
until(p, f, x) := if p(x) then x else until(p, f, f(x)).
% iterate returns list of repeated application of f to x
iterate(f, x) := x.iterate(f, f(x)).
% from gives an integer list from a given start integer
from(n) := n.from(n + 1).
% repeat value in list
repeat(x) := x.repeat(x).
% replicate value in list n times
replicate(0, _) := [];
replicate(n, x) := x.replicate(n - 1, x).
% cycle list
cycle(xs) := xs ++ cycle(xs).
% drop n elements from a list
drop(0, xs) := xs;
drop(n, []) := [];
drop(n, x.xs) := drop(n - 1, xs).
% take n elements from a list
take(0, xs) := [];
take(n, []) := [];
take(n, x.xs) := x.take(n - 1, xs).
% drop elements from list while condition on element is true
dropwhile(p, []) := [];
dropwhile(p, x.xs) := if p(x) then dropwhile(p, xs) else x.xs.
% take elements from list while condition on element is true
takewhile(p, []) := [];
takewhile(p, x.xs) := if p(x) then x.takewhile(p, xs) else [].
% a..b generates a list of integers from a to b
650 infix (..).
a .. b := if a <= b then take(b - a + 1, from(a)) else [].
% list reversal
reverse1(ys, []) := ys;
reverse1(ys, x.xs) := reverse1(x.ys, xs).
reverse(xs) := reverse1([], xs).
% function composition (f @ g reads "f after g")
955 infixr (@).
(f @ g):x := f(g(x)).
% twiddle operands, note the use of f(x) as a formal argument
twiddle(f(x), y) := f(x, y).
% flip operands
flip(f, x, y) := f(y, x).
% curry
curry(f, x, y) := f((x,y)).
% uncurry
uncurry(f, (x,y)) := f(x, y).