[Haskell-cafe] Re: What does the Haskell type system do with
"show (1+2)"?
Daniel Fischer
daniel.is.fischer at web.de
Fri Jan 13 06:37:05 EST 2006
Am Freitag, 13. Januar 2006 11:12 schrieb Christian Maeder:
> Jared Updike wrote:
> > http://www.haskell.org/onlinereport/decls.html#default-decls
> > http://www.haskell.org/tutorial/numbers.html#sect10.4
>
> I still don't see, why it works for show but not for my_show.
Says the report:
Ambiguities in the class Num are most common, so Haskell provides another way
to resolve them---with a default declaration:
default (t1 , ... , tn)
where n>=0, and each ti must be a type for which Num ti holds. 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
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
That's the point!
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6.2--6.3, pages -- show the numeric classes, and Figure 6.1, page , shows the
classes defined in the Prelude.)
Each defaultable variable is replaced by the first type in the default list
that is an instance of all the ambiguous variable's classes. It is a static
error if no such type is found.
>
> > On 1/12/06, Jeff.Harper at handheld.com <Jeff.Harper at handheld.com> wrote:
>
> [...]
>
> >> class (Show a) => My_show a where
> >> my_show :: a -> String
>
> If I let this be
>
> class My_show a where
> my_show :: a -> String
>
> >> instance My_show Int where
> >> my_show a = show a ++ " :: Int"
> >>
> >> instance My_show Integer where
> >> my_show a = show a ++ " :: Integer"
>
> What is the difference to the builtin Show class?
It's not declared in the prelude or standard libraries.
>
> Christian
Now the question is, could that restriction be lifted, i.e., would it be
possible/worthwhile to let defaulting also take place if user defined classes
are involved?
Cheers,
Daniel
More information about the Haskell-Cafe
mailing list