History TinyImp Hoare Logic Imperative Programming Languages Christine Rizkallah CSE, UNSW (and data61) Term 3 2019 1
History TinyImp Hoare Logic Imperative Programming imper ¯ o Definition Imperative programming is where programs are described as a series of statements or commands to manipulate mutable state or cause externally observable effects . States may take the form of a mapping from variable names to their values, or even a model of a CPU state with a memory model (for example, in an assembly language ). 2
History TinyImp Hoare Logic The Old Days Early microcomputer languages used a line numbering system with GO TO statements used to arrange control flow. 3
History TinyImp Hoare Logic Factorial Example in BASIC (1964) 4
History TinyImp Hoare Logic Dijkstra (1968) The structured programming movement brought in control structures to mainstream use, such as conditionals and loops. 5
History TinyImp Hoare Logic Factorial Example in Pascal (1970) 6
History TinyImp Hoare Logic Syntax We’re going to specify a language TinyImp , based on structured programming. The syntax consists of statements and expressions. Grammar Stmt ::= Do nothing skip | x := Expr Assignment | var y · Stmt Declaration | if Expr then Stmt else Stmt fi Conditional | while Expr do Stmt od Loop | Stmt ; Stmt Sequencing Expr ::= � Arithmetic expressions � We already know how to make unambiguous abstract syntax, so we will use concrete syntax in the rules for readability. 7
History TinyImp Hoare Logic Examples Example (Factorial and Fibonacci) var m · var n · var i · var i · m := 1; n := 1; var m · i := 1; i := 0; while i < N do m := 1; var t · t := m ; while i < N do m := n ; i := i + 1; n := m + t ; m := m × i i := i + 1 od od 8
History TinyImp Hoare Logic Static Semantics Types? 9
History TinyImp Hoare Logic Static Semantics Types? We only have one type ( int ), so type checking is a wash. Scopes? 10
History TinyImp Hoare Logic Static Semantics Types? We only have one type ( int ), so type checking is a wash. Scopes? We have to check that variables are declared before use. Anything Else? 11
History TinyImp Hoare Logic Static Semantics Types? We only have one type ( int ), so type checking is a wash. Scopes? We have to check that variables are declared before use. Anything Else? We have to check that variables are initialized before they are used! 12
History TinyImp Hoare Logic Static Semantics Types? We only have one type ( int ), so type checking is a wash. Scopes? We have to check that variables are declared before use. Anything Else? We have to check that variables are initialized before they are used! U ; V ⊢ s ok � W 13
History TinyImp Hoare Logic Static Semantics Types? We only have one type ( int ), so type checking is a wash. Scopes? We have to check that variables are declared before use. Anything Else? We have to check that variables are initialized before they are used! U ; V ⊢ s ok � W Set of initially uninitialized variables 14
History TinyImp Hoare Logic Static Semantics Types? We only have one type ( int ), so type checking is a wash. Scopes? We have to check that variables are declared before use. Anything Else? We have to check that variables are initialized before they are used! Set of initialized variables U ; V ⊢ s ok � W Set of initially uninitialized variables 15
History TinyImp Hoare Logic Static Semantics Types? We only have one type ( int ), so type checking is a wash. Scopes? We have to check that variables are declared before use. Anything Else? We have to check that variables are initialized before they are used! Indicates that no unsafe reads occur Set of initialized variables U ; V ⊢ s ok � W Set of initially uninitialized variables 16
History TinyImp Hoare Logic Static Semantics Types? We only have one type ( int ), so type checking is a wash. Scopes? We have to check that variables are declared before use. Anything Else? We have to check that variables are initialized before they are used! Indicates that no unsafe reads occur Set of initialized variables U ; V ⊢ s ok � W Set of initially uninitialized variables Set of definitely written to variables 17
History TinyImp Hoare Logic Static Semantics Rules U ; V ⊢ skip ok � ∅ 18
History TinyImp Hoare Logic Static Semantics Rules U ; V ⊢ skip ok � ∅ U ; V ⊢ x := e ok � 19
History TinyImp Hoare Logic Static Semantics Rules x ∈ U ∪ V U ; V ⊢ skip ok � ∅ U ; V ⊢ x := e ok � 20
History TinyImp Hoare Logic Static Semantics Rules x ∈ U ∪ V FV ( e ) ⊆ V U ; V ⊢ skip ok � ∅ U ; V ⊢ x := e ok � 21
History TinyImp Hoare Logic Static Semantics Rules x ∈ U ∪ V FV ( e ) ⊆ V U ; V ⊢ skip ok � ∅ U ; V ⊢ x := e ok � { x } 22
History TinyImp Hoare Logic Static Semantics Rules x ∈ U ∪ V FV ( e ) ⊆ V U ; V ⊢ skip ok � ∅ U ; V ⊢ x := e ok � { x } U ; V ⊢ var y · s ok � 23
History TinyImp Hoare Logic Static Semantics Rules x ∈ U ∪ V FV ( e ) ⊆ V U ; V ⊢ skip ok � ∅ U ; V ⊢ x := e ok � { x } U ∪ { y } ; V ⊢ s ok � W U ; V ⊢ var y · s ok � 24
History TinyImp Hoare Logic Static Semantics Rules x ∈ U ∪ V FV ( e ) ⊆ V U ; V ⊢ skip ok � ∅ U ; V ⊢ x := e ok � { x } U ∪ { y } ; V ⊢ s ok � W U ; V ⊢ var y · s ok � W \ { y } 25
History TinyImp Hoare Logic Static Semantics Rules x ∈ U ∪ V FV ( e ) ⊆ V U ; V ⊢ skip ok � ∅ U ; V ⊢ x := e ok � { x } U ∪ { y } ; V ⊢ s ok � W U ; V ⊢ var y · s ok � W \ { y } U ; V ⊢ if e then s 1 else s 2 fi ok � 26
History TinyImp Hoare Logic Static Semantics Rules x ∈ U ∪ V FV ( e ) ⊆ V U ; V ⊢ skip ok � ∅ U ; V ⊢ x := e ok � { x } U ∪ { y } ; V ⊢ s ok � W U ; V ⊢ var y · s ok � W \ { y } FV ( e ) ⊆ V U ; V ⊢ if e then s 1 else s 2 fi ok � 27
History TinyImp Hoare Logic Static Semantics Rules x ∈ U ∪ V FV ( e ) ⊆ V U ; V ⊢ skip ok � ∅ U ; V ⊢ x := e ok � { x } U ∪ { y } ; V ⊢ s ok � W U ; V ⊢ var y · s ok � W \ { y } FV ( e ) ⊆ V U ; V ⊢ s 1 ok � W 1 U ; V ⊢ if e then s 1 else s 2 fi ok � 28
History TinyImp Hoare Logic Static Semantics Rules x ∈ U ∪ V FV ( e ) ⊆ V U ; V ⊢ skip ok � ∅ U ; V ⊢ x := e ok � { x } U ∪ { y } ; V ⊢ s ok � W U ; V ⊢ var y · s ok � W \ { y } FV ( e ) ⊆ V U ; V ⊢ s 1 ok � W 1 U ; V ⊢ s 2 ok � W 2 U ; V ⊢ if e then s 1 else s 2 fi ok � 29
History TinyImp Hoare Logic Static Semantics Rules x ∈ U ∪ V FV ( e ) ⊆ V U ; V ⊢ skip ok � ∅ U ; V ⊢ x := e ok � { x } U ∪ { y } ; V ⊢ s ok � W U ; V ⊢ var y · s ok � W \ { y } FV ( e ) ⊆ V U ; V ⊢ s 1 ok � W 1 U ; V ⊢ s 2 ok � W 2 U ; V ⊢ if e then s 1 else s 2 fi ok � W 1 ∩ W 2 30
History TinyImp Hoare Logic Static Semantics Rules x ∈ U ∪ V FV ( e ) ⊆ V U ; V ⊢ skip ok � ∅ U ; V ⊢ x := e ok � { x } U ∪ { y } ; V ⊢ s ok � W U ; V ⊢ var y · s ok � W \ { y } FV ( e ) ⊆ V U ; V ⊢ s 1 ok � W 1 U ; V ⊢ s 2 ok � W 2 U ; V ⊢ if e then s 1 else s 2 fi ok � W 1 ∩ W 2 U ; V ⊢ while e do s od ok � 31
History TinyImp Hoare Logic Static Semantics Rules x ∈ U ∪ V FV ( e ) ⊆ V U ; V ⊢ skip ok � ∅ U ; V ⊢ x := e ok � { x } U ∪ { y } ; V ⊢ s ok � W U ; V ⊢ var y · s ok � W \ { y } FV ( e ) ⊆ V U ; V ⊢ s 1 ok � W 1 U ; V ⊢ s 2 ok � W 2 U ; V ⊢ if e then s 1 else s 2 fi ok � W 1 ∩ W 2 FV ( e ) ⊆ V U ; V ⊢ while e do s od ok � 32
History TinyImp Hoare Logic Static Semantics Rules x ∈ U ∪ V FV ( e ) ⊆ V U ; V ⊢ skip ok � ∅ U ; V ⊢ x := e ok � { x } U ∪ { y } ; V ⊢ s ok � W U ; V ⊢ var y · s ok � W \ { y } FV ( e ) ⊆ V U ; V ⊢ s 1 ok � W 1 U ; V ⊢ s 2 ok � W 2 U ; V ⊢ if e then s 1 else s 2 fi ok � W 1 ∩ W 2 FV ( e ) ⊆ V U ; V ⊢ s ok � W U ; V ⊢ while e do s od ok � 33
History TinyImp Hoare Logic Static Semantics Rules x ∈ U ∪ V FV ( e ) ⊆ V U ; V ⊢ skip ok � ∅ U ; V ⊢ x := e ok � { x } U ∪ { y } ; V ⊢ s ok � W U ; V ⊢ var y · s ok � W \ { y } FV ( e ) ⊆ V U ; V ⊢ s 1 ok � W 1 U ; V ⊢ s 2 ok � W 2 U ; V ⊢ if e then s 1 else s 2 fi ok � W 1 ∩ W 2 FV ( e ) ⊆ V U ; V ⊢ s ok � W U ; V ⊢ while e do s od ok � ∅ 34
History TinyImp Hoare Logic Static Semantics Rules x ∈ U ∪ V FV ( e ) ⊆ V U ; V ⊢ skip ok � ∅ U ; V ⊢ x := e ok � { x } U ∪ { y } ; V ⊢ s ok � W U ; V ⊢ var y · s ok � W \ { y } FV ( e ) ⊆ V U ; V ⊢ s 1 ok � W 1 U ; V ⊢ s 2 ok � W 2 U ; V ⊢ if e then s 1 else s 2 fi ok � W 1 ∩ W 2 FV ( e ) ⊆ V U ; V ⊢ s ok � W U ; V ⊢ while e do s od ok � ∅ U ; V ⊢ s 1 ; s 2 ok � 35
History TinyImp Hoare Logic Static Semantics Rules x ∈ U ∪ V FV ( e ) ⊆ V U ; V ⊢ skip ok � ∅ U ; V ⊢ x := e ok � { x } U ∪ { y } ; V ⊢ s ok � W U ; V ⊢ var y · s ok � W \ { y } FV ( e ) ⊆ V U ; V ⊢ s 1 ok � W 1 U ; V ⊢ s 2 ok � W 2 U ; V ⊢ if e then s 1 else s 2 fi ok � W 1 ∩ W 2 FV ( e ) ⊆ V U ; V ⊢ s ok � W U ; V ⊢ while e do s od ok � ∅ U ; V ⊢ s 1 ok � W 1 U ; V ⊢ s 1 ; s 2 ok � 36
Recommend
More recommend