Haskell:函数组合的类型不匹配 [英] Haskell: Types of function composition not matching
问题描述
我在功能组成和类型方面有一些麻烦。
我想用 len
来编写 filter
(它返回一个列表),它将列表作为一个参数(技术上是 Foldable
但我在这里简化)。
查看所有类型都符合预期:
> :t长度
长度::可折叠t => t a - > Int
> :t filter
filter ::(a - > Bool) - > [a] - > [a]
现在我期望的类型(len。filter )
为
(length。filter)::(a - > Bool) - > ; [a] - >诠释
实际上是
> :t(length。filter)
(length。filter):: Foldable(( - >)[a])=> (a - > Bool) - >诠释
因此,我似乎失去了一些论据。它是否包含在 Foldable
需求中,我不理解?
请注意,一切都按预期工作如果我做了部分申请:
>让myFilter =过滤奇数
> :t myFilter
myFilter :: Integral a => [a] - > [a]
> :t(length。myFilter)
(length。myFilter):: Integral a => [a] - > Int
> (length。myFilter)[1,2,3]
2
(。)::(b - > c) - > (a - > b) - > a - > c
filter ::(m - > Bool) - > [m] - > [m]
长度::可折叠t => t n - > Int
什么是 u
?
长度。 filter :: u
≡(。)length filter :: u
然后我们必须解决 a,b,c,t,n
:
a - > ; b〜(m→Bool)→> [m] - > [m]
b - > c〜可折叠t => t n - > Int
如下:
a〜m - > Bool
b〜可折叠t => t n
b〜[m] - > [m]
c〜Int
平凡:
a = m - > Bool
b = [m] - > [m]
c = Int
我们必须解决 t, n
from b〜Foldable t => t n
,即 [m] - > [m]〜可折叠t => t $
$ t $($ - $ $ $)> $($ - $) [m]
因此, tn = [m] - > [b]
(([m] - > [m]) - > Int)
- > ((m→Bool)→m [m]→m [m])
- > (m - > Bool) - > Int
filter ::(m - > Bool) - > [m] - > [m]
length ::可折叠(( - >)[m])=> ([m] - > [m]) - > Int
(。)length filter :: Foldable(( - >)[m])=> (m - > Bool) - > Int
理解长度。 filter
并不是你想要看的(。)
。
(。)gfx = g(fx)
因此:
(。)长度过滤
≡\x - >长度(过滤器x)
我们知道 你可能会考虑一些无意义的版本: I am having some troubles with function composition and types.
I would like to compose So now I would expect the type of while in reality is So it seems I lost some arguments. Is it included in the the Note that everything works as expected if I do partial application:
Definitions: What is Then we must solve It follows: Trivially: We have to solve Therefore, Summarising:
An easier way to understand why Therefore: We know that Pointless versions you may consider:
这篇关于Haskell:函数组合的类型不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!过滤器x $ c $
$ b
(长度)。过滤器
过滤器> =>返回。长度
(fmap.fmap)长度过滤器
(id〜> id〜>长度)过滤器 - [1]
过滤器$ * id $$ id * $ length - [2]
lurryA @ N2(length< $>(filter< $> _1 *> 2) [3]
filter
(which returns a list) with len
, which takes a list as an argument (technically a Foldable
but I am simplifying here).
Looking at the types everything is as expected:> :t length
length :: Foldable t => t a -> Int
> :t filter
filter :: (a -> Bool) -> [a] -> [a]
(len . filter)
to be(length . filter) :: (a -> Bool) -> [a] -> Int
> :t (length . filter)
(length . filter) :: Foldable ((->) [a]) => (a -> Bool) -> Int
Foldable
requirements in some way I am not understanding?> let myFilter = filter odd
> :t myFilter
myFilter :: Integral a => [a] -> [a]
> :t (length . myFilter)
(length . myFilter) :: Integral a => [a] -> Int
> (length . myFilter) [1,2,3]
2
(.) :: (b -> c) -> (a -> b) -> a -> c
filter :: (m -> Bool) -> [m] -> [m]
length :: Foldable t => t n -> Int
u
?length . filter :: u
≡ (.) length filter :: u
a, b, c, t, n
:a -> b ~ (m -> Bool) -> [m] -> [m]
b -> c ~ Foldable t => t n -> Int
a ~ m -> Bool
b ~ Foldable t => t n
b ~ [m] -> [m]
c ~ Int
a = m -> Bool
b = [m] -> [m]
c = Int
t, n
from b ~ Foldable t => t n
, i.e. [m] -> [m] ~ Foldable t => t n
.t = ((->) [m])
n = [m]
t n = [m] -> [m]
which trivially unifies.(.) :: Foldable ((->) [m]) =>
(([m] -> [m]) -> Int)
-> ((m -> Bool) -> [m] -> [m])
-> (m -> Bool) -> Int
filter :: (m -> Bool) -> [m] -> [m]
length :: Foldable ((->) [m]) => ([m] -> [m]) -> Int
(.) length filter :: Foldable ((->) [m]) => (m -> Bool) -> Int
length . filter
is not what you want is to look at the definition of (.)
.(.) g f x = g(f x)
(.) length filter
≡ \x -> length (filter x)
filter x
is not a list.
(length .) . filter
filter >=> return . length
(fmap.fmap) length filter
(id ~> id ~> length) filter -- [1]
filter $* id $$ id *$ length -- [2]
lurryA @N2 (length <$> (filter <$> _1 <*> _2)) -- [3]