[Hugs-users] the built-in function enumFrom
Daniel Fischer
daniel.is.fischer at web.de
Thu Oct 29 07:34:10 EDT 2009
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: hugs-bugs at haskell.org
> || || Version: Nov 2002
>
> ---------------------------------
> Paqui Lucio
> Dpto de LSI
> Facultad de Informática
> Paseo Manuel de Lardizabal, 1
> 20080-San Sebastián
> SPAIN
> ---------------------------------
> e-mail: paqui.lucio at ehu.es
> Tfn: (+34) (9)43 015049
> Fax: (+34) (9)43 015590
> Web: http://www.sc.ehu.es/paqui
> ---------------------------------
More information about the Hugs-Users
mailing list