[Haskell-beginners] question on types
Brandon Allbery
allbery.b at gmail.com
Fri Jul 29 02:23:11 CEST 2011
On Thu, Jul 28, 2011 at 19:42, Jake Penton <djp at arqux.com> wrote:
> h:: a
> h = 'a'
>
> to which ghci replies:
>
> Couldn't match type `a' with `Char'
> `a' is a rigid type variable bound by
> the type signature for c :: a
> at /Users/David/Project/EoP/ch04/weak.hs:114:1
> In the expression: 'a'
> In an equation for `c': c = 'a'
>
> This last example is probably the most basic one which I need to
> understand. But, why is the problem apparently a different one than in the
> definition of "f" above?
>
The other one was complicated by polymorphism: a numeric literal is
compiled into a call to fromIntegral, whose result type is Num a => a.
The problem is that, when you say something's type is "a", you are not
saying "pick an appropriate type for me"; you are saying "whoever invokes
this can ask for any type it wants" (equivalently: "I promise to be able to
produce *any possible* type"). But then you give the value as Num a => a in
the first example and Char in the second example, neither of which is "any
possible type".
An explicit type is a complete contract; having contracted to produce an "a"
(any type), you can't then offer only a Char or a Num a => a. You have to
satisfy the contract which says "any type", otherwise you're doing the type
checking equivalent of a bait-and-switch.
You can't express "pick a type for me" in a type signature; types are
concrete, and a type variable in a signature is a concrete "anything",
meaning the caller can request whatever it wants and you must produce it.
The type must be *completely* described by the signature; what it says is
what you're committed to, and you can't then offer something else. If you
need a partial type signature, there are some tricks you can use which let
you force types in the implementation without specifying a concrete
signature (see http://okmij.org/ftp/Haskell/types.html#partial-sigs).
--
brandon s allbery allbery.b at gmail.com
wandering unix systems administrator (available) (412) 475-9364 vm/sms
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20110728/e6a22770/attachment.htm>
More information about the Beginners
mailing list