On 31/05/07, Jon Harrop <[EMAIL PROTECTED]> 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, [EMAIL PROTECTED]
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to