Jump to content

Partial functions

From HaskellWiki

partial function

Now, look at the function defined by the formula

y=x2.

If the input and output domains are both , again this function is well-defined.

However, what if we assume that the input and output domains are both ?

This time, we have a problem when x is odd. For example, 32 is not an integer, so our function is not defined for all of its input values.

It is a partial function, a concept that subsumes the notion of a function but is more general.

Chapter 2: Relations, Functions, Partial Functions, Jean Gallier.

The following applications of partial functions cannot be defined:

  • quot 1 0
  • rem 1 0
  • div 1 0
  • mod 1 0
  • log 0
  • head []
  • tail []

Another example is the n-body problem: which has no general solution for n > 2.

Due to the vagaries of the reality in which programs exist in, I/O is fundamentally partial. For reasons ranging from death to disconnection to disinterest, a user may not provide a timely and suitable response when prompted by a program!

Data obtained from other programs or input devices can also be partial. For example, a remote program or sensor may send values which cannot be processed by the receiving program - as far as the remote sensor or program is concerned, that is entirely the receiver's problem!

Ooh, mommy, mommy, what I have now doesn't work in this extremely unlikely circumstance, so I'll just throw it away and write something completely new.

Linus Torvalds

A more common solution is to let the receiver fail and be restarted.