Semantyka i weryfikacja programów

Similar documents
! Use of formal notations. ! in software system descriptions. ! for a broad range of effects. ! and varying levels of use. !

Consider a description of arithmetic. It includes two equations that define the structural types of digit and operator:

Chapter 3 (part 3) Describing Syntax and Semantics

Programming Languages Third Edition

Intro to semantics; Small-step semantics Lecture 1 Tuesday, January 29, 2013

Certified compilers. Do you trust your compiler? Testing is immune to this problem, since it is applied to target code

Operational Semantics 1 / 13

Supplementary Notes on Abstract Syntax

CITS5501 Software Testing and Quality Assurance Formal methods

Introduction to Axiomatic Semantics

Distributed Systems Programming (F21DS1) Formal Verification

Chapter 3. Semantics. Topics. Introduction. Introduction. Introduction. Introduction

A Hoare Logic Contract Theory: An Exercise in Denotational Semantics

Introduction to Formal Methods

Syntax Analysis. Chapter 4

Lectures 20, 21: Axiomatic Semantics

Formal Semantics. Chapter Twenty-Three Modern Programming Languages, 2nd ed. 1

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

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

System Assistance in Structured Domain Model Development*

6. Hoare Logic and Weakest Preconditions

Liveness and Fairness Properties in Multi-Agent Systems

FUZZY SPECIFICATION IN SOFTWARE ENGINEERING

JOURNAL OF OBJECT TECHNOLOGY

CSE 12 Abstract Syntax Trees

Basic Structure of Denotational Definitions

Introduction to Axiomatic Semantics (1/2)

1 A question of semantics

CMSC 330: Organization of Programming Languages. Operational Semantics

Introduction to Axiomatic Semantics (1/2)

Application: Programming Language Semantics

Semantics of programming languages

Chapter 1. Introduction

In Our Last Exciting Episode

UNIVERSITY OF EDINBURGH COLLEGE OF SCIENCE AND ENGINEERING SCHOOL OF INFORMATICS INFR08008 INFORMATICS 2A: PROCESSING FORMAL AND NATURAL LANGUAGES

Flight Systems are Cyber-Physical Systems

Formal Specification of Software Systems

CSCE 120: Learning To Code

Chapter 3. Describing Syntax and Semantics ISBN

Parsing. Note by Baris Aktemur: Our slides are adapted from Cooper and Torczon s slides that they prepared for COMP 412 at Rice.

Chapter 3. Describing Syntax and Semantics

The design of a programming language for provably correct programs: success and failure

Plan. Language engineering and Domain Specific Languages. Language designer defines syntax. How to define language

Lecture 3: Recursion; Structural Induction

Topic Formal Methods. ICS 121 Lecture Notes. What are Formal Methods? What are Formal Methods? Formal Specification in Software Development

Informal Semantics of Data. semantic specification names (identifiers) attributes binding declarations scope rules visibility

Applications of Program analysis in Model-Based Design

CS2104 Prog. Lang. Concepts

Formal Semantics of Programming Languages

Reasoning About Imperative Programs. COS 441 Slides 10

1. true / false By a compiler we mean a program that translates to code that will run natively on some machine.

COMP 410 Lecture 1. Kyle Dewey

Lecture 5 - Axiomatic semantics

Formal Semantics of Programming Languages

An Annotated Language

Meeting 1 Introduction to Functions. Part 1 Graphing Points on a Plane (REVIEW) Part 2 What is a function?

Chapter 3. Describing Syntax and Semantics ISBN

Tail Calls. CMSC 330: Organization of Programming Languages. Tail Recursion. Tail Recursion (cont d) Names and Binding. Tail Recursion (cont d)

Starting with a great calculator... Variables. Comments. Topic 5: Introduction to Programming in Matlab CSSE, UWA

Propositional Logic. Part I

Warm-Up Problem. 1. What is the definition of a Hoare triple satisfying partial correctness? 2. Recall the rule for assignment: x (assignment)

This is already grossly inconvenient in present formalisms. Why do we want to make this convenient? GENERAL GOALS

CS415 Compilers. Syntax Analysis. These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University

Chapter 3. Describing Syntax and Semantics

Marking Guidelines for MVK Projects. MVK12. Version 6.2 (PPD, URD, RURD, ADD and software demo)

Static Program Analysis

7. Introduction to Denotational Semantics. Oscar Nierstrasz

A Partial Correctness Proof for Programs with Decided Specifications

Formal semantics of loosely typed languages. Joep Verkoelen Vincent Driessen

ALGOL 48 AND ALGOL 50 ALGOLIC LANGUAGES IN MATHE- MATICS

EDAN65: Compilers, Lecture 04 Grammar transformations: Eliminating ambiguities, adapting to LL parsing. Görel Hedin Revised:

2.2 Syntax Definition

Comments on drafts of final paper

CITS2401 Computer Analysis & Visualisation

CSCE 314 Programming Languages

Proving the Correctness of Distributed Algorithms using TLA

Handout 9: Imperative Programs and State

Types and Static Type Checking (Introducing Micro-Haskell)

Compiler Construction Lecture 1: Introduction Winter Semester 2018/19 Thomas Noll Software Modeling and Verification Group RWTH Aachen University

Formal Foundations of Software Engineering

This book is licensed under a Creative Commons Attribution 3.0 License

Semantics of programming languages

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.

Linguistics and Philosophy 23: , Is Compositionality Formally Vacuous? Francis Jeffry Pelletier

Semantics via Syntax. f (4) = if define f (x) =2 x + 55.

Types and Static Type Checking (Introducing Micro-Haskell)

Semantics of programming languages

Formal Methods. CITS5501 Software Testing and Quality Assurance

Formal Verification. Lecture 10

The semantics of a programming language is concerned with the meaning of programs, that is, how programs behave when executed on computers.

Describing Computer Languages

Matching Logic A New Program Verification Approach

CS152 Programming Language Paradigms Prof. Tom Austin, Fall Syntax & Semantics, and Language Design Criteria

Guidelines for Writing Mathematical Proofs

CSE P 501 Exam 8/5/04

Parsing II Top-down parsing. Comp 412

Lecture 1: Overview

Defining Program Syntax. Chapter Two Modern Programming Languages, 2nd ed. 1

Contents. Chapter 1 SPECIFYING SYNTAX 1

Static verification of program running time

Transcription:

Semantyka i weryfikacja programów Andrzej Tarlecki Instytut Informatyki Wydzia l Matematyki, Informatyki i Mechaniki Uniwersytet Warszawski http://www.mimuw.edu.pl/~tarlecki pok. 4750 tarlecki@mimuw.edu.pl tel: (22 55) 44475, 44214 Strona tego wyk ladu: http://www.mimuw.edu.pl/~tarlecki/teaching/semwer/ Andrzej Tarlecki: Semantics & Verification - 1 -

Program Semantics & Verification Andrzej Tarlecki Institute of Informatics Faculty of Mathematics, Informatics and Mechanics University of Warsaw http://www.mimuw.edu.pl/~tarlecki office: 4750 tarlecki@mimuw.edu.pl phone: (48)(22)(55) 44475, 44214 This course: http://www.mimuw.edu.pl/~tarlecki/teaching/semwer/ Andrzej Tarlecki: Semantics & Verification - 2 -

Overall The aim of the course is to present the importance as well as basic problems and techniques of formal description of programs. Various methods of defining program semantics are discussed, and their mathematical foundations as well as techniques are presented. The basic notions of program correctness are introduced together with methods and formalisms for their derivation. The ideas of systematic development of correct programs are introduced. Andrzej Tarlecki: Semantics & Verification - 3 -

Prerequisites Current version: Wstȩp do programowania (1000-211bWPI, 1000-211bWPF) Podstawy matematyki (1000-211bPM) Old version: Wstȩp do programowania (1000-211WPI, 1000-211WPF) Wstȩp do teorii mnogości (1000-211WTM) Logika (1000-212LOG) Andrzej Tarlecki: Semantics & Verification - 4 -

Literature Rather random choice for now: P. Dembiński, J. Ma luszyński. Matematyczne metody definiowania jȩzyków programowania. WNT, 1981. M. Gordon. Denotacyjny opis jȩzyków programowania. WNT, 1983. H. Riis Nielson, F. Nielson. Semantics with Applications: A Formal Introduction. Wiley, 1999. D. Gries. The Science of Programming. Springer-Verlag, 1981. E. Dijkstra. Umiejȩtność programowania. WNT, 1978. Andrzej Tarlecki: Semantics & Verification - 5 -

Programs D207 0C78 F0CE 00078 010D0 D203 0048 F0D6 00048 01CD8 8000 F0EA F0B3 010EC 00ED7 9C00 000C F0DA 0000C... r := 0; q := 1; while q <= n do begin r := r + 1; q := q + 2 * r + 1 end a precise description of an algorithm, understandable for a human reader a precise prescription of computations to be performed by a computer Programs should be: clear; efficient; robust; reliable; user friendly; well documented;... but first of all, CORRECT don t forget though: also, executable... Andrzej Tarlecki: Semantics & Verification - 6 -

Tensions A triangle of tension for programming languages: usable formal effective Andrzej Tarlecki: Semantics & Verification - 7 -

Grand View What we need for a good programming language: Syntax Semantics Logic Pragmatics/methodology Implementation Programming environment Andrzej Tarlecki: Semantics & Verification - 8 -

Syntax To determine exactly the well-formed phrases of the language. concrete syntax (LL(1), LR(1),... ) abstract syntax (CF grammar, BNF notation, etc) type checking (context conditions, static analysis) It is standard by now to present it formally! One consequence is that excellent tools to support parsing are available. Andrzej Tarlecki: Semantics & Verification - 9 -

Semantics To determine the meaning of the programs and all the phrases of the language. Informal description is often not good enough operational semantics (small-step, big-step, machine-oriented): dealing with the notion of computation, thus indicating how the results are obtained denotational semantics (direct-style, continuation-style): dealing with the overall meaning of the language constructs, thus indicating the results without going into the details of how they are obtained axiomatic semantics: centred around the properties of the language constructs, perhaps ignoring some aspects of their meanings and the overall results Andrzej Tarlecki: Semantics & Verification - 10 -

Pragmatics To indicate how to use the language well, to build good programs. user-oriented presentation of programming constructs hints on good/bad style of their use Andrzej Tarlecki: Semantics & Verification - 11 -

Logic To express and prove program properties. Partial correctness properties, based on first-order logic Hoare s logic to prove them Termination properties (total correctness) Also: temporal logics other modal logics algebraic specifications abstract model specifications Andrzej Tarlecki: Semantics & Verification - 12 -

program verification vs. correct program development Methodology specifications stepwise refinement designing the modular structure of the program coding individual modules Andrzej Tarlecki: Semantics & Verification - 13 -

Implementation Compiler/interpreter, with: parsing static analysis and optimisations code generation Programming environment So that we can actually do this: dedicated text/program editor compiler/interpreter debugger libraries of standard modules BUT ALSO: support for writing specifications verification tool... Andrzej Tarlecki: Semantics & Verification - 14 -

Why formal semantics? So that we can sleep at night... precise understanding of all language constructs and the underlying concepts independence of any particular implementation easy prototype implementations necessary basis for trustworthy reasoning Andrzej Tarlecki: Semantics & Verification - 15 -

Example Recall: r := 0; q := 1; while q <= n do begin r := r + 1; q := q + 2 * r + 1 end Or better: rt := 0; sqr := 1; while sqr n do (rt := rt + 1; sqr := sqr + 2 rt + 1) Andrzej Tarlecki: Semantics & Verification - 16 -

Well, this computes the integer square root of n, doesn t it: {n 0} rt := 0; sqr := 1; {n 0 rt = 0 sqr = 1} while {sqr = (rt + 1) 2 rt 2 n} sqr n do (rt := rt + 1; {sqr = rt 2 sqr n} sqr := sqr + 2 rt + 1) {rt 2 n < (rt + 1) 2 } But how do we justify the implicit use of assertions and proof rules? Andrzej Tarlecki: Semantics & Verification - 17 -

Sample proof rule For instance: follows by: {sqr = rt 2 sqr n} sqr := sqr + 2 rt + 1 {sqr = (rt + 1) 2 rt 2 n} {ϕ[e/x]} x := E {ϕ} BUT: although correct in principle, this rule fails in quite a few ways for Pascal (abnormal termination, looping, references and sharing, side effects, assignments to array components, etc) Be formal and precise! Andrzej Tarlecki: Semantics & Verification - 18 -

Justification definition of program semantics definition of satisfaction for correctness statements proof rules for correctness statements proof of soundness of all the rules analysis of completeness of the system of rules Andrzej Tarlecki: Semantics & Verification - 19 -

Course outline Introduction Operational semantics Denotational semantics for simple and somewhat more advanced constructs Foundations of denotational semantics Partial correctness: Hoare s logic Total correctness: proving termination Systematic program derivation Semantics: an algebraic view (with bits and pieces of universal algebra) Program specification and development Andrzej Tarlecki: Semantics & Verification - 20 -

Syntax There are standard ways to define a syntax for programming languages. The course to learn about this: Basic concepts: formal languages Jȩzyki, automaty i obliczenia (generative) grammars: regular (somewhat too weak), context-free (just right), context-dependent (too powerful),... BTW: there are grammar-based mechanisms to define the semantics of programming languages: attribute grammars, perhaps also two-level grammars, see (or rather, go to) Metody implementacji jȩzyków programowania Andrzej Tarlecki: Semantics & Verification - 21 -

Concrete syntax Concrete syntax of a programming language is typically given by a (context-free) grammar detailing all the commas and semicolons that are necessary to write a string of characters that is a well-formed program. Typically, there are also additional context dependent conditions to eliminate some of the strings permitted by the grammar (like thou shalt not use an undeclared variable ). Presenting a formal language by an unambiguous context-free grammar gives a structure to the strings of the language: it shows how a well-formed string is build of its immediate components using some linguistic construct of the language. Andrzej Tarlecki: Semantics & Verification - 22 -

Abstract syntax Abstract syntax presents the structure of the program phrases in terms of the linguistic constructs of the language, by indicating the immediate components of the phrase and the construct used to build it. Think of abstract syntax as presenting each phrase of a language as a tree: the node is labelled by the top construct used, with the subtrees giving the immediate components. Parsing is the way to map concrete syntax to abstract syntax, by building the abstract syntax tree for each phrase of the language as defined by the concrete syntax. All these concepts (and more) are explained at other courses. Andrzej Tarlecki: Semantics & Verification - 23 -

At this course We will not belabour the distinction between concrete and abstract syntax. concrete-like way of presenting the syntax will be used the phrases will be used as if they were given by an abstract syntax if doubts arise, parenthesis and indentation will be used to disambiguate the interpretation of a phrase as an abstract-syntax tree This is inappropriate for true programming languages but quite adequate to deal with our examples Andrzej Tarlecki: Semantics & Verification - 24 -