函数组合类型 [英] Function Composition Types
问题描述
我努力理解函数组合类型结果,例如
ghci> :t(id.const)
(id.const):: a - > b - > a
ghci> :t((:)。(+))
((:)。(+)):: a - > [a - > a] - > [a - > a]
你们通常如何得到函数组合类型?
<让我们看看
>解析方案
:t(。)
(。)::(b - > c) - > (a - > b) - > a - > c
所以一般来说:
- 给定两个函数。把它们看作一个参数的curlic函数,即类型
a - >的函数。 b - > c - > ... - > z
我们将考虑作为一个函数a - > (b - > c - > ... - > z)
,其中一个参数返回函数,其中一个参数少。 - 函数应该与第一个参数类型相同。所以考虑第一个函数的参数作为第二个结果参数。表示为
b
。 - 整个构图的结果类型应该与参数类型第二个函数的结果类型(表示为
a
), code
>
) :t(id。const) - Given two functions. Consider them as curried functions of one parameter, i.e. function of type
a -> b -> c -> ... -> z
we'll consider as a functiona -> ( b -> c -> ... -> z )
of one parameter that returns function with one parameter less. - Result type of second function should be same as parameter type of first. So consider parameter of first function as result parameter of second. Denote it as
b
. - Result type of whole composition should be equal a function from parameter type of second function (denote it as
a
) to result type of first function (denote it asc
)
> :t id
id :: a - > a
将其重命名为 x - > x
> :t const
const :: a - > b - > a
请注意,这里并不需要 a
与之前的类型方程相同,有些将它重命名为 y - > z - > y
a = y
b = z - > y = x
c = x = z - > y
所以结果是 a - > c = y - > (z - > y)
与 a - >相同b - > c
整体语义等于 const
> :t(()。(+))
> :t(:)
(:) :: a - > [a] - > [a]
将其重命名为 x - > [x] - > [x]
> :t(+)
(+):: Num a => a - > a - > a
将其重新命名为 Num y => y - > y - > y
a = y
b = y - > y = x
c = [x] - > [x] = [y - > y] - > [y - > y]
同样,我们对类型为 Num y
所以整体类型是 Num y => a - > c = y - > [y - > y] - > [y - > y]
与相同> Num a => a - > [a - > a] - > [a - > a]
整体语义是
I struggle understanding function composition type results e.g
ghci> :t (id . const)
(id . const) :: a -> b -> a
ghci> :t ((:) . (+))
((:) . (+)) :: a -> [a -> a] -> [a -> a]
How do you guys generally derive function composition types?
Lets see that
> :t (.)
(.) :: (b -> c) -> (a -> b) -> a -> c
So in general:
> :t (id . const)
> :t id
id :: a -> a
rename it to x -> x
> :t const
const :: a -> b -> a
Note that a
here is not necessary the same that in previous type equation, some rename it to y -> z -> y
a = y
b = z -> y = x
c = x = z -> y
So result is a -> c = y -> ( z -> y)
same as a -> b -> c
Overall semantics is equal to const
> :t ((:) . (+))
> :t (:)
(:) :: a -> [a] -> [a]
rename it to x -> [x] -> [x]
> :t (+)
(+) :: Num a => a -> a -> a
rename it to Num y => y -> y -> y
a = y
b = y -> y = x
c = [x] -> [x] = [y -> y] -> [y -> y]
Also we have restriction with type class Num y
So overall type well be Num y => a -> c = y -> [y -> y] -> [y -> y]
same as Num a => a -> [a -> a] -> [a -> a]
Overall semantics is "make single parameter function that adds first parameter to numeric value and prepend that function to a given list of single parameter functions"
这篇关于函数组合类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!