Intro_to_PL (Week 1)
Static / Dynamic Environment
Static Environment checks type, which does not run the program.
Dynamic Environment calculates the value.
1 | val x = 1; |
Expression:
- syntax
- type checking rule
- evaluation rule
For Variable:
- syntax: sequence of letters, digits, _, but not starting with digits.
- type checking: look up at current static environment. If not there, fail.
- evaluation rule: look up at current dynamic environment.
REPL: Read Evaluations Print Loop
Shadowing
1 | val x = 1; |
Function
Syntax: fun $x(x_1 : t_1, x_2 : t_2, \cdots, x_n : t_n) = e$.
Evaluation: a function is a value.
Type checking: check in the static environment
- if $x$ has type $t_1 t_2 \cdots -> t$
- $x_i$ has type $t_i$.
- then $x(x_1 : t_1, x_2 : t_2, \cdots, x_n : t_n)$ has type $t$.
1 | (* val pow = fn : int * int -> int *) |
Pair, Tuple and lists
Pair
- Syntax : $(e_1, e_2)$
- Evaluation : a pair is a value
- Type Checking : $ta * tb$
1 | (* (int * int) * (int * int) -> int *) |
Tuple
1 | (* int * (bool, int) *) |
List
1 | val a = [] |
list functions
1 | fun sum_list (xs : int list) = |
Let Expression
Syntax: let $b_1, b_2, \cdots, b_n$ in $e$ end ($b_i$ is a binding, $e$ is an expression)1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16fun silly () =
let
val x = 1;
in
(let val x = 2 in x + 1 end) + (let val y = x + 2 in y + 1 end)
end
fun countup (x : int) =
let
fun count_from_to (from : int, to : int) =
if from = to
then []
else from::count_from_to(from + 1, to)
in
count_from_to(1, x + 1)
end
Options
1 | fun max1(xs : int list) = |
Boolean
- $e_1$ andalso $e_2$
- $e_1$ orelse $e_2$
- not $e_1$
No Mutation
ML do not have mutations. ML cannot tell aliases, but it does not matter because there is no mutations. (E.g.: tl
function returns an alias, which is faster than returning a copy of a list)
Intro_to_PL (Week 1)