查看 ghci 的输出:
Prelude> :t Data.Map.lookup
Data.Map.lookup :: Ord k => k -> Data.Map.Map k a -> Maybe a
Prelude> :t flip Data.Map.lookup
flip Data.Map.lookup :: Ord a => Data.Map.Map a a1 -> a -> Maybe a1
Prelude> let look = flip Data.Map.lookup
Loading package array-0.3.0.2 ... linking ... done.
Loading package containers-0.4.0.0 ... linking ... done.
Prelude> :t look
look :: Data.Map.Map () a -> () -> Maybe a
为什么look
推断类型与类型不同flip Data.Map.lookup
?
给你一些背景。最初我有一个小程序,并试图弄清楚它为什么会产生编译器错误:
import qualified Data.Map as M
type A = String
type B = String
data C = C1 | C2 | C3
deriving (Eq, Ord)
type D = String
z :: A -> M.Map A B -> M.Map B C -> M.Map C D -> Maybe D
z a aToB bToC cToD = look aToB a >>= look bToC >>= look cToD
where look = flip M.lookup
Ghci 的反应:
Prelude> :load main.hs
[1 of 1] Compiling Main ( main.hs, interpreted )
Failed, modules loaded: none.
main.hs:10:52:
Couldn't match expected type `C' with actual type `[Char]'
Expected type: C -> Maybe D
Actual type: A -> Maybe a0
In the return type of a call of `look'
In the second argument of `(>>=)', namely `look cToD'
我发现这个变体编译得很好(类型定义是一样的):
x :: A -> M.Map A B -> M.Map B C -> Maybe C
x a aToB bToC = look aToB a >>= look bToC
where look = flip M.lookup
y :: A -> M.Map A B -> M.Map B C -> M.Map C D -> Maybe D
y a aToB bToC cToD = (x a aToB bToC) >>= look cToD
where look = flip M.lookup
经过一些实验后发现,如果我look
明确输入 type of - 第一个版本也编译得很好:
z :: A -> M.Map A B -> M.Map B C -> M.Map C D -> Maybe D
z a aToB bToC cToD = look aToB a >>= look bToC >>= look cToD
where look :: (Ord a) => M.Map a b -> a -> Maybe b
look = flip M.lookup
这引出了我的第一个问题。