05.03.2016 Views

Programming in Scala”

fpiscompanion

fpiscompanion

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter notes 12<br />

Notes on chapter 4: Handl<strong>in</strong>g errors without<br />

exceptions<br />

Partial and total functions<br />

A partial function⁴⁷, as opposed to a total function, is a function that is not def<strong>in</strong>ed for some of<br />

its <strong>in</strong>puts. That is, it’s not true for partial functions that every value of the <strong>in</strong>put type maps to a<br />

value of the output type. There are different approaches to represent<strong>in</strong>g this k<strong>in</strong>d of function, but<br />

<strong>in</strong> functional programm<strong>in</strong>g we commit to us<strong>in</strong>g only total functions. The approach we take <strong>in</strong> this<br />

chapter is to augment the return type of partial functions to make it “the same size” as the <strong>in</strong>put<br />

type.<br />

For example, the Option[T] type simply adds one data constructor, None, to the underly<strong>in</strong>g type T.<br />

That way we can have a total function of type X => Option[T] represent a partial function from X<br />

to T. Wherever the function is not def<strong>in</strong>ed for a given X, we simply map that value to None <strong>in</strong> the<br />

result type.<br />

Tur<strong>in</strong>g completeness and the halt<strong>in</strong>g problem<br />

Another possibility <strong>in</strong> languages that are Tur<strong>in</strong>g complete⁴⁸, which <strong>in</strong>cludes Scala, is that a function<br />

may “hang” or “run forever” <strong>in</strong> an <strong>in</strong>f<strong>in</strong>ite loop. Functions that do this are also considered to be<br />

partial. However, <strong>in</strong> a language like Scala we cannot prevent this k<strong>in</strong>d of partiality nor can we<br />

recover from it <strong>in</strong> the general case. It is equivalent to the halt<strong>in</strong>g problem⁴⁹.<br />

Bottoms<br />

A program that hangs (by runn<strong>in</strong>g <strong>in</strong> an <strong>in</strong>f<strong>in</strong>ite loop) or crashes (by throw<strong>in</strong>g an exception) is said<br />

to “evaluate to bottom”. The term “bottom” (sometimes “falsum”) comes from logic, where it is used<br />

to denote a contradiction. The Curry-Howard isomorphism⁵⁰ says that types are propositions, and<br />

a program of a given type is a proof of that proposition. S<strong>in</strong>ce a non-term<strong>in</strong>at<strong>in</strong>g program can have<br />

any type, we can say that it’s a proof of every proposition. This is exactly like the situation with<br />

contradictions <strong>in</strong> logic–if you assume a contradiction, you can prove any proposition.<br />

Total languages<br />

Some programm<strong>in</strong>g languages are total languages⁵¹, and do not provide facilities for unbounded<br />

recursion like Scala does. A program <strong>in</strong> such a language provably term<strong>in</strong>ates, and there are no<br />

⁴⁷http://en.wikipedia.org/wiki/Partial_function<br />

⁴⁸http://en.wikipedia.org/wiki/Tur<strong>in</strong>g-complete<br />

⁴⁹http://en.wikipedia.org/wiki/Halt<strong>in</strong>g_problem<br />

⁵⁰http://en.wikipedia.org/wiki/Curry%E2%80%93Howard_correspondence<br />

⁵¹http://en.wikipedia.org/wiki/Total_functional_programm<strong>in</strong>g

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!