Testing … testing…1…2…3

A couple of weeks ago I attended the latest BCS SPA cambridge talk which was by Simon Peyton Jones  on type driven testing in Haskell. This was a great talk and it was recorded.
 
Most of the audience hadn’t programmed in a functional languages before, so the first part of the talk was a brief introduction to the advantages of functional programming languages. Consiseness of expression, clarity and understandability were the buzzwords here. The lack of side-effects make code much more understandable, and the availability of higher level datatypes often makes the algorithm easier to follow. However, the higher level of abstraction takes the language further away from the machine and it can be difficult to get the compliler to generate efficient code. In the 80’s people looked at using the lack of side effects to generate programs that could be executed in parallel on graph reduction machines; this effort generated a lot of important research results, but didn’t succeed in the market place as commonly available machines obeyed Moore’s laws and doubled in speed over regular time intervals, and the languages of the marketplace weren’t really suitable for parallelising. It was interesting to hear Peyton-Jones state that investments in compiler technology are now making such languages competitve with more mainstream languages in many areas. Tricks like memoization also help too.
 
The next part of the talk was a brief introduction to Haskell. This is a very impressive language which is for the most part pure. Monads are used to isolate side effecting parts of the program into small islands of functions with types which make the presence of side effects obvious.
 
This part introduced type classes, the means for demanding that the types that a function can be applied to offer other support functions. In languages like ML, functions like + and * are special in that there are two versions of the function, one for reals and one for ints, but any use of the these functions inside another must resolve to either the real or the int case. In Haskell, we can instead have a type class that reflects the presence of + and * functions and then constraint the types of the resulting signature to be instances of the given type class.
 
In the following example, we define two type classes, Class1 and Class2 where Class2 extends class Class1.
 
module Play where
 
class Class1 a where
  foo :: a -> [a]
 
class (Class1 a) => Class2 a where
  bar :: a -> [a]
 
instance Class1 Int where
  foo x = [x]
 
instance Class2 Int where
  bar x = foo x
 
play x = bar x
 
play2 x = foo x
 
If we look at the types of play and play2, we can see that the function is fully polymorphic with the proviso that instance types support the Class1 or Class2 ie that the type implements foo and bar methods.
 
Play> :type play
play :: Class2 a => a -> [a]
 
Play> :type play2
play2 :: Class1 a => a -> [a]
 
Because of the instance declarations, we have given suitable definitons for Int of foo and bar and can therefore call play on an integer.
 
Play> play (2 :: Int)
[2]
 
The type classes were used by the testing framework for generating the test cases to pass through the test functions. All very tidy indeed.
 
Peyton-Jones argued that functional languages were easier to test because the lack of side effects means that you don’t have the problems of setting up the world before calling the function and then checking the state of the world afterwards. This is true for the pure parts of the code, though the same could be said of the pure parts of C# code that are being tested. Also, setting up the state of the world isn’t always to hard as you’d typically use mock objects and dependency injection to get these partial models of the world used during the test case.
 
Functional languages do have a lot to offer. When I was doing alot of ML programming, the weird thing was that most of the debuggign was at the type level getting the program to type check – once it type checked correctly then it would often run successfully. It will be interesting to see if F# can finally take these languages into the mainstream.
 
Advertisements
This entry was posted in Computers and Internet. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s