Skip to main content

How to Calculate with Nondeterministic Functions

Part of the Lecture Notes in Computer Science book series (LNPSE,volume 11825)


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

USD   29.95
Price excludes VAT (USA)
  • DOI: 10.1007/978-3-030-33636-3_6
  • Chapter length: 17 pages
  • Instant PDF download
  • Readable on all devices
  • Own it forever
  • Exclusive offer for individuals only
  • Tax calculation will be finalised during checkout
USD   69.99
Price excludes VAT (USA)
  • ISBN: 978-3-030-33636-3
  • Instant PDF download
  • Readable on all devices
  • Own it forever
  • Exclusive offer for individuals only
  • Tax calculation will be finalised during checkout
Softcover Book
USD   89.99
Price excludes VAT (USA)


  1. 1.


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

    Google Scholar 

  2. Harper, R., Honsell, F., Plotkin, G.: A framework for defining logics. J. Assoc. Comput. Mach. 40(1), 143–184 (1993)

    CrossRef  MathSciNet  Google Scholar 

  3. Morris, J.M., Bunkenburg, A.: Specificational functions. ACM Trans. Program. Lang. Syst. 21(3), 677–701 (1999)

    CrossRef  Google Scholar 

  4. Morris, J.M., Bunkenburg, A.: Partiality and nondeterminacy in program proofs. Formal Aspects Comput. 10, 76–96 (1998)

    CrossRef  Google Scholar 

  5. Morris, J.M., Tyrrell, M.: Dually nondeterministic functions. ACM Trans. Program. Lang. Syst. 30(6) (2008). Article 34

    CrossRef  Google Scholar 

Download references

Author information

Authors and Affiliations


Corresponding author

Correspondence to Richard Bird .

Editor information

Editors and Affiliations



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

figure ax

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

figure ay

We have

figure az

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

figure ba

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

figure bb

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

figure bc

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

Rights and permissions

Reprints and Permissions

Copyright information

© 2019 Springer Nature Switzerland AG

About this paper

Verify currency and authenticity via CrossMark

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.

Download citation

  • DOI:

  • 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)