[Haskell-cafe] equations and patterns

Henning Thielemann lemming at henning-thielemann.de
Thu May 31 11:22:39 EDT 2007


On Thu, 31 May 2007, Stefan Holdermans wrote:

> 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.

Like here:
 http://darcs.haskell.org/numericprelude/src/Algebra/Lattice.hs

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

I assume they can in some way also be used for GHC's optimizer.
 http://www.haskell.org/haskellwiki/Playing_by_the_rules


More information about the Haskell-Cafe mailing list