Having decided that computer science isn’t really my thing (I prefer it as a hobby, rather than a career), I’m now training to be a teacher and, as such, spend a certain amount of time thinking about ways to present material. One of the topics that is of no small interest to me is the teaching of programming, as distinct from teaching programming languages.
I spent some of the 2.5 hour bus trip back from classes in Hobart today thinking about ways to present monads. As a result, I’ve decided to write my very own version of that mainstay of the Haskell community – Yet Another Monads Tutorial. The goals of this project are twofold:
to try to solidify my understanding of monads, their uses and theory; and
to present programming in a way that very few or, more likely, no students will experience before advanced undergraduate CS classes.
My current thinking is to introduce functional programming in terms of
expression reduction (using Hugs or, maybe, GHCi).
This will segue into a discussion of evaluation semantics (specifically,
lazy evaluation) which will raise the problem of sequenced computations.
Monad
s (as we already know) provide a solution to this problem which
will be demonstrated with some work in the IO
monad. To really
understand how Monad
s work in Haskell, we’ll have to take a brief
detour through type classes before we look at defining our own instances
of Monad
.
The main example (and the only one that I’ve thought most about) will be
a Monad
that allows us to compose transformation matrices for 3D work
in monadic style. In this Monad
, we’ll be able to create a
transformation matrix by doing something like:
= do
myTransform
identityMatrix;Z 90;
rotateAbout X 1.5;
scale Y 20 translate
rather than explicitly creating the matrix for each transformation and
multiplying them together or, worse, poking at the individual members of
the transformation matrix. Furthermore, this example lends itself quite
well to extension. Instead of calculating a matrix which performs the
appropriate transformations, we might instead create an data-structure
encoding the calculation which will, when evaluated, yield such a
matrix. This data-structure could then be evaluated (like, if I
understand correctly runST does for the ST
monad) or it could be
subjected to symbolic manipulation (perhaps allowing the students to
implement a simple optimiser).
This will probably turn into my major project for next semester (developing plans and materials for an entire unit). If it does, I’ll probably make it available online somewhere.