C LAUS B RABRAND S EMANTICS (Q1,’06) S EP 14, 2006 C LAUS B RABRAND © 2005-2006, University of...

54
CLAUS BRABRAND SEMANTICS (Q1,’06) SEP 14, 2006 CLAUS BRABRAND © 2005-2006, University of Aarhus [ [email protected] ] [ http://www.daimi.au.dk/~brabrand/ ] SEMANTICS (Q1,’06) WEEK 3: BIG- vs. SMALL, ERRORS, TYPE CHECKING
  • date post

    21-Dec-2015
  • Category

    Documents

  • view

    214
  • download

    0

Transcript of C LAUS B RABRAND S EMANTICS (Q1,’06) S EP 14, 2006 C LAUS B RABRAND © 2005-2006, University of...

CLAUS BRABRAND SEMANTICS (Q1,’06) SEP 14, 2006

CLAUS BRABRAND

© 2005-2006, University of Aarhus

[ [email protected] ] [ http://www.daimi.au.dk/~brabrand/ ]

SEMANTICS (Q1,’06)

WEEK 3: ” BIG- vs. SMALL, ERRORS, TYPE CHECKING”

CLAUS BRABRAND © SEMANTICS (Q1,’06)[ 2 ]

SEP 14, 2006

Week 3 - Outline

Repetition: small-step SOS for L Small-step vs. big-step (a comparison):

Non-termination, abnormal termination, non-determinism, and parallelism

Runtime-errors Exceptions and Exception Handling Type Errors Type Checking Structural Induction

CLAUS BRABRAND SEMANTICS (Q1,’06) SEP 14, 2006

Small-step SOS for "L"

CLAUS BRABRAND © SEMANTICS (Q1,’06)[ 4 ]

SEP 14, 2006

Recall ”L” Recall "L":

Arithmetic Expressions (e Exp):

Boolean Expressions (b BExp):

Commands (c Com):

e ::= n | v | e + e’ | e – e’ | e e’

b ::= t | e = e’ | b or b’ | ~ b

c ::= nil | v := e | c ; c’ | if b then c else c’ | while b do c

CLAUS BRABRAND © SEMANTICS (Q1,’06)[ 5 ]

SEP 14, 2006

A:

[SUM1]< e0 + e1 , > A < e0’ + e1 , ’ >

< e0 , > A < e0’ , ’ >

[SUM2]

[SUM3]< n0 + n1 , > A < m , >

m = n0 + n1

< n0 + e1 , > A < n0 + e1’ , ’ >< e1 , > A < e1’ , ’ >

< v , > A < m , >[VAR] m = (v)

CLAUS BRABRAND © SEMANTICS (Q1,’06)[ 6 ]

SEP 14, 2006

< e0 = e1 , > B < t , " >[EQ]B

< e0 , > A* < n0 , ' >

t = tt, n0 = n1

ff, n0 n1

< b0 or b1 , > B < b0’ or b1 , ' >

< b0 , > B < b0’, ' >[OR1]B

[OR2]B

< t0 or t1 , > B < t , >[OR3]B t = t0 t1

< e1 , ' > A* < n1 , " >

< t0 or b1 , > B < t0 or b1' , ' >

< b1 , > B < b1’, ' >

< ~ b , > B < ~ b' , ' >

< b , > B < b' , ' >[NEG1]B

< ~ t , > B < t' , >[NEG2]B

t' = t

B:

CLAUS BRABRAND © SEMANTICS (Q1,’06)[ 7 ]

SEP 14, 2006

< nil , > C [NIL]C

< v := e , > C "

< e , > A* < m , ' >

[ASS]C " = '[m/v]

< c0 ; c1 , > C < c0’ ; c1 , ’ >

< c0 , > C < c0’ , ’ >[SEQ1]C

< c0 ; c1 , > C < c1 , ’ >

< c0 , > C ’[SEQ2]C

< if b then c else c’ , > C < c , ' >

< b , > B* < tt , ' >

[IF1]C

< if b then c else c’ , > C < c’ , ' >

< b , > B* < ff , ' >

[IF2]C

< while b do c, > C < c ; while b do c , ' >

< b , > B* < tt , ' >

[WH1]C

< while b do c , > C '

< b , > B* < ff , ' >

[WH2]C

C:

CLAUS BRABRAND SEMANTICS (Q1,’06) SEP 14, 2006

BIG-STEP vs. SMALL-STEP

CLAUS BRABRAND © SEMANTICS (Q1,’06)[ 9 ]

SEP 14, 2006

Big-step vs. Small-step: SOS

Big-step SOS:

Small-step SOS:

< while b do c , > SS < c ; while b do c , >[WH1]SS

< while b do c , > SS [WH2]SS

[WH1]BS

[WH2]BS

< while b do c , > BS ”

< while b do c , > BS

< while b do c , ’ > BS ”< c , > BS ’ |_ b B* tt

|_ b B* ff

|_ b B* tt

|_ b B* ff

CLAUS BRABRAND © SEMANTICS (Q1,’06)[ 10 ]

SEP 14, 2006

Big-step vs. Small-step: Looping

Big-step:

Small-step:

Looping described as: infinite transition sequence

< while tt do nil , > ?

< while tt do nil , > < nil ; while tt do nil , >

< while tt do nil , > < nil ; while tt do nil , >

< while tt do nil , > ?…

< nil , >

Looping described as: infinite inference tree (actually no inference tree)!

“vertically infinite”

“horizontally infinite”

CLAUS BRABRAND © SEMANTICS (Q1,’06)[ 11 ]

SEP 14, 2006

Extension: Abnormal Termination

Language L:

Commands (c Com):

Big-step SOS ?

Small-step SOS ?

c ::= nil | v := e | c ; c’ | if b then c else c’ | while b do c | abort

CLAUS BRABRAND © SEMANTICS (Q1,’06)[ 12 ]

SEP 14, 2006

Big-step vs. Small-step: Abnormal Termination

Big-step:

Small-step:

Stuck conf.'s described as: terminating transition sequence ( looping)

< nil ; abort , > ?

< nil ; abort , > < abort , >

< abort , > ?< nil , >

Stuck configurations described as: no inference tree (as with looping)!

NB: Big-step cannot distinguish looping and abnormal termination!

NB: Small-step can distinguish looping and abnormal termination!

stuck

stuck

CLAUS BRABRAND © SEMANTICS (Q1,’06)[ 13 ]

SEP 14, 2006

Extension: Non-determinism

Language L:

Commands (c Com):

Big-step SOS ?

Small-step SOS ?

c ::= nil | v := e | c ; c’ | if b then c else c’ | while b do c | c alt c’

CLAUS BRABRAND © SEMANTICS (Q1,’06)[ 14 ]

SEP 14, 2006

Big-step vs. Small-step: Non-determinism

Big-step:

Small-step:

Small-step will commit to a choice (right here, right now)

< abort alt nil, >

< abort alt nil , > < abort , >

< nil , >

Big-step will look ahead for “good” choices (here, only 1 inf. tree exists)

NB: Big-step will suppress non-termination (and abnormal termination)!

NB: Small-step will not suppress looping (or abortion);

< abort alt nil , > < nil , >

or

stuck

CLAUS BRABRAND © SEMANTICS (Q1,’06)[ 15 ]

SEP 14, 2006

Extension: Parallelism

Language L:

Commands (c Com):

Big-step SOS ?

Small-step SOS ?

c ::= nil | v := e | c ; c’ | if b then c else c’ | while b do c | c par c’

with “interleaving semantics”

CLAUS BRABRAND © SEMANTICS (Q1,’06)[ 16 ]

SEP 14, 2006

Big-step vs. Small-step: Parallelism

Big-step:

Small-step:

Small-step can evaluate one step of c0, then c1, then c0, …

< c0 par c1 , > ”

< c0 , > ’

Big-step will have to (chose) evaluate either c0 (or c1) completely first

NB: Big-step cannot express (interleaving) parallelism!

NB: Small-step can easily express (interleaving) parallelism!

< c0 par c1 , >

< c0 par c1’ , ’>

< c0’ par c1’ , ”>

< c1 , ’ > ”

CLAUS BRABRAND SEMANTICS (Q1,’06) SEP 14, 2006

RUNTIME-ERRORS

CLAUS BRABRAND © SEMANTICS (Q1,’06)[ 18 ]

SEP 14, 2006

SOS for division

SOS for division:

Stuck configuration?:

[DIV1]

< e0 / e1 , > < e0’ / e1 , ’ >< e0 , > < e0’ , ’ >

[DIV2]

[DIV3]< n0 / n1 , > < m , >

m = n0 / n1

< n0 / e1 , > < n0 / e1’ , ’ >< e1 , > < e1’ , ’ >

Store = Var Z

< x / 0 , [x=3] > < 3 / 0 , [x=3] > stuck

n1 0

CLAUS BRABRAND © SEMANTICS (Q1,’06)[ 19 ]

SEP 14, 2006

Recall: Terminal Trans. Sys.

A Terminal Transition System is a structure:

is the set of configurations is the transition relation T is a set of final configurations

…satisfying: i.e. “all configurations in ‘T’ really are terminal”.

…but not the “converse”:

However, in practise achieved through runtime-errors!

, , T

T : ’ : ’

T : ’ : ’

CLAUS BRABRAND © SEMANTICS (Q1,’06)[ 20 ]

SEP 14, 2006

So what about “Division by Zero”

We would like:

Add configuration: …and rule:

…but now what about:

< 3 / 0 , > runtime-error

[DIV4]< n0 / n1 , > runtime-error

n1 = 0

L := Exp Store {runtime-error}

< (3 / 0) + 2 , > stuck?!?

CLAUS BRABRAND © SEMANTICS (Q1,’06)[ 21 ]

SEP 14, 2006

Add runtime-errors for [add]/[sub]/..

Propagation of runtime-errors:

[SUM3]

< e0 + e1 , > runtime-error

< e0 , > runtime-error

[SUM4]

< n0 + e1 , > runtime-error

< e1 , > runtime-error

[SUB3]

< e0 - e1 , > runtime-error

< e0 , > runtime-error

[SUB4]

< n0 - e1 , > runtime-error

< e1 , > runtime-error

CLAUS BRABRAND © SEMANTICS (Q1,’06)[ 22 ]

SEP 14, 2006

Propagation…

Even for Boolean Expressions:

And Commands:

[SEQ3]C

< c0 ; c1 , > C runtime-error

< c0 , > C runtime-error

< ~ b , > B runtime-error

< b , > B runtime-error[NOT2]B

CLAUS BRABRAND © SEMANTICS (Q1,’06)[ 23 ]

SEP 14, 2006

All this just for Division by Zero?

Yes Note: the same thing happens in prog. lang.’s)

Same thing for (depending on lang.): Arithmetic overflow Square root of a negative number Overstepping array bounds Reading uninitialized variable Dereferencing null-pointers Dynamic type failure (in dynamically typed lang.s) …

CLAUS BRABRAND SEMANTICS (Q1,’06) SEP 14, 2006

EXCEPTIONS AND Exc-HANDLING

CLAUS BRABRAND © SEMANTICS (Q1,’06)[ 25 ]

SEP 14, 2006

Exception Handling

Suppose we want to recover from div-by-zero Turn it into an exception (instead):

And add exception handler construct: Commands (c Com):

For simplicity, let’s assume x is always dbzexception

< 3 / 0 , > < dbzexception , >

c ::= nil | v := e | c ; c’ | if b then c else c’ | while b do c try c catch x recover c’

CLAUS BRABRAND © SEMANTICS (Q1,’06)[ 26 ]

SEP 14, 2006

SOS for try-catch-recover

SOS for “try-catch-recover”:

[TRY1]< c0 , > < c0’ , ’ >

[TRY2]

[TRY3]< c0 , > < x , ’ >

< try c0 catch x recover c1, > < try c0’ catch x recover c1 , ’ >

< try c0 catch x recover c1 , > < c1 , ’ >

< c0 , > ’

< try c0 catch x recover c1 , > ’

Recall that x is always dbzexception

CLAUS BRABRAND SEMANTICS (Q1,’06) SEP 14, 2006

TYPE ERRORS

CLAUS BRABRAND © SEMANTICS (Q1,’06)[ 28 ]

SEP 14, 2006

Consider Variant of L; L’

Basic Syntactic Sets: Operators

Derived Syntactic Sets: (Mixed) Expressions (e Exp):

Commands (c Com):

e ::= n | t | v | e o e’ | ~ e

c ::= nil | v := e | c ; c’ | if e then c else c’ | while e do c

o { +, -, , /, =, or }Store = Var Z

Assume variables canonly hold integers:

CLAUS BRABRAND © SEMANTICS (Q1,’06)[ 29 ]

SEP 14, 2006

Tons of Problems...(?)

Now what about expressions like…: …

Well, we could make them runtime-errors or we could have "fake, almost-like-booleans" (as C) However, compile-time errors (much better) !!!

2 + tt

~ 42

if 5 then c0 else c1

while 87 do c

x := tt ?

CLAUS BRABRAND © SEMANTICS (Q1,’06)[ 30 ]

SEP 14, 2006

Runtime- vs. compile-time errors

Runtime-error (aka. dynamic error):

i.e., maybe intercepted when the program is run !

Compile-time error (aka. static error):

i.e., always intercepted when program is compiled !

int n; .. x = n / 0; // runtime-error (exception)

int n; .. if (n) x = 42; // compile-time error

[Java]

[Java]

CLAUS BRABRAND © SEMANTICS (Q1,’06)[ 31 ]

SEP 14, 2006

Dynamically vs. Statically Typed Lang.’s

Dynamically Typed Language:

i.e., error found only when the program is run (maybe)

Statically Typed Language:

i.e., error found when the program is compiled

$beer = true; // dynamically typed vars .. $x = $beer - 42; // runtime-error

boolean b = true; // statically typed vars .. x = b - 42; // compile-time error

[Basic]

[Java]

CLAUS BRABRAND © SEMANTICS (Q1,’06)[ 32 ]

SEP 14, 2006

However, …

Not all runtime-errors can be “turned into” compile-time errors:

Consider division-by-zero (in Java): runtime-error here e’ evaluates to 0

We would really like: runtime-error compile-time error

However, we cannot do (compile-time) “static analysis”; Since the error depends on the runtime value of e’,

we can only evaluate e’ in a particular store,

e / e’

CLAUS BRABRAND © SEMANTICS (Q1,’06)[ 33 ]

SEP 14, 2006

What about Type Errors

Again, we would really like: runtime error compile-time type error

However, again:

If we could invent some stronger requirement: runtime error => compile-time type error

Then (by contraposition; i.e. ): no compile-time type error => no runtime error

if (e) b := 7;else b := tt;b := ~ b; // potential runtime-error

Again, the (potential) error depends on the runtime value of e

P => Q ~Q => ~P

CLAUS BRABRAND © SEMANTICS (Q1,’06)[ 34 ]

SEP 14, 2006

Type Declarations(!)

Add type declarations (bool, int, …) And make sure they are respected

(everywhere in the program)

However, now we need to do (static) type checking [in 4 slides…]

bool b; // type declaration (b always bool)if (e) b := 7; // static type errorelse b := tt; // okayb := ~ b; // okay

CLAUS BRABRAND © SEMANTICS (Q1,’06)[ 35 ]

SEP 14, 2006

Approximative Solution

Potential error… …maybe it never happens(!?):

The Type Checking Approximation:

if (ff) b := 7;else b := tt;b := ~ b; // never an error!!!

never error maybe error

Type safe!

.?

. .never error maybe error

undecidable

type-checking: safe (over-)approximation

above program

Quality of a type-system ~ size of “slack” (wrongfully rejected programs)

CLAUS BRABRAND SEMANTICS (Q1,’06) SEP 14, 2006

TYPE CHECKING

CLAUS BRABRAND © SEMANTICS (Q1,’06)[ 37 ]

SEP 14, 2006

Recall L’

Basic Syntactic Sets: Operators

Derived Syntactic Sets: (Mixed) Expressions (e Exp):

Commands (c Com):

e ::= n | t | v | e o e’ | ~ e

c ::= nil | v := e | c ; c’ | if e then c else c’ | while e do c

o { +, -, , /, =, or }Store = Var Z

Assume variables canonly hold integers:

CLAUS BRABRAND © SEMANTICS (Q1,’06)[ 38 ]

SEP 14, 2006

Introducing Types

Define a set of types: Types = { int, bool }

Define (static) type relation: |_ Exp x Types We shall write instead of

Meaning: “the expression 42 has type int”

We would like: whereas for any Types

|_ 42 : int (42,int) ‘|_’

|_ 3+5 : int

|_ 3=5 : bool

|_ 7+tt :

CLAUS BRABRAND © SEMANTICS (Q1,’06)[ 39 ]

SEP 14, 2006

Basic Syn. Sets (Inherently Typed)

Expressions:

Numbers: for any n

Truthvalues: for any t

Variables: for any v (assumption: vars only hold ints)

|_ n : int

|_ t : bool

e ::= n | t | v | e e’ | ~ e

|_ v : int

Store = Var Z

Assume variables canonly hold integers:

CLAUS BRABRAND © SEMANTICS (Q1,’06)[ 40 ]

SEP 14, 2006

Expressions:

Negation: (i.e., only defined if )

Binary Operators:

Where: i.e. a partial function

Composite Definitions

e ::= n | t | v | e e’ | ~ e

|_ e : bool|_ ~e : bool

|_ e0 : 0 |_ e1 : 1

|_ e0 e1 : 2

|_ e : bool

|_ ~e : bool

2 = type(0, 1)

type : Types x Types Types

type+ := [int,int | int]

type= := [int,int | bool]

typeor := [bool,bool | bool]

{ +, -, , /, =, or }

Examples:

CLAUS BRABRAND © SEMANTICS (Q1,’06)[ 41 ]

SEP 14, 2006

Type Checking Example

Type check:

How?

~ (ff or ((1 + 2) = 3))

CLAUS BRABRAND © SEMANTICS (Q1,’06)[ 42 ]

SEP 14, 2006

Commands: Well-formedness

Commands:

Define (static) well-formedness relation: |_wfc Com

We shall write instead of Meaning: “c is well-formed (i.e., has no type errors)”

We would like: whereas

nil | v := e | c ; c’ | if e then c else c’ | while e do c

|_wfc c c ‘|_wfc’

|_wfc x := (1+2)

|_wfc if ~(1=2) then nil else y := 3

|_wfc if 5 then c else c’

CLAUS BRABRAND © SEMANTICS (Q1,’06)[ 43 ]

SEP 14, 2006

WFC: Nil, Ass., and Seq.

Commands:

Nil:

Assignment:

Sequence:

nil | v := e | c ; c’ | if e then c else c’ | while e do c

|_wfc nil

|_ e : int

|_wfc v := e

|_wfc c0 ; c1

|_wfc c0 |_wfc c1

CLAUS BRABRAND © SEMANTICS (Q1,’06)[ 44 ]

SEP 14, 2006

WFC: if-then-else and while-do.

Commands:

if-then-else:

while-do:

nil | v := e | c ; c’ | if e then c else c’ | while e do c

|_wfc if e then c0 else c1

|_wfc c0 |_wfc c1|_ e : bool

|_wfc while e do c

|_wfc c|_ e : bool

CLAUS BRABRAND © SEMANTICS (Q1,’06)[ 45 ]

SEP 14, 2006

Type Checking Examples (wfc)

Type check:

How?

x := 1 ; y:= 2

while ~ff do nil

if tt then nil else x := tt

CLAUS BRABRAND SEMANTICS (Q1,’06) SEP 14, 2006

STRUCTURAL INDUCTION

Keywords:

Induction, base case, induction hypothesis, induction step structural induction

CLAUS BRABRAND © SEMANTICS (Q1,’06)[ 47 ]

SEP 14, 2006

Principle of Mathematical Induction

Let P be a predicate (i.e. a boolean function): then we have that:

Intuitive: ?

P: N { true, false }

nN : P(n)

P(0)

induction stepbase case

Principle of mathematical induction:

P(n) P(n+1)

P(3)

P(0) P(0) => P(1) P(1) => P(2) P(2) => P(3)

CLAUS BRABRAND © SEMANTICS (Q1,’06)[ 48 ]

SEP 14, 2006

Example Induction Proof

Example: Base case (i.e. prove P(0)):

Induction step (i.e. prove P(n) => P(n+1)): Assume the induction hypothesis (I.H.)

(i.e. assume P(n)):

Now prove P(n+1):

P(n) [ 20 + 21 + … + 2n = 2n+1 – 1 ]

P(0) [ 20 = 20+1 – 1 ]

[ 20 + 21 + … + 2n = 2n+1 – 1 ]

[ 20 + 21 + … + 2n+1 = 2(n+1)+1 – 1 ]

20 + 21 + … + 2n + 2n+1 (20 + 21 + … + 2n) + 2n+1=

(2n+1 – 1) + 2n+1 == 2*2n+1 – 1 = 2(n+1)+1 – 1 I.H.

CLAUS BRABRAND © SEMANTICS (Q1,’06)[ 49 ]

SEP 14, 2006

Structural Induction (for Exp)

Given: Arithmetic Expressions (e Exp)

e ::= n | v | e0+e1

e Exp : P(e)

P(n)

composite (inductive) casebase cases

Principle of structural induction:

P(e0) P(e1) P(e0+e1) P(v) and

CLAUS BRABRAND © SEMANTICS (Q1,’06)[ 50 ]

SEP 14, 2006

Intuition: Induction vs. Str’ Induction

Induction: Holds for ?

Structural Induction: Holds for ?

P(0) P(0) => P(1) P(1) => P(2) P(2) => P(3)

P(3)

P(7+(x+y))

P(7)

P(x) P(y)

P(x+y)

P(7+(x+y))

CLAUS BRABRAND © SEMANTICS (Q1,’06)[ 51 ]

SEP 14, 2006

Structural Induction (for BExp)

Boolean Expressions (b BExp):

Live exercise… :) [Think 3 mins; then interactively on the blackboard]

b ::= t | b or b’ | ~ b

CLAUS BRABRAND © SEMANTICS (Q1,’06)[ 52 ]

SEP 14, 2006

Structural Induction Examples

Given: Arithmetic Expressions (e Exp)

Property A: Evaluation of arithmetic expressions (using a small-step

operational semantics) is deterministic

Property B: Evaluation of arithmetic expressions (using a small-step

operational semantics) always terminates

e ::= n | v | e0+e1

CLAUS BRABRAND © SEMANTICS (Q1,’06)[ 53 ]

SEP 14, 2006

"Three minutes paper"

Please spend three minutes writing down the most important things that you have learned today (now).

After 1 dayAfter 1 week

After 3 weeksAfter 2 weeks

Immediately

CLAUS BRABRAND SEMANTICS (Q1,’06) SEP 14, 2006

Next week: Def's: static vs. dynamic semantics

Any Questions?