-
Notifications
You must be signed in to change notification settings - Fork 2
/
readme_examples.tri
343 lines (273 loc) · 10.7 KB
/
readme_examples.tri
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
// -------------------------------------------
// | EXAMPLE 1 |
// -------------------------------------------
// During compilation, the compiler analyses the program and tries to figure out what
// potential values expressions might evaluate to at runtime. It puts these potential
// values into or-patterns for each expression.
//
// Some expressions do however never return; instead they crash or loop forever.
// If the compiler cannot exclude the possibility of this happening for an expression,
// it will add the special `N/A` value to its or-pattern.
//
// (`N/A` = never-returned value = "Never Available" / "Not Available" / "No Answer")
//
// Below is an example of this.
let ($a as nat) - ($b as nat) = $a $b => a - b unless b > a then panic "below 0!";
let n = nat;
// - n == n is True
// - The remaining terms of LHS & RHS are known
// => Compiler can determine if True or False
(n - 1) * 2 + 2 == n * 2
N/A|0|1|... * 2 + 2 == 0|2|4...
// Potential N/A(s) found!
// Add N/A to or-pattern, then
// start over with values not leading to N/A:
0|1|... * 2 + 2 == 2|4|...
0|2|... + 2 == 2|4|...
2|4|... == 2|4|...
// LHS == RHS since:
// - Compiler can determine if True or False
// - LHS & RHS or-patterns consist of the same values in the same order
=> N/A|True
// - n == n2 is True|False
// => Compiler *cannot* determine if True or False
(n - 1) * 2 + 2 == n2 * 2
N/A|0|1|... * 2 + 2 == 0|2|4...
// Potential N/A(s) found!
// Since the compiler cannot determine anything more,
// and it's irrelevant if there are more potential N/As,
// it stops here and returns:
=> N/A|True|False
// Basically, `N/A` is propagating just like `???`.
// It can be given to any function and will make it return `N/A`.
N/A * 2 + 2 == n * 2
=> N/A + 2 == n * 2
=> N/A == n * 2
=> N/A
// Note that this means that you can't directly check if something equals `N/A`,
// since `x == N/A` just returns `N/A`. However, it is still possible to do
// it indirectly by doing `!(x is ~N/A)`. This is because `~N/A` is built-in
// to mean an or-pattern excluding `N/A`. This is necessary because all other
// or-patterns implicitly include `N/A`:
nat :== 0|1|... :== 0|1|...|N/A;
// To override this default behaviour, one can add `& ~N/A` to the or-pattern:
nat & ~N/A :== 0|1|... & ~N/A;
// Really, typing `N/A` has no good use outside of `~N/A`. It could be used
// as an alternative to `???`, but doing so will just cause confusion and problems.
// For that reason, `N/A` on its own will probably give a warning on compilation.
// (Also, `~~N/A` <=> `N/A` if it wasn't obvious, so `x is ~~N/A`
// will still become `x is N/A` and then `N/A`)
// -------------------------------------------
// | EXAMPLE 2 |
// -------------------------------------------
// OUTDATED. Will be either replaced or removed soon.
// 'closed' prevents the compiler from looking at the function body
// before the function is in its final scope.
// 'impure' allows the function to be non-pure.
$x => (
($y $z => (
// Final scope
// 'z' is defined here, so all is fine
(() => y x) () // just to show final scope isn't necessarily call scope
)) (closed impure $a => x++ * z a) 123 // 'impure' allows change of 'x', so all is fine
)
$x => (
($y => (
// Final scope
// ERROR: 'z' is undefined
($z => y x) 123 // just to show final scope isn't necessarily call scope
)) (closed impure $a => x++ * z a) // 'impure' allows change of 'x', so all is fine
)
$x => (
($y $z => (
// Final scope
(() => y x) () // just to show final scope isn't necessarily call scope
)) ($a => x++ * z a) 123 // ERROR: 'z' is undefined, and 'x' is an outside/free pattern being changed
)
// -------------------------------------------
// | EXAMPLE 3 |
// -------------------------------------------
// Declarations to allow use of patterns before they have been defined
decl $x $y $z;
println x;
let x = 123;
// -------------------------------------------
// | EXAMPLE 4 |
// -------------------------------------------
// Example of a cool thing using #-defs
// '#($y)%' defines 'y' inside function body as '5' in this case
// You could also do '#($y as 5)%' if you only want to allow that input
object (num)%;
func ($x as num) * (#($y)%) {
x * y / 100
}
120 * 5% == 6
// This special function catches matching
// terms in the scope its defined for.
// It only catches the terms if they are
// used in a place where all pattern
// matching has failed.
//
// Here we are making it possible to do
// <x>% <op> <y>% for every existing operator
// that works with numbers. This works because
// i.e. '120% * 5%' fails all pattern matching
// but still matches the below catcher pattern.
func __catch__ ($(_ op _) as #(#($x)%) #(#($y)%)) {
(((x / 100) op (y / 100)) * 100)%
}
// The 2 lines below work thanks to
// the above __catch__ overload
120% * 5% == 6%
120% + 5% == 125%
// Running '__catch__' as if it was a normal
// function is allowed only for testing.
// Doing this will get you a warning on
// debug-mode and an error on release-mode.
__catch__ (5% * 120%) == 6%
// Below we instead catch all matching terms
// *before* any other pattern matching is
// done by using an implicit closure.
//
// This results in every single function
// being catched before evaluation.
impure func __catch__ ($f as implicitly _ => __explicit__) {
println "nope!";
}
println "Hello world!"; // prints "nope!"
9 + 10 == 21; // prints "nope!"
9 + 10 == 19; // prints "nope!"
// NOTE: We could ofc just define '%' as '/ 100'
// if we don't care about having an object:
func ($x as any Number)% {x / 100}
// But I chose to use an object for this example
// to better showcase features of the language.
// -------------------------------------------
// | EXAMPLE 5 |
// -------------------------------------------
// Examples of referring to the same function or not
impure func f ($x as any String) $y {println 123};
impure func f ($x as any Int) $y {println 123};
f != f // ERROR! Unable to determine function being referred to
f "hi" != f 789
impure func g ($x as any Int) $y {println 123};
impure func $y g ($x as any Int) {println 123};
g 789 == g 789
g 789 != $y g 789
// Here the second function shadows the first
impure func h ($x as any Int) $y {println 123};
impure func h ($x as any Int) $y {println 456};
h 1 == h 1
h 1 != h 2
// Note! Full application this time
h 1 2 == 456
// Other comparison examples
let id = $x => x;
let plus1 = $x => id x + 1
let f = plus1;
let $x g = f;
f <=> f $x
// g <=> f <=> plus1 is just shorthand for g == f && f == plus1
g <=> f <=> plus1 // because 'g' is just a synonym for 'f' which is a synonym for 'plus1'
g != id
// All 4 lines below error! Anonymous functions are not comparable
f $x != ($x => f x)
plus1 != ($x => id x + 1)
id != ($x => x)
($x => x) != ($x => x)
123 g <=> f 123 <=> plus1 123 <=> id 123 + 1 <=> 123 + 1 <=> 124 // since all evaluate to 124
// -------------------------------------------
// | EXAMPLE 6 |
// -------------------------------------------
func Potato () {};
let potato = Potato;
let potato2 = Potato;
let potato3 = {
func Potato () {};
Potato
};
potato == potato2
potato != potato3 // the Potato:es were created using different constructors, so they are different
// -------------------------------------------
// | EXAMPLE 7 |
// -------------------------------------------
// Pattern matching order:
// - Most specific patterns are checked first.
// - More general patterns are checked second.
// - If order can't be decided based on this,
// follow the order in which the patterns
// are defined.
//
// The most specific pattern is the one with
// the smallest or-patterns.
// - '_' is an or-pattern of everything,
// and therefore the biggest or-pattern
//
// Pattern matching stops when a match is found.
// If no match is found, there's an error.
object Player [
$name as any String,
$health as any Number
];
impure func f ($x as _) {
println "last";
};
impure func f ($x as Player "Steve" 25..75) {
println "fourth";
};
impure func f ($x as any Player) {
println "fifth"
};
impure func f ($x as Player "Steve" 55..75) {
println "first";
};
impure func f ($x as Player "Steve"|"Alex" 50..70) {
println "third";
};
impure func f ($x as Player "Steve" 50..70) {
println "second";
};
f (Player "Steve" 80) // prints "fifth"
f (Player "Steve" 60) // prints "first"
f (Player "Steve" 50) // prints "second"
f (Player "Alex" 40) // prints "fifth"
f (Player "Alex" 60) // prints "third"
f Potato // prints "last"
// -------------------------------------------
// | EXAMPLE 8 |
// -------------------------------------------
// Dictionaries/HashMaps + Error Handling example
func test() --> () implic made Wrapped EXPLIC {
// Only allowing optional Int values for demonstration purposes.
// 'optionally any Int' <=> 'any Int | ()' (read as "any Int or nothing")
func lookup $key in $dict --> optionally any Int implic made Wrapped EXPLIC {
for each [$hash, $val] in dict {
if hash of key == hash {
return val; // all returns implicitly wrapped by default, making this become 'return (Wrapped val)'
};
};
// '`$expr`' can however be used to bypass the implicit wrapping
// '`$expr`' is read as "precisely $expr"
return `()`; // "return precisely nothing"; returns unwrapped '()', here meaning no entry was found
};
// Idk if dictionaries actually will have this syntax,
// but let's assume they do for this example.
let dict = [["abc", 123], ["def", ()], ["ghi", 456]];
println(lookup "abc" in dict); // prints "Wrapped 123"; entry with value '123' found
println(lookup "invalid" in dict); // prints "()"; no entry found
println(lookup "def" in dict); // prints "Wrapped ()"; entry found, but the entry is nothing
// '?' can be used to automatically unwrap.
// If unwrapping fails, it returns to an error handler.
println(lookup "def" in dict)?; // prints "()"; entry found, but the entry is nothing
println(lookup "invalid" in dict)?; // no entry found; returns '`()`' to error handler without printing anything
(lookup "ghi" in dict)? // returning something fallible requires '?'; else 'Wrapped $val' could be returned which doesn't match '()'
};
func handle error () {
// Handle the error somehow...
};
func init() {
// If test() succeeds, it returns 'Wrapped ()'.
// If it however fails, it returns '()':
if test() == () then handle error;
// (We could ofc also have chosen to return 'Err $err_info' or similar instead to know better how to handle the error)
};