Haskell:函数组合的类型不匹配 [英] Haskell: Types of function composition not matching

查看:128
本文介绍了Haskell:函数组合的类型不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在功能组成和类型方面有一些麻烦。
我想用 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]



code>(。):: Foldable(( - >)[m])=>
(([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)

我们知道过滤器x $ b




你可能会考虑一些无意义的版本:

 (长度)。过滤器

过滤器> =>返回。长度

(fmap.fmap)长度过滤器

(id〜> id〜>长度)过滤器 - [1]

过滤器$ * id $$ id * $ length - [2]

lurryA @ N2(length< $>(filter< $> _1 *> 2) [3]




  1. Control.Compose

  2. Data.Function.Meld

  3. Data.Function.Tacit


I am having some troubles with function composition and types. I would like to compose 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]

So now I would expect the type of (len . filter) to be

(length . filter) :: (a -> Bool) -> [a] -> Int

while in reality is

> :t (length . filter)
(length . filter) :: Foldable ((->) [a]) => (a -> Bool) -> Int

So it seems I lost some arguments. Is it included in the the Foldable requirements in some way I am not understanding?

Note that everything works as expected if I do partial application:

> 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

解决方案

Definitions:

(.) :: (b -> c) -> (a -> b) -> a -> c
filter ::  (m -> Bool) -> [m] -> [m]
length :: Foldable t => t n -> Int

What is u?

length . filter :: u
≡ (.) length filter :: u

Then we must solve a, b, c, t, n:

a -> b ~ (m -> Bool) -> [m] -> [m]
b -> c ~ Foldable t => t n -> Int

It follows:

a ~ m -> Bool
b ~ Foldable t => t n
b ~ [m] -> [m]
c ~ Int

Trivially:

a = m -> Bool
b = [m] -> [m]
c = Int

We have to solve t, n from b ~ Foldable t => t n, i.e. [m] -> [m] ~ Foldable t => t n.

t = ((->) [m])
n = [m]

Therefore, t n = [m] -> [m] which trivially unifies.

Summarising:

(.) :: 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


An easier way to understand why length . filter is not what you want is to look at the definition of (.).

(.) g f x = g(f x)

Therefore:

(.) length filter
≡ \x -> length (filter x)

We know that filter x is not a list.


Pointless versions you may consider:

(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]

  1. Control.Compose
  2. Data.Function.Meld
  3. Data.Function.Tacit

这篇关于Haskell:函数组合的类型不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆