1 Semantics Q S EMANTICS (Q1,’07) Week 3 Jacob Andersen PhD student
2 Semantics Q Remember to sign up or check that you have been signed up for the exam !!! Sept
3 Semantics Q Week 3 - Outline 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 Intermediate Syntax Structural Induction
4 Semantics Q B IG-STEP vs. S MALL-STEP
5 Semantics Q Big-step vs. Small-step: Small-step while semantics: Big-step while semantics: SS [ WH 1 ] SS SS [ WH 2 ] SS [ WH 1 ] BS [ WH 2 ] BS BS ” BS BS ” BS ’ | _ b B * tt | _ b B * ff | _ b B * tt | _ b B * ff
6 Semantics Q Big-step vs. Small-step: Looping Small-step: Big-step: Looping described as: infinite transition sequence ? … ? … Looping described as: infinite inference tree (actually no inference tree)! “vertically infinite” “horizontally infinite” stuck
7 Semantics Q Extension: Abnormal Termination Language L: Commands ( c Com): Small-step semantics ? Big-step semantics ? c ::= nil | v := e | c ; c’ | if b then c else c’ | while b do c | abort no rule
8 Semantics Q Big-step vs. Small-step: Abnormal Termination Small-step: Big-step: Stuck conf.'s described as: terminating transition sequence ( looping) ? ? 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
9 Semantics Q Extension: Non-determinism Language L: Commands ( c Com): Small-step semantics ? Big-step semantics ? c ::= nil | v := e | c ; c’ | if b then c else c’ | while b do c | c alt c’ SS BS ’
10 Semantics Q Big-step vs. Small-step: Non-determinism Small-step: Big-step: Small-step will commit to a choice (right here, right now) 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); or stuck
11 Semantics Q Extension: Parallelism Language L: Commands ( c Com): Small-step semantics ? Big-step semantics ? c ::= nil | v := e | c ; c’ | if b then c else c’ | while b do c | c par c’ with “interleaving semantics” SS not possible (with “interleaving semantics”) SS
12 Semantics Q Big-step vs. Small-step: Parallelism Small-step: Big-step: Small-step can evaluate one step of c 0, then c 1, then c 0, … ” ’ Big-step will have to (chose) evaluate either c 0 (or c 1 ) completely first NB: Big-step cannot express (interleaving) parallelism! NB: Small-step can easily express (interleaving) parallelism! ”
13 Semantics Q The transitive closure * Recall the small-step semantics for L: And imagine the corresponding big-step semantics: such that: Note: can be done for L, but in general only is possible. A * is also a big-step evaluation in “disguise”. C := (Com Store) Store C C C T C := Store C … C (Com Store) Store C = C ∩ ( (Com Store) Store ) *
14 Semantics Q R UNTIME-ERRORS
15 Semantics Q SOS for division SOS for division: Division by 0 ? [ DIV 1 ] [ DIV 2 ] [ DIV 3 ] m = n 0 / n 1 stuck n 1 0
16 Semantics Q 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 practice achieved through runtime-errors! , , T T : ’ : ’ T : ’ : ’
17 Semantics Q So what about “Division by Zero” We would like: –Add configuration: –…and rule: …but now what about: runtime-error [ DIV 4 ] runtime-error n 1 = 0 L := Exp Store { runtime-error } stuck?!?
18 Semantics Q Add runtime-errors for [add]/[sub]/.. Propagation of runtime-errors: [ SUM 3 ] runtime-error [ SUM 4 ] runtime-error [ SUB 3 ] runtime-error [ SUB 4 ] runtime-error
19 Semantics Q Propagation… Even for Boolean Expressions: And Commands: [ SEQ 3 ] C C runtime-error … B runtime-error [ NOT 2 ] B …
20 Semantics Q All this just for Division by Zero? Yes Note: the same thing happens in prog. lang.’s Can be done more elegantly (albeit, not in this course) 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) …
21 Semantics Q E XCEPTIONS AND E XCEPTION H ANDLING
22 Semantics Q 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 dbz exception c ::= nil | v := e | c ; c’ | if b then c else c’ | while b do c try c catch x recover c’
23 Semantics Q SOS for try-catch-recover SOS for “try-catch-recover”: [ TRY 1 ] [ TRY 2 ] [ TRY 3 ] ’ Recall that x is always dbz exception
24 Semantics Q T YPE E RRORS
25 Semantics Q 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 can only hold integers:
26 Semantics Q 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 c 0 else c 1 while 87 do c x := tt ?
27 Semantics Q 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]
28 Semantics Q 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]
29 Semantics Q 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’
30 Semantics Q What about Type Errors Again, we would really like: runtime error compile-time type error However: 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 The (potential) error depends on the runtime value of e P => Q ~Q => ~P
31 Semantics Q 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 error else b := tt; // okay b := ~ b; // okay
32 Semantics Q 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)
33 Semantics Q T YPE C HECKING
34 Semantics Q 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 can only hold integers:
35 Semantics Q 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 :
36 Semantics Q 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 can only hold integers:
37 Semantics Q 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 | _ e 0 : 0 | _ e 1 : 1 | _ e 0 e 1 : 2 | _ e : bool | _ ~e : bool 2 = type ( 0, 1 ) type : Types x Types Types type + := [int,int | int] type = := [int,int | bool] type or := [bool,bool | bool] { +, -, , /, =, or } Examples:
38 Semantics Q Type Checking Example Type check: How? ~ (ff or ((1 + 2) = 3)))
39 Semantics Q 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’
40 Semantics Q 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 c 0 ; c 1 | _ wfc c 0 | _ wfc c 1
41 Semantics Q 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 c 0 else c 1 | _ wfc c 0 | _ wfc c 1 | _ e : bool | _ wfc while e do c | _ wfc c | _ e : bool
42 Semantics Q Type Checking Examples (wfc) Type check: How? x := 1 ; y:= 2 while ~ff do nil if tt then nil else x := tt
43 Semantics Q I NTERMEDIATE S YNTAX
44 Semantics Q “Intermediate Syntax” Sometimes “intermediate syntax” must be introduced in order to describe a small-step computation. E.g. (taken from the upcoming hand-in): » small-step semantics ?? You can think of F as a boolean expression with possible side-effects (hence evaluated in small steps). »Introducing new C construct: As always: “intermediate syntax” should not be thought of as concrete (ambiguity problems). »Abstract syntax: Adding a new node type to (intermediate only) ASTs C ::= (do F od) | … C ::= (do F od) | (do F rem F’ od) | … Evaluate F in small- steps as usual. While remembering the original F.
45 Semantics Q S TRUCTURAL I NDUCTION
46 Semantics Q Principle of Mathematical Induction Let P be a predicate (i.e. a boolean function): then we have that: Intuitive: ? P: N { true, false } n N : 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)
47 Semantics Q Example Induction Proof Example: Prove I.e. 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) [ … + 2 n = 2 n+1 – 1 ] P(0) [ 2 0 = – 1 ] [ … + 2 n = 2 n+1 – 1 ] [ … + 2 n+1 = 2 (n+1)+1 – 1 ] … + 2 n + 2 n+1 ( … + 2 n ) + 2 n+1 = (2 n+1 – 1) + 2 n+1 == 2*2 n+1 – 1 = 2 (n+1)+1 – 1 I.H. n N : ∑ 2 i = 2 n+1 – 1 i=0 n
48 Semantics Q Structural Induction (for Exp) Given: Arithmetic Expressions ( e Exp) – e ::= n | v | e 0 +e 1 e Exp : P(e) P(n) composite (inductive) case base cases Principle of structural induction: P(e 0 ) P(e 1 ) P(e 0 +e 1 ) P(v) and
49 Semantics Q 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))
50 Semantics Q Structural Induction (for BExp) Boolean Expressions ( b BExp): – Live exercise… :) [Think 3 mins; then interactively on the whiteboard] b ::= t | b or b’ | ~ b
51 Semantics Q 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 | e 0 +e 1
52 Semantics Q "Three minutes paper" Please spend three minutes writing down the most important things that you have learned today (now). After 1 day After 1 week After 3 weeks After 2 weeks Right away
53 Semantics Q Next week: Def's, static vs. dynamic semantics Any Questions?