Leading on from the slides by Peyton Jones, this lecture sets out how constructor classes and higher-order functions can help to treat effectful computation within a pure functional language; or potentially contain its influence within an imperative one.
Haskell Resources
6 October 2010Lectures 6–8 will address some interesting uses of types in programming, with examples specifically from the Haskell language.
If you haven’t programmed in Haskell before — or you have, but Informatics 1 seems but a distant memory — then over the next week you should learn some basics of the language. The rest of this post suggests some resources, updating last year’s list with new material from students.
Read the rest of this entry »
Lecture 10: State transformers
11 February 2010Stateful computation, and how sometimes that’s just exactly what you need. Using the State
monad to organise imperative code in a functional language: writing Haskell that looks imperative, but the rest of the program sees as functional.
State
makes this possible, but it doesn’t make it happen. Refining this to the ST
monad: abstracting the state type, but adding MutVar
and MutArray
for richer state manipulation. Running stateful code: how a rank-2 polymorphic type means that runST
is sure to insulate local state from outside interference, and preserve outside code from the snares of mutable state.
This is good, and raises the bar for modularity: not just separating routines so that neither need depend on the inner workings of the other; but so that they can even independently use distinct programming paradigms. We’ll see more of this multiparadigmatic metaprogramming later in the course.
How the IO
monad is really an ST
monad, but over a very particular state.
Link: Slides
Homework
Read the following paper, on which the lecture is based:
- Lazy functional state threads. In Proceedings of the 1994 ACM SIGPLAN Conference on Programming Language Design and Implementation, SIGPLAN Notices, 29(6), pages 24–35. ACM Press, June 1994.