Bank kata in Haskell — using and testing date

Recap

Last post we looked at printing whilst holding state. Here’s the code we ended up with:

Our final task to complete the kata is to add a date to the transactions. We will implement this in small logical steps, using the compiler and the tests to drive our design. Let’s begin!

Change Transaction to contain a date

This will cause a compile time error — deposit and withdraw are not creating a valid Transaction type anymore, as well as our test functions. We will add a fixed date to these so we can compile.

Now we have modified our type to be the shape we want, we can think about how to get the current date into the transaction, again without sacrificing testability. We will continue to follow the monad transformers style by adding a type constraint to our m.

Introduce the MonadCurrentDateTime type constraint

Now we want to use this type as a constraint in our deposit and withdraw methods to ensure we can use it.

The compiler tells us what to do next.


Main.hs:15:3: error:
• No instance for (MonadCurrentDateTime IO)
arising from a use of ‘deposit’
BankSpec.hs:31:32: error:
• No instance for (MonadCurrentDateTime Identity)
arising from a use of ‘deposit’

As the compiler says, our usage of deposit is invalid because IO is not an instance of MonadCurrentDateTime, and so can’t give us a currentDateTime function to use. A similar scenario is happening in our test file. Let’s fix both.

Now we’re compiling, and our tests our passing because we hardcoded the date in our Bank code. Now let’s use the ‘real’ date (as far as our functions are concerned).

Use the MonadCurrentDateTime typeclass

It’s as simple as that :). We are now using the date returned in our Identity instance of MonadCurrentDateTime. You can now do the work to add the date into the statement output, which I’ll leave out of the scope of this post.

Let’s write one last test as an acceptance test that proves everything is working together.

Testing it all together

And we are done!

Notice how we didn’t need to change the usage of our functions in Main.hs, the API stayed the same, we merely added some more functionality.