Procedure Specifications

These slides are based on

1 Introduction

2 Behavioral Equivalence

static int findA (int [] a, int val) {
  for (int i = 0; i < a.length; i++) {
    if (a[i] == val)
      return i;
  }
  return a.length;
}

static int findB (int [] a, int val) {
  for (int i = a.length -1 ; i >= 0; i--) {
    if (a[i] == val) return i;
  }
  return -1;
}   

3 Specification Structure

4 Declarative Specifications

public StringBuffer reverse()
  // modifies: this

  // effects: Let n be the length of the old character sequence, the
  // one contained in the string buffer just prior to execution of the
  // reverse method. Then the character at index k in the new
  // character sequence is equal to the character at index n-k-1 in
  // the old character sequence.


  //Or, more formally

  //effects: length (this.seq) = length (this.seq )
  //for all k: 0..length(this.seq)-1 | this.seq [k] =
  //this.seq[length(this.seq)-k-1]
public boolean startsWith(String prefix)
  // Tests if this string starts with the specified prefix.
  // effects:
  // if (prefix = null) throws NullPointerException
  // else returns true iff exists a sequence s such that (prefix.seq ^
  // s = this.seq)

public String substring(int i)
  // effects:
  // if i < 0 or i > length (this.seq) throws IndexOutOfBoundsException
  // else returns r such that
  // some sequence s | length(s) = i && s ^ r.seq = this.seq

4.1 More Declarative Spec. Examples

/*This one does not determine the outcome but it is
  still useful to clients. */
public boolean maybePrime ()
  // effects: if this BigInteger is composite, returns false


/*Same here, we don't know in which order they will be
    notified */

public void addObserver(Observer o)
  // modifies: this
  // effects: this.observers  = this.observers + {o}

public void notifyObservers()
  // modifies the objects in this.observers
  // effects: calls o.notify on each observer o in this.observers

5 Exceptions and Preconditions

6 Specification Ordering

  1. A's precondition is no stronger than B's
  2. A's postcondition is no weaker than B's, for the states that satisfy B's precondition.

7 Judging Specifications

8 User-Defined Types

9 Types of Types

9.1 Types of Operations

9.2 Example: List

public class List {
  public List ();
  public void add (int i, Object e);
  public void set (int i, Object e);
  // modifies this
  // effects
  // throws IndexOutOfBoundsException if i < 0 or i >= length (this.elems)
  // else this.elems  [i] = e and this.elems unchanged elsewhere

  public void remove (int i);
  public int size ();
  public Object get (int i);
  // throws
  // IndexOutOfBoundsException if i < 0 or i > length (this.elems)

  // returns
  // this.elems [i] public void add (int i, Object e);
  }

10 Designing and Abstract Type

11 Representation Independence

Vector v = new Vector();
List l = new List ();
....
v.copyInto (l.elementData);
class List {
  public Entry getEntry (int i);
}

//we can now write

Entry e = l.getEntry (i);
e.element = x;
...
e.element = y;

12 Language Mechanisms

URLs

  1. Procedure Specifications, http://ocw.mit.edu/NR/rdonlyres/Electrical-Engineering-and-Computer-Science/6-170Laboratory-in-Software-EngineeringFall2001/E1DC2389-1837-4638-A27F-8900EE03EA5C/0/lecture04.pdf
  2. Abstract Types, http://ocw.mit.edu/NR/rdonlyres/Electrical-Engineering-and-Computer-Science/6-170Laboratory-in-Software-EngineeringFall2001/FFC1C94C-4CE0-4433-B40F-534AEC76D13F/0/lecture05.pdf

This talk available at http://jmvidal.cse.sc.edu/talks/specifications/
Copyright © 2009 José M. Vidal . All rights reserved.

18 January 2005, 10:44AM