On Partial-Function Application in Z
David A. Du y
Department of Computer Science, University of York
Heslington, York, YO10 5DD
E-mail: [email protected]
Abstract
We discuss the application of partial functions to elements outside their domain
in the context of the Z language and CADiZ to ol. We illustrate some surprising
results that can arise, and show that they may be readily justi ed, but also
show that undesirable results can arise that are less readily resolved. We o er
two p ossible resolutions, one that involves a simple change to, for example,
free typ es, and another more contentious approach that involves, in particular,
a new semantics for Z. We discuss the advantages and disadvantages of b oth
approaches.
1 Intro duction
Partial functions in Z [8] are simply relations with the prop erty that if they
\return a value" that value is unique. There is some debate as to the meaning
of the application of such functions to arguments outside their domain. Arthan
[2] outlines the main options and prop oses a classical approach for reasoning
purp oses in which a function application in a sp eci cation is assigned an
arbitrary value from the function's range in any particular interpretation \pre-
mo del" for . This semantics explains why, if f and g are partial functions
from N to N , then f 0 = f 0 holds in all mo dels, but f 0 = g 0 need not
even if b oth f and g are unde ned for 0: f 0 and g 0 may b e assigned di erent
values from the carrier set of N .
This semantics for partial-function application mayormay not b e emb ed-
ded into the standard semantics for Z currently b eing develop ed; the standard
semantics might assign the same range value to all \unde ned" function ap-
plications of the same typ e. Any pro of system that is sound with resp ect to
Arthan's prop osed semantics is, of course, sound with resp ect to the more re-
strictive semantics. CADiZ [9], a to ol develop ed at the UniversityofYork that
supp orts analysis of Z sp eci cations, is consistent with Arthan's approach in
that it accepts as valid f 0 = f 0, but not f 0 = g 0 unless forced to by
the axioms of the sp eci cation. In this pap er, we take CADiZ as representative
of pro of systems for Z such as Pro ofPower [5], with regard to the treatment
of partiality. Thus, Arthan's approach provides the starting p oint for our inves-
tigation. However, we go a step further, and consider the scenario in whichit
is imp ossible to assign a range value to a function application in any mo del. In
3rd Northern Formal Metho ds Workshop, 1998 1
other words, we consider the scenario in which adding a de nedness condition
suchasf 0 2 N would make a sp eci cation unsatis able.
1
Apparently, this scenario has b een investigated previously, and there are
unpublished notes on the sub ject, but in each case it was decided that any b en-
e ts accrued are outweighed by anomalous results. We argue that the partial-
function applications of realistic sp eci cations mayhave this prop erty, that the
apparently anomalous results they pro duce are not so disconcerting as they
might at rst seem, and that there is a ma jor b ene t from allowing them in
that the sp eci er is no longer required to know the domain of a partial function
in order to avoid contradictions when writing down axioms de ning the function.
However, we show that allowing such function applications can makea typical
Z sp eci cation inconsistent, and we pro ceed to o er two p ossible resolutions
to this problem, one involving mo di cation of our sp eci cations, and the other
involving mo di cation of our inference systems. We discuss the advantages and
disadvantages of each approach.
The presentation of the pap er is as follows. In the next section we discuss
partial functions in a general setting, indep endently of Z, and describ e a simple
realistic sp eci cation in which the de nedness problem arises. In Section 3, we
then review partial functions from the Z standp oint, illustrating some surprising
and undesirable prop erties that function application can havein Z, and show
how the surprising results may b e readily justi ed. We are then in a p osition
to present in Section 4 our resolutions to the undesirable results. The second of
these involves mo di cation to the typical laws and inference rules implemented
in reasoning systems for Z; we sp ecify the necessary changes for a subset of Z
in Section 5, provide a semantics for our Z expressions in Section 6, and justify
our pro of rules with resp ect to this semantics in Section 7. We go on to discuss
some of the consequences of b oth our resolutions in Section 8, including those
that are apparently anomalous, and argue that these apparent anomalies are
in fact reasonable. Finally,we present some conclusions on what wehave done
in Section 9, reviewing some of the advantages and disadvantages of our two
approaches.
We are concerned almost exclusively with the semantics of function appli-
cation in this pap er, and howitinteracts with the semantics of the rest of the
system. For this reason, we will consider only a limited subset of Z, omitting
discussion of, for instance, schemas; the subset should b e suciently inclusiveto
demonstrate our approach in a non-trivial context. For similar reasons, we will
also take only a semi-formal approach to the presentation of our semantics in
Section 6. Typical current formal approaches translate each Z construct into a
core language, and then de ne the semantics of the core language via a mapping
into ZF set theory. We map each Z construct of our subset into an informal
set-theoretic language with typ es. Our approach more closely resembles stan-
dard semantic de nitions given in text b o oks on logic. This approachwas found
2
not to work for the full Z language, principally due to the inclusion of schemas,
but it is adequate for our subset.
While we do not consider in detail Z-like languages such as B [1], we b elieve
that our semantics should b e applicable to them, p erhaps with minor mo di ca-
tions. In the case of B itself, wemust take account of a distinction that may
1
Sam Valentine, private communication
2
Ian Toyn, private communication
3rd Northern Formal Metho ds Workshop, 1998 2
be made between a well-formed and a well-typ ed formula. For instance, the
formula 8x x +0 = x is well-formed, but for typ e-checking purp oses wemust
add the constraint x 2 N ,say, to give 8x x 2 N x +0 = x; wemaysay that
this formula is well-typ ed. If we consider the well-typ ed language of B, then
the application of our Z semantics to B is quite straightforward, since B has
the same typ e system as Z. On the other hand, we consider later the p ossibility
of including in Z formulae that are not well-typ ed; it might b e argued that, in
principle, B already allows such expressions.
2 What are partial functions
From the mathematical p ersp ective, indep endently of Z, wemay distinguish two
typ es of partiality cf. [11].
Case 1: A function f is partial if it is not explicitly assigned any value for
certain arguments. Viewed computationally, that is, viewing the \de ning ax-
ioms" for f as, say, a set of rewrite rules if the axioms are equations, f may
or may not terminate on these arguments.
Example: Supp ose we are given
S pec1==
[f; g; h : N ! N j
8u : nats f succu = u
8v : nats g succv = g 0
8w : nats h0;w=0
8x; y : nats hsuccx;y=hsuccx y; y]:
In Sp ec1, f is not de ned for 0 and g is not de ned for any v 2 N . The function
h is non-terminating for x = 0;y = 0 if its de ning equations are treated as
rewrite rules from left to right as presented, and if, for t of typ e A , t 0
simpli es to t; since there are no other de ning axioms for hsucc0; 0, h is
unde ned over these arguments | hsucc0; 0 could, however, b e assigned the
value 0, say, without inconsistency with Sp ec1 arising.
Case 2: A function f is partial if imp osing de nedness conditions on f results
in inconsistency.
Example: Supp ose we are given
S pec2==
[q : N N ! N j
8w : nats q 0;w=0
8x; y : nats q succx;y=succq succx y; y]:
The partial function q maybeinterpreted as a \quotient" function over N ; the
imp ortant prop erty of Sp ec2 is that q succ0; 0, representing 1/0, is assigned
no numeric value. Not only is q nonterminating for x = 0;y = 0, but we
have q succ0; 0 = succq succ0; 0, so if we add the de nedness constraint
q succ0; 0 2 N , then we get 9z : N z = succz contradicting the usual
axioms for N .
Case 1 is consistent with the standard Z-user view of partial functions in
the sense that any function that satis es Case 1 could b e declared as partial in
3rd Northern Formal Metho ds Workshop, 1998 3
a Z sp eci cation without any p erceived inconsistency arising. This is the case
that Arthan accounts for [2]. Case 2 is more contentious. It might b e argued
that any function inaZspeci cation satisfying Case 2 should b e rejected as
inconsistent with its declaration. According to this view, it should always b e
p ossible to assign to any function application some value from the function's
0 0
range that is, the set S in the declaration f : S ! S for f . However, there
are several counterarguments.
Firstly, S pec2 describ es what seems to b e a reasonable sp eci cation in which
the function q cannot return a numeric value for certain arguments, though
its range is N . Secondly, since we cannot bar non-termination, it would seem
p ointless though not imp ossible for \semi-recursive" functions [6] to bar Case
2. Thirdly, and p erhaps most signi cantly of all, barring Case 2 would, from the
p ersp ective of function application, make redundant the declaration of functions
0
as partial in Z, since a partial function f : S ! S would have the prop erty
0
8x : S 9 y : S f x=y
1
0
at least if S is a typ e expression, exactly as for a total function. We discuss
this fourth justi cation for Case 2 further in the next section.
3 Partial Functions and Z
0 0
In Z, a relation r with declaration r : S $ S , for sets S and S , is a subset of
0 0 0
S S : r has typ e P T T , where P T and P T are the typ es of, resp ectively,
0 0
S and S , and the de ning axiom for r is 8p : r p:1 2 S ^ p:2 2 S . Partial
0 0
! S and f : S ! S , and total functions, with declarations of the form f : S
resp ectively, are relations with further restrictions expressed by axioms in the
language of Z. A partial function f has the extra prop erty
8p; q : f p:1= q:1 p = q;
0
while a total function f : S ! S is a partial function with the restriction that
its domain is the whole of S . These de nitions raise the question: what happ ens
when a partial function is \applied" to an ob ject outside of its domain. In fact,
Z allows arbitrary relations to b e applied as functions, and thus we cannot rely
on the sp ecial prop erty of partial functions in de ning application.
The fundamental p oint to note is that the de nition of relation imp oses only
the constraintofwell-typ edness on the application of a relation as a function,
and the de nition of partial function is only a little more restrictive. A relation
0
application r s is well-typ ed if r has typ e P T T and s has typ e T ; r s
0
then has typ e T . Partial functions imp ose the additional constraint that if
x; y 2 f is a consequence of the sp eci cation, then f x=y is a consequence;
the converse need not hold. Even total functions may be applied to ob jects
outside their domain without intro ducing inconsistency. We illustrate these
p oints with the following sp eci cations.
S pecA ==
[f : N $ N j
1; 1 2 f
f 1 = 2]
3rd Northern Formal Metho ds Workshop, 1998 4
S pecB ==
[f : N ! N j
f 1 = 0
f 2 = 2]
S pecC ==
[f : N ! N j
f 1 = 1
f 2 = 2]
These sp eci cations are \consistent" in Z in the sense that, from the axioms
currently asso ciated with relation and function declarations, we cannot derivea
contradiction P ^:P , for some P via the inference rules normally asso ciated
with Z as in CADiZ. The rst two sp eci cations illustrate the facts that a
relation may b e applied to ob jects e.g. 1 in Sp ecA for which it is not uniquely
de ned, and a partial function may b e applied to or return ob jects outside its
domain or range, as in the rst and second axioms of Sp ecB. As illustrated by
Sp ecC, even a total function may b e consistently applied to ob jects outside its
domain, and, if so, may return an ob ject outside its range. We are not claiming
that anybody would wish to write sp eci cations such as that ab ove, but if they
are consistent in Z, then we should b e able to o er, for instance, a semantical
justi cation for them.
The justi cation for these surprising prop erties of function application, that
is, the consistency of the ab ove sp eci cations, is that the axioms given assert
prop erties ab out each function application, not ab out the functions themselves.
In particular, f a=b, where f a and b are appropriately typ ed, do es not imply
a; b 2 f . Formally, in the mo dels of a sp eci cation, a function application f a
may b e assigned a total function App [2]. To account for the fact that, if there
is a unique b such that a; b 2 f , then f a=b,wemust imp ose constraints on
App as in the following de nition.
De nition: In a sp eci cation , let f b e an expression representing an n-ary
function of typ e PT T , and let ar g b e an expression of typ e T . If, in an
1 2 1
interpretation for , T and T are assigned, resp ectively, the \carrier" sets
1 2
T and T , f is assigned the function f , and ar g is assigned the value a 2 T ,
I
1 2 1
then
Appf ;a == the unique z such that a; z 2 f if suchaz exists
I I
otherwise. == some z in T
2
Thus, the value of a function application is not a value assigned to the function
itself that is, the function is not \totalised" | cf. [12], but to the application
op erator. In fact, what we assign to anyZ function symb ol partial or total
in a mo del is simply a relation satisfying the constraints imp osed by Z's partial
and total function axioms.
More problematic are sp eci cations involving de nitions such as that of q in
the previous section. The problem b ecomes most apparentifwe reexpress the
de nition in terms of a free typ e, as opp osed to N which is de ned as a subset
of the typ e A in standard Z. Wethus intro duce the free typ e nats, and de ne
the functions di and q uot in terms of nats.
3rd Northern Formal Metho ds Workshop, 1998 5
section S pecQ
nats ::= o j shhnatsii
di ; q uot : nats nats ! nats
8x : nats di x; o=x
8x; y : nats di sx;sy = di x; y
8y : nats q uoto; y =o
8x; y : nats q uotsx;y=sq uotdi sx;y;y
The rst line of SpecQ intro duces a free typ e: a given set nats together with
Peano-style axioms for o denoting zero and s denoting successor. The axioms
CADiZ asso ciates with the free typ e nats have some syntactic di erences from
typical Peano axioms for N [4], but the induction principle is typical, and the
prop erty 8x : nats :x = sx is a logical consequence.
The main diculty with Sp ecQ is that it is provably inconsistent in CADiZ.
We discuss a pro of of its inconsistency further in the next section. We note here
merely that we can prove q uotso;o 2 nats in CADiZ, and therefore that
8x; y : nats q uotsx;y=succq uotdi succx;y;y
contradicts the prop erty 8x : nats :x = sx of the free typ e nats.
There are several reasons why the inconsistency of Q is unfortunate. Firstly,
the de nition lo oks reasonable. Secondly, it is correct for the intended set,
fx; y : N j x>y^ y>0g; where N denotes the natural numb ers. Thirdly, the
fact that the de nition for q uot is nonterminating for q uotso;o is consistent
with our assumption that division of 1 by 0 is unde ned that is, is not a natural
numb er. Fourthly, and p erhaps most imp ortantly,ifwe bar this de nition, then
this means that the Z sp eci er must know the domain of q uot, b efore de ning it
by a set of equations, in order to avoid inconsistency; but in that case it would
seem preferable that q uot b e de ned as a total function from nats natsnfog
to nats. Thus, the utility of partial functions is substantially undermined if
de nitions such as that of q uot are inconsistent.
4 Resolving the Inconsistency
We prop ose two resolutions to the inconsistency issue raised in the preceding
section. The rst is the more straightforward, but involves explicitly mo difying
sp eci cations, while the second is more contentious, but leaves sp eci cations
unchanged, mo difying instead the usual laws and inference rules for Z.
4.1 Mo difying Free Typ es
The rst solution is straightforward: the sets intro duced by a free typ e e.g. nats
ab ove are not given typ e status, but are made subsets of typ es. In particular,
wemay asso ciate with the set nats the typ e nats . Thus, nats b ecomes the
given set, and nats is declared by nats : P nats . The other declarations and
axioms for nats in the original free-typ e expansion remain unchanged.
Nowany partial function f : nats ! nats may return avalue from nats
when it is not sp eci ed or is unable consistently to return a value from nats.