newbie's perplexities about instance of "Info a" of Chapter 13 (Thompson's Haskell book)

Hi to everyone, I'm learning Haskell going through Thompson's craft of functional programming (3rd ed). As of now I've reached Chapter 13 where classes definition and instances are discussed. At some point the "Info a" class is introduced which has the following interface: class Info a where examples :: [a] size :: a -> Int then it goes on explaining that every instance of that class must comply to the interface shown. So far so good. My perplexities start when actual instances are implemented. Take, for example the following one: instance Info Int where examples = [-100..100] size _ = 1 I promptly tried to fiddle with this via ghci and while I was able to access the "size" function of the interface I have no clue about how to reach the "examples" part and that prevents me from understanding the next step when an instance of "Info [a]" is discussed. About that I don't quite get the definition of its "examples", and the reasons it is defined taking into accounts "all the one and two element lists that can be built up from exmaples of type a" (cit.) Can anyone shed some light about this, please? Thanks in advance for your help. Best regards, Carmine

In ghci try this:
examples :: [Int]
It is enough that a is mentioned in the type of every function so that the
compiler can figure out which instance is required when it is used. In
size you have to pass an a in to figure out what type it is, but in
examples, it is enough to know that it is a list of a's.
On Tue, Sep 10, 2013 at 1:59 PM, Carmine Moleti
Hi to everyone,
I'm learning Haskell going through Thompson's craft of functional programming (3rd ed).
As of now I've reached Chapter 13 where classes definition and instances are discussed.
At some point the "Info a" class is introduced which has the following interface:
class Info a where examples :: [a] size :: a -> Int
then it goes on explaining that every instance of that class must comply to the interface shown. So far so good.
My perplexities start when actual instances are implemented.
Take, for example the following one:
instance Info Int where examples = [-100..100] size _ = 1
I promptly tried to fiddle with this via ghci and while I was able to access the "size" function of the interface I have no clue about how to reach the "examples" part and that prevents me from understanding the next step when an instance of "Info [a]" is discussed. About that I don't quite get the definition of its "examples", and the reasons it is defined taking into accounts "all the one and two element lists that can be built up from exmaples of type a" (cit.)
Can anyone shed some light about this, please?
Thanks in advance for your help.
Best regards, Carmine
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

David McBride
In ghci try this:> examples :: [Int] It is enough that a is mentioned in the type of every function so that the compiler can figure out which instance is required when it is used. In size you have to pass an a in to figure out what type it is, but in examples, it is enough to know that it is a list of a's.
Makes sense now. Thanks again for your help sir. Regards, Carmine
participants (2)
-
Carmine Moleti
-
David McBride