CADiZ

Reference manual / Extended toolkit / section numdefs


Numbers

section numdefs

This contains the CADi\num version of the definitions of numbers in the mathematical toolkit of Z.

Written by Sam Valentine

Last updated September 1999

A Single Type

The Z community includes a wide spectrum of attitudes to numbers. At one extreme are those who require nothing more abstruse than the natural numbers, and who do not wish to be reminded that anything more complicated exists. At the other extreme are people who are concerned with real and complex floating-point numbers as implemented by particular hardware and software.

The reconciliation of these needs is constrained by the fact that Z does not allow overloading of its operators, so there can be only one addition function, and so on. Furthermore, all users who want to use addition want to use the same familiar operator, _ + _, to do so.

This has led to the decision to adopt a single numeric type, whose whole extent is deliberately left unspecified, but of which all practical versions of numbers are considered to be subsets. This is to be denoted by \arithmos, which can be pronounced ``arithmos''.

Core, Prelude and Toolkit

The description, and usually the implementation of Z are layered into a core language and a mathematical toolkit. Spivey's Reference Manual suggested that everything about numbers could go into the toolkit, but did not spell out the detail.

The trouble with putting numbers wholly into the toolkit is that numbers can appear in the concrete syntax as literals, and also positive integers are generated to form the domain elements of sequence displays, and the ranges of bags. The difficulty of finding a solution to these problems has led to a compromise proposal, whereby natural numbers are given just enough description in the core language to support the definition of natural number literals. Definition of wider sets of numbers is then left to the toolkit.

Rather than specify the required properties of natural numbers incomprehensibly in the core metalanguage, we propose that there should be a ``prelude'', which resembles the core language in being fixed, but is specified in Z like the toolkit.

Subsets of the Reals and their Representations

We here confine our attention to the real numbers, ignoring things like complex numbers, quaternions, and so on. These wider sorts of number could be defined within the framework we are developing here, but we ignore them for now, because there is no pressing need, and the way we develop the theory of the reals should make it clear enough how to go further.

From the natural number base we have described, we develop the theory in the well-known ways:

The things we want to do with our real numbers include the following:

function 40 leftassoc (_ * _)

function 40 leftassoc (_ \divides _)

function 30 leftassoc (_ - _)

The mark-up directives (not typeset) and operator templates above are to assist in allowing us to use, from here on, a multiplication infix function called _ * _, a subtraction infix function called _ - _, a unary negation function called -, and a real division infix function called _ \divides _.

Our presentations of numbers are then as follows:

  1. Any natural number can be represented as a string of digits,

    as described above. This is unique apart from the optional presence of leading zeroes. We can therefore take as the canonical form of a positive integer its representation as a string of digits where the first digit is not 0. Nevertheless, all equivalent representations are equally valid. Thus 7, 007, 49 - 42, 42 \divides 6, \mu x: \nat | x * x = 49 are all equally valid ways of representing the same integer value, although a tool should take the first of them as the preferred one to create, if it has enough information to be able to make the choice.

  2. In a similar way, we can represent all rational numbers.

    Their canonical form is defined as follows:

    1. if the number is equal to 0, the canonical form is 0.
    2. if and only if the number is negative, begin with a single application

      of -, then give the representation of the absolute value as follows.

    3. If the number is a positive integer,

      give its digit string as described above.

    4. If the number is a positive rational non-integer, give it in the form

      numerator \divides denominator, where each of those is described in the canonical form of a natural number, and they have no common factor.

    Again, all representations are equally valid. Thus - 1 \divides 3, - (- 2 \divides - 6), 2 * - 7 \divides 42 are all equally valid ways of representing the same rational value, although a tool should preferably use the first of them.

  3. For irrational values there is no canonical form.

    We represent them as best we can, and a tool may simplify up to a point. All representations are equally valid.

Note that it is not necessary to provide for the representation of numbers using decimal points and explicit decimal exponents as is usual in programming languages. It is enough to write 1.23 as 123 \divides 100, and so on. Note also that the arithmetic we define will be exact. If we want to model the operation of some computer floating-point arithmetic, for example, we can define that separately in terms of the exact arithmetic basis.

To summarise this section:

  • In general numbers are represented by expressions;
  • Natural numbers can be represented by digit strings;
  • For rational numbers there is a canonical simplest expression

    which describes its value.

  • Built-In Inference Rules in the CADi\num  Tool

    In CADi\num  there are currently two built-in rules of inference with specific recognition of numbers, namely

    1. the Presburger decision procedure;
    2. an ``evaluation'' procedure.

    Both of these are subject to change, but they are nevertheless well representative of the sort of support which one wishes a practical tool to give to numeric work in Z. Both now apply to rationals,

    Presburger is a decision procedure. Its result take the form of true or false. The choice of output format is therefore irrelevant in this case.

    The evaluation procedure produces output, and so in that case we need to decide the output format. For natural number results, we can assume that the procedure can generate the appropriate digit string. It is probably safe to assume that the evaluation procedure cannot generate fractional results except where there was a division in the input, and similarly cannot generate negative results except where there was a subtraction or a negation in the input. These divisions and subtractions must be described using operators defined in the toolkit. If we know enough about them to make correct sense of the input, we have enough to generate the correct output. The question of output format thus reduces to the question of soundness.

    A built-in procedure which makes reference to functions and relations defined in the toolkit can be proved sound in two ways. One way would be that all its actions were parametrised to make use of the toolkit definitions for the time being in force. This would be ideal theoretically, but hardly practicable. The other approach is to prove the actions sound with respect to a particular toolkit, and only allow those actions in the presence of that toolkit. This seems to be the only viable solution.

    Thus the urgent need is to define the basic rational arithmetic operations formally, and to show, at least semi-formally, the soundness of the built-in procedures with respect to these definitions.

    The Form of the Definitions Required

    As we have just said, one of the purposes of the definitions of the arithmetic functions and relations is to provide a base for verification of the built-in procedures.

    The other issues which arise here are:

    1. Can these definitions use the full toolkit,

      or is it better that they should stand alone? The answer must be that it is preferable that they stand alone, so that the built-in functions depend as little as possible on other definitions, and are as widely as possible usable.

    2. Are these definitions to be constructive, so that the proof of

      their model conjecture is implicit in the form of the definition, or is it enough to describe them and to prove the consistency of the model separately? My answer is that since the properties which we want numbers to have are very familiar, and since it is still not completely clear precisely what form the logical foundations of Z take, it is more sensible to create a structure which one is sure is right, but expressed in a way most convenient for its other purposes, and rely on the fact that a proof of its consistency must be possible somehow.

    3. Are the definitions to be framed in such a way as to facilitate

      the checking of the built-in procedures which they are to verify? The answer is that this is not strictly required, but would be helpful, and that doing it with this in view may well help to create a form of definition which is generally useful anyway.

    The total amount of defining text is fairly large, but we stratify it into what are, it is hoped, manageable chunks as follows.

    The development introduces positive rationals before it introduces subtraction. This ordering is chosen because:

    1. technically it is at least as easy as the other order;
    2. it corresponds to the probable structure of the

      implementation of rational arithmetic.

    It is also, interestingly, the order in which these concepts were developed historically.

    The prelude

    The "prelude" contains the definition of arithmos, the natural numbers, and the addition function on the natural numbers

    Define succ for Succers

    succ == \lambda x: \nat @ x + 1

    Define the Positive Integers

    \nat1 == { x: \nat @ x + 1 }

    Define Multiplication on the Natural Numbers


    _ * _ : \power ((\arithmos \cross \arithmos) \cross \arithmos)
    \where
    \forall m, n: \nat @ \exists1 p : ( _ * _ ) @ p.1 = (m, n)
    \forall m: \nat @ m * 0 = 0
    \forall m, n: \nat @ m * (n + 1) = m * n + m

    Define the Fractions

    We introduce the set of positive rational numbers, \rat+, which we can pronounce as the ``fractions''. The arithmetic of \nat1 is embedded in that of \rat+, and the latter is also extended to make it an Abelian group under _ * _.


    \rat+ : \power \arithmos
    _ \divides _ : \power ((\arithmos \cross \arithmos) \cross \arithmos)
    \where
    "the functions are total and closed"
    \forall x, y: \rat+ @ \exists1 p: (_ \divides _) @ p . 1 = (x, y)
    \forall x, y: \rat+ @ \exists1 p: (_ + _) @ p . 1 = (x, y)
    \forall x, y: \rat+ @ \exists1 p: (_ * _) @ p . 1 = (x, y)
    \forall x, y: \rat+ @ x \divides y \in \rat+
    \forall x, y: \rat+ @ x + y \in \rat+
    \forall x, y: \rat+ @ x * y \in \rat+
    "define the embedding and the extension"
    \forall x: \nat1 @ x \in \rat+
    \forall z: \rat+ @ \exists x, y: \nat1 @ z = x \divides y
    "make it an Abelian group under multiplication"
    \forall x, y, z: \rat+ @ z = x \divides y \iff z * y = x
    \forall x, y, z: \rat+ @ x * (y * z) = (x * y) * z
    \forall x, y: \rat+ @ x * y = y * x
    \forall x, y, z: \rat+ @ x * (y + z) = (x * y) + (x * z)

    Define the Rationals

    Embed \rat+ in the rationals, \rat, and extend to make \rat into a field.


    \rat: \power \arithmos
    - : \power (\arithmos \cross \arithmos)
    _ - _ : \power ((\arithmos \cross \arithmos) \cross \arithmos)
    \where
    "the functions are total and closed"
    \forall x, y: \rat @ \exists1 p: (_ - _) @ p . 1 = (x, y)
    \forall x, y: \rat | \lnot y = 0 @
    \exists1 p: (_ \divides _) @ p . 1 = (x, y)
    \forall x, y: \rat @ \exists1 p: (_ + _) @ p . 1 = (x, y)
    \forall x, y: \rat @ \exists1 p: (_ * _) @ p . 1 = (x, y)
    \forall x: \rat @ \exists1 p: - @ p . 1 = x
    \forall x, y: \rat @ x - y \in \rat
    \forall x, y: \rat | \lnot y = 0 @ x \divides y \in \rat
    \forall x, y: \rat @ x + y \in \rat
    \forall x, y: \rat @ x * y \in \rat
    "define the embedding and the extension"
    \forall x: \rat+ @ x \in \rat
    \forall z: \rat @ \exists x, y: \rat+ @ z = x - y
    "define enough properties to guarantee a field"
    \forall x, y, z: \rat @ z = x - y \iff z + y = x
    \forall x, y, z: \rat @ x + (y + z) = (x + y) + z
    \forall x, y, z: \rat | \lnot y = 0 @
    z = x \divides y \iff z * y = x
    \forall x, y, z: \rat @ x * (y * z) = (x * y) * z
    \forall x, y: \rat @ x * y = y * x
    \forall x, y, z: \rat @ x * (y + z) = (x * y) + (x * z)
    \forall x: \rat @ - x = 0 - x

    Define Ordering on the Rationals

    relation (_ < _)


    _ < _ : \power (\arithmos \cross \arithmos)
    \where
    "< is an irreflexive total order"
    \forall x: \rat @ \lnot x < x
    \forall x, y: \rat @ x = y \lor x < y \lor y < x
    \forall x, y, z: \rat | x < y \land y < z @ x < z
    "define how it is mapped onto the numbers"
    \forall x: \nat @ x < x + 1
    \forall x, y, z: \rat+ @ x < y \iff x * z < y * z
    \forall x, y: \rat @ x < y \iff \exists z: \rat+ @ x + z = y

    Define the Real Numbers

    Embed the ordered field of rationals into the reals, which have the property of completeness, where least upper bounds always exist.


    \real: \power \arithmos
    \where
    "the functions are total and closed"
    \forall x, y: \real @ \exists1 p: (_ - _) @ p . 1 = (x, y)
    \forall x, y: \real | \lnot y = 0 @
    \exists1 p: (_ \divides _) @ p . 1 = (x, y)
    \forall x, y: \real @ \exists1 p: (_ + _) @ p . 1 = (x, y)
    \forall x, y: \real @ \exists1 p: (_ * _) @ p . 1 = (x, y)
    \forall x: \real @ \exists1 p: - @ p . 1 = x
    \forall x, y: \real @ x - y \in \real
    \forall x, y: \real | \lnot y = 0 @ x \divides y \in \real
    \forall x, y: \real @ x + y \in \real
    \forall x, y: \real @ x * y \in \real
    "< is an irreflexive total order"
    \forall x: \real @ \lnot x < x
    \forall x, y: \real @ x = y \lor x < y \lor y < x
    \forall x, y, z: \real | x < y \land y < z @ x < z
    "define the embedding"
    \forall x: \rat @ x \in \real
    "define completeness"
    \forall S: \power \real | \exists z: S; b: \real @ \forall x: S @ x < b @
        \exists1 m: \real | \forall x: S @ x < m \lor x = m @
        \forall n: \real | n < m @ \exists x: S @ n < x
    "confirm field properties on the wider set"
    \forall x, y, z: \real @ z = x - y \iff z + y = x
    \forall x, y, z: \real | \lnot y = 0 @
    z = x \divides y \iff z * y = x
    \forall x, y, z: \real @ x < y \iff x + z < y + z
    \forall x, y: \real @ x + y = y + x
    \forall x, y, z: \real | 0 < z @ x < y \iff x * z < y * z
    \forall x, y: \real @ x * y = y * x
    \forall x: \real @ - x = 0 - x

    Define the Remaining Comparison Relations

    relation (_ \leq _)
    relation (_ \geq _)
    relation (_ > _)


    _ \leq _ : \power (\arithmos \cross \arithmos)
    _ \geq _ : \power (\arithmos \cross \arithmos)
    _ > _ : \power (\arithmos \cross \arithmos)
    \where
    \forall x, y: \real @ x \leq y \iff x = y \lor x < y
    \forall x, y: \real @ x > y \iff y < x
    \forall x, y: \real @ x \geq y \iff x = y \lor x > y

    Define the Integers and Integer Divide Functions

    \num == { x: \rat; y, z: \nat | x + y = z @ x }

    \num1 == { x: \num | \lnot x = 0 }

    function 40 leftassoc (_ div _)
    function 40 leftassoc (_ mod _)


    _ div _ : \power ((\arithmos \cross \arithmos) \cross \arithmos)
    _ mod _ : \power ((\arithmos \cross \arithmos) \cross \arithmos)
    \where
    \forall x, y: \real | \lnot y = 0 @
    \exists1 p: (_ div _) @ p . 1 = (x, y)
    \forall x, y: \real | \lnot y = 0 @
    \exists1 p: (_ mod _) @ p . 1 = (x, y)
    \forall x, y: \real | \lnot y = 0 @ x div y \in \num
    \forall x, y: \real | \lnot y = 0 @ x mod y \in \real
    \forall x, y: \real | \lnot y = 0 @ x = (x div y) * y + x mod y
    \forall x, y: \real | 0 < y @ 0 \leq x mod y
    \forall x, y: \real | 0 < y @ x mod y < y
    \forall x, y: \real | y < 0 @ x mod y \leq 0
    \forall x, y: \real | y < 0 @ y < x mod y

    A Pick'n'Mix Approach

    The structure of definitions given above is readily adaptable for different needs:

    1. in contexts where all numbers are, or can be assumed to be,

      rational, the rest of the reals can be ignored. To do this, replace the section ``Define the Real Numbers'' with the definition \real == \rat, and leave everything else the same.

    2. to prune down to support of integers only, replace the section

      ``Define the Fractions'' with the definition \rat+ == \nat1, delete all predicates containing _ \divides _ from ``Define the Rationals'' and replace the section ``Define the Real Numbers'' with the definition \real == \rat.

    3. to prune down to support of natural numbers only,

      replace the section ``Define the Fractions'' with the definition \rat+ == \nat1, replace the section ``Define the Rationals'' with \rat == \nat, and replace the section ``Define the Real Numbers'' with the definition \real == \nat.

    Related and Supporting Literature

    This material has been worked over several times in the recent past. A constructive definition of numbers, building the whole theory up from free type definitions, is given in ``Putting Numbers into the Mathematical Toolkit'', by S. H. Valentine, in Proceedings of the Seventh Z User Meeting December 1992, edited Bowen and Nicholls, published Springer 1993.

    The same material also appears in ``Enhancing the Z Mathematical Toolkit'', by Sam Valentine, University of Brighton Department of Computing Technical Report UBC 93/3, February 1993.

    The algebraic approach, where we describe only the properties we require, was issued as document 172 of the Z Standards Panel, entitled Mathematical Tool-kit Draft 3, March 1995.

    The same material also appeared as ``An Algebraic Introduction of Real Numbers into Z'', by S. H. Valentine, published in Proceedings of Conference ``Z Twenty Years On'' in Nantes, October 1995, proceedings edited by H Habrias.

    The document ``Arithmetic for Z'', by Rob Arthan, Z Standards Panel document D-188, is another treatment of the algebraic approach, differing in style of presentation from the other documents, but not in substantive content.

    The presentation here is algebraic. It differs from the previous versions in the following respects

    1. It has been written without any use of other toolkit definitions.
    2. It has been structured so as to facilitate the ``pick'n'mix''

      approach.

    3. It has been written in a style intended to facilitate proof,

      as guided by experience with proof attempts in CADi\num.

    Conclusions

    This material is primarily a draft specification of the numeric toolkit which is to be included in CADi\num. All the definitions given here should be included. The definitions of the rationals will be used to verify the built-in numeric inference rules of CADi\num.

    A description of computer arithmetic on the lines of that given by Geoff Barrett in his monograph ``Formal Methods Applied to a Floating-point Number System'' (Oxford 1987) can now be undertaken. Barrett's work was as right as could reasonably be attempted at the time, but simply assumed the existence and properties of real numbers without definition. No Z proof tool was available to him, and his proof style now seems very informal, as indeed does some of his specification.


    IT 5-Jan-2002