Programming Haskell

Its about time we got some job done in Haskell, eh? Now, one of my favourite programming books as an undergraduate was the Camel Book, “Programming Perl”. It was full of lots of practical examples of Perl code, written well. (And I’m grateful to Larry Wall, Tom Christiansen and Randal Schwartz for writing the book that made programming fun).

So what would it look like if we wrote a Haskell tutorial in this style? Let’s have at it!

Getting started

Like some languages Haskell can be both compiled and interpreted. The most widely used implementation of Haskell currently is GHC, which provides both an optimising native code compiler, and an interactive bytecode interpreter. I’ll be using GHC (or its interactive front end, GHCi, for all code. So grab a copy of GHC now, from your package system, or the GHC home page.

Start up GHCi:

    $ ghci
       ___         ___ _
      / _  /  // __(_)
     / /_// /_/ / /  | |      GHC Interactive, version 6.6, for Haskell 98.
    / /_\/ __  / /___| |      http://www.haskell.org/ghc/
    ____// /_/____/|_|      Type :? for help.

    Loading package base ... linking ... done.
    Prelude>

The interpreter now waits for your code fragments. The “Prelude” prompt indicates which library modules are in scope, and in this case, only the basic language module, known as the Prelude.

Now we can start running Haskell code.

    Prelude> "G'day, world!"
    "G'day, world!"

    Prelude> putStrLn "G'day, world!"
    G'day, world!

You can compile this code to a native binary using GHC, by writing in a source file:

    main = putStrLn "G'day, world!"

and then compiling the source to native code. Assuming your file is A.hs:

    $ ghc A.hs

This produces a new executable, ./a.out (a.out.exe on windows), which you can run like any other program on your system:

    $ ./a.out 
    G'day, world!

Variables

We can name arbitrary fragments of Haskell using variables. Like so:

    phrase = "G'day, world!"
    main = putStrLn phrase

We don’t have to define what type phrase is, as Haskell uses type inference to infer at compile time the types of all expressions in the program. As “G’day, world!” is a string, so must phrase be a string. There are a bunch of basic types of values to play with. Here’s a small sample:

    answer      = 42
    pi          = 3.141592653589793238462643383279502884197169399375105820974944592
    avocados    = 6.02e23
    pet         = "Lambda"
    sign        = "I love my " ++ pet
    coat        = "It costs $100"
    hence       = "whence"
    thence      = hence
    moles       = 2.5
    x           = moles * avocados
    c           = '#'
    pair        = (2.5, "lambdas")
    list        = [5,6,4,3,1]
    options     = Just "done"
    failed      = Nothing
    void        = ()

One important thing to remember is that Haskell’s variables, like in most functional programming languages, are like variables in mathematics, and are just names for expressions. They’re explicitly not mutable boxes, like in most imperative programming languages. As a result, you never need to worry about initialising a Haskell variable, nor do you need to worry about the current value in a variable: it always has the same value, and can always be replaced with its definition. So the following behaves just like it would in maths:

    answer      = 42
    another     = answer + 1
    more        = another + answer
    main        = print more

That is,

    $ ghc A.hs
    $ ./a.out 
    85

Now, since variables are just names for program fragments, you can evaluate Haskell on paper by replacing all names with their definition, until you reach a final value, like so:

    main = print more 
  =>
    main = print (another + answer)
  =>
    main = print ((answer + 1) + answer)
  =>
    main = print ((answer + 1) + 42)
  =>
    main = print ((42 + 1) + 42)
  =>
    main = print (43 + 42)
  =>
    main = print 85
  =>
    85

Having such a simple system for variables allows for a wide range of interesting optimisations, and makes understanding what a program is doing at any point much easier, since you don’t have to worry about what state a variable might currently be in. (Of course, some problems need (theadsafe) mutable boxes, and they’re available as a library for when you need that).

Collections

Often you need to collect a bunch of values together into some kind of collection. Haskell has many many collection types, but in particular, it has lists and finite maps, which operate much like arrays and hashes of the imperative world.

Lists

A list is just an ordered, um, list of values. They can be nested, and transformed in all sorts of ways, using functions. Assuming your file, A.hs, contains:

    home  = ["couch", "chair", "table", "stove"]

We can play around with this stuff like so:

    $ ghci A.hs

    *Main> home
    ["couch","chair","table","stove"]

    *Main> head home
    "couch"

    *Main> tail home
    ["chair","table","stove"]

    *Main> last home  
    "stove"

    *Main> home !! 2
    "table"

    *Main> reverse home
    ["stove","table","chair","couch"]

    *Main> map reverse home
    ["hcuoc","riahc","elbat","evots"]

Loading in the List library gives us some more functions to use:

    *Main> :m + Data.List

    *Main Data.List> intersperse "#" home
    ["couch","#","chair","#","table","#","stove"]

    *Main Data.List> concat (intersperse "#" home)
    "couch#chair#table#stove"

    *Main Data.List> home \ ["table","stove"]
    ["couch","chair"]

Finite Maps

Finite maps (or maps) are the lookup tables of purely functional programming. Whenever you’d use some kind of hash in an imperative language, you can replace it with a Map in Haskell. Like hashes, maps can be seen as a table of pairs of keys and values. You can declare a new map:

    import Data.Map

    days = fromList
        [ ("Sun",  "Sunday"     )
        , ("Mon",  "Monday"     )
        , ("Tue",  "Tuesday"    )
        , ("Wed",  "Wednesday"  )
        , ("Thu",  "Thursday"   )
        , ("Fri",  "Friday"     )
        , ("Sat",  "Saturday"   ) ]

You can also convert a map to a list, using (well, duh!) toList:

    *Main> toList days
    [("Fri","Friday"),("Mon","Monday"),("Sat","Saturday"),("Sun","Sunday"),("Thu","Thursday"),("Tue","Tuesday"),("Wed","Wednesday")]

Note that they come out unordered, just like in hashes. If you just want the keys of the map:

    *Main> keys days
    ["Fri","Mon","Sat","Sun","Thu","Tue","Wed"]

    *Main> elems days
    ["Friday","Monday","Saturday","Sunday","Thursday","Tuesday","Wednesday"]

Since maps are a good structure for looking up values, you can search them using the lookup function. This function returns the element, if found:

    *Main> Data.Map.lookup "Tue" days
    "Tuesday"

Since the name ‘lookup’ is also used by a list function of similar purpose in the Prelude, we use the qualified name here to disambiguate which ‘lookup’ to use.

On failure

But what happens if the key is not found? (Feel free to skip this section if you don’t care about errors yet) lookup will then fail, and how it fails depends on what kind of failure you want. Haskell goes to great lengths to make programming for failure flexible. For example, to fail with an exception:

    *Main> Data.Map.lookup "Thor" days
    *** Exception: user error (Data.Map.lookup: Key not found)

Which is the same as failing with an IO error. We can specify this specifically with a type annotation, to say “fail with an IO error”:

    *Main> Data.Map.lookup "Thor" days :: IO String
    *** Exception: user error (Data.Map.lookup: Key not found)

Often you might instead prefer that some special value is returned on failure:

    *Main> Data.Map.lookup "Thors" days :: Maybe String
    Nothing

Maybe you’d just like an empty list:

    *Main> Data.Map.lookup "Thor" days :: [String]
    []

Finally, you can always provide an explicit default value:

    *Main> findWithDefault "Not found" "Thor" days            
    "Not found"

Failure is entirely under your control!

Actions

Now, real programs interact with the outside world. They call functions which do IO, as a side effect, and then return some value. In Haskell, functions with side effects are often called actions, to distinguish them from normal Haskell functions (which behave like mathematical functions: they take inputs and return a result, with no side effects). Programming with side effects is carefully handled in Haskell, again to control the possibility of errors, and all functions which have side effects have a special type: the IO type.

For example, the function to print a string has the following type (and you can ask the interpreter for the type interactively):

    Prelude> :t putStr
    putStr :: String -> IO ()

which tells you that this function takes a String as an argument, does some IO side effect, and returns the null value. It is equivalent to the following C type:

    void putStr(char *);

but with a bit of extra information, namely, that the function does some IO. We would print out some element of our map like so:

    main = print ("Tue in long form is " ++ findWithDefault "Not found" "Tue" days)

    *Main> main
    "Tue in long form is Tuesday"

An example

One of the classic programming puzzles for introducing real world problems is the ‘class grade’ problem. You have a text file containing a list of student names and their grades, and you’d like to extract various information and display it. In deference to The Camel Book, we’ll follow this lead, and start with a file “grades”, containing something like this:

    Alonzo 70
    Simon 94
    Henk 79
    Eugenio 69
    Bob 80
    Oleg 77
    Philip 73
    ...

Student’s appear multiple times, with entries for each of their subjects Let’s read this file, populate a map with the data, and print some statistical information about the results. First thing to do is import some basic libraries:

    import Data.Char
    import Data.Maybe
    import Data.List
    import Data.Map hiding (map)
    import Text.Printf

And now here’s the entire program, to read the grades file, compute all the averages, and print them:

    main = do
        src <- readFile "grades"
        let pairs   = map (split.words) (lines src)
            grades  = foldr insert empty pairs
        mapM_ (draw grades) (sort (keys grades))
      where
        insert (s, g) = insertWith (++) s [g]
        split [name,mark] = (name, read mark)

    draw g s = printf "%st%stAverage: %fn" s (show marks) avg
      where
        marks = findWithDefault (error "No such student") s g
        avg   = sum marks / fromIntegral (length marks) :: Double

Running it

How do we run this program? There’s lots of ways:

Compile it to native code

    $ ghc -O Grades.hs

    $ ./a.out 
    Alonzo  [70.0,71.0]     Average: 70.5
    Bob     [80.0,88.0]     Average: 84.0
    Eugenio [69.0,98.0]     Average: 83.5
    Henk    [79.0,81.0]     Average: 80.0
    Oleg    [77.0,68.0]     Average: 72.5
    Philip  [73.0,71.0]     Average: 72.0
    Simon   [94.0,83.0]     Average: 88.5

Run it in the bytecode interpreter

    $ runhaskell Grades.hs
    Alonzo  [70.0,71.0]     Average: 70.5
    Bob     [80.0,88.0]     Average: 84.0
    Eugenio [69.0,98.0]     Average: 83.5
    Henk    [79.0,81.0]     Average: 80.0
    Oleg    [77.0,68.0]     Average: 72.5
    Philip  [73.0,71.0]     Average: 72.0
    Simon   [94.0,83.0]     Average: 88.5

Execute it interactively

    $ ghci Grades.hs
    Prelude Main> main
    Alonzo  [70.0,71.0]     Average: 70.5
    Bob     [80.0,88.0]     Average: 84.0
    Eugenio [69.0,98.0]     Average: 83.5
    Henk    [79.0,81.0]     Average: 80.0
    Oleg    [77.0,68.0]     Average: 72.5
    Philip  [73.0,71.0]     Average: 72.0
    Simon   [94.0,83.0]     Average: 88.5

Make the script executable

Under unix, you can use the #! convention to make a script executable. Add the following to the top of the source file:

    #!/usr/bin/env runhaskell

And then set the script executable:

    $ chmod +x Grades.hs

    $ ./Grades.hs       
    Alonzo  [70.0,71.0]     Average: 70.5
    Bob     [80.0,88.0]     Average: 84.0
    Eugenio [69.0,98.0]     Average: 83.5
    Henk    [79.0,81.0]     Average: 80.0
    Oleg    [77.0,68.0]     Average: 72.5
    Philip  [73.0,71.0]     Average: 72.0
    Simon   [94.0,83.0]     Average: 88.5

Next week: more IO!

Roll your own IRC bot

This tutorial is designed as a practical guide to writing real world code in Haskell and hopes to intuitively motivate and introduce some of the advanced features of Haskell to the novice programmer. Our goal is to write a concise, robust and elegant IRC bot in Haskell.

Getting started

You’ll need a reasonably recent version of GHC or Hugs. Our first step is to get on the network. So let’s start by importing the Network package, and the standard IO library and defining a server to connect to.

import Network
import System.IO

server = "irc.freenode.org"
port   = 6667

main = do
    h <- connectTo server (PortNumber (fromIntegral port))
    hSetBuffering h NoBuffering
    t <- hGetContents h
    print t

The key here is the main function. This is the entry point to a Haskell program. We first connect to the server, then set the buffering on the socket off. Once we’ve got a socket, we can then just read and print any data we receive.

Put this code in the module 1.hs and we can then run it. Use whichever system you like:

Using runhaskell:

   $ runhaskell 1.hs
   "NOTICE AUTH :*** Looking up your hostname...rnNOTICE AUTH :***
   Checking identrnNOTICE AUTH :*** Found your hostnamern ...

Or we can just compile it to an executable with GHC:

   $ ghc --make 1.hs -o tutbot
   Chasing modules from: 1.hs
   Compiling Main             ( 1.hs, 1.o )
   Linking ...
   $ ./tutbot
   "NOTICE AUTH :*** Looking up your hostname...rnNOTICE AUTH :***
   Checking identrnNOTICE AUTH :*** Found your hostnamern ...

Or using GHCi:

   $ ghci 1.hs
   *Main> main
   "NOTICE AUTH :*** Looking up your hostname...rnNOTICE AUTH :***
   Checking identrnNOTICE AUTH :*** Found your hostnamern ...

Or in Hugs:

   $ runhugs 1.hs
   "NOTICE AUTH :*** Looking up your hostname...rnNOTICE AUTH :***
   Checking identrnNOTICE AUTH :*** Found your hostnamern ...

Great! We’re on the network.

Talking IRC

Now we’re listening to the server, we better start sending some information back. Three details are important: the nick, the user name, and a channel to join. So let’s send those.

import Network
import System.IO
import Text.Printf

server = "irc.freenode.org"
port   = 6667
chan   = "#tutbot-testing"
nick   = "tutbot"

main = do
    h <- connectTo server (PortNumber (fromIntegral port))
    hSetBuffering h NoBuffering
    write h "NICK" nick
    write h "USER" (nick++" 0 * :tutorial bot")
    write h "JOIN" chan
    listen h

write :: Handle -> String -> String -> IO ()
write h s t = do
    hPrintf h "%s %srn" s t
    printf    "> %s %sn" s t

listen :: Handle -> IO ()
listen h = forever $ do
    s <- hGetLine h
    putStrLn s
  where
    forever a = do a; forever a

Now, we’ve done quite a few things here. Firstly, we import Text.Printf, which will be useful. We also set up a channel name and bot nickname. The main function has been extended to send messages back to the IRC server using a write function. Let’s look at that a bit more closely:

write :: Handle -> String -> String -> IO ()
write h s t = do
    hPrintf h "%s %srn" s t
    printf    "> %s %sn" s t

We’ve given write an explicit type to help document it, and we’ll use explicit types signatures from now on, as they’re just good practice (though of course not required, as Haskell uses type inference to work out the types anyway).

The write function takes 3 arguments; a handle (our socket), and then two strings representing an IRC protocol action, and any arguments it takes. write then uses hPrintf to build an IRC message and write it over the wire to the server. For debugging purposes we also print to standard output the message we send.

Our second function, listen, is as follows:

listen :: Handle -> IO ()
listen h = forever $ do
    s <- hGetLine h
    putStrLn s
  where
    forever a = do a; forever a

This function takes a Handle argument, and sits in an infinite loop reading lines of text from the network and printing them. We take advantage of two powerful features; lazy evaluation and higher order functions to roll our own loop control structure, forever, as a normal function! forever takes a chunk of code as an argument, evaluates it and recurses – an infinite loop function. It is very common to roll our own control structures in Haskell this way, using higher order functions. No need to add new syntax to the language, lisp-like macros or meta programming – you just write a normal function to implement whatever control flow you wish. We can also avoid do-notation, and directly write: forever a = a >> forever a.

Let’s run this thing:

$ runhaskell 2.hs
> NICK tutbot
> USER tutbot 0 * :tutorial bot
> JOIN #tutbot-testing
NOTICE AUTH :*** Looking up your hostname...
NOTICE AUTH :*** Found your hostname, welcome back
NOTICE AUTH :*** Checking ident
NOTICE AUTH :*** No identd (auth) response
:orwell.freenode.net 001 tutbot :Welcome to the freenode IRC Network tutbot
:orwell.freenode.net 002 tutbot :Your host is orwell.freenode.net
...
:tutbot!n=tutbot@aa.bb.cc.dd JOIN :#tutbot-testing
:orwell.freenode.net MODE #tutbot-testing +ns
:orwell.freenode.net 353 tutbot @ #tutbot-testing :@tutbot
:orwell.freenode.net 366 tutbot #tutbot-testing :End of /NAMES list.

And we’re in business! From an IRC client, we can watch the bot connect:

   15:02 -- tutbot [n=tutbot@aa.bb.cc.dd] has joined #tutbot-testing
   15:02  dons> hello

And the bot logs to standard output:

   :dons!i=dons@my.net PRIVMSG #tutbot-testing :hello

We can now implement some commands.

A simple interpreter

Add these additional imports before changing the listen function.

import Data.List
import System.Exit
listen :: Handle -> IO ()
listen h = forever $ do
    t <- hGetLine h
    let s = init t
    if ping s then pong s else eval h (clean s)
    putStrLn s
  where
    forever a = a >> forever a

    clean     = drop 1 . dropWhile (/= ':') . drop 1

    ping x    = "PING :" `isPrefixOf` x
    pong x    = write h "PONG" (':' : drop 6 x)

We add 3 features to the bot here by modifying listen. Firstly, it responds to PING messages: if ping s then pong s .... This is useful for servers that require pings to keep clients connected. Before we can process a command, remember the IRC protocol generates input lines of the form:

:dons!i=dons@my.net PRIVMSG #tutbot-testing :!id foo

so we need a clean function to simply drop the leading ‘:’ character, and then everything up to the next ‘:’, leaving just the actual command content. We then pass this cleaned up string to eval, which then dispatches bot commands.

eval :: Handle -> String -> IO ()
eval h    "!quit"                = write h "QUIT" ":Exiting" >> exitWith ExitSuccess
eval h x | "!id " `isPrefixOf` x = privmsg h (drop 4 x)
eval _   _                       = return () -- ignore everything else 

So, if the single string “!quit” is received, we inform the server and exit the program. If a string beginning with “!id” appears, we echo any argument string back to the server (id is the Haskell identity function, which just returns its argument). Finally, if no other matches occur, we do nothing.

We add the privmsg function – a useful wrapper over write for sending PRIVMSG lines to the server.

privmsg :: Handle -> String -> IO ()
privmsg h s = write h "PRIVMSG" (chan ++ " :" ++ s)

Here’s a transcript from our minimal bot running in channel:

   15:12 -- tutbot [n=tutbot@aa.bb.cc.dd] has joined #tutbot-testing
   15:13  dons> !id hello, world!
   15:13  tutbot> hello, world!
   15:13  dons> !id very pleased to meet you.
   15:13  tutbot> very pleased to meet you.
   15:13  dons> !quit
   15:13 -- tutbot [n=tutbot@aa.bb.cc.dd] has quit [Client Quit]

Now, before we go further, let’s refactor the code a bit.

Roll your own monad

A small annoyance so far has been that we’ve had to thread around our socket to every function that needs to talk to the network. The socket is essentially immutable state, that could be treated as a global read only value in other languages. In Haskell, we can implement such a structure using a state monad. Monads are a very powerful abstraction, and we’ll only touch on them here. The interested reader is referred to All About Monads. We’ll be using a custom monad specifically to implement a read-only global state for our bot.

The key requirement is that we wish to be able to perform IO actions, as well as thread a small state value transparently through the program. As this is Haskell, we can take the extra step of partitioning our stateful code from all other program code, using a new type.

So let’s define a small state monad:

data Bot = Bot { socket :: Handle }

type Net = ReaderT Bot IO

Firstly, we define a data type for the global state. In this case, it is the Bot type, a simple struct storing our network socket. We then layer this data type over our existing IO code, with a monad transformer. This isn’t as scary as it sounds and the effect is that we can just treat the socket as a global read-only value anywhere we need it. We’ll call this new io + state structure the Net monad. ReaderT is a type constructor, essentially a type function, that takes 2 types as arguments, building a result type: the Net monad type.

We can now throw out all that socket threading and just grab the socket when we need it. The key steps are connecting to the server, followed by the initialisation of our new state monad and then to run the main bot loop with that state. We add a small function, which takes the intial bot state and evaluates the bot’s run loop “in” the Net monad, using the Reader monad’s runReaderT function:

loop st = runReaderT run st

where run is a small function to register the bot’s nick, join a channel, and start listening for commands.

While we’re here, we can tidy up the main function a little by using Control.Exception.bracket to explicitly delimit the connection, shutdown and main loop phases of the program – a useful technique. We can also make the code a bit more robust by wrapping the main loop in an exception handler using catch:

main :: IO ()
main = bracket connect disconnect loop
  where
    disconnect = hClose . socket
    loop st    = catch (runReaderT run st) (const $ return ())

That is, the higher order function bracket takes 3 arguments: a function to connect to the server, a function to disconnect and a main loop to run in between. We can use bracket whenever we wish to run some code before and after a particular action – like forever, this is another control structure implemented as a normal Haskell function.

Rather than threading the socket around, we can now simply ask for it when needed. Note that the type of write changes – it is in the Net monad, which tells us that the bot must already by connected to a server (and thus it is ok to use the socket, as it is initialised).

--
-- Send a message out to the server we're currently connected to
--
write :: String -> String -> Net ()
write s t = do
    h <- asks socket
    io $ hPrintf h "%s %srn" s t
    io $ printf    "> %s %sn" s t

In order to use both state and IO, we use the small io function to lift an IO expression into the Net monad making that IO function available to code in the Net monad.

io :: IO a -> Net a
io = liftIO

Similarly, we can combine IO actions with pure functions by lifting them into the IO monad. We can therefore simplify our hGetLine call:

do t <- io (hGetLine h)
   let s = init t

by lifting init over IO:

do s <- init `fmap` io (hGetLine h)

The monadic, stateful, exception-handling bot in all its glory:

import Data.List
import Network
import System.IO
import System.Exit
import Control.Monad.Reader
import Control.Exception
import Text.Printf
import Prelude hiding (catch)

server = "irc.freenode.org"
port   = 6667
chan   = "#tutbot-testing"
nick   = "tutbot"

-- The 'Net' monad, a wrapper over IO, carrying the bot's immutable state.
type Net = ReaderT Bot IO
data Bot = Bot { socket :: Handle }

-- Set up actions to run on start and end, and run the main loop
main :: IO ()
main = bracket connect disconnect loop
  where
    disconnect = hClose . socket
    loop st    = catch (runReaderT run st) (const $ return ())

-- Connect to the server and return the initial bot state
connect :: IO Bot
connect = notify $ do
    h <- connectTo server (PortNumber (fromIntegral port))
    hSetBuffering h NoBuffering
    return (Bot h)
  where
    notify a = bracket_
        (printf "Connecting to %s ... " server >> hFlush stdout)
        (putStrLn "done.")
        a

-- We're in the Net monad now, so we've connected successfully
-- Join a channel, and start processing commands
run :: Net ()
run = do
    write "NICK" nick
    write "USER" (nick++" 0 * :tutorial bot")
    write "JOIN" chan
    asks socket >>= listen

-- Process each line from the server
listen :: Handle -> Net ()
listen h = forever $ do
    s <- init `fmap` io (hGetLine h)
    io (putStrLn s)
    if ping s then pong s else eval (clean s)
  where
    forever a = a >> forever a
    clean     = drop 1 . dropWhile (/= ':') . drop 1
    ping x    = "PING :" `isPrefixOf` x
    pong x    = write "PONG" (':' : drop 6 x)

-- Dispatch a command
eval :: String -> Net ()
eval     "!quit"               = write "QUIT" ":Exiting" >> io (exitWith ExitSuccess)
eval x | "!id " `isPrefixOf` x = privmsg (drop 4 x)
eval     _                     = return () -- ignore everything else

-- Send a privmsg to the current chan + server
privmsg :: String -> Net ()
privmsg s = write "PRIVMSG" (chan ++ " :" ++ s)

-- Send a message out to the server we're currently connected to
write :: String -> String -> Net ()
write s t = do
    h <- asks socket
    io $ hPrintf h "%s %srn" s t
    io $ printf    "> %s %sn" s t

-- Convenience.
io :: IO a -> Net a
io = liftIO

Note that we threw in a new control structure, notify, for fun. Now we’re almost done! Let’s run this bot. Using runhaskell:

   $ runhaskell 4.hs

or using GHC:

   $ ghc --make 4.hs -o tutbot
   Chasing modules from: 4.hs
   Compiling Main             ( 4.hs, 4.o )
   Linking ...
   $ ./tutbot

If you’re using Hugs, you’ll have to use the -98 flag:

   $ runhugs -98 4.hs

And from an IRC client we can watch it connect:

   15:26 -- tutbot [n=tutbot@aa.bb.cc.dd] has joined #tutbot-testing
   15:28  dons> !id all good?
   15:28  tutbot> all good?
   15:28  dons> !quit
   15:28 -- tutbot [n=tutbot@aa.bb.cc.dd] has quit [Client Quit]

So we now have a bot with explicit read-only monadic state, error handling, and some basic IRC operations. If we wished to add read-write state, we need only change the ReaderT transformer to StateT.

Extending the bot

Let’s implement a basic new command: uptime tracking. Conceptually, we need to remember the time the bot starts. Then, if a user requests, we work out the total running time and print it as a string. A nice way to do this is to extend the bot’s state with a start time field:

import System.Time
data Bot = Bot { socket :: Handle, starttime :: ClockTime }

We can then modify the initial connect function to also set the start time.

connect :: IO Bot
connect = notify $ do
    t <- getClockTime
    h <- connectTo server (PortNumber (fromIntegral port))
    hSetBuffering h NoBuffering
    return (Bot h t)

We then add a new case to the eval function, to handle uptime requests:

eval "!uptime" = uptime >>= privmsg

This will just run the uptime function and send it back to the server. uptime itself is:

uptime :: Net String
uptime = do
    now  <- io getClockTime
    zero <- asks starttime
    return . pretty $ diffClockTimes now zero

That is, in the Net monad, find the current time and the start time, and then calculate the difference, returning that number as a string. Rather than use the normal representation for dates, we’ll write our own custom formatter for dates:

--
-- Pretty print the date in '1d 9h 9m 17s' format
--
pretty :: TimeDiff -> String
pretty td = join . intersperse " " . filter (not . null) . map f $
    [(years          ,"y") ,(months `mod` 12,"m")
    ,(days   `mod` 28,"d") ,(hours  `mod` 24,"h")
    ,(mins   `mod` 60,"m") ,(secs   `mod` 60,"s")]
  where
    secs    = abs $ tdSec td  ; mins   = secs   `div` 60
    hours   = mins   `div` 60 ; days   = hours  `div` 24
    months  = days   `div` 28 ; years  = months `div` 12
    f (i,s) | i == 0    = []
            | otherwise = show i ++ s

And that’s it. Running the bot with this new command:

   16:03 -- tutbot [n=tutbot@aa.bb.cc.dd] has joined #tutbot-testing
   16:03  dons> !uptime
   16:03  tutbot> 51s
   16:03  dons> !uptime
   16:03  tutbot> 1m 1s
   16:12  dons> !uptime
   16:12  tutbot> 9m 46s

Where to now?

This is just a flavour of application programming in Haskell, and only hints at the power of Haskell’s lazy evaluation, static typing, monadic effects and higher order functions. There is much, much more to be said on these topics. Some places to start:

Or take the bot home and hack! Some suggestions:

  • Use forkIO to add a command line interface, and you’ve got yourself an irc client with 4 more lines of code.
  • Port some commands from Lambdabot.