Exercises on Semantics of Programming Languages

Similar documents
Semantics with Applications 3. More on Operational Semantics

Formal Semantics of Programming Languages

Formal Semantics of Programming Languages

Formal Semantics of Programming Languages

Formal Syntax and Semantics of Programming Languages

Formal Syntax and Semantics of Programming Languages

The exercises and answer for programming language class

Application: Programming Language Semantics

Program Analysis: Lecture 02 Page 1 of 32

Lecture 5 - Axiomatic semantics

Note that in this definition, n + m denotes the syntactic expression with three symbols n, +, and m, not to the number that is the sum of n and m.

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages

Big-step Operational Semantics (aka Natural Semantics)

Goals: Define the syntax of a simple imperative language Define a semantics using natural deduction 1

axiomatic semantics involving logical rules for deriving relations between preconditions and postconditions.

PROGRAM ANALYSIS & SYNTHESIS

Denotational Semantics

Induction and Semantics in Dafny

Introduction to Denotational Semantics. Class Likes/Dislikes Survey. Dueling Semantics. Denotational Semantics Learning Goals. You re On Jeopardy!

Formal semantics of loosely typed languages. Joep Verkoelen Vincent Driessen

02157 Functional Programming Interpreters for two simple languages including exercises

Introduction to Denotational Semantics. Brutus Is An Honorable Man. Class Likes/Dislikes Survey. Dueling Semantics

CMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics

3.7 Denotational Semantics

Static Program Analysis

Denotational Semantics

Mosig M1 - PLSCD Written exam

An Annotated Language

Semantics. A. Demers Jan This material is primarily from Ch. 2 of the text. We present an imperative

Homework 6: Big- & small-step semantics for LC

2 Introduction to operational semantics

CS422 - Programming Language Design

Static Program Analysis

Proving Properties on Programs From the Coq Tutorial at ITP 2015

Programming Languages Third Edition

Hoare Logic: Proving Programs Correct

CIS 500 Software Foundations. Final Exam. May 3, Answer key

1. M,M sequential composition: try tactic M; if it succeeds try tactic M. sequential composition (, )

Lambda Calculus. Type Systems, Lectures 3. Jevgeni Kabanov Tartu,

Automated Reasoning. Natural Deduction in First-Order Logic

In Our Last Exciting Episode

CS422 - Programming Language Design

Foundations: Syntax, Semantics, and Graphs

A CRASH COURSE IN SEMANTICS

Softwaretechnik. Lecture 03: Types and Type Soundness. Peter Thiemann. University of Freiburg, Germany SS 2008

Chapter 3. The While programming language

Fall Compiler Principles Lecture 5: Intermediate Representation. Roman Manevich Ben-Gurion University of the Negev

Database Theory VU , SS Codd s Theorem. Reinhard Pichler

CIS 500 Software Foundations Midterm I

Fall Compiler Principles Lecture 6: Intermediate Representation. Roman Manevich Ben-Gurion University of the Negev

Computation Club: Gödel s theorem

White-Box Testing Techniques III

Lecture Note: Types. 1 Introduction 2. 2 Simple Types 3. 3 Type Soundness 6. 4 Recursive Types Subtyping 17

Programming Languages Fall 2014

Introduction to Axiomatic Semantics

Deductive Methods in Computer Science

Higher-Order Logic. Specification and Verification with Higher-Order Logic

CONVENTIONAL EXECUTABLE SEMANTICS. Grigore Rosu CS422 Programming Language Design

Formal Systems II: Applications

Propositional Calculus: Boolean Algebra and Simplification. CS 270: Mathematical Foundations of Computer Science Jeremy Johnson

CS5371 Theory of Computation. Lecture 8: Automata Theory VI (PDA, PDA = CFG)

COSC252: Programming Languages: Semantic Specification. Jeremy Bolton, PhD Adjunct Professor

3.4 Denotational Semantics

CS4215 Programming Language Implementation. Martin Henz

Formal Systems and their Applications

Lecture Notes on Program Equivalence

Lectures 20, 21: Axiomatic Semantics

Complexity Theory VU , SS General Information. Reinhard Pichler

1 Introduction. 3 Syntax

CS 6110 S11 Lecture 25 Typed λ-calculus 6 April 2011

Logic and Computation Lecture 20 CSU 290 Spring 2009 (Pucella) Thursday, Mar 12, 2009

Chapter 3. Describing Syntax and Semantics ISBN

Calculating Correct Compilers

Semantics. There is no single widely acceptable notation or formalism for describing semantics Operational Semantics

Programming Languages Fall 2013

Bachelor/Master Exam Version V3B

Verifying Safety Property of Lustre Programs: Temporal Induction

3.2 Big-Step Structural Operational Semantics (Big-Step SOS)

6. Hoare Logic and Weakest Preconditions

Overview. Probabilistic Programming. Dijkstra s guarded command language: Syntax. Elementary pgcl ingredients. Lecture #4: Probabilistic GCL

(a) (4 pts) Prove that if a and b are rational, then ab is rational. Since a and b are rational they can be written as the ratio of integers a 1

Denotational semantics

To prove something about all Boolean expressions, we will need the following induction principle: Axiom 7.1 (Induction over Boolean expressions):

Hoare Logic. COMP2600 Formal Methods for Software Engineering. Rajeev Goré

Notes for Recitation 8

CSE-321 Programming Languages 2012 Midterm

CSE 505, Fall 2008, Midterm Examination 29 October Please do not turn the page until everyone is ready.

To prove something about all Boolean expressions, we will need the following induction principle: Axiom 7.1 (Induction over Boolean expressions):

Decision Procedures in First Order Logic

Propositional Logic Formal Syntax and Semantics. Computability and Logic

Program certification with computational

Handout 9: Imperative Programs and State

Type Safety. Java and ML are type safe, or strongly typed, languages. C and C++ are often described as weakly typed languages.

3. According to universal addressing, what is the address of vertex d? 4. According to universal addressing, what is the address of vertex f?

CMSC 336: Type Systems for Programming Languages Lecture 5: Simply Typed Lambda Calculus Acar & Ahmed January 24, 2008

Lecture 2: Big-Step Semantics

Specifications. Prof. Clarkson Fall Today s music: Nice to know you by Incubus

TAFL 1 (ECS-403) Unit- V. 5.1 Turing Machine. 5.2 TM as computer of Integer Function

Shared Variables and Interference

Hoare Logic and Model Checking

Transcription:

Technische Universität Wien SS 2014 Fakultät für Informatik Repetition sheet Assist. Prof. Florian Zuleger Tuesday, 8 April 2014 Assist. Prof. Georg Weissenbacher Univ. Prof. Agata Ciabattoni Moritz Sinn, M.Sc. Exercises on Semantics of Programming Languages Exercise 1 Proof by induction Consider the While language introduced in the lectures. We define the natural semantics of arithmetic expressions as follows. It has two kinds of configurations: a, s denoting that a has to be evaluated in state s, and z denoting the final value (an element of Z) The transition relation Aexp has the form: a, z Aexp z. The semantics is given in Table 1. n, s Aexp N n x, s Aexp s x a 1 + a 2, s Aexp z a 1 a 2, s Aexp z a 1 a 2, s Aexp z where z = z 1 + z2 where z = z 1 z2 where z = z 1 z2 Table 1: Natural semantics of arithmetic expressions Let us define a function A Aexp (State Z) as follows: Prove that A is a total function. Hint1: You need to prove that a Aexp, s State. a, s Aexp z A a s = z A is well-defined (i.e. there is at most one fuction value for each a Aexp) A is total (i.e. function is defined for each a Aexp). To summarise, you have to show that for each a Aexp and each s State there is exactly one value v Z such that A a s = v.

Hint 2: Use structural induction on the arithmetic expressions for the proof: First show that the property holds for every basis element of the syntactic category. Then prove that the property holds for every composite element using the assumption that it holds for all the immediate consituents of the element. Exercise 2 Extension of boolean expressions. Semantic equivalence. Again consider the While language introduced in the lectures. The semantic function for boolean expressions is given by the function The definition of B is given in Table 2. B Bexp (State {tt, ff}) B true s = tt B f alse s = ff B a 1 = a 2 s = { tt ff if A a 1 s = A a 2 s if A a 1 s A a 2 s B a 1 a 2 s = { tt ff if A a 1 s A a 2 s if A a 1 s>a a 2 s tt if B b s = ff B b s = { ff if B b s = tt B b 1 b 2 s = { tt if B b 1 s = tt and B b 2 s = tt ff if B b 1 s = ff or B b 2 s = ff Table 2: The semantics of boolean expressions The syntactic category Bexp is defined as the following extension of Bexp: b = true false a 1 = a 2 a 1 a 2 a 1 a 2 a 1 a 2 a 1 <a 2 a 1 >a 2 b b 1 b 2 b 1 b 2 b 1 b 2 b 1 b 2 a) Give a compositional extension of the semantic function B of Table 2, i.e. for every new expression provide a semantic clause in terms of the semantics of their immediate constituents. b) Two boolean expressions b 1 and b 2 are equivalent if for all states s, B b 1 s = B b 2 s Show that for each b of Bexp there exists a boolean expression b of Bexp such that b and b are equivalent.

Exercise 3 Substitution We define semantic function A for arithmetic expression in Table 3. A n s = N n A x, s = s x A a 1 + a 2 s = A a 1 s + A a 2 s A a 1 a 2 s = A a 1 s A a 2 s A a 1 a 2 s = A a 1 s A a 2 s Table 3: Semantic function for arithmetic expressions We define substitution for arithmetic expressions a[y a 0 ] in Table 4. n[y a 0 ] = n x[y a 0 ] = { a 0 if x = y x if x y (a 1 + a 2 )[y a 0 ] = a 1 [y a 0 ] + a 2 [y a 0 ] (a 1 a 2 )[y a 0 ] = a 1 [y a 0 ] a 2 [y a 0 ] (a 1 a 2 )[y a 0 ] = a 1 [y a 0 ] a 2 [y a 0 ] Table 4: Substitution for arithmetic expressions We also define a notion of substitution for states: (s[y v])x = { v s x if x = y if x y Prove that the following property holds: A a[y a 0 ] s = A a (s[y A a 0 s])

Exercise 4 Determinism of Structural Operational Semantics Consider the structural operational semantics of the While language from Table 5. [ass sos ] [skip sos ] [comp 1 sos] [comp 2 sos] [if tt sos] [if ff sos] x = a, s s[x A x s] skip, s s S 1, s S 1, s S 1 ; S 2 S 1; S 2, s S 1, s s S 1 ; S 2 S 2, s if b then S 1 else S 2, s S 1, s if B b s = tt if b then S 1 else S 2, s S 2, s if B b s = ff [while sos ] while b do S if b then (S; while b do S) else skip, s Table 5: Structural operational semantics of While Prove that the semantics is deterministic, i.e. for all choices of S, s, γ and γ we have that S, s γ and S, s γ imply γ = γ Hint: Use rule induction: Prove that the property holds for all axioms of the transition system. Prove that the property holds for all composite rules: for each rule assume that the property holds for its premises (this is called the induction hypothesis) and prove that it also holds for the conclusion of the rule provided that the conditions of the rule are satisfied.

Exercise 5 Operational semantics We extend the language While with the statement for x := a 1 to a 2 do S a) Define the semantics of the for statement with 1. natural semantics 2. structural operational semantics Note: The semantics of the for-construct is not allowed to rely on the existence of the while-construct in the language. b) Evaluate the statement y := 1; for z := 1 to x do (y := y*x; x := x-1) from a state where x has the value 5 using both semantics. Provide a derivation tree for the evaluation which uses natural semantics. For the evaluation which uses structural operational semantics provide a derivation sequence.