the built-in function enumFrom

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

the built-in function enumFrom

Paqui Lucio-2
the built-in function enumFrom

Hi,
In Prelude.hs you can find the definition
enumFrom x = map toEnum [ fromEnum x ..]
Load a file.hs of just one line:
data TBool = Falsity | Undefined |Truth deriving (Enum, Show)
Then, I evaluate
enumFrom Falsity
and, as expected, it gives
[Falsity,Undefined,Truth]
However, the expression
map toEnum [ fromEnum Falsity ..]
gives first an ambiguity problem and then the problem I expected

Main> map toEnum [ fromEnum Falsity ..]
ERROR - Unresolved overloading
*** Type : Enum a => [a]
*** Expression : map toEnum (enumFrom (fromEnum Falsity))

Main> map toEnum [ fromEnum Falsity ..]::[TBool]
[Falsity,Undefined,Truth,
Program error: {_toEnum TBool_Falsity 3}
(98 reductions, 301 cells)

Why the function enumFrom works perfectly in spite of its erroneous definition?
Thanks in advance,
Paqui

P.D.
I am using the
|| || || || || || ||__ Hugs 98: Based on the Haskell 98 standard
||___|| ||__|| ||__|| __|| Copyright (c) 1994-2002
||---|| ___|| World Wide Web:
http://haskell.org/hugs
|| || Report bugs to: [hidden email]
|| || Version: Nov 2002


---------------------------------
Paqui Lucio                               
Dpto de LSI                   
Facultad de Informática
Paseo Manuel de Lardizabal, 1
20080-San Sebastián
SPAIN
---------------------------------
e-mail: [hidden email]
Tfn: (+34) (9)43 015049 
Fax: (+34) (9)43 015590
Web: http://www.sc.ehu.es/paqui
---------------------------------


_______________________________________________
Hugs-Users mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/hugs-users
Reply | Threaded
Open this post in threaded view
|

Re: the built-in function enumFrom

Daniel Fischer-4
Am Donnerstag 29 Oktober 2009 11:06:31 schrieb Paqui Lucio:
> Hi,
> In Prelude.hs you can find the definition
> enumFrom x = map toEnum [ fromEnum x ..]

That's the default implementation, when an instance declaration doesn't provide an
explicit method.

> Load a file.hs of just one line:
> data TBool = Falsity | Undefined |Truth deriving (Enum, Show)
> Then, I evaluate
> enumFrom Falsity
> and, as expected, it gives
> [Falsity,Undefined,Truth]
> However, the expression
> map toEnum [ fromEnum Falsity ..]
> gives first an ambiguity problem

Of course.
[fromEnum Falsity .. ] is a list of Ints, namely [0 .. ]
(which is [0 .. maxBound :: Int]).
Mapping toEnum over that list can give a list of any type belonging to Enum, so it must
somehow be specified which type to use (Bool, Int, Integer, (), Char, ...)

> and then the problem I expected
> Main> map toEnum [ fromEnum Falsity ..]
> ERROR - Unresolved overloading
> *** Type : Enum a => [a]
> *** Expression : map toEnum (enumFrom (fromEnum Falsity))
> Main> map toEnum [ fromEnum Falsity ..]::[TBool]
> [Falsity,Undefined,Truth,
> Program error: {_toEnum TBool_Falsity 3}
> (98 reductions, 301 cells)
> Why the function enumFrom works perfectly in spite of its erroneous
> definition?

Because the implementation is smarter than the default method :)
Given a type with k constructors, the implementation will, upon a clause "deriving Enum",
create methods enumFrom(Then) that work, probably by doing something like

enumFrom val = map toEnum [fromEnum val .. k-1]

> Thanks in advance,
> Paqui
> P.D.
> I am using the
>
> || || || || || || ||__ Hugs 98: Based on the Haskell 98 standard
> ||
> ||___|| ||__|| ||__|| __|| Copyright (c) 1994-2002
> ||---|| ___|| World Wide Web: http://haskell.org/hugs
> ||
> || || Report bugs to: [hidden email]
> || || Version: Nov 2002
>
> ---------------------------------
> Paqui Lucio
> Dpto de LSI
> Facultad de Informática
> Paseo Manuel de Lardizabal, 1
> 20080-San Sebastián
> SPAIN
> ---------------------------------
> e-mail: [hidden email]
> Tfn: (+34) (9)43 015049
> Fax: (+34) (9)43 015590
> Web: http://www.sc.ehu.es/paqui
> ---------------------------------

_______________________________________________
Hugs-Users mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/hugs-users