220 likes | 311 Views
CAS2545: WEEK 11. LECTURE: The meaning of Algebraic Specifications TUTORIAL/PRACTICAL: Do the exercises given in last week’s handout Look at the revision questions on the web and attempt them without looking at the answers. Algebras and Algebraic Specifications. Last week we saw:
E N D
CAS2545: WEEK 11 LECTURE: • The meaning of Algebraic Specifications TUTORIAL/PRACTICAL: Do the exercises given in last week’s handout Look at the revision questions on the web and attempt them without looking at the answers
Algebras and Algebraic Specifications Last week we saw: - what an algebra was (values + closed, total operations) - a way to specify algebras by writing Signatures of operation - we can give a semantics to data types via algebras But how can we show that an algebra ‘is specified’ by an algebraic specification? And what is the set of algebras specified by the specification?
MODELS of the “Metre” in Paris The “Metre” in London The “Metre” in Berlin The “Metre” in Paris The “Metre” in Rome The “Metre” in Madrid
MODELS of the “Algebraic Specification” of some Data Type Algebras (data types) Algebras (data types) The Algebraic Specification Of the Data Type Algebras (data types) Algebras (data types)
Models of An Algebra Roughly: A model of an algebraic spec. AS is an algebra A which conforms to the spec. THE ALGEBRAIC SPEC is LIKE THE METRE IN PARIS! To show an algebra A conforms to a specification AS we have to be able to do the following: 1. Map every operation in AS to a unique operation in algebra A. 2. Using this mapping (interpretation), show that the equations in AS are made true.
Models of An Algebra 1. e.g. Truth table logic, 3 valued logicare models of Boolean specification. 2. e.g. decimal positive numbers, decimal positive numbers modulo 5, are models of the ‘Natural’ specification given out in the notes. 3. e.g. the C implementation of boolean type, with true = 1, false = 0, and = &&, not = !, can be said to be a model of the Boolean specification. 4. e.g. all ‘good’ implementations of a STACK data structure are models of the ‘Stack’ specification given out in the notes.
Good and Bad Models of An Algebraic Spec Models can be good or bad! For example, 3-valued logic is not a good model of Boolean. The set of models of an alg. spec. as defined is not restricted enough. To find the ‘good’ models we must look closely at an algebraic specification….
The Term Algebra of an Algebraic Specification The Term Algebra of an Alg. Spec. AS is a SPECIAL model (its similar idea to the Herbrand Universe of a set of well formed formulae in first order logic) The Term Algebra is defined by: • set of values = the set of all terms that can be generated using the signature as a generative grammar • set of operations = operations as in the signature of the spec.
Focus on the Type Of Interest If we have a heterogeneous algebra with N carrier sets, then the values in its term algebra will split into those N sets. E.g. in stack, (push init 3) is in stack, (is_empty init) is in bool, (top (push init 3)) is in nat. We focus in on the values of the TYPE OF INTEREST (TOI) e.g. stack in the ‘Stack’ spec
Equivalence Classes in a Term Algebra The values of the term algebra of the TOI fall into distinct equivalence classes If X, Y are in the TOI, then X = Y if and only if X can be transformed to Y using the equations. E.g. (push init 3) and (push init (top (push init 3))) are in the same equivalence class
Equational reasoning (READ section 8.8 in the online book) • Assume we have an equation X = Y in an Algebraic Specification and a member of its term algebra T. • X and Y may contain (universally quantified) variables, T contains only operators / values (no variables). • Then we can use the equation to REWRITE T to another (equal) term T1. • The process is as follows: • 1. Find a substring of T called T' that MATCHES with X under substitution sequence S . • 2. Apply S to Y to get Y' • 3. Replace T' in T with Y' to form new term T1.
Equational reasoning example • Let T = or(and(true,not(false)),false) • Using the axiom: • (3) and(true,b) = b • Substring of T “and(true,not(false))” matches with the LHS of this equation under the substitution S = [not(false) / b ] • Thus we can re-write term T = or(and(true,not(false)),false) to new term: • or(b,false) [not(false) / b ] = or(not(false),false)
Initial Models of An Algebraic Spec A model is INITIAL if two conditions hold: 1. All distinct values in the model are represented by an equivalence class in the spec’s term algebra This means NO JUNK - there are no DISTINCT values in the model (algebra) which aren’t represented by some DISTINCT equivalence class.
Initial Models of An Algebraic Spec 2. Each equivalence class in the spec’s term algebra represents a unique value in the model This means NO CONFUSION - we cannot have 2 equivalence classes in the model that map to the same value in the model.
INITIAL MODEL of an “Algebraic Specification” Algebras (data types) values A B C D Equivalence classes The Algebraic Specification Of the Data Type
JUNK! Algebras (data types) values A B C D E F Equivalence classes The Algebraic Specification Of the Data Type
CONFUSION! Algebras (data types) values A B C Equivalence classes The Algebraic Specification Of the Data Type
The Semantics of An Algebraic Spec Given an algebraic specification, its meaning is the set of all its INITIAL MODELS
Algebraic Specs as ‘Objects’ • An algebraic spec provides an INTERFACE SPECIFICATION informing a client what services are on offer (class and operations). • This spec. is a good one because it provides the formal syntax AND semantics. • The TOI is a defined CLASS.
Algebraic Specs as ‘Objects’ • A specification of an object’s states are the equiv classes of the spec’s term algebra. • However, algebraic specs give no notion of identity and persistence! • Alg. Specs are easily implemented in HASKELL.
Operational semantics of algebraic specifications Alg. Specs are easily operationalised (and implemented in HASKELL) The equations are treat as Left to Right re-write rules (or function definitions). But the equations must be in a certain form to make this work..
Conclusions • Algebraic Specs are using to abstractly define algebras • An algebra conforms to a spec if it is an initial model of that spec. • Equational Algebraic Specs can be prototyped (operationalised) by using the equations are L-R re-write rules