I've been neglecting my blog posts for the first half of this semester, but I'm going to do my best to post a new blog post every day from here on out.
Lately, I've been doing research on Haskell libraries and Monads. Most recently, I've discovered the Haskell QuickCheck library which generates random input sets, tests functions, and outputs the results all in a single import and function call.
Needless to say, QuickCheck is incredibly small, easy to use, and powerful, as is the way with Haskell libraries. The library includes the Arbitrary data type, which has many different built-in data structures, as well as a simple API for defining more. The Arbitrary class allows for a set to draw a random sampling from, for example, Arbitrary Int defines a randomGen that returns only integer values, while Arbitrary [a] defines a randomGen that gets lists of any single consistent type of any length. Tests are made by defining a function that takes an Arbitrary and returns a boolean. Haskell's powerful type inference means that an average user doesn't even need to know about the Arbitrary class. If you make a function that takes something and returns a boolean, and feed it to the quickCheck method, it'll fill in that something with an Arbitrary version of it, if one is available.
Like most Haskell libraries, it's not without its headaches. It's a bit tough to install the right version for your purposes, since QuickCheck-2.0 and higher removed the "Batch" module from 1.0, which is very useful for running large number of test suites simultaneously and amalgamating their inputs into one simple line, so instead of "Test #1 succeeded on 100 inputs ... Test #500 succeeded on 100 inputs" you have one line that says "500 tests succeeded on 50000 inputs". Uninstalling the most recent version (which is easy enough to accidentally do, assuming it keeps the proper modules) was a hassle in and of itself, as Haskell's package manager, Cabal, has no way to uninstall packages. To remove the most recent QuickCheck, I had to track down the lib files it download to some random place on my file system, remove them, update Cabal so it forgets that the lib was there, then reinstall the older version with the module I needed.
No comments:
Post a Comment