[Haskell-cafe] Re: Implementing fixed-sized vectors (using datatype algebra?)

Wolfgang Jeltsch g9ks157k at acme.softbase.org
Tue Feb 19 20:26:41 EST 2008


Am Mittwoch, 20. Februar 2008 00:39 schrieben Sie:
> Why are the value-level reflecting functionsimplemented as type-class
> methods? It makes the code more verbose and I don't see any advantage
> compared to simply defining a function per class. Let me show you an
> example:
>
> This is your implementation of Not:
>
>     class (Boolean boolean, Boolean boolean') =>
>           Not boolean boolean' | boolean -> boolean', boolean' -> boolean
> where not :: boolean -> boolean'
>
>     instance Not False True where
>         not _ = true
>
>     instance Not True False where
>         not _ = false
>
> This is how I would do it:
>
>     class (Boolean boolean, Boolean boolean') =>
>           Not boolean boolean' | boolean -> boolean', boolean' -> boolean
> where
>
>     instance Not False True
>     instance Not True False
>
>    not :: Not a b => a -> b
>    not = undefined

Your definition of the not function uses the implementation detail that false 
and true are actually undefined.  My implementation of the not function also 
works if false and true are defined to be something different.  Of course, 
false and true are in the same library, so we know this implementation detail 
and could make use of it.

> Furthermore, why did you choose to use Boolean instead of simply Bool?

To avoid a name clash.  Feel free to change this.

Note that False and True don’t cause a name clash since they live in a 
namespace different from the one Prelude’s False and True live in.

> Cheers,
>
> Fons

Best wishes,
Wolfgang


More information about the Haskell-Cafe mailing list