Lecture 8: Monads and I/O

15 October 2010

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.

Read the rest of this entry »


Haskell Resources

6 October 2010

Lectures 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 2010

Stateful 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.

Lecture 9: Monads and I/O

8 February 2010

Organising imperative programs within a purely functional language; the identification of monads as a general notion of “computation”. Examples: exceptions, mutable store, input, output, nondeterminism, environment, … Metaprogramming: once you have code encapsulated in a monad, you can do more than just execute it.

Haskell do-syntax as sugar for monadic computation. How the IO monad took over the world of interactive functional programs; and how it can be efficiently implemented by an audacious one-off optimisation. Challenges with monads, and future directions for development.

Link: Slides

Homework

Find an online tutorial or other explanation of monads in programming, and post a link on the blog. Write a comment reviewing whether you found the explanation helpful, or otherwise. Bonus points if the language is not Haskell.

Read the following paper, which introduced the IO monad:

  • Simon L Peyton Jones and Phil Wadler. Imperative Functional Programming. In Conference Record of the Twentieth Annual ACM Symposium on Principles of Programming Languages, POPL ’93, pages 71–84. ACM Press, 1993

Optional: The final slides from the lecture contain some further references. If you are interested in learning more about programming with monads, try the awkward squad tutorial.