[Haskell-cafe] type checking that I can't figure out ...

Henning Thielemann lemming at henning-thielemann.de
Wed Jun 3 17:57:35 EDT 2009


Ross Mellgren schrieb:
> You've applied two solutions to get the value out -- pattern matching
> (Just reinfo) and fromJust. You should use one or the other, but not both:
> 
> -- pattern matching
> remLookupFwd :: (ReVars m t) => SimplRe t -> ReM m t (ReInfo t)
> remLookupFwd re
>    = do fwd <- gets resFwdMap
>         let { Just reinfo = M.lookup re fwd }                    -- PROBLEM
>         return reinfo
> 
> -- fromJust
> remLookupFwd :: (ReVars m t) => SimplRe t -> ReM m t (ReInfo t)
> remLookupFwd re
>    = do fwd <- gets resFwdMap
>         let { reinfo = fromJust (M.lookup re fwd) }                   
> -- PROBLEM
>         return reinfo
> 
> BTW, I would personally write this as one line (untested)
> 
> gets (fromJust . M.lookup re . resFwdMap)

fromJust should be avoided, since it is partial and if it results in an
error, the error message points to the implementation of fromJust, not
its application. Pattern matching is better, but 'maybe' and 'fromMaybe'
are best.



More information about the Haskell-Cafe mailing list