[Haskell-cafe] Is it possible to represent such polymorphism?

2011-10-02 Thread Du Xi

--I tried to write such polymorphic function:

expand (x,y,z) = (x,y,z)
expand (x,y) = (x,y,1)

--And it didn't compile. Then I added a type signature:

expand::a-b
expand (x,y,z) = (x,y,z)
expand (x,y) = (x,y,1)

--It still didn't compile. I think the reason is that the following is  
disallowed:


f::a-b
f x = x

--Is it possible to get around this and write the expand function?  
Of course, x and y may be of different types




___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Is it possible to represent such polymorphism?

2011-10-02 Thread Du Xi

Quoting Andrew Coppin andrewcop...@btinternet.com:


On 02/10/2011 02:04 PM, Du Xi wrote:


--It still didn't compile. I think the reason is that the following is
disallowed:

f::a-b
f x = x


The type a - b doesn't mean what you think it does.

It does /not/ mean that f is allowed to return any type it wants to. It
means that f must be prepaired to return any type that /the caller/
wants it to. So, given ANY POSSIBLE INPUT, the function must be able to
construct a value of ANY POSSIBLE TYPE.

This is, of course, impossible. The only way you can implement a
function with this type signature is to cheat.



Also, you can't just take x, which has type a, and then pretend that it
has type b instead. Haskell doesn't work like that. Your type signature
says that the result type can be different than the input type, but
your function definition forces the result to always be /the same/ type
as the input. Hence, it is rejected.



That aside, the fundamental problem here is that each tuple type is a
different, completely unrelated type, as far as the type system is
concerned. (x,y) and (x,y,z) might look similar to you, but to the type
system they're as similar as, say, Either x y and StateT x y z.

In Haskell, the only way to get a function to work for several
unrelated types (but not /every/ possible type) is to use classes.
Depending on exactly what you're trying to do, you might be better
using lists, or perhaps some custom data type. It depends what you want
to do.

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe



Then again , in typeclass definition how can I express the type a-b  
where a is the type parameter of the class and b is a type deduced  
from the rules defined in each instance of the class, which varies on  
a per-instance basis? e.g.


instance ExampleClass a where
f :: a-SomeTypeWhichIsDifferentInEachInstance

What I want is some thing like this in C++:

float f(char x){ return 0.1f; }
int f(double x){ return 1; }




___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Is it possible to represent such polymorphism?

2011-10-02 Thread Du Xi

Quoting Victor Gorokgov m...@rkit.pp.ru:


02.10.2011 19:55, David Barbour пишет:

Use TypeFamilies.


{-# LANGUAGE TypeFamilies #}
...
type family FType a :: *
type instance FType Char = Float
type instance FType Double = Int

class ExampleClass a where
f :: a - FType a



Better to include type in class.

class ExampleClass a where
type FType a
f :: a - FType a

instance ExampleClass Char where
type FType Char = Float
f char = ...

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


I guess this is what I want, thank you all. Although I still wonder  
why something so simple in C++ is actually more verbose and requires  
less known features in Haskell...What was the design intent to  
disallow simple overloading?



___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe