Is there a more concise way to write the following haskell code:
{-# LANGUAGE FlexibleInstances #-}
class Greetable g where
hi :: g -> String
-- Here i should list all instances of Num
instance Greetable Float where
hi s = "Hi number! " ++ show s
instance Greetable Int where
hi s = "Hi number! " ++ show s
instance Greetable Double where
hi s = "Hi number! " ++ show s
-- Etc.
-- Here is other stuff
instance Greetable String where
hi s = "Hi string! " ++ s
EDIT: I would like the hi
function to be able to account for any future instances of the class Num
.
There is the super-primitive way
Essentially the same can be done nicer with Template Haskell, but AFAIK you'll need two modules (one for defining the macro and one for using it), which is probably overkill here.
A perhaps more sensible solution is to simply write