map - Type check error in Haskell monad chaining -


i'm new haskell , i'm following example on rwh. i'm having problems following programs on chapter 14:

import qualified data.map m  type personname = string type phonenumber = string type billingaddress = string data mobilecarrier = honest_bobs_phone_network                    | morrisas_marvelous_mobiles                    | petes_plutocratic_phones                      deriving (eq, ord)  findcarrierbillingaddress :: personname                           -> m.map personname phonenumber                           -> m.map phonenumber mobilecarrier                           -> m.map mobilecarrier billingaddress                           -> maybe billingaddress -- work findcarrierbillingaddress person phonemap carriermap addressmap =   phone <- m.lookup person phonemap   carrier <- m.lookup phone carriermap   address <- m.lookup carrier addressmap   return address  -- not work: findcarrierbillingaddress person phonemap carriermap addressmap =     return person >>=     lookup phonemap >>=     lookup carriermap >>=     lookup addressmap   lookup = flip m.lookup 

it seems when writing findcarrierbillingaddres in monad chaining format using >>=, not type check:

/home/bruce/programming/haskell/real/ch14/hello.hs:21:9:     couldn't match type `[char]' `mobilecarrier'     expected type: mobilecarrier -> maybe billingaddress       actual type: personname -> maybe billingaddress     in return type of call of `lookup'     in second argument of `(>>=)', namely `lookup addressmap'     in expression:       return person >>= lookup phonemap >>= lookup carriermap       >>= lookup addressmap  /home/bruce/programming/haskell/real/ch14/hello.hs:21:16:     couldn't match type `mobilecarrier' `[char]'     expected type: m.map personname billingaddress       actual type: m.map mobilecarrier billingaddress     in first argument of `lookup', namely `addressmap'     in second argument of `(>>=)', namely `lookup addressmap'     in expression:       return person >>= lookup phonemap >>= lookup carriermap       >>= lookup addressmap failed, modules loaded: none. 

the question is.. why second format using >>= not type check?

it's monomorphism restriction acting again. since have pattern binding no type signature inferred type monomorphic , therefore determined first usage.

just change to

lookup m k = flip m.lookup m k 

or even

lookup m = flip m.lookup m 

you have convince ghc generalize function won't when it's simple pattern binding. adding parameter turns function binding means generalized.

if lost bit there, i've blogged this


Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

css - Firefox for ubuntu renders wrong colors -