Is it possible to represent such polymorphism? Classic List Threaded 31 messages 12
Open this post in threaded view
|

Is it possible to represent such polymorphism?

 --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 [hidden email] http://www.haskell.org/mailman/listinfo/haskell-cafe
Open this post in threaded view
|

Re: Is it possible to represent such polymorphism?

 2-tuple and 3-tuple are not the same type.So to do this you must use typeclasses.Plus you have to deal with the type parametersclass To3Tuple a where   expand :: a -> (Int, Int, Int)instance To3Tuple (Int, Int, Int) where    expand = idinstance To3Tuple (Int, Int) where   expand (x,y) = (x,y,1)Here I had to force my tuples to be tuples of integers.It's more complicated if you want polymorphism. 2011/10/2 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 [hidden email] http://www.haskell.org/mailman/listinfo/haskell-cafe _______________________________________________ Haskell-Cafe mailing list [hidden email] http://www.haskell.org/mailman/listinfo/haskell-cafe
Open this post in threaded view
|

Re: Is it possible to represent such polymorphism?

 In reply to this post by sdiyazg 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 [hidden email] http://www.haskell.org/mailman/listinfo/haskell-cafe
Open this post in threaded view
|

Re: Is it possible to represent such polymorphism?

 In reply to this post by sdiyazg On Sun, Oct 2, 2011 at 6:04 AM, Du Xi wrote: --Is it possible to get around this and write the "expand" function? Of course, x and y may be of different typesNot as written, but try HList.  _______________________________________________ Haskell-Cafe mailing list [hidden email] http://www.haskell.org/mailman/listinfo/haskell-cafe
Open this post in threaded view
|

Re: Is it possible to represent such polymorphism?

Open this post in threaded view
|

Re: Is it possible to represent such polymorphism?

 On Sun, Oct 2, 2011 at 8:45 AM, Du Xi wrote: 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; }Use TypeFamilies.{-# LANGUAGE TypeFamilies #}...type family FType a :: *type instance FType Char = Float type instance FType Double = Intclass ExampleClass a where  f :: a -> FType a _______________________________________________ Haskell-Cafe mailing list [hidden email] http://www.haskell.org/mailman/listinfo/haskell-cafe
Open this post in threaded view
|

Re: Is it possible to represent such polymorphism?

 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 [hidden email] http://www.haskell.org/mailman/listinfo/haskell-cafe
Open this post in threaded view
|

Re: Is it possible to represent such polymorphism?

Open this post in threaded view
|

Re: Is it possible to represent such polymorphism?

 Finally I got what I meant: class ExpandTuple t where         type Result t         expand :: t->Result t instance (Integral a)=>ExpandTuple (a,a) where         type Result (a,a) = (a,a,a)         expand (x,y) = (x,y,1) instance (Integral a)=>ExpandTuple (a,a,a) where         type Result (a,a,a) = (a,a,a)         expand = id But it's so verbose (even more so than similar C++ template code I   guess), introduces an additional name (the typeclass) into the current   scope, and requires 2 extensions: TypeFamilies and   FlexibleInstances.Is there a cleaner way to do this? _______________________________________________ Haskell-Cafe mailing list [hidden email] http://www.haskell.org/mailman/listinfo/haskell-cafe
Open this post in threaded view
|

Re: Is it possible to represent such polymorphism?

 What are you actually trying to do?  This seems like a rather unusual function. Edward Excerpts from sdiyazg's message of Sun Oct 02 15:17:07 -0400 2011: > Finally I got what I meant: > > > class ExpandTuple t where >     type Result t >     expand :: t->Result t > > instance (Integral a)=>ExpandTuple (a,a) where >     type Result (a,a) = (a,a,a) >     expand (x,y) = (x,y,1) > > instance (Integral a)=>ExpandTuple (a,a,a) where >     type Result (a,a,a) = (a,a,a) >     expand = id > > But it's so verbose (even more so than similar C++ template code I   > guess), introduces an additional name (the typeclass) into the current   > scope, and requires 2 extensions: TypeFamilies and   > FlexibleInstances.Is there a cleaner way to do this? > _______________________________________________ Haskell-Cafe mailing list [hidden email] http://www.haskell.org/mailman/listinfo/haskell-cafe
Open this post in threaded view
|

Re: Is it possible to represent such polymorphism?

 On Sun, Oct 2, 2011 at 4:26 PM, Edward Z. Yang <[hidden email]> wrote: > What are you actually trying to do?  This seems like a rather > unusual function. If you're new to the language, most likely you're doing something wrong if you need this kind of function.  =) -- Felipe. _______________________________________________ Haskell-Cafe mailing list [hidden email] http://www.haskell.org/mailman/listinfo/haskell-cafe
Open this post in threaded view
|

Re: Is it possible to represent such polymorphism?

Open this post in threaded view
|

Re: Is it possible to represent such polymorphism?

 In reply to this post by sdiyazg On Sun, Oct 2, 2011 at 2:17 PM,  <[hidden email]> wrote: > Finally I got what I meant: > > > class ExpandTuple t where >        type Result t >        expand :: t->Result t > > instance (Integral a)=>ExpandTuple (a,a) where >        type Result (a,a) = (a,a,a) >        expand (x,y) = (x,y,1) > > instance (Integral a)=>ExpandTuple (a,a,a) where >        type Result (a,a,a) = (a,a,a) >        expand = id > If I were writing this sort of function, I would simply write: > expand (x, y) = (x, y, 1) and I would leave it at that. Since your 'expand' doesn't do anything the three-tuples, I don't see why I would want to call the function with a three-tuple argument. But I don't know your full use case. Antoine > But it's so verbose (even more so than similar C++ template code I guess), > introduces an additional name (the typeclass) into the current scope, and > requires 2 extensions: TypeFamilies and FlexibleInstances.Is there a cleaner > way to do this? > > > _______________________________________________ > Haskell-Cafe mailing list > [hidden email] > http://www.haskell.org/mailman/listinfo/haskell-cafe> _______________________________________________ Haskell-Cafe mailing list [hidden email] http://www.haskell.org/mailman/listinfo/haskell-cafe
Open this post in threaded view
|

Re: Is it possible to represent such polymorphism?

 In reply to this post by sdiyazg Assuming that z :: Int, you can declare an algebraic datatype data TwoOrThree a b = Three (a, b, Int)                     | Two (a, b)    deriving(Show, Eq) -- so you can experiment And then define expand as expand :: TwoOrThree a b -> (a, b, Int) expand (Three tuple) = tuple expand (Two (a, b)) = (a, b, 1) Tom (amindfv) On Oct 2, 2011 6:04 AM, "Du Xi" <[hidden email]> wrote: _______________________________________________ Haskell-Cafe mailing list [hidden email] http://www.haskell.org/mailman/listinfo/haskell-cafe
Open this post in threaded view
|

Re: Is it possible to represent such polymorphism?

Open this post in threaded view
|

Re: Is it possible to represent such polymorphism?

Open this post in threaded view
|

Re: Is it possible to represent such polymorphism?

Open this post in threaded view
|

Re: Is it possible to represent such polymorphism?

Open this post in threaded view
|