How to write a function whose both parameters and results are polymorphic in Haskell

How can I write a function like int gog(float i) and float gog(int i) (which is usually called "overloading")? Some simple overloads can be achieved by

class PP a where
  gog :: a -> Int

instance PP Bool where
  gog _ = 1

instance PP Char where
  gog _ = 1

But the above example only makes the parameter polymorphic. If we want to make both the parameter and result polymorphic, we have to write something like this:

class PP a where
  gog :: Uu b => a -> b

class UU a where
  -- This function can convert between different types of UU.
  fromUuToUu :: UU b => a -> b 

Without fromUuToUu, the polymorphism in the result of gog is impossible. But I can't write fromUuToUu, and that is relevant to the theme of this question, which is how to make a function whose parameter and result are both polymorphic.

Answers


{-# LANGUAGE MultiParamTypeClasses, TypeSynonymInstances, FlexibleInstances #-}

class Poly a b where
  gog :: a -> b

instance Poly Int String where
  gog = show

instance Poly String Int where
  gog = read

instance Poly Int Float where
  gog = fromIntegral

instance Poly Float Float where
  gog = (*) 2

gog is now "full" polymorphic.


Need Your Help

Sencha touch 2 Can't read json from .net server

.net json sencha-touch-2

I have a store that read json string from .Net Server.

How to set SelectedItem on a RibbonComboBox using MVVM?

wpf mvvm ribboncontrolslibrary

How do I set the SelectedItem on a RibbonComboBox using MVVM pattern?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.