[Haskell-cafe] equations and patterns

Stefan Holdermans stefan at cs.uu.nl
Thu May 31 02:27:05 EDT 2007


Mingli,

> >  class Lattice e where
> >      join :: e -> e -> e
> >      meet :: e -> e -> e
> >
> >      -- associative law
> >      join x (join y z) = join (join x y) z
> >      join (join x y) z = join x (join y z)

If you are not to sell your soul to advanced and perhaps obscure type  
hacking, you cannot express laws like this *in* Haskell.

More concretely, one usually does not provide such laws as default  
implementations of a class' methods. Instead, they are stated in, for  
instance, comments and the documentation that goes with your library.  
These then form an informal obligation for programmers that provide  
instances of your class to let these instances obey the laws.

If you provide an instance of the class you could use testing  
framework, e.g., QuickCheck [1], to assert that the required  
properties hold.

Cheers,

   Stefan


[1] www.cs.chalmers.se/~rjmh/QuickCheck/





More information about the Haskell-Cafe mailing list