-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.plum
416 lines (365 loc) · 11.2 KB
/
test.plum
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
# Welcome to Plum!
# Nothing (aka Unit or Void)
# Nothing is a type with only one instance. From an information theoretical
# perspective, being given an instance of a Nothing type gives you no
# information whatsoever. The size of a Nothing instance is zero – it disappears
# at compile time! Like a ghost!
# Functions without an explicit return type return Nothing by default. Functions
# that don't have anything useful to return (such as print) return Nothing
# instead. Empty bodies return Nothing.
Nothing =
| Nothing
#fun write[W](writer: W, nothing: Nothing) { writer."nothing" }
#fun write_debug[W](writer: W, nothing: Nothing) { writer."nothing" }
#fun ==(a: Nothing, b: Nothing): Bool { true }
equals a: Nothing b: Nothing -> Bool = True
ignore value: t -> Nothing = Nothing
# Never
# Never is a type with zero instances. If you write a function that accepts an
# argument of Never, it can never be called (otherwise, the caller would have
# a Never instance).
# Why do we need something like this? Some expressions always abort control
# flow, for example `crash "Oh no!"`. These evaluate to the Never type. Because
# Never is never instantiated, it can be assigned to anything:
#
# some_bool = crash "Oh no!"
crash message: String -> Never = builtin_crash message
# Bool
# A type with two instances.
Bool =
| True
False
not bool: Bool -> Bool =
bool
% True: False
False: True
equals a: Bool b: Bool -> Bool =
a
% True: b
False: not b
and a: Bool b: Bool -> Bool =
a
% True: b
False: False
or a: Bool b: Bool -> Bool =
a
% True: True
False: b
xor a: Bool b: Bool -> Bool =
a
% True: not b
False: b
implies a: Bool b: Bool -> Bool =
a
% True: b
False: True
# fun write[W](writer: W, b: Bool) { writer.write(if b then "true" else "false") }
# fun hash(hasher: &Hasher, value: Bool) { hasher.hash(if value then 0 else 1) }
# Comparisons
Ordering =
| Less
Equal
Greater
flip ordering: Ordering -> Ordering =
ordering
% Less: Greater
Equal: Equal
Greater: Less
less_than a: Int b: Int -> Bool =
compare a b
% Less: True
Equal: False
Greater: False
# fun <[T](a: T, b: T): Bool { a <=> b is less }
# fun >[T](a: T, b: T): Bool { a <=> b is greater }
# fun >=[T](a: T, b: T): Bool { not(a <=> b is less) }
# fun <=[T](a: T, b: T): Bool { not(a <=> b is greater) }
# fun min[T](a: T, b: T): T { if a < b then a else b }
# fun max[T](a: T, b: T): T { if a > b then a else b }
# fun clamp[T](a: T, range: Range[T]): T { max(range.start, min(range.end.dec(), a)) }
# Hashing
# You should implement a hash(&Hasher, T) function for types that can be hashed.
# In this function, they can contribute some uniqueness/entropy to the Hasher by
# calling other hash functions on it. In the end, all hash functions boil down
# to hash(&Hasher, Int). The Hasher uses the djb2 algorithm.
#Hasher =
# | Hasher Int
#initial_hasher -> Hasher = Hasher 5381
#add hasher: Hasher value: Int -> Hasher =
# hasher
# % Hasher state: state | * 33 | + value | Hasher
#finish hasher: Hasher -> Int =
# hasher
# % Hasher state: state | * 33
#fun hash_all[I](hasher: &Hasher, iter: I) {
# for item in iter do hasher.hash(item)
#}
# Byte
# A value from 0 to 255. All arithmetic operations wrap around. TODO: do they?
# add a: Byte b: Byte -> Byte = builtin_add_bytes a b
# sub a: Byte b: Byte -> Byte = builtin_sub_bytes a b
# mul a: Byte b: Byte -> Byte = builtin_mul_bytes a b
# / a: Byte b: Byte -> Byte = builtin_/_bytes a b
# mod a: Byte b: Byte -> Byte = builtin_mod_bytes a b
# and a: Byte b: Byte -> Byte = builtin_and_bytes a b
# or a: Byte b: Byte -> Byte = builtin_or_bytes a b
# xor a: Byte b: Byte -> Byte = builtin_xor_bytes a b
# compare a: Byte b: Byte -> Ordering = builtin_compare_bytes a b
# to_int byte: Byte -> Int = builtin_byte_to_int byte
# equals a: Byte b: Byte -> Bool =
# compare a b
# % Less: False
# Equal: True
# Greater: False
# fun hash(hasher: &Hasher, byte: Byte) { hasher.hash(byte.to_int()) }
# fun copy(byte: Byte) { byte }
#fun write[W](writer: W, int: Byte) { writer.write(radix(int, 10)) }
#fun radix(int: Byte, radix: Int): RadixFormat { radix(int.to_int(), radix) }
#fun digit_to_char(digit: Byte): Char {
# if digit.to_int() > 9
# then {digit - 10.lower_byte() + #a.byte}.to_char()
# else {digit + #0.byte}.to_char()
#}
#fun write_debug[W](writer: W, byte: Byte) { writer."{byte}" }
# Int
# If you write a number such as 3 in the code, it's an Int.
# var min_int = 0 - 9223372036854775806
# var max_int = 9223372036854775807
+ a: Int b: Int -> Int = builtin_add_ints a b
- a: Int b: Int -> Int = builtin_sub_ints a b
* a: Int b: Int -> Int = builtin_mul_ints a b
/ a: Int b: Int -> Int = builtin_div_ints a b
mod a: Int b: Int -> Int = builtin_mod_ints a b
and a: Int b: Int -> Int = builtin_and_ints a b
or a: Int b: Int -> Int = builtin_or_ints a b
xor a: Int b: Int -> Int = builtin_xor_ints a b
shift_left int: Int by: Int -> Int =
by | equals 0
% True: int
False: int | mul 2 | shift_left (by | - 1)
shift_right int: Int by: Int -> Int =
by | equals 0
% True: int
False: int | / 2 | shift_left (by | - 1)
# pow base: Int exponent: Int -> Int = pow_rec 1 base exponent
pow_rec result: Int base: Int exponent: Int -> Int =
# TODO: do this more efficiently
exponent | equals 0
% True: result
False: pow_rec (result | * base) base (exponent | - 1)
negate int: Int -> Int = 0 | - int
abs int: Int -> Int =
compare int 0
% Less: negate int
Equal: 0
Greater: int
compare a: Int b: Int -> Ordering = builtin_compare_ints a b
round_up_to_multiple_of number: Int factor: Int -> Int =
number | + factor | - 1 | / factor | mul factor
round_up_to_power_of number: Int factor: Int -> Int =
round_up_to_power_of_rec 1 number factor
round_up_to_power_of_rec power: Int number: Int factor: Int -> Int =
# TODO: do this more efficiently
is_big_enough =
compare power number
% Less: False
Equal: True
Greater: True
is_big_enough
% True: power
False: round_up_to_power_of_rec (power | * factor) number factor
sqrt num: Int -> Int =
# num >= 0 or panic("you can't take the sqrt of a negative number")
sqrt_rec 0 num
sqrt_rec candidate: Int target: Int -> Int =
next = candidate | + 1
mul next next | compare target
% Less: sqrt_rec next target
Equal: next
Greater: candidate
log_two value: Int -> Int =
value | equals 1
% True: 0
False: log_two (value | / 2) | + 1
# lower_byte int: Int -> Byte = builtin_lower_byte int
equals a: Int b: Int -> Bool =
builtin_compare_ints a b
% Less: False
Equal: True
Greater: False
#fun parse_int(string: String): Maybe[Int] {
# var num = 0
# for char in string do {
# if not({#0..=#9}.contains(char))
# then return none[Int]()
# num = num * 10 + {char - #0}.to_int()
# }
# some(num)
#}
to_string int: Int -> String =
int | less_than 0
% True: + "-" (negate int | to_string)
False:
last_digit = int | mod 10
last_digit_string =
last_digit | equals 0
% True: "0"
False:
last_digit | equals 1
% True: "1"
False:
last_digit | equals 2
% True: "2"
False:
last_digit | equals 3
% True: "3"
False:
last_digit | equals 4
% True: "4"
False:
last_digit | equals 5
% True: "5"
False:
last_digit | equals 6
% True: "6"
False:
last_digit | equals 7
% True: "7"
False:
last_digit | equals 8
% True: "8"
False:
last_digit | equals 9
% True: "9"
False:
crash "unreachable"
+
int | less_than 10
% True: ""
False: int | / 10 | to_string
last_digit_string
# Strings
+ a: String b: String -> String = builtin_concat_strings a b
# Maybe
Maybe t =
| Some t
None
unwrap maybe: (Maybe t) message: String -> t =
maybe
% Some t: t
None: builtin_crash message
unwrap maybe: (Maybe t) -> t = maybe | unwrap "Called unwrap on None"
# Result
Result o e =
| Ok o
Error e
unwrap result: (Result o e) message: String -> o =
result
% Ok value: value
Error error: builtin_crash message
unwrap result: (Result o e) -> o = result | unwrap "Called unwrap on Error"
# Types
TypeInfo =
| Int
String
Struct (List Field)
Enum (List Variant)
Type
Field =
& name: String
type: Type
Variant =
& name: String
args: List Type
type_of value: t -> Type = builtin_type_of value
type_info type: Type -> TypeInfo = builtin_type_info type
# List
List t =
| Empty
More t (List t)
list a: t -> (List t) = More a Empty
list a: t b: t -> (List t) = More a (list b)
list a: t b: t c: t -> (List t) = More a (list b c)
len list: (List t) -> Int =
list
% Empty: 0
More item rest: rest | len | + 1
is_empty list: (List t) -> Bool = list | len | equals 0
get_maybe list: (List t) index: Int -> (Maybe t) =
list
% Empty: None
More item rest:
index | equals 0
% True: Some item
False: rest | get_maybe (index | - 1)
get list: (List t) index: Int -> t =
list | get_maybe index | unwrap "out of bounds"
map list: (List a) mapper: (\ a -> b) -> (List b) =
list
% Empty: Empty
More item rest:
More
item | mapper
rest | map mapper
sum list: (List Int) -> Int =
list
% Empty: 0
More item rest: + item (rest | sum)
####################### Actual program #######################
#compile
# implementation: ByteCode
# input: Type
# output: Type
# -> (input -> output) = ...
Task =
| Print String (\ -> Task)
Exit
Point =
& x: Int
y: Int
Foo =
& a: Maybe Int
b: Int
make_foo n: Int -> Foo =
& a: Some n
b: n
main a: Int -> Int =
b = 1
list 1 2 3
| map
\ item: Int = + item b
| sum
stupidlen list: (List t) -> Int =
list
% Empty: 42
More item rest: rest | stupidlen
make_incremeter a: Int -> (\ Int -> Int) =
\ b: Int =
+ a b
to_deutsch type: Type -> String =
type
% Int: "Zahl"
String: "Zeichenkette"
Struct fields:
"Struktur (Felder sind " | + (fields | to_deutsch) | + ")"
Enum variants: "Enumeration (" | + (variants | to_deutsch) | + ")"
Type: "Typ"
to_deutsch fields: (List Field) -> String =
fields
% Empty: "keine Felder"
More field rest:
rest
% Empty: field.name
More a b:
field.name
| + "("
| + (field.type | to_deutsch)
| + ") und "
| + (rest | to_deutsch)
to_deutsch variants: (List Variant) -> String =
variants
% Empty: "keine Varianten"
More variant rest:
rest
% Empty: variant.name
More a b: variant.name | + " oder " | + (rest | to_deutsch)