Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Abstra
t
We des
ribe a method for extra
ting high-level spe
i
ations from unstru
tured sour
e
ode. The method
is based on a theory of program renement and transformation, whi
h is used as the bases for the development of a
atalogue of powerful semanti
s-preserving
transformations. Ea
h transformation is an operation on a program whi
h has a me
hani
ally-
he
kable
orre
tness
ondition, and whi
h has been rigorously
proved to produ
e a semanti
ally equivalent result. The
transformations are
arried out in a wide spe
trum
programming language (
alled WSL). This language
in
ludes high-level spe
i
ations as well as low-level
programming
onstru
ts. As a result, the formal reverse engineering pro
ess (from sour
e
ode to equivalent spe
i
ations) and the redevelopment pro
ess (renement of spe
i
ations into sour
e
ode)
an both be
arried out within a single language and transformation theory.
We also dis
uss a tool (FermaT) whi
h has been
developed to support this approa
h to reengineering.
The tool is a program transformation system, largely
written in an extension to WSL
alled METAWSL.
Thus it is possible to use the tool in the maintenan
e
of its own sour
e
ode, and this is starting to be the
ase.
1 Introdu
tion
This paper des
ribes the appli
ation of formal program transformations to un
over spe
i
ations from
sour
e
ode. We take as an example a small report
writing program. Due to errors in the original design,
this program had several bugs whi
h were gradually
un
overed and xed in the usual way (pat
hes, rsttime-through swit
hes, dupli
ated
ode et
. et
.). The
resulting program appears to work, but has a
omplex
WP(S1 ; true) WP(S2 ; true) (see [19). Another advantage is that the innitary logi
we use is
omplete,
so if there is a renement then there is also guaranteed
to be a proof of the
orresponding formula|although
the proof may be innitely long! However, it is
perfe
tly pra
ti
al to
onstru
t innitely long proofs:
in fa
t the proofs of many transformations involving
re
ursion or iteration are innite proofs
onstru
ted by
indu
tion. Thus innitary logi
is both ne
essary and
su
ient for proving renements and transformations.
We
onsider the following
riteria to be important
for any pra
ti
al wide-spe
trum language and transformation theory:
1. General spe
i
ations in any \su
iently pre
ise"
notation should be in
luded in the language. For
su
iently pre
ise we will mean anything whi
h
an
be expressed in terms of mathemati
al logi
with
suitable notation. This will allow a wide range
of forms of spe
i
ation, for example Z spe
i
ations [8 and VDM [10 both use the language
of mathemati
al logi
and set theory (in dierent
notations) to dene spe
i
ations. The \Representation Theorem" [19 proves that our spe
i
ation
statement is su
ient to spe
ify any WSL program
(and therefore any
omputable fun
tion, sin
e WSL
is
ertainly Turing
omplete);
2. Nondeterministi
programs. Sin
e we do not want
to have to spe
ify everything about the program
we are working with (
ertainly not in the rst
versions) we need some way of spe
ifying that some
exe
utions will not ne
essarily result in a parti
ular
out
ome but one of an allowed range of out
omes.
The implementor
an then use this latitude to
provide a more e
ient implementation whi
h still
satises the spe
i
ation;
3. A well-developed
atalogue of proven transformations whi
h do not require the user to dis
harge
omplex proof obligations before they
an be applied. In parti
ular, it should be possible to introdu
e, analyse and reason about imperative and
re
ursive
onstru
ts without requiring loop invariants;
4. Te
hniques to bridge the \abstra
tion gap" between
spe
i
ations and programs. See [23,30 for examples;
5. Appli
able to real programs|not just those in a
\toy" programming language with few
onstru
ts.
This is a
hieved by the (programming) language
independen
e and extendibility of the notation via
\denitional transformations". See [15,17,24 for
examples;
3 Program Renement
and Transformation
The WSL language in
ludes both spe
i
ation
onstru
ts, su
h as the general assignment, and programming
onstru
ts. One aim of our program transformation work is to develop programs by rening a
spe
i
ation, expressed in rst order logi
and set
theory, into an e
ient algorithm. This is similar to
the \renement
al
ulus" approa
h of Morgan et al
[9,12; however, our wide spe
trum language has been
extended to in
lude general a
tion systems and loops
4 Inverse Engineering
Inverse engineering is the pro
ess of extra
ting
high-level abstra
t spe
i
ations from sour
e
ode using program transformations. By developing our program transformation theory in a wide spe
trum language, we are able to use transformations not only for
restru
turing at the same abstra
tion level, but also
for
rossing levels of abstra
tion: transforming from
low-level
ode to high-level abstra
t spe
i
ations (see
[23). Be
ause the transformations are proved to be
orre
t, they
an be applied without needing to understand the program rst, and with a very high degree
of
onden
e that the resulting spe
i
ations
orre
tly
represent the initial program. Similar transformations
an be used to rene the spe
i
ations (either immediately or after modi
ations and enhan
ements) ba
k
to exe
utable sour
e
ode, whi
h may be in a dierent
METAWSL en
apsulates mu
h of the expertise developed over the last 10 years of resear
h in program
transformation theory and transformation systems.
As a result, this expertise is readily available to the
programmers, some of whom have only re
ently joined
the proje
t. Working in METAWSL, it takes only
a small amount of training before new programmers
be
ome ee
tive at implementing transformations and
enhan
ing the fun
tionality of existing transformations.
6 Inverse Engineering
the Example Program
In this se
tion we show how FermaT
opes with the
example program of Se
tion 2. The rst three stages
were
arried out on a prototype of FermaT, starting with the original program and applying generalpurpose transformations. The nal stage (going to
an abstra
t spe
i
ation) was
arried out manually
be
ause the ne
essary transformations are still being
implemented in the tool.
od;
write(\Changed items:";
hanged);
end.
od;
if total 6= 0
then write(re
ords[i - 1:name; total);
hanged :=
hanged + 1
od;
write(\Changed items:";
hanged);
end.
write q;
write(\Changed items:"; `(q))
end
where
fun
t same name?(x; y)
x:name = y:name.
fun
t summarise(g)
hg[1:name; +=(:number g)i.
fun
t
hange?(g)
g[2 6= 0.
end
Our spe
i
ation may therefore be written informally
as follows:
1. First split the list of re
ords into se
tions, where
the re
ords in ea
h se
tion all have the same name;
2. Then summarise ea
h se
tion to produ
e a list of
summary pairs. The summarise fun
tion returns a
pair
onsisting of the name (
ommon to all re
ords
in the se
tion) and the sum of all the number
omponents of the re
ords;
3. Then lter out the summaries with zero totals
(these should not appear on the report);
4. The print a report
onsisting of a header, the list
of summaries (one per line) and the number of
summaries listed.
This spe
i
ation illustrates very
learly the relationship between the input data and the nal report. It
also shows expli
itly that the \Changed items" number is pre
isely the number of summaries listed, whi
h
in turn is the number of items whose sto
k has
hanged
sin
e the last report. This fa
t would require a
areful
analysis of the program plus some indu
tive reasoning
if it was to be dedu
ed from the original version of the
program.
7 Con
lusions
Our approa
h to reengineering, based on inverse
engineering followed by formal renement, has proved
very su
essful with a number of
hallenging smalls
ale
ase study programs [13,20,21,23,24. The theoreti
al work has been developed further to a
ommodate real time and parallel programs with some
su
ess [29,30. More re
ently, the development of
industrial-strength tool support has allowed us to
ta
kle large JOVIAL restru
turing proje
ts, IBM 370
Assembler restru
turing proje
ts for modules of up
to 20,000 lines, and Assembler to COBOL migration
proje
ts [26,27. Durham Software Engineering Ltd
and Durham University are a
tively developing the
tool with translators to and from various languages
(in
luding C and COBOL) whi
h will extend the migration and reengineering
apabilities of the tool. For
the Assembler restru
turing and migration proje
ts we
have developed te
hniques for uns
rambling the (often
onvoluted)
ontrol
ow between subroutines: this has
to
ope with subroutines whi
h overwrite or modify
the stored return address, routines whi
h
all other
subroutines dire
tly (without storing a return address)
and so on. In addition, we have developed te
hniques
for
hanging the data model from a monolithi
blo
k
of memory, a
essed via pointers (whi
h is essentially
how assembler
ode treats its data) to the equivalent
high-level data stru
tures and operations.
Referen
es
[1 R. J. R. Ba
k, Corre
tness Preserving Program Renements , Mathemati
al Centre Tra
ts #131, Mathematis
h Centrum, Amsterdam, 1980.
[2 R. J. R. Ba
k & J. von Wright, \Renement Con
epts
Formalised in Higher-Order Logi
," Formal Aspe
ts of
Computing 2 (1990), 247{272.
[3 G. D. Bergland, \A Guided Tour of Program Design
Methodologies," Computer 14, 18{37.
[4 R. Bird, \Le
tures on Constru
tive Fun
tional Programming," Oxford University, Te
hni
al Monograph
PRG-69, Sept., 1988.
[5 T. Bull, \An Introdu
tion to the WSL Program Transformer," Conferen
e on Software Maintenan
e 26th{
29th November 1990, San Diego (Nov., 1990).
[6 E. W. Dijkstra, A Dis
ipline of Programming , Prenti
e-Hall, Englewood Clis, NJ, 1976.
[7 E. Engeler, Formal Languages: Automata and Stru
tures , Markham, Chi
ago, 1968.
[8 I. J. Hayes, Spe
i
ation Case Studies , Prenti
e-Hall,
Englewood Clis, NJ, 1987.
[9 C. A. R. Hoare, I. J. Hayes, H. E. Jifeng, C. C. Morgan, A. W. Ros
oe, J. W. Sanders, I. H. Srensen, J.
M. Spivey & B. A. Sufrin, \Laws of Programming,"
Comm. ACM 30 (Aug., 1987), 672{686.
[10 C. B. Jones, Systemati
Software Development using
VDM , Prenti
e-Hall, Englewood Clis, NJ, 1986.
[11 C. R. Karp, Languages with Expressions of Innite
Length, North-Holland, Amsterdam, 1964.
[12 C. C. Morgan, Programming from Spe
i
ations , Prenti
e-Hall, Englewood Clis, NJ, 1994, Se
ond Edition.
[13 H. A. Priestley & M. Ward, \A Multipurpose Ba
ktra
king Algorithm," J. Symb. Comput. 18 (1994), 1{
40, hhttp: // www. dur. a
. uk/ d
s0mpw/ martin/
papers/ ba
ktr-t.ps.gzi.
[14 M. Ward, \Proving Program Renements and Transformations," Oxford University, DPhil Thesis, 1989.
[15 M. Ward, \Derivation of a Sorting Algorithm,"
Durham University, Te
hni
al Report, 1990, hhttp:
// www. dur. a
. uk/ d
s0mpw/ martin/ papers/
sorting-t.ps.gzi.
ap.ps.gzi.
[30 E. J. Younger & M. Ward, \Inverse Engineering a
simple Real Time program," J. Software Maintenan
e:
Resear
h and Pra
ti
e 6 (1993), 197{234.
Sour e
Translator
File
Transformation
Library
Internal
Representation
of WSL
ode
Stru
ture
Editor
Program
Transformer
Sele
t
Edit
View
The
Maintainer
ASCII
X-Windows
Front End
Browser
Interfa
e
High-Level
WSL to
Z
Low-Level
WSL to
Sour
e