When Haskell runs it's type checker, it tries to "guess" the type of
each function. Thats why you can write: map (+1) and it knows that you're talking about a function of type: Num a => [a] -> [a] Another thing, called 'defaulting' resolves this, but you didn't ask about that, so I won't go into it. An expected type is one that you provide to the compiler in the form of a type signature, this can be used to specialize a general type (like the one I showed) or to resolve ambiguous types the compiler can't, or just for documentation/good practice. So when I write: foo :: Num a => [a] -> [a] foo ls = map (+1) ls The "expected type" for `foo` is `Num a => [a] -> [a]`. I imagine you're asking this because you got an error which said your expected type doesn't match your inferred type. That might, for instance, happen if I wrote: bar :: String bar = 'a' 'a' has type `Char`, since `String` is not `Char`, the type checker infers that 'a' has type char, but _expects_ it to be type String. Two solutions are as follows: --- Method 1 bar :: Char bar = 'a' --- Method 2 bar :: String bar = "a" Can you see why those two changes fix the problem? Also, just as a matter of process, I forwarded this to the haskell-beginners list, as I imagine type errors like these come up a lot, and someone probably has a better explanation over there. /Joe michael rice wrote: > as opposed to an "inferred type"? > > Michael > > > ------------------------------------------------------------------------ > > _______________________________________________ > Haskell-Cafe mailing list > [hidden email] > http://www.haskell.org/mailman/listinfo/haskell-cafe > A non-text attachment was scrubbed... Name: jfredett.vcf Type: text/x-vcard Size: 296 bytes Desc: not available Url : http://www.haskell.org/pipermail/beginners/attachments/20090628/b0eeda45/jfredett-0001.vcf |
Hey Joe, all,
Got it. Thanks! An associated question: In programming a local "helper" or "auxilliary" function such as dec2bin' in dec2bin :: Integer -> [Integer] dec2bin n = dec2bin' n [] ??????????? where dec2bin' n acc ??????????????????? | n == 0 = acc ??????????????????? | otherwise = let r = rem n 2 ????????????????????????????????????? m = div (n - r) 2 ????????????????????????????????? in dec2bin' m (r : acc) is there any way to assign a type signature to the helper function? Michael --- On Sun, 6/28/09, Joe Fredette <[hidden email]> wrote: From: Joe Fredette <[hidden email]> Subject: Re: [Haskell-cafe] What is an "expected type" ... To: "michael rice" <[hidden email]> Cc: "Haskell Cafe mailing list" <[hidden email]>, [hidden email] Date: Sunday, June 28, 2009, 11:29 AM When Haskell runs it's type checker, it tries to "guess" the type of each function. Thats why you can write: ???map (+1) and it knows that you're talking about a function of type: ???Num a => [a] -> [a] Another thing, called 'defaulting' resolves this, but you didn't ask about that, so I won't go into it. An expected type is one that you provide to the compiler in the form of a type signature, this can be used to specialize a general type (like the one I showed) or to resolve ambiguous types the compiler can't, or just for documentation/good practice. So when I write: ???foo :: Num a => [a] -> [a] ???foo ls = map (+1) ls The "expected type" for `foo` is `Num a => [a] -> [a]`. I imagine you're asking this because you got an error which said your expected type doesn't match your inferred type. That might, for instance, happen if I wrote: ???bar :: String ???bar = 'a' 'a' has type `Char`, since `String` is not `Char`, the type checker infers that 'a' has type char, but _expects_ it to be type String. Two solutions are as follows: ???--- Method 1 ???bar :: Char ???bar = 'a' ???--- Method 2 ???bar :: String ???bar = "a" Can you see why those two changes fix the problem? Also, just as a matter of process, I forwarded this to the haskell-beginners list, as I imagine type errors like these come up a lot, and someone probably has a better explanation over there. /Joe michael rice wrote: > as opposed to an "inferred type"? > > Michael > > > ------------------------------------------------------------------------ > > _______________________________________________ > Haskell-Cafe mailing list > [hidden email] > http://www.haskell.org/mailman/listinfo/haskell-cafe >??? -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20090628/28ff14de/attachment-0001.html |
Skipped content of type multipart/alternative-------------- next part --------------
A non-text attachment was scrubbed... Name: PGP.sig Type: application/pgp-signature Size: 195 bytes Desc: This is a digitally signed message part Url : http://www.haskell.org/pipermail/beginners/attachments/20090628/112a0bb5/PGP.bin |
Am Sonntag 28 Juni 2009 18:06:52 schrieb Brandon S. Allbery KF8NH:
> On Jun 28, 2009, at 12:02 , michael rice wrote: > > dec2bin :: Integer -> [Integer] > > dec2bin n = dec2bin' n [] > > where dec2bin' n acc > > > > | n == 0 = acc > > | otherwise = let r = rem n 2 > > > > m = div (n - r) 2 > > in dec2bin' m (r : acc) > > > > is there any way to assign a type signature to the helper function? > > Same way you do for a top level binding: > >> dec2bin :: Integer -> [Integer] > >> dec2bin n = dec2bin' n [] > >> where dec2bin' :: Integer -> [Integer] -> [Integer] > >> dec2bin' n acc > >> > >> | n == 0 = acc > >> | otherwise = let r = rem n 2 > >> > >> m = div (n - r) 2 > >> in dec2bin' m (r : acc) But, to mention it before it bites, putting type signatures involving type variables on local helper functions is not entirely straightforward. Consider inBase :: Integral a => a -> a -> [a] 0 `inBase` b = [0] n `inBase` b = local n [] where local 0 acc = acc local m acc = case m `divMod` b of (q,r) -> local q (r:acc) Now try giving a type signature to local. You can't. What is the type of local? It's (type of b) -> [type of b] -> [type of b], but "type of b" isn't available. If you try local :: a -> [a] -> [a] or local :: Integral a => a -> [a] -> [a], you are saying that local works for *every* type a (or for every type a which is an instance of Integral), because the 'a' from local's type signature is a new (implicitly forall'd) type variable. To be able to give local a type signature, you must bring the type variable 'a' into scope: {-# LANGUAGE ScopedTypeVariables #-} inBase :: forall a. Integral a => a -> a -> [a] 0 `inBase` b = [0] n `inBase` b = local n [] where local :: a -> [a] -> [a] -- now this a is the same a as the one above local 0 acc = acc local m acc = case m `divMod` b of (q,r) -> local q (r:acc) |
In reply to this post by Brandon S Allbery KF8NH
How else? ;-)
Thanks, Michael --- On Sun, 6/28/09, Brandon S. Allbery KF8NH <[hidden email]> wrote: From: Brandon S. Allbery KF8NH <[hidden email]> Subject: Re: [Haskell-cafe] What is an "expected type" ... To: "michael rice" <[hidden email]> Cc: "Brandon S. Allbery KF8NH" <[hidden email]>, "Joe Fredette" <[hidden email]>, [hidden email], "Haskell Cafe mailing list" <[hidden email]> Date: Sunday, June 28, 2009, 12:06 PM On Jun 28, 2009, at 12:02 , michael rice wrote:dec2bin :: Integer -> [Integer] dec2bin n = dec2bin' n [] ??????????? where dec2bin' n acc ??????????????????? | n == 0 = acc ??????????????????? | otherwise = let r = rem n 2 ????????????????????????????????????? m = div (n - r) 2 ????????????????????????????????? in dec2bin' m (r : acc) is there any way to assign a type signature to the helper function? Same way you do for a top level binding: dec2bin :: Integer -> [Integer] dec2bin n = dec2bin' n [] ??????????? where dec2bin' :: Integer -> [Integer] -> [Integer] ?? ? ? ? ? ? ? ? ?dec2bin' n acc ??????????????????? | n == 0 = acc ??????????????????? | otherwise = let r = rem n 2 ????????????????????????????????????? m = div (n - r) 2 ????????????????????????????????? in dec2bin' m (r : acc) --?brandon s. allbery [solaris,freebsd,perl,pugs,haskell] [hidden email] administrator [openafs,heimdal,too many hats] [hidden email] and computer engineering, carnegie mellon university ? ?KF8NH -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20090628/f1f2fb7e/attachment.html |
Free forum by Nabble | Edit this page |