nntpnews.net

Global Usenet Archiver


Register

[Haskell-beginners] Typeclass problems

Reply

  #1  
Old 04-02-10, 10:14 PM
Patrick LeBoutillier
 
Posts: n/a
Default [Haskell-beginners] Typeclass problems

Hi all,

I've written a small type class that is meant to behave a bit like Show:

class Show a => Echo a where
echo :: a -> String
echoList :: [a] -> String
echoListSep :: a -> String

echo = show
echoListSep _ = " "

echoList [] = ""
echoList [x] = echo x
echoList (x:xs) = echo x ++ echoListSep x ++ echoList xs


instance Echo Char where
echo c = [c]
echoListSep _ = ""

instance Echo a => Echo [a] where
echo = echoList

instance Echo Int where
instance Echo Integer where
instance Echo Double where
instance Echo Float where


gen = map (const 1)

f xs = map echo $ gen xs


However the code doesn't compile on the last line.
But it does compile if I replace 'echo' with 'show'.
What's missing to make my typeclass behave like Show and accept input
of any type (besides all the missing instances...)?
I looked at the code for Show but I didn't see anything that looked "magical"...


Thanks,

Patrick


--
=====================
Patrick LeBoutillier
Rosemère, Québec, Canada
_______________________________________________
Beginners mailing list
Code:
Content visible to registered users only.
Code:
Content visible to registered users only.
Reply With Quote
  #2  
Old 04-02-10, 10:30 PM
Stephen Blackheath [to Haskell-Beginners]
 
Posts: n/a
Default Re: [Haskell-beginners] Typeclass problems

Patrick,

I fixed it by adding a type signature to this line:

gen = map (const (1::Int))

The problem is that the literal '1' is of type Num t => t, which is
never tied to a concrete type anywhere. It has to know the type
definitely before it can decide which type class to use.


Steve

Patrick LeBoutillier wrote:
> Hi all,
>
> I've written a small type class that is meant to behave a bit like Show:
>
> class Show a => Echo a where
> echo :: a -> String
> echoList :: [a] -> String
> echoListSep :: a -> String
>
> echo = show
> echoListSep _ = " "
>
> echoList [] = ""
> echoList [x] = echo x
> echoList (x:xs) = echo x ++ echoListSep x ++ echoList xs
>
>
> instance Echo Char where
> echo c = [c]
> echoListSep _ = ""
>
> instance Echo a => Echo [a] where
> echo = echoList
>
> instance Echo Int where
> instance Echo Integer where
> instance Echo Double where
> instance Echo Float where
>
>
> gen = map (const 1)
>
> f xs = map echo $ gen xs
>
>
> However the code doesn't compile on the last line.
> But it does compile if I replace 'echo' with 'show'.
> What's missing to make my typeclass behave like Show and accept input
> of any type (besides all the missing instances...)?
> I looked at the code for Show but I didn't see anything that looked "magical"...
>
>
> Thanks,
>
> Patrick
>
>

_______________________________________________
Beginners mailing list
Code:
Content visible to registered users only.
Code:
Content visible to registered users only.
Reply With Quote
  #3  
Old 04-02-10, 10:39 PM
Daniel Fischer
 
Posts: n/a
Default Re: [Haskell-beginners] Typeclass problems

Am Donnerstag 04 Februar 2010 23:14:33 schrieb Patrick LeBoutillier:
> Hi all,
>
> I've written a small type class that is meant to behave a bit like Show:
>
> class Show a => Echo a where
> echo :: a -> String
> echoList :: [a] -> String
> echoListSep :: a -> String
>
> echo = show
> echoListSep _ = " "
>
> echoList [] = ""
> echoList [x] = echo x
> echoList (x:xs) = echo x ++ echoListSep x ++ echoList xs
>
>
> instance Echo Char where
> echo c = [c]
> echoListSep _ = ""
>
> instance Echo a => Echo [a] where
> echo = echoList
>
> instance Echo Int where
> instance Echo Integer where
> instance Echo Double where
> instance Echo Float where
>
>
> gen = map (const 1)
>
> f xs = map echo $ gen xs
>
>
> However the code doesn't compile on the last line.
> But it does compile if I replace 'echo' with 'show'.
> What's missing to make my typeclass behave like Show and accept input
> of any type (besides all the missing instances...)?
> I looked at the code for Show but I didn't see anything that looked
> "magical"...


The "magic" is that Show is defined in the standard libraries.
Read
Code:
Content visible to registered users only.
In particular, the last bullet point of

"In situations where an ambiguous type is discovered, an ambiguous type
variable, v, is defaultable if:

* v appears only in constraints of the form C v, where C is a class,
and
* at least one of these classes is a numeric class, (that is, Num or a
subclass of Num), and
* all of these classes are defined in the Prelude or a standard library
(Figures 6.2--6.3, pages -- show the numeric classes, and Figure 6.1, page
, shows the classes defined in the Prelude.) "


Now,

gen xs :: Num a => [a]

And you want to map echo over it, so we get the constraint
(Num a, Echo a).

Echo is not defined in the standard libraries, thus no defaulting takes
place, you have to specify the type of 1 explicitly,

map echo (gen xs :: [Int])

should work.

In real programmes, the type is often deducible from the context, so you'll
be bitten by the defaulting restrictions less often then than at the ghci
prompt.

There's a proposal (or a couple) to change the defaulting rules in coming
language standards, see e.g.
Code:
Content visible to registered users only.
prime/wiki/Defaulting


>
>
> Thanks,
>
> Patrick


HTH,
Daniel

_______________________________________________
Beginners mailing list
Code:
Content visible to registered users only.
Code:
Content visible to registered users only.
Reply With Quote
  #4  
Old 06-02-10, 01:19 PM
Patrick LeBoutillier
 
Posts: n/a
Default Re: [Haskell-beginners] Typeclass problems

Daniel,

Thanks a lot for the info! That's a subtlety I wasn't aware of.

Patrick

On Thu, Feb 4, 2010 at 5:37 PM, Daniel Fischer < - > wrote:
> Am Donnerstag 04 Februar 2010 23:14:33 schrieb Patrick LeBoutillier:
>> Hi all,
>>
>> I've written a small type class that is meant to behave a bit like Show:
>>
>> class Show a => Echo a where
>> * echo :: a -> String
>> * echoList :: [a] -> String
>> * echoListSep :: a -> String
>>
>> * echo = show
>> * echoListSep _ = " "
>>
>> * echoList [] = ""
>> * echoList [x] = echo x
>> * echoList (x:xs) = echo x ++ echoListSep x ++ echoList xs
>>
>>
>> instance Echo Char where
>> * echo c = [c]
>> * echoListSep _ = ""
>>
>> instance Echo a => Echo [a] where
>> * echo = echoList
>>
>> instance Echo Int where
>> instance Echo Integer where
>> instance Echo Double where
>> instance Echo Float where
>>
>>
>> gen = map (const 1)
>>
>> f xs = map echo $ gen xs
>>
>>
>> However the code doesn't compile on the last line.
>> But it does compile if I replace 'echo' with 'show'.
>> What's missing to make my typeclass behave like Show and accept input
>> of any type (besides all the missing instances...)?
>> I looked at the code for Show but I didn't see anything that looked
>> "magical"...

>
> The "magic" is that Show is defined in the standard libraries.
> Read
Code:
Content visible to registered users only.
> In particular, the last bullet point of
>
> "In situations where an ambiguous type is discovered, an ambiguous type
> variable, v, is defaultable if:
>
> * ** v appears only in constraints of the form C v, where C is a class,
> and
> * ** at least one of these classes is a numeric class, (that is, Num or a
> subclass of Num), and
> * ** all of these classes are defined in the Prelude or a standard library
> (Figures 6.2--6.3, pages -- show the numeric classes, and Figure 6.1, page
> , shows the classes defined in the Prelude.) "
>
>
> Now,
>
> gen xs :: Num a => [a]
>
> And you want to map echo over it, so we get the constraint
> (Num a, Echo a).
>
> Echo is not defined in the standard libraries, thus no defaulting takes
> place, you have to specify the type of 1 explicitly,
>
> map echo (gen xs :: [Int])
>
> should work.
>
> In real programmes, the type is often deducible from the context, so you'll
> be bitten by the defaulting restrictions less often then than at the ghci
> prompt.
>
> There's a proposal (or a couple) to change the defaulting rules in coming
> language standards, see e.g.
Code:
Content visible to registered users only.
> prime/wiki/Defaulting
>
>
>>
>>
>> Thanks,
>>
>> Patrick

>
> HTH,
> Daniel
>
>




--
=====================
Patrick LeBoutillier
Rosemère, Québec, Canada
_______________________________________________
Beginners mailing list
Code:
Content visible to registered users only.
Code:
Content visible to registered users only.
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
[Haskell-beginners] Typeclass question Patrick LeBoutillier fa.haskell 2 01-12-09 07:24 PM
[Haskell-beginners] typeclass error Amitava Shee fa.haskell 4 21-04-09 05:44 PM
[Haskell-cafe] Declaring each instance of a typeclass to be also aninstance of another typeclass Mikhail Glushenkov fa.haskell 1 11-01-09 07:02 PM
[Haskell-beginners] Problems with one of my first examples Jeff C. Britton fa.haskell 2 15-12-08 10:38 PM
[Haskell-beginners] Ensuring consistency in typeclass instances? Colin Paul Adams fa.haskell 3 26-11-08 08:04 PM


All times are GMT +1. The time now is 12:01 AM. Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0



For ads on this site use independent advertising companies. These companies may use some data (which does not include your name, address, email address or telephone number) about your visits to this and other websites to create advertisements on products and services you might enjoy. If you'd like more information and to know the options available to prevent the use of such information by these companies, click here

Abuse Ticket System