[Haskell-cafe] Crazy idea: overloading function application
notation
David House
dmhouse at gmail.com
Thu May 31 09:10:02 EDT 2007
On 31/05/07, Jon Harrop <jon at ffconsultancy.com> wrote:
> Is it possible to implement this in Haskell using type classes? Is there any
> way this could actually be practicable?
I had a go but didn't manage to quite get it. Here's my attempt, and
the error it produces:
{-# OPTIONS_GHC -fglasgow-exts #-}
type Endo a = a -> a
-- Dummy instances to satisfy the (Show n, Eq n) => Num n constraints
instance Show (Endo a) where show _ = "<function>"
instance Eq (Endo a) where _ == _ = False
instance Num a => Num (Endo a) where
fromInteger x = (fromInteger x *)
x + y = \z -> x z + y z
x * y = \z -> x z * y z
abs x = error "Aaargh."
signum x = error "Aaargh."
main = print (2 3)
/home/david/hs/sandbox/application-multiplication.hs:15:14:
No instance for (Num (t -> a))
arising from the literal `2'
at /home/david/hs/sandbox/application-multiplication.hs:15:14-16
Possible fix: add an instance declaration for (Num (t -> a))
In the first argument of `print', namely `(2 3)'
In the expression: print (2 3)
In the definition of `main': main = print (2 3)
Failed, modules loaded: none.
It seems to be wanting a more general instance than the one I'm
providing, for whatever reason. Using print ((2 :: Endo Integer) 3)
works, but that's hardly satisfactory.
--
-David House, dmhouse at gmail.com
More information about the Haskell-Cafe
mailing list