Title: Algebraic specifications : Specification and SPECalgebra
1Algebraic specifications Specification and
SPEC-algebra
- Definition (derivation of rewriting of terms)
- Given a set E of equations for a signature with
a fixed set of variables X Xe for each
equation e. (L,R) ? E defines two substitution
rules - (1) L gt R (L-R-rule)
- (2) R gt L (R-L-rule)
- A rule t1 gt t2 is applicable to a term t ?
TF(X) if there is an assignment assX? TF(X) with
extension ass TF(X) ? TF(X) such that we have
for t1 ass(t1) and t2 ass(t2) - (3) t1 is a subterm of t.
2Algebraic specifications Specification and
SPEC-algebra
- The replacement of t1 in t by t2 yields a term
t, the replacement of t1 by t2 in t is denoted
by - (4) t t(t1 / t2)
- In this case we write
- (5) t gt t, called direct
derivation from t to t via E using the rule
t1gt t2 and assignement ass. - (6) t gt t represent any sequence
- t0gtt1 gt ....gt tn with t t0 and t
tn. It is called derivation from t to t via E
and it is correct w.r.t. SIG-algebra A if for
each assignment ass X ? A - (7) ass(t) gt ass(t)
3Algebraic specifications Specification and
SPEC-algebra
- Definition (occurrence or positions in terms)
- Given a term t, the set of positions in t,
denoted by Dom(t), is the set of sequences of
natural numbers defined as - If t is constant or variable, then Dom(t) ?
- If t is of the form f(t1, ..., tn) then
- Dom(t) ? ? i.p / i ? 1,..,n and p ?
Dom(ti) - Definition (subterms)
- Given a term t, and a position p ? Dom(t) we
define a subterm of t rooted at a position
denoted tp as - p ?, then tp t
- If p i.pthen t f(t1, ...,ti,...)i.p
tip - A term t is said to be a subterm of a t is there
is a position p such that t tp
4Algebraic specifications Specification and
SPEC-algebra
- Definition (Term replacement)
- Given a term t, a position p, and a term t, we
define tp t as - If p ? then tp t t
- If p i.p then t f(t1, ..., ti-1,ti,
ti1...)i.p t - f(t1, ..., ti-1,ti p t,
ti1...) - Definition (Rewriting term)
- Given a system of rules (oriented equations), R,
we define a rewrite relation by gtR , as t gt
t, if - There is a rule r l gt r is R there is an
assignement (substitution) ? X ? TF(X) and a
position p in t such that tp ? (l) and t
tp ? (r)
5Algebraic specifications Specification and
SPEC-algebra
- Definition (Congruence on Ground Terms)
- Given a specification SPEC (S, F, E) the
relation ? on ground terms defined for all t1, t2
? TF by - t1 ? t2 if and only if evalA(t1) evalA(t2)
for all SPEC-algebra A is called congruence on
ground terms. - It satisfies the following conditions for all t1,
t2, t3 ? TF - - t1 ? t1 (reflexivity) t1 ? t2 implies t2 ?
t1 (symmetry) - t1 ? t2 and t2 ? t3 implies t1 ? t3
(transitivity) - - t1 ? t1 ,..., tn ? tn implies f(t1,...tn) ?
f(t1,....,tn) (congruence) - - each derivation t1 gt t2 via E between ground
terms t1, t2 ? TF implies t1 ? t2 .
6Algebraic specifications Specification and
SPEC-algebra
- A rewriting relation gtR is like a congruence
relation without the reflexivity property. - Top(push(pop(push(empty,0)), succ(m)))
- top(push(empty, succ(m)))
- succ(m)
7Algebraic specifications Specification
- Definition (Algebra of Terms)
- Given a signature SIG (S, F). We define the
algebra of terms T (ST, FT) w.r.t. SIG and a
set of variables X or simply termalgebra as - ST (TF,s(X))s?S as the family of base sets
- fT f as the constant for f ? s
- fT TF,s1(X) x . . . x TF,sn (X) ? TF,s(X)
defined by - fT(t1,..,tn) f(t1,..,tn) for fs1 ...sn ? s
and - ti ?TF,si(X)
8Algebraic specifications Specification and
SPEC-algebra
- Definition (Quotient Term Algebra TSPEC)
- Given a specification SPEC (S, F, E) the
quotient term algebra - TSPEC ((Qs) s?S, (fQ) f?F) is defined by
- 1. For each s ? S, we have a base set
- Qs t / t ? TF,s
- where the congruence class t is defined by
- t t / t ? t
- 2. For each constant symbol f ? s in F the
constant Qs is the congruence class generated
by f fQ f - 3. For each operation symbol fs1 ...sn ? s in F
the operation - fQ Qs1 x ... x Qsn ? Qs is defined by
- fQ(t1, ...,tn) f(t1,...,tn)
9Algebraic specifications Specification and
SPEC-algebra
- Example (Quotient Term Algebra Tnat)
- Tnat (Qnat , 0Q, SUCCQ, ADDQ)
- With
- - Qnat SUCCn(0) / n ? 0
- - 0Q 0, and for n, m ? 0
- - SUCCQ(SUCCn(0)) SUCCn1(0)
- - ADDQ(SUCCn(0), SUCCm(0)) SUCCnm(0)
- Fact TSPEC is a SPEC-Algebra and it is called
the initial semantics with ADT(SPEC) A / A ?
TSPEC is called the (initial) abstract data
type defined by SPEC. -
10Algebraic specifications Specification
- The quotient term algebra TSPEC of a
specification SPEC (S, F, E) has the following
properties - The evaluation eval TF ? TSPEC is equal to
- nat TF ? TSPEC, defined by nat(t) t for
all - t ?TF, and hence surjective.
- Each equation e (t1, t2) of ground term
- t1, t2 ? TF is valid in TSPEC if and only if it
is valid in each SPEC-algebra A. - TSPEC is a SPEC-algebra.
11Algebraic specifications Specification
- Defintions (Equational Rules and Proofs)
- An equational rule (over SIG) is given by a pair
- (E, e)
- Where E is a set of equations and e is a single
equation w.r.t. SIG. We also write E -- e - 2. Given a set R of equational rules and a set of
E of equations w.r.t. SIG. Then an (equational)
proof - With rules R and premisses E is a sequence E is a
sequence e1, ..,er.
12Algebraic specifications Specification
- Definition ( Equational calculus) The
equational calculus, is defined to contain
exactly the following equational rules - for t1, t2, t3 ?TF(X) and t TF(Y).
- R1 -- t1 t1 (identity)
- R2 t1 t2 -- t2 t1 (symmetry)
- R3 t1 t2 and t2 t3 -- t1 t3
(transitivity) - R4 (X, t1 t2) -- (X ? Y, h(t1) h(t2) )
- for ass X ?TF(Y) (substitivity).
13Algebraic specifications Specification
- When the equations are used as rewrite rules,
the symmetric rule is to be droped. - In rewriting techniques, the process of orienting
equations is based on the so-called simplication
orderings a partial order between operations
extended to terms. - example add gt succ gt 0 gt add(..,..) gt
succ(..) - In order to ensure the termination of rewriting
of a term, such ordering is required to be
well-founded (any ordering should have has a
small element). - The small element a any term is called the normal
form of the term.
14Algebraic specifications Specification
- In order to ensure the uniqueness of computation,
the so-called confluence property is required - t1 gt t2 gtNf(t1) and
- t1 gt t3
- then t3 should be rewritten to Nf(t1) i.e.
- t3 gtNf(t1)
- This property is ensured by the so-called
Knuth-Bendix completion procedure. It takes a set
of equations and an ordering, and it generate a
set of rewrite rules which terminate and are
confluent.
15Algebraic specifications Specification
- The confluence property is verified by
eliminating all ambiguities that may be hidden
between different rules of the system. These
ambiguities are called critical-pairs. - Fot their definition, we need the notion of
unification - Two terms t1 and t2 are said to be unfiable is
there is a substitution ? such ? (t1) ? (t2)
. - Example let t1 f(a,g(y)) and t2 f(x,g(h(b))),
then is it easy to proof that ? x --gt a, y --gt
h(b) is a unfier of t1 and t2. That is, ? (t1)
f(a,g(h(b))) ? (t2)
16Algebraic specifications Specification
- Definition (critical pair)
- If l ? r and s ? t are two rewrite rules with
distinct variables, p is the position of a
nonvariable subterm of s, and ? is the unifier of
sp and l, then the equation ?(t) ?(s?(r)p )
is a critical pair formed from those rules. - Example suppose we want to add the alternation
in the stack specification using the following
two rewrite rules - alternate(push(x,y),z) ? push(x, alternate(z,y))
- Alternate(y1,?) ? y1
- Then, by applying the above definition, we can
notice that alternate in the second rule occurs
at position ? in the first rule. That is, s
alternate(push(x,y),z) and l aternate(y1, ?).
So, we have to check for a unification of
alternate(z,y)) and Alternate(y,?). The unfier
here is ? y1 ? push(x,y) , z ? ?
17Algebraic specifications Specification
- So, the resulting members of the critical pair
are - ?(t) ?(push(x, alternate(z,y)))
push(x,alternate(?,y))) - ?(t) ?(s?(r)p ) ?(s?(r)?) ?(r)
- ?(y1) push(x,y)
- And the critical pair is therefore the resulting
equation - push(x, alternate(?, y)) push(x,y)
18Advanced Algebraic specifications
- To deal a maximal of cases and errors, subsorts
may be defined. (S lt S) - Parametrized specifications are specifications
based on others stack(string) list(nat) .... - Parametrized specifications are interpreted using
category on algebras . - To go beyong the non-changing or fixed notions of
algebras, and thereby interpreting state-based
reactive (information) systems, several
extensions have been proposed to the algebraic
semantics. - Rewriting logic a computation is a functor from
an algebra to an another. - Hidden sorted algebra some sorts modelling
states are hidden.