Julie Moronuki
2018-07-18

Finishing the first course

The first course I planned for Type Classes is called the Validation course. Since one of the first things people want to know about Haskell is what monads are and why they are, I had tried to answer that question in a blog post called The Nesting Instinct.The Nesting Instinct on Julie’s blog.

I had written that post for non-Haskellers in particular, but the original motivation and code came from materials I developed for the Austin Haskell Users meetup. So I knew it could be expanded. The result is a nine-lesson course with a total of 2.5 hours of video.

What I like about this course is that it starts from a simple idea and with very little Haskell knowledge. Over time, together, we refactor and extend the code we wrote in our first pass at solving our problem – checking some user inputs against our requirements for passwords.The password requirements we use as examples resemble ones you might see in the wild, but are for demonstration purposes only; please do not impose such restrictions upon your users!

In doing that, we have the opportunity to see what some of Haskell’s most important aspects do and how they tie together.

What’s included

We start from conditional expressions, because nearly everyone understands those already. We then work extensively with case expressions, which are similar to conditionals. We refactor those with Maybe and Either, which gives us experience working with two common monads.

Up to this point, other than Maybe and Either, we’ve been mostly working with the String type, but as we want to extend the code to differentiate password inputs, username inputs, and error messages, we have the opportunity to use newtypes and also write a product type that makes a User from a Username and Password. Adding those types gives us a measure more type safety, and it also gives us an opportunity to learn a few things about typeclasses and how an instance relates a type to a typeclass. We also, in making a User from our validated inputs, gain experience with applicative functors.

Next we would like to add an ability to accumulate error messages in a way that the Either monad cannot do, so we switch to a type nearly identical to Either called Validation. Validation is an applicative functor that allows error messages to accumulate on the left. Here we learn the key differences between applicative functors and monads as well as find out about semigroups and what they’re for. We even write a Semigroup instance for one of our newtypes.

Finally, we would like to present the results, whether they are messages of success or of failure, in a readable, user-friendly manner, so that gives us an opportunity to learn about coercing a newtype to its underlying type.

Since this all builds up gradually in the course of solving one relatively small problem at a time, I think the course gives a good sense to an early Haskeller of how different bits of the language fit together and form a whole.

What’s next

This course took longer than expected because we had a number of technical issues to sort out, I got bronchitis and couldn’t talk, and then we went to Europe to teach for a couple of weeks. We’re grateful for the patience of our early subscribers in this regard and believe the final result is a good, solid introduction to important concepts in Haskell.

Chris will lead the next course, which goes from a low-level explanation of HTTP to a discussion of what the abstractions that Haskell libraries provide for web servers. My next project is a command-line app to address a problem that many of us experience with Twitter. Stay tuned!