Lecture B2

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

Introduction to Programming

Lecture B2

Holger Hermanns

partly rooted in lecture notes

by Gert Smolka
- Programmer produces character representation.

- Lexer turns this into word representation.

- Parser turns this into syntax tree.


Elaborator (1) scans for unbound identifiers
and (2) checks types.

- Typing judgements

- Typing rules - Inference rules

- Free / bound identifier occurrences - Binding occurrence


- Open / closed programs - Using occurrence
More Rules

Assume : and f : ∗ → .

Provide a typing judgement for

if f (x, true) then 5 + x else xelse x


How does she know?
The interpreter collects information about identifiers declared upfront in a type environment.

Assume : and f : ∗ → .

A type environment collects bindings of identifiers to typing judgments.

[ : ,f: ∗ → ]

- Environments are functional. ... in that they describe a partial function from identifiers to types.

- Environments can be updated. overwriting insertion "adjunction", mathematically.

[ : , : ] , (z : )=[ : , : , : ]
[ : , : ] , ( : bool) , ( : ) = [ : bool, : , : ]

- This is an environment as well: []


Rules Revisited

Assume E = [ : ,f: ∗ → ].

Provide a typing judgement for

if f (x, true) then 5 + x else x


Typing judgements are now interpreted
in the given type environment
Free or Bound?
let p (x : int) : int = if x > 0 then 3 else 4
known or unknown?

let p = if x > 0 then 3 else 4

We distinguish binding occurences


and using occurences of identifiers.

Another example:

let rec h (y : int) : int = h y


let p = (fun (y : int) : int -> y)
let y = y
let q (y : int) : int = 3 + (p y)

Everything bound? Your program is closed. Good!

Otherwise? It is open. Not good.


Free or Bound revisited
let p (x : int) : int = if x > 0 then 3 else 4
known or unknown?

let p = if x > 0 then 3 else 4

We distinguish binding occurences


and using occurences of identifiers.

Another example:

let rec h (y : int) : int = h y


let p = (fun (y : int) : int -> y)
let y = y
let q (y : int) : int = 3 + (p y)

Everything bound? Your program is closed. Good!

Otherwise? It is open. Not good.


Free or Bound Assume X is a set of bound identifiers.

Is a given atomic expression closed in X? e


If e is a constant c then yes. If instead it is an identifier x
then it is closed if we find x in X.
Is a given operator application closed in X? e1 o e2
If e1 and e2 are each closed in X then it is closed in X.
Is a given function application closed in X? e1 e2
If e1 and e2 are each closed in X then it is closed in X.
Is a given conditional closed in X? if e1 then e2 else e3
If e1, e2 , and e3 are each closed in X then it is closed in X.
Is a given tuple expression closed in X? (e1, ... , en )
If e1 to en are each closed in X then it is closed in X.
Is a given let expression closed in X? let x = e1 in e2
If e1 is closed in X then it is closed in X if e2 is closed in X ∪ { x }.
Is a given lambda expression closed in X? fun (x : t1) : t2 -> e
If e is closed in X ∪ { x } then it is closed in X.
Is a given let rec expression closed in X? let rec f (x:t) : t = e1 in e2
If e1 is closed in X ∪ { f, x } then it is closed in X if e2 is closed in X ∪ {f }.
Free or Bound Assume X is a set of bound identifiers.

Is a given atomic expression closed in X? e


If e is a constant c then yes. If instead it is an identifier x
then it is closed if we find x in X.
Is a given operator application closed in X? e1 o e2
If e1 and e2 are each closed in X then it is closed in X.
Is a given function application closed in X? e1 e2
If e1 and e2 are each closed in X then it is closed in X.
Is a given conditional closed in X? if e1 then e2 else e3
If e1, e2 , and e3 are each closed in X then it is closed in X.
Is a given tuple expression closed in X? (e1, ... , en )
If e1 to en are each closed in X then it is closed in X.
Is a given let expression closed in X? let x = e1 in e2
If e1 is closed in X then it is closed in X if e2 is closed in X ∪ { x }.
Is a given lambda expression closed in X? fun (x : t1) : t2 -> e
If e is closed in X ∪ { x } then it is closed in X.
Is a given let rec expression closed in X? let rec f (x:t) : t = e1 in e2
If e1 is closed in X ∪ { f, x } then it is closed in X if e2 is closed in X ∪ {f }.
Alonzo Church
1903 – 1995

- Mathematician and Philosopher.

- Showed undecidability of the "Entscheidungsproblem".

- Showed undecidability of Peano Arithmetics.

- Mother of the lambda calculus, as


"a method for defining functions".

- Originator of the Church-Turing conjecture.


Example

let rec h (y : int) : int = h y in


let p = (fun (y : int) : int -> y) in
let y = y in
3 + (p y)

Is this a closed expression ?


Again More Typing Rules

Assume E = [ : ,f: ∗ → ].

Provide a typing judgement for

let y = true in f (x,y)


Typing judgements are now interpreted
in the given type environment

let y = true in f (x,y)


Again More Typing Rules

has type t
if e1 has type t1 in E,
and e2 has type t in E
where x is updated to have type t1.

has type t
if e2 has type t in E
where f is updated to have type t1 →t2
and e1 has type t2 in E
where f is updated to have type t1 →t2 , and
where x is updated to have type t1.

has type t1 →t2


if e has type t2 in E
where x is updated to have type t1.
Static Semantics Are all identifiers known? Is it well-typed?

1) Checking for absence of unbound identifiers.

2) Type checking.

Works with
syntax tree
- unique representation
- no parentheses

The parser has produced a syntax tree.

The elaborator has two tasks.


Static Semantics Are all identifiers known? Is it well-typed?

1) Checking for absence of unbound identifiers.


It has no unknown identifiers if it is closed in {}.
2) Type checking.
It is well-typed if the type checker returns a type judgement in [].

Works with
syntax tree
- unique representation
- no parentheses

The parser has produced a syntax tree.

The elaborator has two tasks.


Execution [ ▷ 0, y ▷ 3 ]

The interpreter collects information about identifiers declared upfront in a value environment.

Given a value environment V and a well-typed and closed* program P:


* in the set of identifiers
appearing in V

Intuitively, the interpreter does


what we do when writing down
an execution log.

It does so along the syntactic structure


of the program.

Upon termination, the execution of P


will have updated V by new bindings.
Execution along the structure of the program
How to execute a program?

How to execute a declaration?

How to execute an expression?

How to execute an atomic expression?

How to execute an operator application?

How to execute a function application?

How to execute a conditional?


Evaluation of expressions
How to execute a tuple expression? so that they get a value

How to execute a let expression?

How to execute ...?


Evaluation of expressions
How to evaluate an atomic expression? e
If e is a constant then return its value. If instead it is an
identifier then return the value the identifier is bound to in V.
How to evaluate an operator application? e1 o e2
Evaluate e1 and e2 in V. If these evaluations return values v1 and v2,
then evaluate v1 o v2 in V and return the value thus obtained.
How to evaluate a conditional? if e1 then e2 else e3
Evaluate e1 in V. If this results in value true then evaluate
e2 in V and return the value thus obtained. If instead the result
is false then evaluate e3 in V and return the value thus obtained.
How to evaluate a tuple expression? (e1, ... , en )
Evaluate e1 to en in V. If these evaluations return
values v1 to vn then return the value (v1, ..., vn ).
How to evaluate a let expression? let x = e1 in e2
Evaluate e1 in V. If this results in value v then evaluate e2 in V, x ▷ v, and return the value thus obtained.
How to evaluate a function application? e1 e2
Evaluate e2 and e1 in V. If these evaluations return value v2 and function f, the latter as a triple (x, e, V' ),
then evaluate e in V', x ▷ v2 , and return the value thus obtained.
How to evaluate a lambda expression? fun (x : t1) : t2 -> e Return the triple (x, e, V ).
Evaluation of expressions
How to evaluate an atomic expression? e
If e is a constant then return its value. If instead it is an
identifier then return the value the identifier is bound to in V.
How to evaluate an operator application? e1 o e2
Evaluate e1 and e2 in V. If these evaluations return values v1 and v2,
then evaluate v1 o v2 in V and return the value thus obtained.
How to evaluate a conditional? if e1 then e2 else e3
Evaluate e1 in V. If this results in value true then evaluate
e2 in V and return the value thus obtained. If instead the result
is false then evaluate e3 in V and return the value thus obtained.
How to evaluate a tuple expression? (e1, ... , en )
Evaluate e1 to en in V. If these evaluations return
values v1 to vn then return the value (v1, ..., vn ).
How to evaluate a let expression? let x = e1 in e2
Evaluate e1 in V. If this results in value v then evaluate e2 in V, x ▷ v, and return the value thus obtained.
How to evaluate a function application? e1 e2
Evaluate e2 and e1 in V. If these evaluations return value v2 and function f, the latter as a triple (x, e, V' ),
then evaluate e in V', x ▷ v2 , and return the value thus obtained.
How to evaluate a lambda expression? fun (x : t1) : t2 -> e Return the triple (x, e, V ).
Where we stand
The programmer produces a character representation.

The lexer produces a word representation.

The parser produces a syntax tree representation.

The elaborator checks for unbound identifiers and checks typing.

The executor evaluates expressions, resulting in fresh bindings.

You might also like