Comment by kccqzy
7 hours ago
Since we are talking about Haskell, its type system can be explored to answer the question of what is considered a Property by the quickcheck library.
The Property type is a datatype for which the definition is private. So we look at functions that creates Property objects. We find
class Testable prop where
property :: prop -> Property
This means anything of the class Testable can be turned into a Property. So let's look at what are the instances of this class. We immediately find
instance Testable Bool
This means any boolean can be turned into a Property. But more interestingly we find
instance (Arbitrary a, Show a, Testable prop) => Testable (a -> prop)
This means any function where the argument has a generator and the result can be turned into a Property can itself be turned into a Property. This implies that for example a function of `Int -> Bool` can be turned into a property. And due to currying, `Int -> Int -> Bool` can also be turned into a property.
This basically covers the first half of the article.
We continue to find other functions that returns Property objects. We find
forAll :: (Show a, Testable prop) => Gen a -> (a -> prop) -> Property
This states that if we have a function where we explicitly provide a generator and it returns anything that can be turned into a Property, it can also be turned into a Property. This is different from the instance above only by providing an explicit generator.
We also find the implication operator:
(==>) :: Testable prop => Bool -> prop -> Property
This states that if we have a Bool, and anything that can be turned into a property, we also have a property.
You see, the beauty of Haskell and similar languages is that you don't need to first gain an intuitive understanding of those vocabularies of a library. The library designer has built up an abstraction of those vocabularies and Nouns and the compiler is tasked to enforce it. You can get things wrong, and the compiler just yells at you. Or you can just mechanically find things to fill in the blanks ("hole-driven development") without understanding the vocabularies. It's your choice.
No comments yet
Contribute on Hacker News ↗