Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 1 Behavioral Refinement Principles of Object-Oriented Software Development.

Slides:



Advertisements
Similar presentations
Automated Theorem Proving Lecture 1. Program verification is undecidable! Given program P and specification S, does P satisfy S?
Advertisements

Design by Contract.
Formal Methods in Software Engineering
Semantics Static semantics Dynamic semantics attribute grammars
ICE1341 Programming Languages Spring 2005 Lecture #6 Lecture #6 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
Giving a formal meaning to “Specialization” In these note we try to give a formal meaning to specifications, implementations, their comparisons. We define.
Rigorous Software Development CSCI-GA Instructor: Thomas Wies Spring 2012 Lecture 11.
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Axiomatic Semantics.
ISBN Chapter 3 Describing Syntax and Semantics.
Fall Semantics Juan Carlos Guzmán CS 3123 Programming Languages Concepts Southern Polytechnic State University.
1 Semantic Description of Programming languages. 2 Static versus Dynamic Semantics n Static Semantics represents legal forms of programs that cannot be.
CS 355 – Programming Languages
Formal Methods of Systems Specification Logical Specification of Hard- and Software Prof. Dr. Holger Schlingloff Institut für Informatik der.
Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 1 Polymorphism Principles of Object-Oriented Software Development (Chapter.
Constraint Logic Programming Ryan Kinworthy. Overview Introduction Logic Programming LP as a constraint programming language Constraint Logic Programming.
Principles of Object-Oriented Software Development The language Eiffel.
Principles of Object-Oriented Software Development Behavioral refinement.
Axiomatic Semantics Dr. M Al-Mulhem ICS
CS 330 Programming Languages 09 / 18 / 2007 Instructor: Michael Eckmann.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 18 Program Correctness To treat programming.
PSUCS322 HM 1 Languages and Compiler Design II Formal Semantics Material provided by Prof. Jingke Li Stolen with pride and modified by Herb Mayer PSU Spring.
Dr. Muhammed Al-Mulhem 1ICS ICS 535 Design and Implementation of Programming Languages Part 1 Fundamentals (Chapter 4) Axiomatic Semantics ICS 535.
Chapter 1 Principles of Programming and Software Engineering.
CS 330 Programming Languages 09 / 16 / 2008 Instructor: Michael Eckmann.
Describing Syntax and Semantics
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Chapter 2 Principles of Programming & Software Engineering.
1 Abstraction  Identify important aspects and ignore the details  Permeates software development programming languages are abstractions built on hardware.
Mathematical Modeling and Formal Specification Languages CIS 376 Bruce R. Maxim UM-Dearborn.
Introduction CS 3358 Data Structures. What is Computer Science? Computer Science is the study of algorithms, including their  Formal and mathematical.
Copyright 2002 Prentice-Hall, Inc. Modern Systems Analysis and Design Third Edition Jeffrey A. Hoffer Joey F. George Joseph S. Valacich Chapter 20 Object-Oriented.
Overview of Formal Methods. Topics Introduction and terminology FM and Software Engineering Applications of FM Propositional and Predicate Logic Program.
111 Protocols CS 4311 Wirfs Brock et al., Designing Object-Oriented Software, Prentice Hall, (Chapter 8) Meyer, B., Applying design by contract,
Low-Level Detailed Design SAD (Soft Arch Design) Mid-level Detailed Design Low-Level Detailed Design Design Finalization Design Document.
ISBN Chapter 3 Describing Semantics -Attribute Grammars -Dynamic Semantics.
Introduction CS 3358 Data Structures. What is Computer Science? Computer Science is the study of algorithms, including their  Formal and mathematical.
CS 363 Comparative Programming Languages Semantics.
Program Analysis and Verification Spring 2014 Program Analysis and Verification Lecture 4: Axiomatic Semantics I Roman Manevich Ben-Gurion University.
CSC 480 Software Engineering Design by Contract. Detail Design Road Map Begin with architectural models  Class model: domain classes  Overall state.
Object-Oriented Modeling: Static Models. Object-Oriented Modeling Model the system as interacting objects Model the system as interacting objects Match.
Chapter 3 Part II Describing Syntax and Semantics.
Programming Languages and Design Lecture 3 Semantic Specifications of Programming Languages Instructor: Li Ma Department of Computer Science Texas Southern.
Semantics In Text: Chapter 3.
COP4020 Programming Languages Introduction to Axiomatic Semantics Prof. Robert van Engelen.
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Chapter 2 Principles of Programming & Software Engineering.
© 2006 Pearson Addison-Wesley. All rights reserved 2-1 Chapter 2 Principles of Programming & Software Engineering.
Protocols Software Engineering II Wirfs Brock et al, Designing Object-Oriented Software, Prentice Hall, Mitchell, R., and McKim, Design by Contract,
Testing OO software. State Based Testing State machine: implementation-independent specification (model) of the dynamic behaviour of the system State:
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
PROGRAMMING PRE- AND POSTCONDITIONS, INVARIANTS AND METHOD CONTRACTS B MODULE 2: SOFTWARE SYSTEMS 13 NOVEMBER 2013.
CSSE501 Object-Oriented Development. Chapter 10: Subclasses and Subtypes  In this chapter we will explore the relationships between the two concepts.
Types and Programming Languages Lecture 10 Simon Gay Department of Computing Science University of Glasgow 2006/07.
CSC3315 (Spring 2009)1 CSC 3315 Languages & Compilers Hamid Harroud School of Science and Engineering, Akhawayn University
Operational Semantics Mooly Sagiv Tel Aviv University Sunday Scrieber 8 Monday Schrieber.
Interpreting the Object Constraint Presented by: Ed Kausmeyer.
Object Design More Design Patterns Object Constraint Language Object Design Specifying Interfaces Review Exam 2 CEN 4010 Class 18 – 11/03.
A UML-Based Pattern Specification Technique Presented by Chin-Yi Tsai IEEE TRANSACTION ON SOFTWARE ENGINEERING, VOL. 30, NO. 3, MARCH 2004 Robert B. France,
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
Formal Methods in Software Engineering 1
Mathematical Structures for Computer Science Chapter 1
Programming Languages and Compilers (CS 421)
Programming Languages 2nd edition Tucker and Noonan
Semantics In Text: Chapter 3.
619 Final Review Last updated Spring 2010 © : Paul Ammann.
Predicate Transformers
ISpec: A Compositional Approach to Interface Specification
Assertions References: internet notes; Bertrand Meyer, Object-Oriented Software Construction; 4/25/2019.
Programming Languages and Compilers (CS 421)
Programming Languages 2nd edition Tucker and Noonan
COP4020 Programming Languages
Presentation transcript:

Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 1 Behavioral Refinement Principles of Object-Oriented Software Development (Chapter 10)

Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 2 Outline l Types as behavior l Verifying behavioral properties l Specifying the behavior of a program l Verifying behavioral consistency l Specifying behavioral compositions

Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 3 Objectives l Present notion of behavioral subtypes l Apply formal program verification techniques to method calls

Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 4 Liskov Substitution Principle l General principle: If an object of type S can be substituted in all places where an object of type T is expected, then S is a subtype of T l Object-oriented interpretation: A class S, which inherits from a superclass T, is considered a subtype of T if S can be substituted in all places where T is expected. Advantage: new subclasses of T can be written without modifying the methods of T. Specification inheritance follows the Liskov substitution principle. l How can we formally define this substitution principle?

Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 5 Review: Subtype refinement relations to verify if  is a subtype of  l Sub-range inclusion l Functions l Records l Variants (contravariance)

Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 6 Functions l If  is a subtype of  and  is a subtype of  then The function  is a subtype of the function  (Function notation:  means function taking parameter of type  and return value of type  ) Subtype can have looser parameter type and/or tighter return type l Examples: float  float is not a subtype of float  int int  float is not a subtype of float  int int  int is a not subtype of float  int float  char is a subtype of float  int double  int is a subtype of float  int

Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 7 Contravariance rule for function subtyping l Function subtypes can have looser parameter type and/or tighter return type l Intuitively, for a subtype function to be used in place of its supertype: It must be able to support at least the same set of values as the input parameters of the supertype, and, It must return no value more than what its supertype returns l A subtype function is a refinement of its supertype, relaxing the restrictions for its calling function, while strengthening its obligations to the calling function.

Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 8 Records l If type  i is a subtype of  i, for all i = 1..m, then The record defined by a 1 :  1,…,a n :  n is a subtype of the record defined by a 1 :  1,…,a m :  m Note that n could be greater than m  the subtype can introduce additional fields not in the supertype l Example: {age:int, speed:int, fuel:int}  {age:int, speed:int} l Locations that use the supertype are not aware of the new fields so they are never used in those contexts.

Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 9 Types as behavior l Previously, we defined types and subtypes in terms of refinement relations. l This gives a syntactic definition for determining what is and is not a subtype. l A subtype can still behave in a manner that is semantically inconsistent with its supertype.  Need a way to define the semantic behavior of a subtype and verify its consistency to its supertype.

Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 10 Questions l How can we extend our notion of types to include a behavioral description? l How can we verify that a subtype respects the behavioral constraints imposed by the supertype?

Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 11 Questions l How can we extend our notion of types to include a behavioral description? Contracts (invariants and pre- and post-conditions) l How can we verify that a subtype respects the behavioral constraints imposed by the supertype? Borrow from axiomatic verification techniques Hoare logic

Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 12 Outline l Types as behavior l Verifying behavioral properties l Specifying the behavior of a program l Verifying behavioral consistency l Specifying behavioral compositions

Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 13 l Formal verification – mathematically proving that a program satisfies certain assertions about its behavior. l State model Program execution as a sequence of state transitions Program state – the collective value of all variables in a program State: Verifying behavioral properties

Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 14 State transformations l Program state is changed whenever a variable value is changed l Substitution: l Actions correspond to state transformations:

Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 15 Verifying state transformations l Correctness formulae (Hoare logic) {P}S{Q} P: pre-condition Q: post-condition S: program statement l Verification

Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 16 l Axioms Assignment: Composition: Conditional: Iteration: l Consequence rules l Procedural abstraction Correctness calculus

Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 17 Outline l Types as behavior l Verifying behavioral properties l Specifying the behavior of a program l Verifying behavioral consistency l Specifying behavioral compositions

Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 18 Specifying the operational behavior of a system l Recall that a program is a sequence of state transformations l Need a way to specify the operational behavior of a program – a transition system l We now define a transition system for a simple programming language (also called operational semantics)

Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 19 Simple OO language syntax l Expressions l Elementary statements l Compound statement

Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 20 Transition system rules for simple OO language l Assignment l Object creation l Method call  represents the local context of the object

Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 21 Compound statements l Composition l Conditional

Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 22 Compound statements l Iteration

Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 23 Transition example l Example program x = new ctr; x.inc(); v = x.n l Transitions  x = new ctr,  1  ,  1 [x := ctr 1 ]   n := n + 1,  2   → ,  2 [ .n := .n + 1]   x.inc(),  2  ,  2 [  2 (x).n :=  2 (x).n + 1]   v := x.n,  3  → ,  3 [v :=  3 (x).n]  l Trace   ’ with  =  1,  ’ =  3 and = ctr1  inc 

Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 24 Outline l Types as behavior l Verifying behavioral properties l Specifying the behavior of a program l Verifying behavioral consistency l Specifying behavioral compositions

Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 25 Verifying correctness of behavioral refinements l Behavioral refinement Abstract specification to concrete implementation Supertype to subtype correspondence l How can we verify that a concrete implementation is consistent with its abstract specification? l Behavioral types – characterize the behavioral properties of objects in terms of modifications of an abstract state. l Need a representation abstraction function to map concrete data structures and operations to abstract counterparts.

Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 26 Abstraction and representation l  – representation abstraction function l Must prove that  ’ corresponds to  (  ’)

Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 27 Proof strategy l To prove that an implementation is consistent with its abstract specification Assume that pre- and post-conditions have been defined for the abstract type Find representation invariant I relating concrete and abstract representations Derive pre- and post-conditions P and Q for the concrete implementation Prove P  I   (P) and  (Q)  I  Q

Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 28 An abstract stack specification type stack T { s: seq T; axioms: {true} push(t:T) {s’ = s   t  } {s   } pop() {s = s’   result  } }; where:  = empty sequence  x1,…,xn    y1,…,ym  =  x1,…,xn,y1,…,ym 

Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 29 A stack realization template class as { int t; T a[MAX]; public: as() { t = 0;} void push(T e) { require(t<MAX-1); a[t++] = e; } T pop(){ require(t>0); return a[--t];} invariant: 0 <= t && t < MAX; };

Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 30 Verify push() function Abstract: push(e:T) Pre-condition: true Post-condition: s’ = s   e  Concrete: push(T e) Pre-condition: t < MAX – 1 Post-condition: t’ = t + 1 ^ a’[t’-1] = e Invariant: I(a,t,s)  t=length(s) ^ t>=0 ^ s=  (a,t) where  (a,t)=  a[0],…,a[t-1] 

Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 31 Verify push() function Pre-condition: t < MAX – 1 ^ I(a,t,s)  true  Pre-condition of concrete implies that of abstract Post-condition: s’ = s   e  ^ I(a’,t’,s’)  t’ = t + 1 ^ a’[t’-1] = e I(a’,t’,s’)  t’=length(s’) ^ t’>=0 ^ s=  (a’,t’)  (a’,t’)=  a’[0],…,a’[t’-1]  s’ = s   e   length(s’) = length(s) + 1  t’ = t + 1 s’ = s   e    a[0],…,a[t-1]    e  =  a’[0],…,a’[t-1],e  =  a’[0],…,a’[t’-2],e    (a’,t’)  a[0],…,a[t’-2],e   a’[t’-1] = e  Post-condition of abstract implies that of concrete

Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 32 Verifying subtype correspondence l How can we verify that a type is a behavioral subtype of another type? l Subtype correspondence mapping Need to define three sets of maps  - abstraction function – maps  values to  values  - renaming rule – maps subtype to supertype methods  - extension map – explains effects of extra methods

Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 33 Behavioral subtyping constraints l If  is a subtype of  l Signature dom(m  )  dom(m  ) range(m  )  range(m  ) l Behavior pre(m  )[x  :=  (x  )]  pre(m  ) post(m  )  post(m  ) [x  :=  (x  )] invariant(  )  invariant(  ) [x  :=  (x  )] l Satisfy correspondence mapping

Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 34 Extension maps l Extension map must be defined such that each method call for object x of type , x.m(a), is mapped to a program  m l  (x.m(a)) =  m l  m – only calls methods shared by  and its supertype  or external functions and methods l Diamond rule:

Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 35 Object hierarchies l Extension subtypes Subtypes add more “state” and methods One-to-one abstraction map: if more methods but same “state” Additional methods must have well-defined extension map Many-to-one abstraction map: if more “state” Methods that change attributes not present in the supertype are safe Usual methods, such as equality, must be defined in terms of the supertype’s state l Constrained subtypes Subtypes are subsets of supertype’s behavior Methods must satisfy the function subtyping rules

Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 36 Outline l Types as behavior l Verifying behavioral properties l Specifying the behavior of a program l Verifying behavioral consistency l Specifying behavioral compositions

Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 37 The need for global invariants l Verification of object behavior is inherently a local verification activity l Some inconsistencies cannot be detected locally l May be caused by actions that do not involve the object directly

Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 38 Example class A { public: A() { forward = 0; } attach(B* b) { forward = b; b->attach(this); } bool invariant() { return !forward || forward->backward == this; } private: B* forward; }; class B { public: B() { backward = 0; } attach(A* a) { backward = a; } bool invariant() { return !backward || backward->forward == this; } private: A* backward; }; A a1, a2; B b; a1.attach(b); a2.attach(b); // violates invariant a1

Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 39 Formalisms for specifying interactions l Behavioral contracts – behavior of compositions Formalisms for specification, refinement, conformance, declarations Characterize the behavioral of compositions of objects l Scripts – cooperation by enrollment Defines formalisms for roles, initialization/termination policies, critical role set Specify behavior as a set of roles and the interaction between objects as subscribing/enrolling to a role Critical role set – minimal set of participants required for successful computation l Multiparty interactions – communication primitive Communication primitive allows multiple objects to interact simultaneously Frozen state – invariant during the interaction Useful in specification of fault tolerant systems Can assumption of synchrony be weakened in favor of efficiency? l Joint action systems – action-oriented Assumes there exists some global decision procedure that decides which actions (and interactions) are appropriate Object behaviors specified by state charts Implications on inheritance, and refinement by superposition

Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 40 Joint action systems - example action service() by client c; server s is when c.requesting && s.free do

Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 41 contract model-view { subject: model supports [ state: V; value(val:V) [state = val]; notify(); notify()  v  views  v.update(); attach()(v:view) v  views; detach()(v:view) v  views; ] views: set where view suppports [ update() [view reflects state]; subject(m:model) subject = m; ] invariant:  v  views  [v reflects subject.state] instantiation:  v  views  subject.attach(v) & v.subject(subject); subject.notify(); } The Model-View contract

Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 42 Summary l We extended the notion of subtyping to include behavioral properties l Reviewed a correctness calculus for verifying behavioral properties l Defined a set of state transformations corresponding to programming language constructs l Defined mappings to reason about correspondence between abstract and concrete types and between subtypes and supertypes l Presented an overview of formalisms for specifying global interactions between objects