Lecture 02
Syntax and Semantics:
Expressions, Functions
Programming Languages
UW CSE 341 - Winter 2022
Updates
Syntax + Semantics
Why Two Semantics?
ML Carefully So Far
Expressions
Defining Expressions: Syntax, Typing, Evaluation
Systematic approach to defining / understanding programming languages!
Values
Expressions
Values
Variables
Syntax: sequence of letters, numbers, _, or ‘
Type Checking
Evaluation
Why no “else” case?
Type checking ensures variable always bound!
Addition
Syntax: e1 + e2
Type Checking
Evaluation
But what if e1 or e2 do not evaluate to ints?
Type checking ensures they will be ints!
Comparison (less than)
Syntax: e1 < e2
Type Checking
Evaluation
Comparison (less than)
Syntax: e1 < e2
Type Checking
Evaluation
Comparison (less than)
Syntax: e1 < e2
Type Checking
Evaluation
Comparison (less than)
Syntax: e1 < e2
Type Checking
Evaluation
Conditionals (if-then-else)
Syntax: if e1 then e2 else e3
Type Checking
Evaluation
Conditionals (if-then-else)
Syntax: if e1 then e2 else e3
Type Checking
Evaluation
Conditionals (if-then-else)
Syntax: if e1 then e2 else e3
Type Checking
Evaluation
Conditionals (if-then-else)
Syntax: if e1 then e2 else e3
Type Checking
Evaluation
Both branches must have the same type!
Conditionals (if-then-else)
Syntax: if e1 then e2 else e3
Type Checking
Evaluation
Conditionals (if-then-else)
Syntax: if e1 then e2 else e3
Type Checking
Evaluation
Only evaluate one of the branches!
Both branches must have the same type!
The Foundation We Need
Functions
let max ((x:int),(y:int)) =
if x > y then x else y
max (3,17)
Example With Recursion
let rec pow ((x:int),(y:int)) =
if y = 0
then 1
else x * pow(x,y-1)
let cube (x:int) =
pow(x,3)
let sixtyfour = cube 4
let fortytwo = pow(2,4) + pow(4,2) + cube 2 + 2
Heads Up: Recursion!
Six questions
Three questions for a new language construct:
syntax, typing rules, evaluation rules
We have two new language constructs:
function bindings, function calls
Function Bindings
Syntax, with or without rec:
let f ((x1 : t1),…,(xN : tN)) = e
let rec f ((x1 : t1),…,(xN : tN)) = e
Where:
Meta-syntax
let [rec] f ((x1 : t1),…,(xN : tN)) = e
Actually
This is a simplified-for-now syntax story:
Note: Be careful on argument syntax -- slight errors produce strange messages or behavior
Function Bindings: Type Checking
let [rec] f ((x1 : t1),…,(xN : tN)) = e
If has e has type t in the static environment containing:
Then add f ↦ t1 * … * tN -> t to the static environment
Else, report error and fail
Function Bindings : Need Function Types!
How this is used for type-checking function bindings:
Type-checker figures out t even when f is recursive
Function Bindings: Evaluation Rules
let [rec] f ((x1 : t1),…,(xN : tN)) = e
Evaluation rules:
Function Calls
Syntax: f (e1,…, eN)
Where f, e1, …, eN are expressions
Type Checking:
If:
Then f (e1,…, eN) has type t
Function Calls
e0 (e1,…, eN)
Evaluation
In an extended dynamic environment with x1 ↦ v1, …, xN ↦ vN
Technically: extend dynamic environment where f was defined.
But more on this later!
Heads Up: Function Gotchas
So much power in 6 questions
Three questions per language construct: syntax, typing rules, evaluation rules
Two new language constructs: function bindings, function calls
Most important building block in the entire course
let rec pow ((x:int),(y:int)) =
if y = 0
then 1
else x * pow(x,y-1)
let cube (x:int) =
pow(x,3)
let sixtyfour = cube 4