CS 476 Programming Language Design William Mansky Dynamic

  • Slides: 35
Download presentation
CS 476 – Programming Language Design William Mansky

CS 476 – Programming Language Design William Mansky

Dynamic Semantics • What is the “meaning” of a program? • The values it

Dynamic Semantics • What is the “meaning” of a program? • The values it computes, its intermediate states: operational semantics • The specification it fulfills, the pre- and postconditions it satisfies: axiomatic semantics • The mathematical function it implements, the program in another language it corresponds to: denotational semantics 1

Axiomatic Semantics • What is true about a program? What can we assert about

Axiomatic Semantics • What is true about a program? What can we assert about it? r = 1; while(x > 0){ r = r * x; x = x – 1; } “r is x!” 2

Axiomatic Semantics • What is true about a program? What can we assert about

Axiomatic Semantics • What is true about a program? What can we assert about it? “x is some number n” r = 1; while(x > 0){ r = r * x; x = x – 1; } “r is n!” 3

Axiomatic Semantics • What is true about a program? What can we assert about

Axiomatic Semantics • What is true about a program? What can we assert about it? {x=n} r = 1; while(x > 0){ r = r * x; x = x – 1; } { r = n! } precondition postcondition 4

Floyd-Hoare Logic • 5

Floyd-Hoare Logic • 5

Floyd-Hoare Logic • 6

Floyd-Hoare Logic • 6

IMP: Syntax E : : = <#> | <ident> |E+E|E–E|E*E | <bool> | E

IMP: Syntax E : : = <#> | <ident> |E+E|E–E|E*E | <bool> | E and E | E or E | not E |E=E | if E then E else E C : : = <ident> : = E | C; C | skip | if E then C else C | while E do C 7

Floyd-Hoare Logic: Rules 8

Floyd-Hoare Logic: Rules 8

Floyd-Hoare Logic: Rules “rule of consequence” 9

Floyd-Hoare Logic: Rules “rule of consequence” 9

Floyd-Hoare Logic: Rules “rule of consequence” 10

Floyd-Hoare Logic: Rules “rule of consequence” 10

Floyd-Hoare Logic: Rules “rule of consequence” 11

Floyd-Hoare Logic: Rules “rule of consequence” 11

Floyd-Hoare Logic: Rules “rule of consequence” 12

Floyd-Hoare Logic: Rules “rule of consequence” 12

Floyd-Hoare Logic: Control Flow Rules 13

Floyd-Hoare Logic: Control Flow Rules 13

Floyd-Hoare Logic: Control Flow Rules 14

Floyd-Hoare Logic: Control Flow Rules 14

Floyd-Hoare Logic: Control Flow Rules 15

Floyd-Hoare Logic: Control Flow Rules 15

Floyd-Hoare Logic: Control Flow Rules 16

Floyd-Hoare Logic: Control Flow Rules 16

Floyd-Hoare Logic: Control Flow Rules loop invariant • Note: this doesn’t guarantee that the

Floyd-Hoare Logic: Control Flow Rules loop invariant • Note: this doesn’t guarantee that the loop will terminate! 17

Floyd-Hoare Logic: Example 18

Floyd-Hoare Logic: Example 18

Floyd-Hoare Logic: Example 19

Floyd-Hoare Logic: Example 19

Floyd-Hoare Logic: Example 20

Floyd-Hoare Logic: Example 20

Floyd-Hoare Logic: Example 21

Floyd-Hoare Logic: Example 21

Floyd-Hoare Logic: Example 22

Floyd-Hoare Logic: Example 22

Floyd-Hoare Logic: Example 23

Floyd-Hoare Logic: Example 23

Floyd-Hoare Logic: Example 24

Floyd-Hoare Logic: Example 24

Floyd-Hoare Logic: Example 25

Floyd-Hoare Logic: Example 25

Floyd-Hoare Logic: Annotating Code • 26

Floyd-Hoare Logic: Annotating Code • 26

Floyd-Hoare Logic: Annotating Code • 27

Floyd-Hoare Logic: Annotating Code • 27

Floyd-Hoare Logic: Annotating Code • 28

Floyd-Hoare Logic: Annotating Code • 28

Floyd-Hoare Logic: Annotating Code • 29

Floyd-Hoare Logic: Annotating Code • 29

Floyd-Hoare Logic: Annotating Code • { r * x! = n!} 30

Floyd-Hoare Logic: Annotating Code • { r * x! = n!} 30

Floyd-Hoare Logic: Annotating Code • 31

Floyd-Hoare Logic: Annotating Code • 31

Floyd-Hoare Logic: Annotating Code • 32

Floyd-Hoare Logic: Annotating Code • 32

Axiomatic and Operational Semantics • 33

Axiomatic and Operational Semantics • 33

Advantages of Axiomatic Semantics • Pre- and postcondition summarize the meaning of a program

Advantages of Axiomatic Semantics • Pre- and postcondition summarize the meaning of a program • Axiomatic semantics is a form of symbolic execution • A Hoare triple is also a concise specification for a program: we can write down the intended behavior, and then prove that the program has that behavior • Trivia: Sir Tony Hoare also invented quicksort, and was knighted for his service to computer science 34