Haskell手动键入此功能

时间:2018-06-26 23:23:08

标签: haskell type-inference

有人可以解释一下如何手动推断此函数f的类型吗? f =地图(。) (:)甚至

我想逐步了解解决方案,以解释与优先级和事物有关的事情。

预先感谢!4

1 个答案:

答案 0 :(得分:1)

步骤1:解析:

f = (map (.)) . ((:) even)

第2步。推断某些类型:

(.) :: (b -> c) -> (a -> b) -> (a -> c)
map (.) :: [b -> c] -> [(a -> b) -> (a -> c)]
(:) :: d -> [d] -> [d]
(:) even :: Integral e => [e -> Bool] -> [e -> Bool]

第3步。推导一些方程式:

x = [e -> Bool] -- the output of (:) even
x = [b -> c] -- the input to map (.)
e = b
c = Bool

第4步。记下f的类型:

f :: [(a -> b) -> (a -> c)]
f :: Integral e => [(a -> e) -> (a -> Bool)]