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
|
Tutorial
========
This tutorial consists of a language tour
and a set of exercises, in the order of
mentioning.
Tour
====
Arithmetic operations
---------------------
Addition, multiplication, subtraction and division
are supported with +, *, - and / respectively.
```console
> (+ 1 1)
2 : Int
> (* 2 3)
6 : Int
> (- 5 4)
1 : Int
> (/ 4 2)
2 : Int
```
As addition and multiplication are associative,
multiple arguments can be used (even just one,
even though that doesn't really make sense):
```console
> (+ 1 2 3)
6 : Int
> (* 2 3 4 5)
120 : Int
```
Decimals are truncated in division:
```console
> (/ 5 2)
2 : Int
```
Increment and decrement operators are supplied by the
standard library:
```console
> (-- 1)
0 : Int
> (++ 1)
2 : Int
```
Booleans
--------
Myslip supports booleans and `and`, `or`, `xor` and `not`
for their comparisons.
```console
> true
true : Bool
> false
false : Bool
> (and true true)
true : Bool
> (or true false)
true : Bool
> (xor true true)
false : Bool
> (not true)
false : Bool
```
If-expressions are provided by the standard library in the
form `(if [condition] [iftrue] [iffalse])`:
```console
> (if true 1 0)
1 : Int
> (if false 1 0)
0 : Int
```
Integer comparisons
-------------------
To generate booleans from integers, some basic and quite
self-explanatory operators are supplied.
```console
> (> 2 1)
true : Bool
> (< 1 2)
true : Bool
> (>= 1 1)
true : Bool
> (<= 1 1)
true : Bool
> (= 1 1)
true : Bool
> (!= 1 1)
false : Bool
```
Variables
---------
Values can be bound to variables using the let expression.
```console
> (let x 1)
x saved
> x
1 : Int
```
The REPL interprets this as `((let x 1) x)`, which you
could also type but would make a more cumbersome REPLing
experience (and would make loading definitions from files
nigh impossible).
Shadowing works as expected:
```console
> ((let x 1) (+ x ((let x 2) x) x))
4 : Int
```
Here, before the definition inside the addition, `x = 1`,
and after it it is too, while in the middle term where
`x = 2` is defined, `x = 2`.
Functions
---------
Functions are written in the form of
`(fn [argument list] [argument type list] [return type] [function body])`.
They don't have names of themselves, but they can be bound
using `let`.
The following example features a simple increment function
and a function for checking if a integer is between two
others.
```console
> (let ++ (fn a Int Int (+ a 1)))
++ saved
> (++ 1)
2 : Int
> (let between (fn (a b c) (Int Int Int) bool (and (< b c) (> b a))))
between saved
> (between 1 2 3)
true : Bool
> (between 1 0 3)
false : Bool
```
Lists
-----
Lists in myslip correspond to what is known as tuples in many
other programming languages. This difference exists because
myslip is a list processor, and not using this underlying
construct of the language would be odd.
In principle,
```myslip
(1 2 3 4 5)
```
is a valid list, but it is evaluated by the interpreter,
which assumes that the first term, `1`, is an operator.
That's why constructing a list requires the operator
`quote`:
```console
> quote
quote : (T -> (Quote T))
> (quote 1 2 3 4 5)
(quote 1 2 3 4 5) : (Quote (Int Int Int Int Int))
```
In contrast from many other lisp-variants, in myslip
sub-expressions are simplified.
```console
> (quote (+ 1 1) (+ 2 2))
(quote 2 4) : (Quote (Int Int))
```
The elements of a list can of course be of different types:
```console
> (quote - 0 (quote 1 2))
(quote - 0 (quote 1 2)) : (Quote ((Int Int) -> Int) Int (Quote (Int Int)))
```
TODO: List destructuring
Vectors
-------
Vectors behave roughly the same as lists, except
their length is not specified on type-level, and
their elements can be only of one type.
```console
> vector
vector : ((T ...) -> (Vector (T ...)))
> (vector 1 2 3 4)
(vector 1 2 3 4) : (Vector (Int ...))
```
TODO: vector destructuring
Pattern matching
----------------
Values can be inspected using pattern matching.
For basic lists, the `case`-operator can be used.
It's syntax is:
```myslip
(case [condition]
([pattern 1] [value 1])
([pattern 2] [value 2])
...)
```
Just note that every case expression needs to
have one wildcard pattern, to ensure exhaustiveness.
Here's a very basic example:
```console
> (let x 1)
x saved
> (case (> x 5) (true (- x 1)) (_ x))
1 : Int
> (case (quote 1 2 3 4 5) ((5 4 3 2 1) 0) ((a b c 4 e) 1) (_ 2))
1 : Int
```
The list can be destructured into parts. If there
is a `..[variable name]` at the end of a pattern,
it matches the rest of the list.
```console
> (case (quote 1 2 3 4 5) ((h1 h2 ..tail) (+ h1 h2)) ((h ..t) h) (_ 0))
3 : Int
```
Nesting in pattern matching works fine too.
```console
> (case (quote 1 2 (quote 3 4) 5) ((1 2 (quote 3 4) 5) true) (_ false))
true : Bool
```
Pattern matching can be done on vectors too.
Just remember that the rest pattern `..r` needs to be at
the end of the pattern — not at the end of a subpattern.
```console
> (let myvec (vector 1 2 3 4 5))
myvec saved
> (case myvec ((head ..tail) head) (_ 0))
1 : Int
```
Here's a failing example of trying to put a rest pattern at
the end of a subpattern:
```console
> (case (quote myvec false) (((head ..tail) false) head) (_ 0))
Type error: unbound generic type 'T' in 'T'
```
Try extracting the vector first through one pattern and
then pattern matching on the extracted vector for your
desired behaviour.
General recursion
-----------------
General recursion in myslip is achieved through the
fixed point operator `fix`.
TODO
Understanding error messages
----------------------------
TODO: div zero
TODO: unclosed parenthesis
TODO: type errors
Exercises
=========
TODO
|