Algebraic Specifications
Total Page:16
File Type:pdf, Size:1020Kb
Algebraic specifications Dr. Constantinos Constantinides, P.Eng. Department of Computer Science and Software Engineering Concordia University Montreal, Canada 7 January, 2020 1/101 Types ◮ A type is a collection of values. ◮ Examples of types include the integer or the Boolean type. ◮ We can distinguish between simple types and composite (or aggregate) types based on whether or not the values of a type can contain subparts. ◮ As an example, we can say that the integer type is simple, whereas a student record is a composite data type. ◮ A data item is an instance (also: a member) of a type. 2/101 Abstract data types ◮ An abstract data type (ADT) is a mathematical model: A definition for a type solely in terms of the set of values and a set of operations on that type. ◮ The behavior of each operation is determined by its inputs and outputs. ◮ This implies that an ADT is implementation-independent. 3/101 Data types (or concrete data types) ◮ A (concrete) data type is the concrete description of how an ADT is actually implemented. ◮ For example in int x = 7;, we instruct the compiler about the type of variable x (and implicitly about the legal operations as well as about its range of values). 4/101 ADTs and data structures ◮ Do not confuse ADTs with data structures. The former are abstract definitions solely in terms of interfaces, whereas the latter are particular ways of storing and organizing data. ◮ Data structures can be deployed in order to define ADT’s. 5/101 Formal specifications and algebraic specifications ◮ A formal specification is a mathematical description of software system. ◮ It describes what the system should do, but not necessarily how it should do it. ◮ An algebraic specification is a technique that supports the specification of a system through its representation as a collection of sets together with a description of operations on the sets. 6/101 Formal specifications and algebraic specifications /cont. ◮ This aligns with the notion of an abstract data type. ◮ Each specification has a name (with an optional generic parameter list) and a body. ◮ Generic parameters allow abstract types which are collections of other types (such as lists and stacks) to be specified without committing to a particular type of the elements in the collection. 7/101 Abstraction: Interfaces and information hiding ◮ Clients of a system need to know what services a system provides (i.e. what operations are provided by the interface of the system). ◮ Implementors may provide an initial implementation which they can subsequently modify in order to improve performance or other non-functional requirement. 8/101 Specification template ◮ A specification consists of five parts: 1. A declaration of the sort (type) of the entity to be specified and possibly an import of other specifications to be reused in the current specification. 2. Informal description of its operations. 3. Signatures of operations (syntax). 4. Variables (to be used in axioms). 5. Axioms: Statements (rules) that must always hold (semantics: meaning of operations). 9/101 Operations on ADTs ◮ Operations can be classified into the following: ◮ Primitive constructors take no arguments and create objects of the underlying abstract data type. ◮ Non-primitive constructors take arguments and create objects. ◮ Mutators access an object in order to write. ◮ Observers access an object in order to read. 10/101 Example: The Boolean ADT Declaration of the sort and signatures of operations Spec: Boolean; Sort: Boolean; Operations: true → Boolean; false → Boolean; not : Boolean → Boolean; and : Boolean × Boolean → Boolean; or : Boolean × Boolean → Boolean; impl : Boolean × Boolean → Boolean; bicond : Boolean × Boolean → Boolean; ◮ Signatures tell us how to form complex terms from primitive operations. 11/101 Axioms on ADTs ◮ Axioms specify the semantics (meaning) of the ADT, and they characterize its behavior. ◮ They are specified using the operations defined in the signature part. ◮ As a rule of thumb, we write an axiom for each inspection operation over each constructor or mutator operation. 12/101 Axioms on ADTs /cont. ◮ The specification may also include exceptions that define conditions under which the axioms do not hold. ◮ This can be done either by using undefined as the output of certain axioms (indicating that the evaluation of the operation results in an error), or by using an exceptions clause in the specification. 13/101 Example: The Boolean ADT: Variables and axioms Variables: x, y: Boolean; Axioms: [A1] not(true)= false; [A2] not(false) = true; [A3] false and x = false; [A4] true and x = x; [A5] true or x = true; [A6] false or x = x; [A7] x or y = y or x; [A8] x and y = y and x; [A9] not (x and y) = (not x) or (not y); [A10] not (x or y) = (not x) and (not y); [A11] x impl y = not(x) or y; [A12] x bicond y = (x impl y) and (y impl x); 14/101 Example: The Boolean ADT Discussion on Axioms ◮ Axioms A7 and A8 capture the commutativity of conjunction and disjunction respectively: ◮ xory=yorx: (x ∨ y) ⊢ (y ∨ x) , and ◮ xandy=yandx: (x ∧ y) ⊢ (y ∧ x) 15/101 Example: The Boolean ADT Discussion on Axioms /cont. ◮ Axioms A9 and A10 capture De Morgan’s Laws: ◮ not (x and y) = (not x) or (not y) : ¬(x ∧ y) ⊢ (¬x ∨¬y), and ◮ not (x or y) = (not x) and (not y) : ¬(x ∨ y) ⊢ (¬x ∧¬y) 16/101 Example: The set of natural numbers Declaration of the sort Spec: Natural; Sort: Nat; Imports: Boolean; 17/101 Example: The set of natural numbers Informal description of operations Description: Operations on natural numbers include succ which returns the successor number, and add which adds two natural numbers. 18/101 Example: The set of natural numbers Signatures of operations Operations: zero → Nat; iszero : Nat → Boolean; succ : Nat → Nat; add : Nat × Nat → Nat; 19/101 Signatures of operations ◮ For variables x, y : Nat, the following terms are legal: ◮ iszero(succ(x)) ◮ add(succ(x), y) ◮ The following term is not legal ◮ succ(iszero(x)) 20/101 Example: The set of natural numbers Variables and axioms Variables: x, y : Nat; Axioms: [A1] iszero(succ(x)) = false; [A2] add(zero, x) = x; [A3] add(succ(x), y) = add(x, succ(y)); 21/101 Example: The Set ADT ◮ Consider the ADT Set. ◮ Since a set can hold a collection of elements of any type, we will include a generic parameter Element in the specification. 22/101 Example: The Set ADT Declaration of the sort Spec: Set (Element); Sort: Set; Imports: Boolean, N0; 23/101 Example: The Set ADT Description of operations ◮ newset creates an empty set. ◮ add (Set, Element) adds Element into Set. ◮ remove (Set, Element) removes Element from Set. ◮ size(Set) returns the number of elements in Set. ◮ isempty(Set) returns true if Set contains no elements and it returns false otherwise. ◮ ismember(Set, Element) returns true if Element is a member of Set and it returns false otherwise. 24/101 Example: The Set ADT Signatures of operations Operations: newset → Set; add : Set × Element → Set; remove : Set × Element → Set; size : Set → N0; isempty : Set → Boolean; ismember : Set × Element → Boolean; 25/101 Example: The Set ADT Variables and axioms Variables: s: Set; x, y: Element; Axioms: [A1] isempty(newset) = true; [A2] ismember(newset, x) = false; [A3] ismember(add(s, x), x) = true; 26/101 Example: The Set ADT Axioms /cont. The axiom [A1] isempty(newset) = true; can be also captured by size(newset) = 0; 27/101 Example: The Set ADT Axioms /cont. ◮ The axiom [A4] add(add(s, x), y) = add(add(s, y), x) ensures that the order is not important in sets. 28/101 Example: The Set ADT Axioms /cont. ◮ The axiom [A5] size(add(add(newset, x), x)) = 1 ensures that no repetitions are allowed. ◮ An alternative axiom would be add(add(newset, x), x) = add(newset, x) 29/101 Example: The Set ADT Axioms /cont. [A6] size(add(s, x)) = if (not(ismember(s, x))) then size(s) + 1 else size(s); 30/101 This slide is left empty. 31/101 Example: The Set ADT Axioms are universally quantified ◮ Note that we interpret each axiom as being universally quantified by all variables appearing in it. ◮ For example, the axiom [A3] ismember(add(s, x), x) = true; is an abbreviation of ∀(s : Set, x : Element) | ismember(add(s, x), x)= true; 32/101 Example: String Declaration of the sort Spec: String; Sort: String; Imports: Char, N0, Boolean; 33/101 Example: String Description of operations Description: Operation new creates an empty string, append concatenates two strings, add places a character at the end of a string, length returns the size of the string, isempty returns true if the string is empty and it returns false otherwise, and equal compares two strings for equality. 34/101 Example: String Signatures of operations Operations: new → String; append : String × String → String; add : String × Char → String; length : String → N0; isempty : String → Boolean; equal : String × String → Boolean; 35/101 Example: String Variables and axioms Variables: x, y, z: String; c: Char; Axioms: [A1] isempty(new) = true; [A2] length(new) = 0; [A3] isempty(add(x, c)) = false; [A4] length(add(x, c)) = length(x) + 1; [A5] append(x, new) = x; [A6] append(x, add(y, c)) = add(append(x, y), c); [A7] equal(new, new) = true; [A8] equal(new, add(new, c)) = false; 36/101 Example: Array Declaration of the sort Spec: Array (Element); Sort: Array; Imports: N; 37/101 Example: Array Description of operations Description: ◮ Arrays are collections of elements of generic type Element. They have a lower and an upper bound returned by operations first and last. ◮ Individual elements are accessed by their numeric indices. ◮ Operation create takes a lower and an upper bound parameter and constructs an array, initializing its values to undefined.