简体   繁体   English

Haskell中a-> b Bool类型是什么意思?

[英]What does the type a -> b Bool mean in Haskell?

I know that type: 我知道这种类型:

a -> Bool

is a function that takes as input something of type a and outputs a boolean. 是一个将类型a的东西作为输入并输出布尔值的函数。

What does type: 键入什么:

a -> b Bool

mean? 意思?

In b Bool , b stands for a parametrized type that takes one type parameter (in Haskell parlance, b is a type constructor of kind * -> * ), such as Maybe , IO or [] . b Boolb代表采用一个类型参数的参数化类型(在Haskell看来, b是类型* -> *的类型构造函数),例如MaybeIO[]

So a function of type a -> b Bool could for example take an Int and produce a Maybe Bool , IO Bool , [Bool] etc. 因此, a -> b Bool类型的函数可以采用一个Int并生成Maybe BoolIO Bool[Bool]等。

In this case b is a higher kinded type, ie a function from types to types. 在这种情况下, b是种类更高的类型,即从类型到类型的函数。 Sounds scary? 听起来吓人吗? Fear not! 不要怕! A higher kinded type is something like the list type. 更高种类的类型类似于列表类型。 ([]) on it's own is not a type, but it still takes an argument to become one, so ([]) Int = [Int] is a type. ([])本身不是类型,但是它仍然需要一个参数才能成为一个类型,因此([]) Int = [Int]是一种类型。

Kinds are like the types of types and the kind of a fully applied type is * . 种类就像类型的类型,完全应用类型的类型是* The kind of list on the other hand is * -> * . 另一方面,列表的类型为* -> * So it takes a type * (eg Int ) and returns a type (eg [Int] ). 因此,它采用类型* (例如Int )并返回类型(例如[Int] )。 Other examples would be Maybe , IO or the function type (->) which has kind * -> * -> * because it still takes to types, an argument type and a result type (eg (->) Int Bool = Int -> Bool ). 其他示例可能是MaybeIO或具有类型* -> * -> *的函数类型(->) ,因为它仍然需要处理类型,参数类型和结果类型(例如(->) Int Bool = Int -> Bool )。

The function that you have given is actually impossible to implement, just like a function of type a -> b . 您提供的函数实际上是无法实现的,就像类型a -> b的函数一样。 However a function we could implement is the following: 但是,我们可以实现的功能如下:

pureEq :: (Eq a, Applicative b) => a -> a -> b Bool
pureEq x y = pure (x == y)

It might be clearer if you write it out with explicit quantification and kind annotations. 如果使用显式量化和种类注释将其写出,可能会更清楚。 a -> b Bool means... a -> b Bool表示...

forall (a :: *) (b :: * -> *). a -> b Bool

b is therefore a type constructor taking a single type argument. 因此, b是采用单个类型参数的类型构造函数。 Examples of single-argument type constructors abound: Maybe , [] , IO are all examples of things which you could use to instantiate b . 单参数类型构造函数的示例比比皆是: Maybe []IO都是可用于实例化b的事物的示例。

A function f :: a -> b Bool takes an argument of any type a ; 函数f :: a -> b Bool接受任何类型a的参数; its return value is the type constructor of your choice with its parameter filled in as Bool . 它的返回值是您选择的类型构造函数,其参数填充为Bool

Parametricity tells us that there aren't any (non-⊥) terms with such a type. 参数告诉我们,没有任何(非⊥)项具有这种类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM