Type classes question
Iavor Diatchki
diatchki@cse.ogi.edu
Fri, 29 Aug 2003 14:32:39 -0700
hello,
Eugene Nonko wrote:
> Hello,
>
> I am trying to define class Dual and few instances as follows:
>
> ===
> class Dual a where
> dual :: a -> a
>
> instance Dual Bool where
> dual = not
>
> instance (Dual a, Dual b) => Dual (a -> b) where
> dual f = dual . f . dual
>
> instance Dual a => Dual [a] where
> dual = reverse . map dual
>
> instance Num a => Dual a where
> dual = negate
> ===
>
> For some reason Hugs does not lake the last definition saying 'ERROR
> dual.hs:13 - syntax error in instance head (constructor expected)'. What
> am I doing wrong?
instance declarations in Haskell'98 require the type that is being made
an instance to be of the form "<constructor> <variable>*", (a
constructor followd by some type variables). your last declaration is
not of this form (it is simply a variable). if you run hugs with -98
flag, this restriction is lifted. however, note that your last
instance will overlap with any other instance as a type variable matches
anything.
hope this helps
iavor
--
==================================================
| Iavor S. Diatchki, Ph.D. student |
| Department of Computer Science and Engineering |
| School of OGI at OHSU |
| http://www.cse.ogi.edu/~diatchki |
==================================================