【问题标题】:How is the syntax of type systems read?如何阅读类型系统的语法?
【发布时间】:2013-04-12 20:42:22
【问题描述】:

我有以下函数类型:

Quad = square · square
Square :: Integer -> Integer (So this function takes an integer as an input and outputs an integer)

The operator · is used in the definition of quad with type:

(·) :: (Integer -> Integer) -> (Integer -> Integer) -> Integer -> Integer

我不确定上面的内容如何解读以及背后的含义。

【问题讨论】:

    标签: haskell semantics type-systems


    【解决方案1】:

    :: 表明这是 Haskell,但所有受 ML 启发的语言的一般原则都是相同的(大多数和类型理论约定是使用单个 :)。

    :: 符号表示其左侧具有其右侧的类型。所以

    1 :: Integer
    

    -> 构造函数类型。

    timesTwo :: Integer -> Integer
    

    此外,-> 是右结合。

    plus :: Integer -> Integer -> Integer
    

    说函数plus 接受一个整数并返回一个函数,该函数接受一个整数并返回一个整数。这相当于取两个整数,但在技术上有所不同(从某种意义上说,更简单)。它被称为柯里化。

    square :: Integer -> Integer
    

    说square接受一个整数并返回一个整数。

    通常,在类型理论和函数式编程语言中,我们使用类型变量,所以

    id :: forall a. a -> a
    id x = x
    

    对于任何类型a id 是从该类型的值到相同类型的另一个值的函数。当您的. 运算符被赋予使用变量的更通用类型时,它会更有意义

    (·) :: (b -> c) -> (a -> b) -> a -> c
    f . g x = f (g (x))
    

    是函数组合函数。它是一个高阶函数,将两个函数作为参数。更正式地说,对于任何类型 abc(.) 是从 bc 的函数的函数a 的函数b 到一个函数ac。最后一个函数就是两个参数函数中的composition

    您已将. 专门用于仅处理整数。但是,想法是一样的。您从Integer -> IntegerInteger 中获取两个函数,应用第一个函数,然后应用第二个函数作为结果。

    (.)(+) 只是 Haskell,因为“这是一个中缀运算符,但我现在想以前缀形式讨论它。”

    所以,Quad 只是来自Integer -> Integer 的一个函数,它在其参数上调用 square,然后在结果上再次调用 square。应该是一样的

    quad x = square (square x)
    

    (haskell区分大小写,函数必须以小写字母开头)

    【讨论】:

      猜你喜欢
      • 2017-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多