Begin at The Beginning : Exec-Time "Dynamic"
Begin at The Beginning : Exec-Time "Dynamic"
Exec-time
Dynamic
Expressions (Syntax)
Compile-time
Static
Values (Semantics)
Types
Base Types
i:int
i )i
2+3;
e1 + e2
e1:int e2:int
e1+e2:int
e1)v1 e2)v2
e1+e2)v1+v2
7-4;
e1 - e2
e1:int e2:int
e1-e2:int
e1)v1 e2)v2
e1-e2)v1-v2
(2+3)*(7-4);
15
e1 * e2
e1:int e2:int
e1 * e2:int
e1)v1 e2)v2
e1*e2)v1*v2
2.0
r:float
r )r
2.0 +.3.0
5.0
e1 +. e2
e1:float e1:float
e1+.e2:float
7.0 . 4.0
3.0
e1 -. e2
1.66.. e1 /. e2
(2.0 +. 3.0) /.
(7.0 -. 4.0)
e1)v1 e2)v2
e1+.e2)v1+.v2
ab ^ cd
ab
abcd
e1^e2
s:string
s )s
true
b:bool
b )b
2 < 3
true
e1 < e2
e1:int e1:int
e1 < e2:bool
e1)v1 e2)v2
e1<e2 ) v1<v2
not(2<3)
false
not e
e : bool
not e : bool
e
)
v
not e ) not v
e1 = e2
e1:T e2:T
e1=e2 : bool
e1)v1 e2)v2
e1=e2 ) v1=v2
e1 && e2
e1:bool e2:bool
e1&&e2:bool
e1)v1 e2)v2
e1&&e2) v1 && v2
(ab=cd)
not (2<3)
&&
(ab=cd)
false
false
except for ?
function values why ?
(ab=cd)
false
e1 = e2
e1:T e2:T
e1=e2 : bool
e1)v1 e2)v2
e1=e2 ) v1=v2
Type Errors
pq ^ 9;
e1:string e2:string
e1^e2:string
(2 + a);
e1:int e2:int
e1 + e2 : int
(4,false)
int * bool
(4,false)
int * bool
e1:T1 e2:T2
(e1,e2) : T1 * T2
e1)v1 e2)v2
(e1,e2)) (v1,v2)
e1:T1 e2:T2
en: Tn
(e1,e2,,en) : T1 * T2* * Tn
e1)v1 e2)v2 en ) vn
(e1,e2,,en)) (v1,v2,,vn)
int
{age=31;name=sarah;pass=false}.age
31
int
{age=31;name=sarah;pass=false}.pass
false
bool
{name=sarah;age=31;pass=false}.age
But wait
All evaluation rules look like:
e1)v1 e2)v2
e1 OP e2 ) v1 OP v2
[2;4;6;8]
int list
[a;b;c^d];
[a;b;cd]
string list
[(1;a^b);(3+4,c)];
[(1,ab);(7,c)]
(int*string) list
[[1];[2;3];[4;5;6]];
[[1];[2;3];[4;5;6]];
Unbounded size
Can have lists of anything (e.g. lists of lists)
[]:a list
[] ) []
1::[2;3] [1;2;3]
int list
e1)v1 e2 ) v2
e1::e2 ) v1::v2
1::[b; cd];
Can only cons element to a list of same type
[1;2]@[3;4] [1;2;3;4]
int list
e1)v1 e2 ) v2
e1@e2 ) v1@v2
1@[b; cd];
[1]@[b; cd];
hd [1;2;3;4;5] 1
[2;3;4;5]
tl [1;2;3;4;5]
int
[a;b; cd]
hd [a;b;cd]
int list
tl [a;b;cd]
[b; cd]
string
[(1,a);(7,c)]
[[];[1;2;3];[4;5]]
string list
hd [[];[1;2;3];4;5]
int list
tl [[];[1;2;3];4;5]
[2;3;4;5]
int list list
e :T list
hd e : T
e )v1::v2
hd e ) v1
e :T list
tl e : T list
e ) v1::v2
tl e ) v2
e1:T e2:T
e1=e2 : bool
string list
Recap
Exec-time
Dynamic
Expressions (Syntax)
Compile-time
Static
Values (Semantics)
Types
Recap
Integers: +,-,*
floats: +,-,*
Booleans: =,<, andalso, orelse, not
Strings: ^
Tuples, Records: #i
Fixed number of values, of different types
Lists:
::,@,hd,tl,null
If-then-else expressions
5
int
[ab,cd]
string list
If-then-else expressions
5
int
[ab,cd]
string list
: bool
e2: T
e3: T
if e1 then e2 else e3 : T
e1 ) true
e2 ) v2
if e1 then e2 else e3 ) v2
e1 ) false
e3 ) v3
if e1 then e2 else e3 ) v3
If-then-else expressions
if (1 < 2) then [1;2] else 5
if false then [1;2] else 5
e1
: bool
e2: T
e3: T
if e1 then e2 else e3 : T
If-then-else expressions
e1
: bool
e2: T
e3: T
if e1 then e2 else e3 : T
int list
string list
(if 1>2 then [1,2] else [])=(if 1<2 then [] else [a])
Next: Variables
let x = e;;
Bind the value of expression e
to the variable
x
:
y
:
z
:
= 2+2;;
int = 4
= x * x * x;;
int = 64
= [x;y;x+y];;
int list = [4;64;68]
x
y
z
x
4 : int
64 : int
[4;64;68] : int list
8 : int
let x = e;;
ML program = Sequence of variable bindings
Environments
Phone book
Variables = names
Values = phone number
1. Evaluate:
Find and use most recent value of variable
2. Extend:
Add new binding at end of phone book
Example
# let x = 2+2;;
val x : int = 4
4 : int
# let y = x * x * x;;
val y : int = 64
# let z = [x;y;x+y];;
val z : int list = [4;64;68]
# let x = x + x ;;
val x : int = 8
New binding!
4 : int
64 : int
x
y
4 : int
64 : int
[4;64;68] : int list
x
y
z
x
y
z
x
4 : int
64 : int
[4;64;68] : int list
8 : int
Environments
1. Evaluate: Use most recent bound value of var
2. Extend: Add new binding at end
4 : int
x
f
4 : int
fn <code,
>: int->int
New binding:
No change or mutation
Old binding frozen in f
Environments
1. Evaluate: Use most recent bound value of var
2. Extend: Add new binding at end
4 : int
4 : int
fn <code,
x
f
>: int->int
4 : int
fn <code,
8 : int
>: int->int
Environments
1. Evaluate: Use most recent bound value of var
2. Extend: Add new binding at end
4 : int
fn <code,
8 : int
>: int->int
x
f
4 : int
fn <code,
8 : int
>: int->int
Examples of no sharing
Remember: No addresses, no sharing.
Each variable is bound to a fresh instance of a value
Tuples, Lists
Efficient implementation without sharing ?
There is sharing and pointers but hidden from you
Compilers job is to optimize code
Efficiently implement these no-sharing semantics
Your job is to use the simplified semantics
Write correct, cleaner, readable, extendable systems
Recap: Environments
Phone book
Variables = names
Values = phone number
1. Evaluate:
Find and use most recent value of variable
2. Extend: let x = e ;;
Add new binding at end of phone book
Next: Functions
Values
Expressions
Types
Functions
expr
Functions
Type
Functions
Type
e1 : T2 -> T
e1 e2 : T
e2: T2
Functions
Values
(e1 e2)
Functions
Values
(e1 e2)
Function value =
<code + environment at definition>
closure
# let x = 2+2;;
val x : int = 4
# let f = fun y -> x + y;;
val f : int -> int = fn
# let x = x + x ;;
val x : int = 8
# f 0;;
val it : int = 4
4 : int
fn <code,
8 : int
>: int->int
Application Value:
1. Evaluate e1 in current env to get (function) v1
v1 is code + env
code is (formal x + body e) , env is E
Example 1
let x = 1;;
let f y = x + y;;
let x = 2;;
let y = 3;;
f (x + y);;
Example 1
let x = 1;;
let f y = x + y;;
let x = 2;;
let y = 3;;
f (x + y);;
Example 2
let x = 1;;
let f y =
let x = 2 in
fun z -> x + y + z
;;
let x = 100;;
let g =(f 4);;
let y = 100;;
(g 1);;
Example 2
let x = 1;;
let f y =
let x = 2 in
fun z -> x + y + z
;;
let x = 100;;
let g = (f 4);;
let y = 100;;
(g 1);;
Example 3
let f g =
let x = 0 in
g 2
;;
let x = 100;;
let h y = x + y;;
f h;;
Static/Lexical Scoping
For each occurrence of a variable,
Unique place in program text where variable defined
Most recent binding in environment