[Haskell-beginners] fromIntegral

Brandon S Allbery KF8NH allbery at ece.cmu.edu
Sat Oct 2 18:58:02 EDT 2010


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 10/2/10 17:28 , Thomas wrote:
>> Is there a way to display a value along with its type during program
>> execution?  I know about Show; is there something similar like ShowWithType
>> (or even ShowType) that (if implemented) will generate a string of a value
>> along with its type?
> 
> AFAIK in GHCi you can do both, but not simultaneously:
> Prelude> let a = 5::Int
> Prelude> :t a
> a :: Int
> Prelude> a
> 5

Data.Typeable.typeOf works for monomorphic types.  I think there's some Oleg
deep magic for (a subset of?) polymorphic types.

> Prelude> [] == []
> True
> 
> You can actually compare [] to itself.

This only works in ghci, because of extended defaulting; both []s are given
the type [()].  In GHC proper, unless you explicitly enable the extended
defaulting rules, defaulting only works for numeric types; [] won't compare
to [] because neither can be typed, but [0] compares to [0] despite 0 being
polymorphic (depending on context it can be any instance of typeclass Num)
because defaulting gives it the type Integer.

>> It also makes sense to me to say that == is a collection of more concrete
>> functions from which one is selected depending on the type required by the
>> expression within which == appears.
> 
> See above - [] is not a function.

[] isn't relevant here.

>> Since the required type is known at compile time, it would seem that the
>> selection of which == to use could be made at compile time. One shouldn't

Not always; Haskell is "open", by default all functions are exported from
modules and therefore use in another module must be considered by the
compiler unless you told it not to export it.

If you turn on optimization, then the compiler *may* decide to "specialize"
particular uses of (==) to avoid the dictionary --- provided it can prove
that it knows all the possible types that the particular use can take.  (You
can influence this by use of the {-# SPECIALIZE #-} pragma; see the ghc manual.)

>> standard definition of an overloaded function.  So why is there an objection
>> to simply saying that == is overloaded and letting it go at that?

Precision.  "Overloading" as it's normally used with respect to programming
languages doesn't constrain what the overloads do.  Imagine, if you will,
what the typeclass dictionary for C++'s "<<" (which in Haskell is
(Data.Bits.shiftL :: Bits b => b -> Int -> b)) would look like, and how you
would define, say, Data.Bits.bit for the ostream variant.

Aside:  Haskell doesn't try to prove that a given instance of a typeclass
makes sense beyond making sure the types match, but users of the typeclass
can and do assume they make sense; also, type inference can lead the
compiler to do unexpected things when oddball instances such as Num Char are
defined, in expressions where one would not expect that instance to be used.

This is one reason why many of us use type inference only sparingly;
specifying the types you expect in as many places as possible both
sanity-checks your thinking and helps force type errors to be detected where
they actually occur instead of where type inference causes the compiler to
realize something doesn't make sense, which may be in a completely different
function elsewhere.  This may be most common with numeric types, where
forgetfully using (/) on an Int or Integer causes the compiler to say it
can't find a a type that is both Integral and Fractional somewhere else
where the result of said (/) is ultimately passed to something requiring an
Integral instance.

> For example function arity is not required to be the same when overloading
> methods - and restrictions on types are very different, too.

(==) is a poor example here, as most languages won't let you alter the arity
of operators.

- -- 
brandon s. allbery     [linux,solaris,freebsd,perl]      allbery at kf8nh.com
system administrator  [openafs,heimdal,too many hats]  allbery at ece.cmu.edu
electrical and computer engineering, carnegie mellon university      KF8NH
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.10 (Darwin)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAkynuPoACgkQIn7hlCsL25XsQgCgmCrP6TF9a2+jUE07KxhM4Cj4
n8UAoJWsBCvlogvZsU9/5eWXqtszfmPM
=3ta5
-----END PGP SIGNATURE-----


More information about the Beginners mailing list