A testing dead end that felt appropriate at the time.

This article is one of a series on finding properties for a budgeting kata. I expect you to have read the previous articles in the series.

As I often do when I do katas, I imposed some constraints in addition to 'just' solving the problem. I wanted to do test-driven development (TDD) with QuickCheck properties taking the place of individual test cases. I also desired to identify 'mathematical' or computer-science-like properties instead of (which is much easier) 'examples on steroids'. All this made me add the next property although, should I had followed the transformation priority premise, I should have chosen a property that would have prompted a change to the System Under Test.

This didn't happen here, but still I felt that it was worthwhile establishing this property as groundwork for the property I intended to add after this one.

The code is available on GitHub, in the commit 1051c2d.

An Arbitrary instance for Budget #

So far, the properties I defined all constructed budgets from QuickCheck-generated bills. For this property, I wanted to be able to work with arbitrary budgets. As I also did with Item, I first added a newtype wrapper for Budget:

newtype AnyBudget a = AnyBudget { getBudget :: Budget a } deriving (Show, Eq)

You can now give AnyBudget an Arbitrary instance.

instance (Arbitrary a, Ord a) => Arbitrary (AnyBudget a) where
  arbitrary = do
    Positive tl <- arbitrary
    cls <- fmap getPositive . Map.fromList <$> arbitrary
    return $ AnyBudget $ Budget tl cls
  shrink (AnyBudget (Budget tl cls)) =
    [ AnyBudget $ Budget tl' (fmap getPositive . Map.fromList $ cls')
    | (Positive tl', cls') <-
        shrink (Positive tl, Map.toList (fmap Positive cls))
    ]

Although Haskell syntax can be dense, there's not a lot going on. Each category limit is generated from some identifier and a Positive Int. The shrink method is similar to AnyItem: Wrap both the total limit (tl) and the category limits (cls) in Positive, then shrink that tuple, and unwrap the values again.

Monotonicity in items #

A fundamental property of many functions is whether or not they are monotonic. If you have a function that returns a complex object, such as a list of customers, the concept of monotonicity may not be meaningful, but when the return value is a simple value, it may. Even though the return type of canAfford is Bool, if we (as I think is intuitive) associate False with nought and True with one, we can meaningfully talk about monotonicity.

Consider a fixed sequence of items and a budget. You may begin with an empty bill, which should always be affordable. Then you pick the first item from your given sequence and add it to the bill. Perhaps the bill is still affordable. Then you take the second item and add it to the bill. Perhaps it's still affordable. Depending on the budget and the sequence of items, there will come an item that, when you add it, causes the bill to become unaffordable. After that, no matter how many more items you add, the bill will remain unaffordable.

Graph with items on the horizontal axis and nought and one on the vertical. A step function starts at one, but after twelve items drops to nought and stays there to the right of twelve items.

It may be that for a given budget and a sequence of items, only the empty bill is affordable, and adding even a single item immediately makes it unaffordable. Even so, for a given budget and sequence of items, the canAfford function is non-increasing in items. Or, as it turned out that I expressed the following property, removing an item can never make an affordable bill unaffordable. Removing an item means 'moving left' on the above graph. Once you're in the part of the graph where the bill is affordable, removing an item (i.e. going left) will never make the bill unaffordable.

This suggests a property like this:

testProperty "Monotonocity: Removing item from affordable bill can never make it unaffordable" $ \
  (AnyBudget (budget :: Budget String))
  (fmap getItem . getNonEmpty -> bill :: Bill String) ->
  do
    itemToRemove <- elements bill
    let bill' = filter (/= itemToRemove) bill
 
        actual = canAfford budget bill'
 
    return $ counterexample ("Modified bill: " ++ show bill') $
             not (canAfford budget bill) || actual

Like previous properties this one also uses view patterns to pull the bill out of a list of AnyItem values, but now this list is a NonEmptyList. Likewise, pattern matching on AnyBudget makes budget a Budget String. The entire type for the lambda expression (everything after $ \ ) is

AnyBudget String -> NonEmptyList (AnyItem String) -> Gen Property

Since bill is guaranteed to be non-empty, it's safe to pick one of the items to remove. In fact, now that I think about it, the filter expression might actually remove more than one item, but that would only correspond to moving more than one step to the left on the above graph, so that's not consequential.

The assertion finally states that if the original bill was unaffordable, we can't really say anything, in which case the property automatically passes. If, on the other hand, that's not the case (i.e. the original bill was affordable), we also expect actual to be True.

Conclusion #

This property seems redundant, since it immediately passes. The incarnation of canAfford that exists so far already has the desired monotonicity property. I don't quite remember what I had in mind when I added this one, but I think that I thought that this property would become an underlying assumption for a later property, so it felt safer to add this one explicitly.

If nothing else, this introduced the concept of monotonicity, which the next property will also explore.

Next: QuickCheck property: Monotonicity in categories.



Wish to comment?

You can add a comment to this post by sending me a pull request. Alternatively, you can discuss this post on Twitter or somewhere else with a permalink. Ping me with the link, and I may respond.

Published

Monday, 23 November 2026 07:53:00 UTC

Tags



"Our team wholeheartedly endorses Mark. His expert service provides tremendous value."
Hire me!
Published: Monday, 23 November 2026 07:53:00 UTC