[Haskell-cafe] Re: function types as instances of Num
oleg at pobox.com
oleg at pobox.com
Sun Oct 29 23:19:29 EST 2006
The problem seems equivalent to the following:
http://pobox.com/~oleg/ftp/Haskell/typecast.html#local-fd
That is, the inferred type is too general to chose the appropriate
instance. The solution is also the same: either add type annotations
to restrict the inferred type (and so make it _match_ the desired
instance) -- or use local type inference. Here's the working code of
your Haskell embedding of the Forth-like stack language:
> {-# OPTIONS -fglasgow-exts #-}
> {-# OPTIONS -fallow-undecidable-instances #-}
>
> main = print $ test ()
>
> test = square . 4
>
> dup (a, b) = (a, (a, b))
> mult (a, (b, c)) = (b*a, c)
> square = mult . dup
>
> instance Num c => Eq (a -> (c, b))
> instance Num c => Show (a -> (c, b))
> instance (Num c, TypeCast a b) => Num (a -> (c, b)) where
> fromInteger val stack = (fromInteger val,typeCast stack)
[TypeCast elided]
More information about the Haskell-Cafe
mailing list