Presentation is loading. Please wait.

Presentation is loading. Please wait.

C LAUS B RABRAND S EMANTICS (Q1,’05) S EP 8, 2005 C LAUS B RABRAND © 2005, University of Aarhus [ ] [

Similar presentations


Presentation on theme: "C LAUS B RABRAND S EMANTICS (Q1,’05) S EP 8, 2005 C LAUS B RABRAND © 2005, University of Aarhus [ ] ["— Presentation transcript:

1 C LAUS B RABRAND S EMANTICS (Q1,’05) S EP 8, 2005 C LAUS B RABRAND © 2005, University of Aarhus [ brabrand@daimi.au.dk ] [ http://www.daimi.au.dk/~brabrand/ ] S EMANTICS (Q1,’05) W EEK 3: ” BIG- vs. SMALL, ERRORS, TYPE CHECKING AND S.I.”

2 C LAUS B RABRAND © S EMANTICS (Q1,’05) [ 2 ] S EP 8, 2005 Reorganization Big-step vs. Small-step (week 3) Exceptions and Exception Handling (week 3) Type Checking (week 7  3) Definitions and Declarations (week 3  4) Recursion (week 4  /dev/null) Semantics in the industry/practice (week 7)

3 C LAUS B RABRAND © S EMANTICS (Q1,’05) [ 3 ] S EP 8, 2005 Week 3 - Outline Repetition (from week 2) Small-step vs. big-step (a comparison) Runtime-errors Exceptions and Exception Handling Type Errors Type Checking Structural Induction (more + examples)

4 C LAUS B RABRAND © S EMANTICS (Q1,’05) [ 4 ] S EP 8, 2005 Repetition (“3x3 things…”) Class X: 1x. SOS: big-step vs. small-step ? 2x. Structural induction: really hard ! 3x. Programming exercice:.grm /.lex ?!? Class Y: 1y. SOS: big-step vs. small-step ? 2y. Structural induction: really hard ! 3y. Late updating: exercises was updated during the w-e !?! Class Z: 1z. SOS: big-step vs. small-step ? 2z. Structural induction: really hard ! 3z. SOS: repeat-until; only indirectly in terms of while-do ?

5 C LAUS B RABRAND © S EMANTICS (Q1,’05) [ 5 ] S EP 8, 2005 Repeat-until 1. Can be done in terms of while-do 2. Add intermediate syntax: e.g. Actually, already seen similar thing: Used in literals vs. numbers Also used in recursion [maybe later] …and in the evaluation of definitions [later]  repeat c until b rem c repeat c’ until b rem c  (“1”+“2”)+“3” 3+“3”  Literal (syntactic) Number (semantic)

6 C LAUS B RABRAND S EMANTICS (Q1,’05) S EP 8, 2005 B IG-STEP vs. S MALL-STEP

7 C LAUS B RABRAND © S EMANTICS (Q1,’05) [ 7 ] S EP 8, 2005 Recall L (commands) Recall L Let’s consider commands only for now…: Commands ( c  Com): c ::= nil | v := e | c ; c’ | if b then c else c’ | while b do c

8 C LAUS B RABRAND © S EMANTICS (Q1,’05) [ 8 ] S EP 8, 2005 Big-step vs. Small-step: SOS Big-step SOS: Small-step SOS:  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

9 C LAUS B RABRAND © S EMANTICS (Q1,’05) [ 9 ] S EP 8, 2005 Big-step vs. Small-step: Looping Big-step: Small-step: Looping described as: infinite transition sequence  ?     …  ? …   Looping described as: infinite inference tree (actually no inference tree)! “vertically infinite” “horizontally infinite”

10 C LAUS B RABRAND © S EMANTICS (Q1,’05) [ 10 ] S EP 8, 2005 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

11 C LAUS B RABRAND © S EMANTICS (Q1,’05) [ 11 ] S EP 8, 2005 Big-step vs. Small-step: Abnormal Termination Big-step: Small-step: Looping 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

12 C LAUS B RABRAND © S EMANTICS (Q1,’05) [ 12 ] S EP 8, 2005 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’

13 C LAUS B RABRAND © S EMANTICS (Q1,’05) [ 13 ] S EP 8, 2005 Big-step vs. Small-step: Non-determinism Big-step: Small-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

14 C LAUS B RABRAND © S EMANTICS (Q1,’05) [ 14 ] S EP 8, 2005 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”

15 C LAUS B RABRAND © S EMANTICS (Q1,’05) [ 15 ] S EP 8, 2005 Big-step vs. Small-step: Parallelism Big-step: Small-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!    ”

16 C LAUS B RABRAND S EMANTICS (Q1,’05) S EP 8, 2005 R UNTIME-ERRORS

17 C LAUS B RABRAND © S EMANTICS (Q1,’05) [ 17 ] S EP 8, 2005 SOS for division SOS for division: Stuck configuration?: [ DIV 1 ]  [ DIV 2 ] [ DIV 3 ]  m = n 0 / n 1  Store = Var  Z  stuck  n 1  0

18 C LAUS B RABRAND © S EMANTICS (Q1,’05) [ 18 ] S EP 8, 2005 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 :   ’   :    ’

19 C LAUS B RABRAND © S EMANTICS (Q1,’05) [ 19 ] S EP 8, 2005 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?!?

20 C LAUS B RABRAND © S EMANTICS (Q1,’05) [ 20 ] S EP 8, 2005 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

21 C LAUS B RABRAND © S EMANTICS (Q1,’05) [ 21 ] S EP 8, 2005 Propagation… Even for Boolean Expressions: And Commands: [ SEQ 3 ] C  C runtime-error …  B runtime-error [ NOT 2 ] B …

22 C LAUS B RABRAND © S EMANTICS (Q1,’05) [ 22 ] S EP 8, 2005 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) …

23 C LAUS B RABRAND S EMANTICS (Q1,’05) S EP 8, 2005 E XCEPTIONS AND Exc - H ANDLING

24 C LAUS B RABRAND © S EMANTICS (Q1,’05) [ 24 ] S EP 8, 2005 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’

25 C LAUS B RABRAND © S EMANTICS (Q1,’05) [ 25 ] S EP 8, 2005 SOS for try-catch-recover SOS for “try-catch-recover”: [ TRY 1 ]  [ TRY 2 ] [ TRY 3 ]    ’ Recall that x is always dbz exception

26 C LAUS B RABRAND S EMANTICS (Q1,’05) S EP 8, 2005 T YPE E RRORS

27 C LAUS B RABRAND © S EMANTICS (Q1,’05) [ 27 ] S EP 8, 2005 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:

28 C LAUS B RABRAND © S EMANTICS (Q1,’05) [ 28 ] S EP 8, 2005 Tons of Problems...(?) Now what about expressions like…: … Well, we could make them runtime-errors However, compile-time errors (much better) !!! 2 + tt ~ 42 if 5 then c 0 else c 1 while 87 do c x := tt ?

29 C LAUS B RABRAND © S EMANTICS (Q1,’05) [ 29 ] S EP 8, 2005 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., intercepted when the program is compiled !!! int n;.. x = n / 0; // runtime-error (exception) int n;.. if (n) x = 42; // compile-time error [Java]

30 C LAUS B RABRAND © S EMANTICS (Q1,’05) [ 30 ] S EP 8, 2005 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]

31 C LAUS B RABRAND © S EMANTICS (Q1,’05) [ 31 ] S EP 8, 2005 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’

32 C LAUS B RABRAND © S EMANTICS (Q1,’05) [ 32 ] S EP 8, 2005 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

33 C LAUS B RABRAND © S EMANTICS (Q1,’05) [ 33 ] S EP 8, 2005 Type Declarations(!) Add type declarations (bool, int, …) And make sure they are repected (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

34 C LAUS B RABRAND © S EMANTICS (Q1,’05) [ 34 ] S EP 8, 2005 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)

35 C LAUS B RABRAND S EMANTICS (Q1,’05) S EP 8, 2005 T YPE C HECKING

36 C LAUS B RABRAND © S EMANTICS (Q1,’05) [ 36 ] S EP 8, 2005 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:

37 C LAUS B RABRAND © S EMANTICS (Q1,’05) [ 37 ] S EP 8, 2005 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 : 

38 C LAUS B RABRAND © S EMANTICS (Q1,’05) [ 38 ] S EP 8, 2005 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:

39 C LAUS B RABRAND © S EMANTICS (Q1,’05) [ 39 ] S EP 8, 2005 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:

40 C LAUS B RABRAND © S EMANTICS (Q1,’05) [ 40 ] S EP 8, 2005 Type Checking Example Type check: How? ~ (ff or ((1 + 2) = 3))

41 C LAUS B RABRAND © S EMANTICS (Q1,’05) [ 41 ] S EP 8, 2005 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’

42 C LAUS B RABRAND © S EMANTICS (Q1,’05) [ 42 ] S EP 8, 2005 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

43 C LAUS B RABRAND © S EMANTICS (Q1,’05) [ 43 ] S EP 8, 2005 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

44 C LAUS B RABRAND © S EMANTICS (Q1,’05) [ 44 ] S EP 8, 2005 Type Checking Examples (wfc) Type check: How? x := 1 ; y:= 2 while ~ff do nil if tt then nil else x := tt

45 C LAUS B RABRAND S EMANTICS (Q1,’05) S EP 8, 2005 S TRUCTURAL I NDUCTION

46 C LAUS B RABRAND © S EMANTICS (Q1,’05) [ 46 ] S EP 8, 2005 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

47 C LAUS B RABRAND © S EMANTICS (Q1,’05) [ 47 ] S EP 8, 2005 Structural Induction (for BExp) Boolean Expressions ( b  BExp): What is the proof structure for BExp? b ::= t | b or b’ | ~ b

48 C LAUS B RABRAND © S EMANTICS (Q1,’05) [ 48 ] S EP 8, 2005 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) terminate e ::= n | v | e 0 +e 1


Download ppt "C LAUS B RABRAND S EMANTICS (Q1,’05) S EP 8, 2005 C LAUS B RABRAND © 2005, University of Aarhus [ ] ["

Similar presentations


Ads by Google