Specifying the Java Collections Framework in JavaDL cand. inform. - - PowerPoint PPT Presentation

specifying the java collections framework in javadl
SMART_READER_LITE
LIVE PREVIEW

Specifying the Java Collections Framework in JavaDL cand. inform. - - PowerPoint PPT Presentation

Specifying the Java Collections Framework in JavaDL cand. inform. Denis Lohner Institut f ur Theoretische Informatik - Universit at Karlsruhe 6th KeY Symposium, 2007 Betreuer: Dipl.Inform. R. Bubel verantw. Betreuer: Prof. Dr. P. H.


slide-1
SLIDE 1

Specifying the Java Collections Framework in JavaDL

  • cand. inform. Denis Lohner

Institut f¨ ur Theoretische Informatik - Universit¨ at Karlsruhe

6th KeY Symposium, 2007 Betreuer: Dipl.Inform. R. Bubel

  • verantw. Betreuer: Prof. Dr. P. H. Schmitt
slide-2
SLIDE 2

Motivation Specification Interfaces Using specs Method contract rule Conclusion Appendix

Outline

1

Motivation

2

Specification by example

3

Interface specification

4

Using specifications

5

A ”new” method contract rule

6

Demo

7

Conclusion

Denis Lohner Specifying the JCF 2/20

slide-3
SLIDE 3

Motivation Specification Interfaces Using specs Method contract rule Conclusion Appendix

Outline

1

Motivation

2

Specification by example

3

Interface specification

4

Using specifications

5

A ”new” method contract rule

6

Demo

7

Conclusion

Denis Lohner Specifying the JCF 2/20

slide-4
SLIDE 4

Motivation Specification Interfaces Using specs Method contract rule Conclusion Appendix

Outline

1

Motivation

2

Specification by example

3

Interface specification

4

Using specifications

5

A ”new” method contract rule

6

Demo

7

Conclusion

Denis Lohner Specifying the JCF 2/20

slide-5
SLIDE 5

Motivation Specification Interfaces Using specs Method contract rule Conclusion Appendix

Outline

1

Motivation

2

Specification by example

3

Interface specification

4

Using specifications

5

A ”new” method contract rule

6

Demo

7

Conclusion

Denis Lohner Specifying the JCF 2/20

slide-6
SLIDE 6

Motivation Specification Interfaces Using specs Method contract rule Conclusion Appendix

Outline

1

Motivation

2

Specification by example

3

Interface specification

4

Using specifications

5

A ”new” method contract rule

6

Demo

7

Conclusion

Denis Lohner Specifying the JCF 2/20

slide-7
SLIDE 7

Motivation Specification Interfaces Using specs Method contract rule Conclusion Appendix

Outline

1

Motivation

2

Specification by example

3

Interface specification

4

Using specifications

5

A ”new” method contract rule

6

Demo

7

Conclusion

Denis Lohner Specifying the JCF 2/20

slide-8
SLIDE 8

Motivation Specification Interfaces Using specs Method contract rule Conclusion Appendix

Outline

1

Motivation

2

Specification by example

3

Interface specification

4

Using specifications

5

A ”new” method contract rule

6

Demo

7

Conclusion

Denis Lohner Specifying the JCF 2/20

slide-9
SLIDE 9

Motivation Specification Interfaces Using specs Method contract rule Conclusion Appendix

Motivation

Problem

1

No sources of the JDK library available in KeY ⇒ symbolical execution of library calls fail

2

For native methods sources not even exist Why specifying the Java Collections Framework? JCF used in many projects Case study

Denis Lohner Specifying the JCF 3/20

slide-10
SLIDE 10

Motivation Specification Interfaces Using specs Method contract rule Conclusion Appendix

Motivation

Problem

1

No sources of the JDK library available in KeY ⇒ symbolical execution of library calls fail

2

For native methods sources not even exist Why specifying the Java Collections Framework? JCF used in many projects Case study

Denis Lohner Specifying the JCF 3/20

slide-11
SLIDE 11

Motivation Specification Interfaces Using specs Method contract rule Conclusion Appendix

Motivation

Problem

1

No sources of the JDK library available in KeY ⇒ symbolical execution of library calls fail

2

For native methods sources not even exist Why specifying the Java Collections Framework? JCF used in many projects Case study

Denis Lohner Specifying the JCF 3/20

slide-12
SLIDE 12

Motivation Specification Interfaces Using specs Method contract rule Conclusion Appendix

Motivation

Problem

1

No sources of the JDK library available in KeY ⇒ symbolical execution of library calls fail

2

For native methods sources not even exist Why specifying the Java Collections Framework? JCF used in many projects Case study

Denis Lohner Specifying the JCF 3/20

slide-13
SLIDE 13

Motivation Specification Interfaces Using specs Method contract rule Conclusion Appendix

Motivation

Problem

1

No sources of the JDK library available in KeY ⇒ symbolical execution of library calls fail

2

For native methods sources not even exist Why specifying the Java Collections Framework? JCF used in many projects Case study

Denis Lohner Specifying the JCF 3/20

slide-14
SLIDE 14

Motivation Specification Interfaces Using specs Method contract rule Conclusion Appendix Normal Case Exceptional Case General Concept

Specification by example

Normal case

Example Method

SomeLibrary.copy(java.lang.Object[] src, java.lang.Object[] dest)

Precondition

src != null & src.<created> = TRUE & dest != null & dest.<created> = TRUE & src.length = dest.length & \forall int i; ( (0 <= i & i < src.length) -> arrayStoreValid(dest, src[i]) )

Postcondition

\forall int i; ( (0 <= i & i < src.length) -> dest[i] = src[i] )

Modifies

dest[0 .. src.length]

Denis Lohner Specifying the JCF 4/20

slide-15
SLIDE 15

Motivation Specification Interfaces Using specs Method contract rule Conclusion Appendix Normal Case Exceptional Case General Concept

Specification by example

Normal case

Example Method

SomeLibrary.copy(java.lang.Object[] src, java.lang.Object[] dest)

Precondition

src != null & src.<created> = TRUE & dest != null & dest.<created> = TRUE & src.length = dest.length & \forall int i; ( (0 <= i & i < src.length) -> arrayStoreValid(dest, src[i]) )

Postcondition

\forall int i; ( (0 <= i & i < src.length) -> dest[i] = src[i] )

Modifies

dest[0 .. src.length]

Denis Lohner Specifying the JCF 4/20

slide-16
SLIDE 16

Motivation Specification Interfaces Using specs Method contract rule Conclusion Appendix Normal Case Exceptional Case General Concept

Specification by example

Normal case

Example Method

SomeLibrary.copy(java.lang.Object[] src, java.lang.Object[] dest)

Precondition

src != null & src.<created> = TRUE & dest != null & dest.<created> = TRUE & src.length = dest.length & \forall int i; ( (0 <= i & i < src.length) -> arrayStoreValid(dest, src[i]) )

Postcondition

\forall int i; ( (0 <= i & i < src.length) -> dest[i] = src[i] )

Modifies

dest[0 .. src.length]

Denis Lohner Specifying the JCF 4/20

slide-17
SLIDE 17

Motivation Specification Interfaces Using specs Method contract rule Conclusion Appendix Normal Case Exceptional Case General Concept

Specification by example

Normal case

Example Method

SomeLibrary.copy(java.lang.Object[] src, java.lang.Object[] dest)

Precondition

src != null & src.<created> = TRUE & dest != null & dest.<created> = TRUE & src.length = dest.length & \forall int i; ( (0 <= i & i < src.length) -> arrayStoreValid(dest, src[i]) )

Postcondition

\forall int i; ( (0 <= i & i < src.length) -> dest[i] = src[i] )

Modifies

dest[0 .. src.length]

Denis Lohner Specifying the JCF 4/20

slide-18
SLIDE 18

Motivation Specification Interfaces Using specs Method contract rule Conclusion Appendix Normal Case Exceptional Case General Concept

Specification by example

Normal case

Example Method

SomeLibrary.copy(java.lang.Object[] src, java.lang.Object[] dest)

Precondition

src != null & src.<created> = TRUE & dest != null & dest.<created> = TRUE & src.length = dest.length & \forall int i; ( (0 <= i & i < src.length) -> arrayStoreValid(dest, src[i]) )

Postcondition

\forall int i; ( (0 <= i & i < src.length) -> dest[i] = src[i] )

Modifies

dest[0 .. src.length]

Denis Lohner Specifying the JCF 4/20

slide-19
SLIDE 19

Motivation Specification Interfaces Using specs Method contract rule Conclusion Appendix Normal Case Exceptional Case General Concept

Specification by example

Normal case

Example Method

SomeLibrary.copy(java.lang.Object[] src, java.lang.Object[] dest)

Precondition

src != null & src.<created> = TRUE & dest != null & dest.<created> = TRUE & src.length = dest.length & \forall int i; ( (0 <= i & i < src.length) -> arrayStoreValid(dest, src[i]) )

Postcondition

\forall int i; ( (0 <= i & i < src.length) -> dest[i] = src[i] )

Modifies

dest[0 .. src.length]

Denis Lohner Specifying the JCF 4/20

slide-20
SLIDE 20

Motivation Specification Interfaces Using specs Method contract rule Conclusion Appendix Normal Case Exceptional Case General Concept

Specification by example

Normal case

Example Method

SomeLibrary.copy(java.lang.Object[] src, java.lang.Object[] dest)

Precondition

src != null & src.<created> = TRUE & dest != null & dest.<created> = TRUE & src.length = dest.length & \forall int i; ( (0 <= i & i < src.length) -> arrayStoreValid(dest, src[i]) )

Postcondition

\forall int i; ( (0 <= i & i < src.length) -> dest[i] = src[i] )

Modifies

dest[0 .. src.length]

Denis Lohner Specifying the JCF 4/20

slide-21
SLIDE 21

Motivation Specification Interfaces Using specs Method contract rule Conclusion Appendix Normal Case Exceptional Case General Concept

Specification by example

Exceptional case

Precondition

src.length = dest.length & \forall int i; ( (0 <= i & i < src.length) -> arrayStoreValid(dest, src[i]) )

Postcondition

exc = null -> \forall int i; ( (0 <= i & i < src.length) -> dest[i] = src[i] ) & exc != null -> ( NullPointerException::instance(exc) = TRUE & ( src != null & dest != null ) -> ( NullPointerException::instance(exc) = FALSE ) & NullPointerException::instance(exc) = TRUE -> ( dest = null | \forall int i; dest[i] = dest[i]@pre ) )

Denis Lohner Specifying the JCF 5/20

slide-22
SLIDE 22

Motivation Specification Interfaces Using specs Method contract rule Conclusion Appendix Normal Case Exceptional Case General Concept

Specification by example

Exceptional case

Precondition

src.length = dest.length & \forall int i; ( (0 <= i & i < src.length) -> arrayStoreValid(dest, src[i]) )

Postcondition

exc = null -> \forall int i; ( (0 <= i & i < src.length) -> dest[i] = src[i] ) & exc != null -> ( NullPointerException::instance(exc) = TRUE & ( src != null & dest != null ) -> ( NullPointerException::instance(exc) = FALSE ) & NullPointerException::instance(exc) = TRUE -> ( dest = null | \forall int i; dest[i] = dest[i]@pre ) )

Denis Lohner Specifying the JCF 5/20

slide-23
SLIDE 23

Motivation Specification Interfaces Using specs Method contract rule Conclusion Appendix Normal Case Exceptional Case General Concept

Specification by example

Exceptional case

Precondition

src.length = dest.length & \forall int i; ( (0 <= i & i < src.length) -> arrayStoreValid(dest, src[i]) )

Postcondition

exc = null -> \forall int i; ( (0 <= i & i < src.length) -> dest[i] = src[i] ) & exc != null -> ( NullPointerException::instance(exc) = TRUE & ( src != null & dest != null ) -> ( NullPointerException::instance(exc) = FALSE ) & NullPointerException::instance(exc) = TRUE -> ( dest = null | \forall int i; dest[i] = dest[i]@pre ) )

Denis Lohner Specifying the JCF 5/20

slide-24
SLIDE 24

Motivation Specification Interfaces Using specs Method contract rule Conclusion Appendix Normal Case Exceptional Case General Concept

Specification by example

Exceptional case

Precondition

src.length = dest.length & \forall int i; ( (0 <= i & i < src.length) -> arrayStoreValid(dest, src[i]) )

Postcondition

exc = null -> \forall int i; ( (0 <= i & i < src.length) -> dest[i] = src[i] ) & exc != null -> ( NullPointerException::instance(exc) = TRUE & ( src != null & dest != null ) -> ( NullPointerException::instance(exc) = FALSE ) & NullPointerException::instance(exc) = TRUE -> ( dest = null | \forall int i; dest[i] = dest[i]@pre ) )

Denis Lohner Specifying the JCF 5/20

slide-25
SLIDE 25

Motivation Specification Interfaces Using specs Method contract rule Conclusion Appendix Normal Case Exceptional Case General Concept

Specification by example

Exceptional case

Precondition

src.length = dest.length & \forall int i; ( (0 <= i & i < src.length) -> arrayStoreValid(dest, src[i]) )

Postcondition

exc = null -> \forall int i; ( (0 <= i & i < src.length) -> dest[i] = src[i] ) & exc != null -> ( NullPointerException::instance(exc) = TRUE & ( src != null & dest != null ) -> ( NullPointerException::instance(exc) = FALSE ) & NullPointerException::instance(exc) = TRUE -> ( dest = null | \forall int i; dest[i] = dest[i]@pre ) )

Denis Lohner Specifying the JCF 5/20

slide-26
SLIDE 26

Motivation Specification Interfaces Using specs Method contract rule Conclusion Appendix Normal Case Exceptional Case General Concept

Specification by example

Exceptional case

Precondition

src.length = dest.length & \forall int i; ( (0 <= i & i < src.length) -> arrayStoreValid(dest, src[i]) )

Postcondition

exc = null -> \forall int i; ( (0 <= i & i < src.length) -> dest[i] = src[i] ) & exc != null -> ( NullPointerException::instance(exc) = TRUE & ( src != null & dest != null ) -> ( NullPointerException::instance(exc) = FALSE ) & NullPointerException::instance(exc) = TRUE -> ( dest = null | \forall int i; dest[i] = dest[i]@pre ) )

Denis Lohner Specifying the JCF 5/20

slide-27
SLIDE 27

Motivation Specification Interfaces Using specs Method contract rule Conclusion Appendix Normal Case Exceptional Case General Concept

General Concept for specifying methods

Precondition Nearly all the time ”true” Postcondition Let φN be the postcondition for normal behaviour Let ψExci (1 ≤ i ≤ n, n ∈ N) be the condition where the exception Exci is thrown Let φExci be the postcondition that holds after Exci has been thrown Then the postcondition should look like this:

( exc = null -> φN ) & exc != null -> ( ( W

i Exci::instance(exc) = TRUE ) &

V

i( !ψExci -> Exci::instance(exc) = FALSE ) &

V

i( Exci::instance(exc) = TRUE -> φExci )

)

Denis Lohner Specifying the JCF 6/20

slide-28
SLIDE 28

Motivation Specification Interfaces Using specs Method contract rule Conclusion Appendix Normal Case Exceptional Case General Concept

General Concept for specifying methods

Precondition Nearly all the time ”true” Postcondition Let φN be the postcondition for normal behaviour Let ψExci (1 ≤ i ≤ n, n ∈ N) be the condition where the exception Exci is thrown Let φExci be the postcondition that holds after Exci has been thrown Then the postcondition should look like this:

( exc = null -> φN ) & exc != null -> ( ( W

i Exci::instance(exc) = TRUE ) &

V

i( !ψExci -> Exci::instance(exc) = FALSE ) &

V

i( Exci::instance(exc) = TRUE -> φExci )

)

Denis Lohner Specifying the JCF 6/20

slide-29
SLIDE 29

Motivation Specification Interfaces Using specs Method contract rule Conclusion Appendix Normal Case Exceptional Case General Concept

General Concept for specifying methods

Precondition Nearly all the time ”true” Postcondition Let φN be the postcondition for normal behaviour Let ψExci (1 ≤ i ≤ n, n ∈ N) be the condition where the exception Exci is thrown Let φExci be the postcondition that holds after Exci has been thrown Then the postcondition should look like this:

( exc = null -> φN ) & exc != null -> ( ( W

i Exci::instance(exc) = TRUE ) &

V

i( !ψExci -> Exci::instance(exc) = FALSE ) &

V

i( Exci::instance(exc) = TRUE -> φExci )

)

Denis Lohner Specifying the JCF 6/20

slide-30
SLIDE 30

Motivation Specification Interfaces Using specs Method contract rule Conclusion Appendix Normal Case Exceptional Case General Concept

General Concept for specifying methods

Precondition Nearly all the time ”true” Postcondition Let φN be the postcondition for normal behaviour Let ψExci (1 ≤ i ≤ n, n ∈ N) be the condition where the exception Exci is thrown Let φExci be the postcondition that holds after Exci has been thrown Then the postcondition should look like this:

( exc = null -> φN ) & exc != null -> ( ( W

i Exci::instance(exc) = TRUE ) &

V

i( !ψExci -> Exci::instance(exc) = FALSE ) &

V

i( Exci::instance(exc) = TRUE -> φExci )

)

Denis Lohner Specifying the JCF 6/20

slide-31
SLIDE 31

Motivation Specification Interfaces Using specs Method contract rule Conclusion Appendix Normal Case Exceptional Case General Concept

General Concept for specifying methods

Precondition Nearly all the time ”true” Postcondition Let φN be the postcondition for normal behaviour Let ψExci (1 ≤ i ≤ n, n ∈ N) be the condition where the exception Exci is thrown Let φExci be the postcondition that holds after Exci has been thrown Then the postcondition should look like this:

( exc = null -> φN ) & exc != null -> ( ( W

i Exci::instance(exc) = TRUE ) &

V

i( !ψExci -> Exci::instance(exc) = FALSE ) &

V

i( Exci::instance(exc) = TRUE -> φExci )

)

Denis Lohner Specifying the JCF 6/20

slide-32
SLIDE 32

Motivation Specification Interfaces Using specs Method contract rule Conclusion Appendix Normal Case Exceptional Case General Concept

General Concept for specifying methods

Precondition Nearly all the time ”true” Postcondition Let φN be the postcondition for normal behaviour Let ψExci (1 ≤ i ≤ n, n ∈ N) be the condition where the exception Exci is thrown Let φExci be the postcondition that holds after Exci has been thrown Then the postcondition should look like this:

( exc = null -> φN ) & exc != null -> ( ( W

i Exci::instance(exc) = TRUE ) &

V

i( !ψExci -> Exci::instance(exc) = FALSE ) &

V

i( Exci::instance(exc) = TRUE -> φExci )

)

Denis Lohner Specifying the JCF 6/20

slide-33
SLIDE 33

Motivation Specification Interfaces Using specs Method contract rule Conclusion Appendix Normal Case Exceptional Case General Concept

General Concept for specifying methods

Precondition Nearly all the time ”true” Postcondition Let φN be the postcondition for normal behaviour Let ψExci (1 ≤ i ≤ n, n ∈ N) be the condition where the exception Exci is thrown Let φExci be the postcondition that holds after Exci has been thrown Then the postcondition should look like this:

( exc = null -> φN ) & exc != null -> ( ( W

i Exci::instance(exc) = TRUE ) &

V

i( !ψExci -> Exci::instance(exc) = FALSE ) &

V

i( Exci::instance(exc) = TRUE -> φExci )

)

Denis Lohner Specifying the JCF 6/20

slide-34
SLIDE 34

Motivation Specification Interfaces Using specs Method contract rule Conclusion Appendix Normal Case Exceptional Case General Concept

General Concept for specifying methods

Precondition Nearly all the time ”true” Postcondition Let φN be the postcondition for normal behaviour Let ψExci (1 ≤ i ≤ n, n ∈ N) be the condition where the exception Exci is thrown Let φExci be the postcondition that holds after Exci has been thrown Then the postcondition should look like this:

( exc = null -> φN ) & exc != null -> ( ( W

i Exci::instance(exc) = TRUE ) &

V

i( !ψExci -> Exci::instance(exc) = FALSE ) &

V

i( Exci::instance(exc) = TRUE -> φExci )

)

Denis Lohner Specifying the JCF 6/20

slide-35
SLIDE 35

Motivation Specification Interfaces Using specs Method contract rule Conclusion Appendix Model functions Problems

Interface specification

Model functions

Problem Method behaviour is described by attribute changes But: Interfaces don’t contain any attributes Solution Introduce some function symbols for storing necessary information (”model functions”) E.g. \nonRigid[Location] int

size(java.util.List) for remembering

a Lists actual size

Denis Lohner Specifying the JCF 7/20

slide-36
SLIDE 36

Motivation Specification Interfaces Using specs Method contract rule Conclusion Appendix Model functions Problems

Interface specification

Model functions

Problem Method behaviour is described by attribute changes But: Interfaces don’t contain any attributes Solution Introduce some function symbols for storing necessary information (”model functions”) E.g. \nonRigid[Location] int

size(java.util.List) for remembering

a Lists actual size

Denis Lohner Specifying the JCF 7/20

slide-37
SLIDE 37

Motivation Specification Interfaces Using specs Method contract rule Conclusion Appendix Model functions Problems

Interface specification

Example

Method to be specified

s = myList.size()@java.util.List;

with s ⊏ − − jint and myList ⊏ − java.util.List Precondition

true

Postcondition

\if ( size(myList) <= java.lang.Integer.MAX VALUE) \then (s = size(myList)) \else (s = java.lang.Integer.MAX VALUE)

Modifies

s

Denis Lohner Specifying the JCF 8/20

slide-38
SLIDE 38

Motivation Specification Interfaces Using specs Method contract rule Conclusion Appendix Model functions Problems

Interface specification

Example

Method to be specified

s = myList.size()@java.util.List;

with s ⊏ − − jint and myList ⊏ − java.util.List Precondition

true

Postcondition

\if ( size(myList) <= java.lang.Integer.MAX VALUE) \then (s = size(myList)) \else (s = java.lang.Integer.MAX VALUE)

Modifies

s

Denis Lohner Specifying the JCF 8/20

slide-39
SLIDE 39

Motivation Specification Interfaces Using specs Method contract rule Conclusion Appendix Model functions Problems

Interface specification

Example

Method to be specified

s = myList.size()@java.util.List;

with s ⊏ − − jint and myList ⊏ − java.util.List Precondition

true

Postcondition

\if ( size(myList) <= java.lang.Integer.MAX VALUE) \then (s = size(myList)) \else (s = java.lang.Integer.MAX VALUE)

Modifies

s

Denis Lohner Specifying the JCF 8/20

slide-40
SLIDE 40

Motivation Specification Interfaces Using specs Method contract rule Conclusion Appendix Model functions Problems

Interface specification

Example

Method to be specified

s = myList.size()@java.util.List;

with s ⊏ − − jint and myList ⊏ − java.util.List Precondition

true

Postcondition

\if ( size(myList) <= java.lang.Integer.MAX VALUE) \then (s = size(myList)) \else (s = java.lang.Integer.MAX VALUE)

Modifies

s

Denis Lohner Specifying the JCF 8/20

slide-41
SLIDE 41

Motivation Specification Interfaces Using specs Method contract rule Conclusion Appendix Model functions Problems

Interface specification

Problems with model functions

Introducing model methods yields to two additional problems.

1

How to initialize a model function? Answer Write a method contract for the <init> function of the appropriate class

2

Symbolical execution <-> use of method contracts Solution Never use both for the same object in one proof and assure correctness by

Proof obligation inserts new non rigid predicate check in preconditions of contracts for it

Denis Lohner Specifying the JCF 9/20

slide-42
SLIDE 42

Motivation Specification Interfaces Using specs Method contract rule Conclusion Appendix Model functions Problems

Interface specification

Problems with model functions

Introducing model methods yields to two additional problems.

1

How to initialize a model function? Answer Write a method contract for the <init> function of the appropriate class

2

Symbolical execution <-> use of method contracts Solution Never use both for the same object in one proof and assure correctness by

Proof obligation inserts new non rigid predicate check in preconditions of contracts for it

Denis Lohner Specifying the JCF 9/20

slide-43
SLIDE 43

Motivation Specification Interfaces Using specs Method contract rule Conclusion Appendix Model functions Problems

Interface specification

Problems with model functions

Introducing model methods yields to two additional problems.

1

How to initialize a model function? Answer Write a method contract for the <init> function of the appropriate class

2

Symbolical execution <-> use of method contracts Solution Never use both for the same object in one proof and assure correctness by

Proof obligation inserts new non rigid predicate check in preconditions of contracts for it

Denis Lohner Specifying the JCF 9/20

slide-44
SLIDE 44

Motivation Specification Interfaces Using specs Method contract rule Conclusion Appendix Model functions Problems

Interface specification

Problems with model functions

Introducing model methods yields to two additional problems.

1

How to initialize a model function? Answer Write a method contract for the <init> function of the appropriate class

2

Symbolical execution <-> use of method contracts Solution Never use both for the same object in one proof and assure correctness by

Proof obligation inserts new non rigid predicate check in preconditions of contracts for it

Denis Lohner Specifying the JCF 9/20

slide-45
SLIDE 45

Motivation Specification Interfaces Using specs Method contract rule Conclusion Appendix Model functions Problems

Interface specification

Problems with model functions

Introducing model methods yields to two additional problems.

1

How to initialize a model function? Answer Write a method contract for the <init> function of the appropriate class

2

Symbolical execution <-> use of method contracts Solution Never use both for the same object in one proof and assure correctness by

Proof obligation inserts new non rigid predicate check in preconditions of contracts for it

Denis Lohner Specifying the JCF 9/20

slide-46
SLIDE 46

Motivation Specification Interfaces Using specs Method contract rule Conclusion Appendix Model functions Problems

Interface specification

Problems with model functions

Introducing model methods yields to two additional problems.

1

How to initialize a model function? Answer Write a method contract for the <init> function of the appropriate class

2

Symbolical execution <-> use of method contracts Solution Never use both for the same object in one proof and assure correctness by

Proof obligation inserts new non rigid predicate check in preconditions of contracts for it

Denis Lohner Specifying the JCF 9/20

slide-47
SLIDE 47

Motivation Specification Interfaces Using specs Method contract rule Conclusion Appendix Behavioral subtyping Problems

Using specification

Libraries

Loading of contracts The Library mechanism of KeY is used to load the contracts, i.e. the specifications are stored in KeY-files Application of Contracts Applying contracts within a proof is done via the MethodContractRule

Denis Lohner Specifying the JCF 10/20

slide-48
SLIDE 48

Motivation Specification Interfaces Using specs Method contract rule Conclusion Appendix Behavioral subtyping Problems

Using specification

Libraries

Loading of contracts The Library mechanism of KeY is used to load the contracts, i.e. the specifications are stored in KeY-files Application of Contracts Applying contracts within a proof is done via the MethodContractRule

Denis Lohner Specifying the JCF 10/20

slide-49
SLIDE 49

Motivation Specification Interfaces Using specs Method contract rule Conclusion Appendix Behavioral subtyping Problems

Using specifications

Let S, T be types with S ⊑ T Let obj ⊏ − − S Method call vs. method body statement Method call

  • bj.m(params)

will be expanded to Method body statement

  • bj.m(params)@T

where T specifies where to find the implementation of m(params)

Denis Lohner Specifying the JCF 11/20

slide-50
SLIDE 50

Motivation Specification Interfaces Using specs Method contract rule Conclusion Appendix Behavioral subtyping Problems

Using specifications

Let S, T be types with S ⊑ T Let obj ⊏ − − S Method call vs. method body statement Method call

  • bj.m(params)

will be expanded to Method body statement

  • bj.m(params)@T

where T specifies where to find the implementation of m(params)

Denis Lohner Specifying the JCF 11/20

slide-51
SLIDE 51

Motivation Specification Interfaces Using specs Method contract rule Conclusion Appendix Behavioral subtyping Problems

Using specifications

Let S, T be types with S ⊑ T Let obj ⊏ − − S Method call vs. method body statement Method call

  • bj.m(params)

will be expanded to Method body statement

  • bj.m(params)@T

where T specifies where to find the implementation of m(params)

Denis Lohner Specifying the JCF 11/20

slide-52
SLIDE 52

Motivation Specification Interfaces Using specs Method contract rule Conclusion Appendix Behavioral subtyping Problems

Using specifications

Behavioral subtyping

Let S, T be types with S ⊑ T Let obj.m(params)@T be a method body statement with obj ⊏ − − S Which contracts are available? Contracts written for Method m(params) in type T or a supertype Which contracts should be available? Contracts written for Method m(params) in type S or a supertype

Denis Lohner Specifying the JCF 12/20

slide-53
SLIDE 53

Motivation Specification Interfaces Using specs Method contract rule Conclusion Appendix Behavioral subtyping Problems

Using specifications

Behavioral subtyping

Let S, T be types with S ⊑ T Let obj.m(params)@T be a method body statement with obj ⊏ − − S Which contracts are available? Contracts written for Method m(params) in type T or a supertype Which contracts should be available? Contracts written for Method m(params) in type S or a supertype

Denis Lohner Specifying the JCF 12/20

slide-54
SLIDE 54

Motivation Specification Interfaces Using specs Method contract rule Conclusion Appendix Behavioral subtyping Problems

Using specifications

Behavioral subtyping

Let S, T be types with S ⊑ T Let obj.m(params)@T be a method body statement with obj ⊏ − − S Which contracts are available? Contracts written for Method m(params) in type T or a supertype Which contracts should be available? Contracts written for Method m(params) in type S or a supertype

Denis Lohner Specifying the JCF 12/20

slide-55
SLIDE 55

Motivation Specification Interfaces Using specs Method contract rule Conclusion Appendix Behavioral subtyping Problems

Using specifications

Behavioral subtyping

Let S, T be types with S ⊑ T Let obj.m(params)@T be a method body statement with obj ⊏ − − S Which contracts are available? Contracts written for Method m(params) in type T or a supertype Which contracts should be available? Contracts written for Method m(params) in type S or a supertype

Denis Lohner Specifying the JCF 12/20

slide-56
SLIDE 56

Motivation Specification Interfaces Using specs Method contract rule Conclusion Appendix Behavioral subtyping Problems

Using specifications

Problems

MethodContractRule available only on method body statement ⇒ Possible huge proof split up (e.g. java.util.List has many subtypes), hence same proof has to be done n times Solution Adapt MethodContractRule to use method call Used specifications must be proven Solution Need possibility to give feedback which contracts can not be proven (native methods)

Denis Lohner Specifying the JCF 13/20

slide-57
SLIDE 57

Motivation Specification Interfaces Using specs Method contract rule Conclusion Appendix Behavioral subtyping Problems

Using specifications

Problems

MethodContractRule available only on method body statement ⇒ Possible huge proof split up (e.g. java.util.List has many subtypes), hence same proof has to be done n times Solution Adapt MethodContractRule to use method call Used specifications must be proven Solution Need possibility to give feedback which contracts can not be proven (native methods)

Denis Lohner Specifying the JCF 13/20

slide-58
SLIDE 58

Motivation Specification Interfaces Using specs Method contract rule Conclusion Appendix Behavioral subtyping Problems

Using specifications

Problems

MethodContractRule available only on method body statement ⇒ Possible huge proof split up (e.g. java.util.List has many subtypes), hence same proof has to be done n times Solution Adapt MethodContractRule to use method call Used specifications must be proven Solution Need possibility to give feedback which contracts can not be proven (native methods)

Denis Lohner Specifying the JCF 13/20

slide-59
SLIDE 59

Motivation Specification Interfaces Using specs Method contract rule Conclusion Appendix

A ”new” method contract rule

Remember from creating specifications Let φN be the postcondition for normal behaviour Let ψExci (1 ≤ i ≤ n, n ∈ N) be the condition where the exception Exci is thrown Let φExci be the postcondition that holds after Exci has been thrown Let Exc1 to Exck(1 ≤ k ≤ n) be caught by a program

Denis Lohner Specifying the JCF 14/20

slide-60
SLIDE 60

Motivation Specification Interfaces Using specs Method contract rule Conclusion Appendix

A ”new” method contract rule

Remember from creating specifications Let φN be the postcondition for normal behaviour Let ψExci (1 ≤ i ≤ n, n ∈ N) be the condition where the exception Exci is thrown Let φExci be the postcondition that holds after Exci has been thrown Let Exc1 to Exck(1 ≤ k ≤ n) be caught by a program

Denis Lohner Specifying the JCF 14/20

slide-61
SLIDE 61

Motivation Specification Interfaces Using specs Method contract rule Conclusion Appendix

A ”new” method contract rule

Remember from creating specifications Let φN be the postcondition for normal behaviour Let ψExci (1 ≤ i ≤ n, n ∈ N) be the condition where the exception Exci is thrown Let φExci be the postcondition that holds after Exci has been thrown Let Exc1 to Exck(1 ≤ k ≤ n) be caught by a program

Denis Lohner Specifying the JCF 14/20

slide-62
SLIDE 62

Motivation Specification Interfaces Using specs Method contract rule Conclusion Appendix

A ”new” method contract rule

Remember from creating specifications Let φN be the postcondition for normal behaviour Let ψExci (1 ≤ i ≤ n, n ∈ N) be the condition where the exception Exci is thrown Let φExci be the postcondition that holds after Exci has been thrown Let Exc1 to Exck(1 ≤ k ≤ n) be caught by a program

Denis Lohner Specifying the JCF 14/20

slide-63
SLIDE 63

Motivation Specification Interfaces Using specs Method contract rule Conclusion Appendix

A ”new” method contract rule

Then the contract that should be applied is Precondition

V

k<i≤n !ψExci

Postcondition

( exc = null -> φN ) & exc != null -> ( ( W

1≤i≤k Exci::instance(exc) = TRUE ) &

V

1≤i≤k( !ψExci -> Exci::instance(exc) = FALSE ) &

V

1≤i≤k( Exci::instance(exc) = TRUE -> φExci )

)

Denis Lohner Specifying the JCF 15/20

slide-64
SLIDE 64

Motivation Specification Interfaces Using specs Method contract rule Conclusion Appendix

A ”new” method contract rule

Then the contract that should be applied is Precondition

V

k<i≤n !ψExci

Postcondition

( exc = null -> φN ) & exc != null -> ( ( W

1≤i≤k Exci::instance(exc) = TRUE ) &

V

1≤i≤k( !ψExci -> Exci::instance(exc) = FALSE ) &

V

1≤i≤k( Exci::instance(exc) = TRUE -> φExci )

)

Denis Lohner Specifying the JCF 15/20

slide-65
SLIDE 65

Motivation Specification Interfaces Using specs Method contract rule Conclusion Appendix

A ”new” method contract rule

Then the contract that should be applied is Precondition

V

k<i≤n !ψExci

Postcondition

( exc = null -> φN ) & exc != null -> ( ( W

1≤i≤k Exci::instance(exc) = TRUE ) &

V

1≤i≤k( !ψExci -> Exci::instance(exc) = FALSE ) &

V

1≤i≤k( Exci::instance(exc) = TRUE -> φExci )

)

Denis Lohner Specifying the JCF 15/20

slide-66
SLIDE 66

Motivation Specification Interfaces Using specs Method contract rule Conclusion Appendix

A ”new” method contract rule

Then the contract that should be applied is Precondition

V

k<i≤n !ψExci

Postcondition

( exc = null -> φN ) & exc != null -> ( ( W

1≤i≤k Exci::instance(exc) = TRUE ) &

V

1≤i≤k( !ψExci -> Exci::instance(exc) = FALSE ) &

V

1≤i≤k( Exci::instance(exc) = TRUE -> φExci )

)

Denis Lohner Specifying the JCF 15/20

slide-67
SLIDE 67

Motivation Specification Interfaces Using specs Method contract rule Conclusion Appendix

A ”new” method contract rule

Then the contract that should be applied is Precondition

V

k<i≤n !ψExci

Postcondition

( exc = null -> φN ) & exc != null -> ( ( W

1≤i≤k Exci::instance(exc) = TRUE ) &

V

1≤i≤k( !ψExci -> Exci::instance(exc) = FALSE ) &

V

1≤i≤k( Exci::instance(exc) = TRUE -> φExci )

)

Denis Lohner Specifying the JCF 15/20

slide-68
SLIDE 68

Motivation Specification Interfaces Using specs Method contract rule Conclusion Appendix

A ”new” method contract rule

Then the contract that should be applied is Precondition

V

k<i≤n !ψExci

Postcondition

( exc = null -> φN ) & exc != null -> ( ( W

1≤i≤k Exci::instance(exc) = TRUE ) &

V

1≤i≤k( !ψExci -> Exci::instance(exc) = FALSE ) &

V

1≤i≤k( Exci::instance(exc) = TRUE -> φExci )

)

Denis Lohner Specifying the JCF 15/20

slide-69
SLIDE 69

Motivation Specification Interfaces Using specs Method contract rule Conclusion Appendix

Demo

Demo Proving the contract of a simple method containsNullElements(java.util.List)

Denis Lohner Specifying the JCF 16/20

slide-70
SLIDE 70

Motivation Specification Interfaces Using specs Method contract rule Conclusion Appendix

Conclusion

Method contracts are capable of specifying library behaviour For interfaces: use of model functions necessary Need for thinking about the method contract rule

Denis Lohner Specifying the JCF 17/20

slide-71
SLIDE 71

Motivation Specification Interfaces Using specs Method contract rule Conclusion Appendix

Conclusion

Method contracts are capable of specifying library behaviour For interfaces: use of model functions necessary Need for thinking about the method contract rule

Denis Lohner Specifying the JCF 17/20

slide-72
SLIDE 72

Motivation Specification Interfaces Using specs Method contract rule Conclusion Appendix

Conclusion

Method contracts are capable of specifying library behaviour For interfaces: use of model functions necessary Need for thinking about the method contract rule

Denis Lohner Specifying the JCF 17/20

slide-73
SLIDE 73

Tanks for your attention

slide-74
SLIDE 74

Motivation Specification Interfaces Using specs Method contract rule Conclusion Appendix

Combining contracts

Assume 2 contracts given for one method Let φ1 be the precondition of the first and φ2 the precondition of the second Let ψ1 be the postcondition of the first and ψ2 the postcondition of the second Let M1 be the modifier set of the first and M2 the modifier set of the second Then a valid contract for the method is Precondition φ1 | φ2 Postcondition ( φ1@pre -> ψ1 ) & ( φ2@pre -> ψ2 ) Modifies M1 ∪ M2

Denis Lohner Specifying the JCF 19/20

slide-75
SLIDE 75

Motivation Specification Interfaces Using specs Method contract rule Conclusion Appendix

Behavioral subtyping

AbstractCollection {abstract} +remove(o) <<interface>> Collection +remove(o) <<interface>> List +remove(o) AbstractList {abstract} LinkedList ArrayList

implements implements

Denis Lohner Specifying the JCF 20/20

slide-76
SLIDE 76

Motivation Specification Interfaces Using specs Method contract rule Conclusion Appendix

Behavioral subtyping

AbstractCollection {abstract} +remove(o) <<interface>> Collection +remove(o) <<interface>> List +remove(o) AbstractList {abstract} LinkedList ArrayList

implements implements

One occurence of Object o will removed

Denis Lohner Specifying the JCF 20/20

slide-77
SLIDE 77

Motivation Specification Interfaces Using specs Method contract rule Conclusion Appendix

Behavioral subtyping

AbstractCollection {abstract} +remove(o) <<interface>> Collection +remove(o) <<interface>> List +remove(o) AbstractList {abstract} LinkedList ArrayList

implements implements

One occurence of Object o will removed First occurence of Object o will removed

Denis Lohner Specifying the JCF 20/20

slide-78
SLIDE 78

Motivation Specification Interfaces Using specs Method contract rule Conclusion Appendix

Behavioral subtyping

AbstractCollection {abstract} +remove(o) <<interface>> Collection +remove(o) <<interface>> List +remove(o) AbstractList {abstract} LinkedList ArrayList

implements implements

Denis Lohner Specifying the JCF 20/20