aboutsummaryrefslogtreecommitdiff
path: root/doc/docs/frontend/syntax.md
blob: 836201ca782bad9feaa91c6031e19db3da7c5bb7 (plain) (blame)
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
---
vim: noexpandtab tabstop=2 shiftwidth=2
---

# Syntax

Snug syntax is based on [S-expressions][] to simplify parsing, with some
modifications.

Importantly, parentheses are meaningful as they are used to express lists in
the syntax. For instance, the list of arguments to a function is enclosed in
parentheses. In such contexts it is an error to use redundant parentheses, or
to omit parentheses in case of a single element.

## Data types

Algebraic data types can be defined with:

```snug
(data List (a) (
	Nil
	(Cons a (List a))))
```

In abstract terms, the syntax is:

```snug
(data NAME (VARIABLES) (CONSTRUCTORS))
```

where constructors are of the form `NAME` (without arguments) or
`(NAME TYPE [TYPE..])` (with arguments).

Type synonyms can be defined with:

```snug
(type String (List Char))
```

## Functions

```snug
(fun length ((xs : List a)) : Int
	(length_acc 0 xs))

(fun length_acc ((n : Int) (xs : List a)) : Int
	(case xs (
		(Nil -> n)
		(Cons _ xs -> length_acc (+ n 1) xs))))
```

In abstract terms, the syntax is:

```snug
(fun NAME (ARGUMENTS) : TYPE (EXPRESSION))
```

where arguments are of the form `(NAME : TYPE)`.

## Expressions

Expressions are:

- Basic values like `37` or `'A'`
- Symbol identifiers, either global or local
- Data constructor identifiers
- Function applications
- Case expressions

Case expressions have the following syntax:

```snug
(case EXPRESSION (ALTERNATIVES))
```

and alternatives have the form `(PATTERN -> EXPRESSION)`. Patterns use the same
form as expressions, but only basic values, symbol identifiers (for variable
binding), and fully saturated data constructors are allowed.

[S-expressions]: https://en.wikipedia.org/wiki/S-expression