add functions to a class

Christopher Sharpe ctsharpe@earthlink.net
Fri, 29 Jun 2001 11:54:59 -0400


Hello,
  Can you help me understand how to make something more quantified than
just (a -> b) a member of a class?

Thanks in advance!



==============================================================================

This is a "literate" program and may be saved as "t.lhs" (say) then
loaded into Haskell.
I am using O'Hugs version 0.5 under Windows 98.

We want to make ((Int,Float) -> String) a Show.

Try the direct approach:
-------------------------------------------
instance Show ((Int,Float) -> String) where
   showsPrec _ f _ =  f (3,5.4)
-------------------------------------------
It fails with 'Type variable expected in instance type'.


Try defining a type synonym:
-------------------------------------------

>type MyF = ((Int,Float) -> String)

instance Show MyF where
   showsPrec _ f _ =  f (3,5.4)
-------------------------------------------
It fails with 'Type synonym "MyF" not permitted in instance of "Show" '.



Try defining a newtype:
-------------------------------------------

>newtype MyNewtype = MyDataConstructor ((Int,Float) -> String)
>instance Show MyNewtype where
>  showsPrec _ (MyDataConstructor f) _ =  f (3,5.4)

>--We never really made MyF a Show, but function "show'" is a
workaround.
>show' myF = show (MyDataConstructor myF)

-------------------------------------------

Testing:
-------------------------------------------

>f :: MyF
>f x | x == (3,5.4) = "This shows the value of my function applied to a
certain argument."
>    | otherwise   = "OK"

>test = show' f

-------------------------------------------