函数组合类型 [英] Function Composition Types

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

问题描述

我努力理解函数组合类型结果,例如

  ghci> :t(id.const)
(id.const):: a - > b - > a
ghci> :t((:)。(+))
((:)。(+)):: a - > [a - > a] - > [a - > a]

你们通常如何得到函数组合类型?
<让我们看看

 >解析方案

:t(。)
(。)::(b - > c) - > (a - > b) - > a - > c

所以一般来说:


  1. 给定两个函数。把它们看作一个参数的curlic函数,即类型
    a - >的函数。 b - > c - > ... - > z 我们将考虑作为一个函数 a - > (b - > c - > ... - > z),其中一个参数返回函数,其中一个参数少。

  2. 函数应该与第一个参数类型相同。所以考虑第一个函数的参数作为第二个结果参数。表示为 b

  3. 整个构图的结果类型应该与参数类型第二个函数的结果类型(表示为 a ), code

    >

  4. :t(id。const)

     > :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:

    1. 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 function a -> ( b -> c -> ... -> z ) of one parameter that returns function with one parameter less.
    2. 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.
    3. 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 as c)

    > :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屋!

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