## Abstract

While simple equational reasoning is adequate for the calculation of many algorithms from their functional specifications, it is not up to the task of dealing with others, particularly those specified as optimisation problems. One approach is to replace functions by relations, and equational reasoning by reasoning about relational inclusion. But such a wholesale approach means one has to adopt a new and sometimes subtle language to argue about the properties of relational expressions. A more modest proposal is to generalise our powers of specification by allowing certain nondeterministic, or multi-valued functions, and to reason about refinement instead. Such functions will not appear in any final code. Refinement calculi have been studied extensively over the years and our aim in this article is just to explore the issues in a simple setting and to justify the axioms of refinement using the semantics suggested by Morris and Bunkenburg.

This is a preview of subscription content, access via your institution.

## Buying options

## References

Bird, R.S., de Moor, O.: The Algebra of Programming. Prentice-Hall International Series in Computer Science, Hemel Hempstead (1997)

Harper, R., Honsell, F., Plotkin, G.: A framework for defining logics. J. Assoc. Comput. Mach.

**40**(1), 143–184 (1993)Morris, J.M., Bunkenburg, A.: Specificational functions. ACM Trans. Program. Lang. Syst.

**21**(3), 677–701 (1999)Morris, J.M., Bunkenburg, A.: Partiality and nondeterminacy in program proofs. Formal Aspects Comput.

**10**, 76–96 (1998)Morris, J.M., Tyrrell, M.: Dually nondeterministic functions. ACM Trans. Program. Lang. Syst.

**30**(6) (2008). Article 34

## Author information

### Authors and Affiliations

### Corresponding author

## Editor information

### Editors and Affiliations

## Appendix

### Appendix

Here is the example, known as the *paragraph* problem. Consider the task of dividing a list of words into a list of lines so that each line is subject to a maximum line width of \( w \). Each line is a list of words and its width is the sum of the length of the words plus the number of inter-word spaces. There is an obvious greedy algorithm for this problem, namely to add the next word to the current line if it will fit, otherwise to start a newline with the word. For what cost function does the greedy algorithm produce a division with minimum cost?

The obvious answer is that such a division has the minimum possible number of lines. So it has, but we cannot calculate this algorithm from a specification involving \( minWith \; length \). To see why, consider a list of words whose lengths are \([\mathrm {3},\mathrm {6},\mathrm {1},\mathrm {8},\mathrm {1},\mathrm {8}]\) (the words are not important, only their lengths matter). Taking \( w \mathrel {=}\mathrm {12}\), there are four shortest possible layouts, of which two are

Let \( add \; x \; p \) be the function that adds the next word \( x \) to the end of the last line if the result will still fit into a width of \(\mathrm {12}\), or else begins a new line. In particular

We have

so the monotonicity condition fails. The situation can be redeemed by strengthening the cost function to read

In words one paragraph costs less than another if its length is shorter, or if the lengths are equal and the width of the last line is shorter. Minimising \( cost \) will also minimise \( length \). This time we do have

as can be checked by considering the various cases, so the monotonicity condition holds. However, we also have

and \( cost \; p _{2}\mathbin {<} cost \; p _{1}\), so the strong monotonicity condition (1) fails.

## Rights and permissions

## Copyright information

© 2019 Springer Nature Switzerland AG

## About this paper

### Cite this paper

Bird, R., Rabe, F. (2019). How to Calculate with Nondeterministic Functions. In: Hutton, G. (eds) Mathematics of Program Construction. MPC 2019. Lecture Notes in Computer Science(), vol 11825. Springer, Cham. https://doi.org/10.1007/978-3-030-33636-3_6

### Download citation

DOI: https://doi.org/10.1007/978-3-030-33636-3_6

Published:

Publisher Name: Springer, Cham

Print ISBN: 978-3-030-33635-6

Online ISBN: 978-3-030-33636-3

eBook Packages: Computer ScienceComputer Science (R0)