了解功能类型 [英] Understanding function types

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

问题描述

在试图理解Haskell如何确定函数类型时,我感到有些困惑。这是一个例子:

  boolFcn x y = x / = 3&& y / = 4 

当我检查上述函数的类型时, (数字a1,数字a,数学式a1,数学式a)=> a - > a1 - > Bool

这是另一个例子:

  triangles = [(a,b,c)| c < -  [1..10],b < -  [1..10],a < -  [1..10]] 

然后在三角形结果中使用<:code>:t (num t2,Num t1,Num t,Enum t2,Enum t1,Enum t)=>

  [(t,t1,t2)] 

我脑海中出现了一些问题,自己解决它们:


    为什么boolFcn的类型由 a,a1 文字,而三角形的类型包含 t,t1 文字? a t
  1. 是否这样boolFcn的类型不能被简化为:

    (Num a,Eq a)=> a - > a - > Bool


a a1 具有相同的类型类,所以为什么我不能只用一个 a 来编写它们呢?当我检查函数的类型时:

  let sumthis x y = if x> y then x + y else xy 

我得到一个结果:

 (Ord a,Num a)=> a  - > a  - > a 

为什么它不会导致:

 (Ord a,Num a,Ord a1,Num a1)=> a  - > a1  - > a 

如果问题不大,我很抱歉,但我很乐意听到任何解释/提示这个问题。

解决方案


  1. 是, a t 在这些例子中基本相同。


  2. 那么,在 boolFcn (Num a,Eq a)=> a - > a - > Bool 不够通用,因为前两个参数不需要是相同的类型。考虑以下调用:

    boolFcn(4 :: Int)(4 :: Double)



    这是有效的,因为 Int Double 都是 Num Eq 类型类,但它们显然不是同一类型。在你的 sumthis 例子中, x y 必须是因为它们被用作需要相同类型参数的函数的输入。



    我们可以看到通过检查:t(+) ,它返回(+):: Num a => a - > a - >一个。由于 + 的参数必须是相同的类型,所以 x y 必须是相同的类型,所以 sumthis 必须要求相同类型的参数。因此

    sumthis(4 :: Int)(4 :: Double)



    无效。



I am getting a bit confused in my attempts to understand how Haskell determines function types. Here is an example:

boolFcn x y = x/=3 && y/=4

When I check the type of the above function, it gives me result:

(Num a1, Num a, Eq a1, Eq a) => a -> a1 -> Bool

Here is another example:

triangles = [ (a,b,c) | c <- [1..10], b <- [1..10], a <- [1..10] ]   

And apllying :t on triangles results with:

(Num t2, Num t1, Num t, Enum t2, Enum t1, Enum t) =>  [(t, t1, t2)]

A few questions arised in my head and I have serious trouble solving them by myself:

  1. Why does the type of boolFcn consists of a, a1 literals whilst the type of triangles consists of t,t1 literals? Is there any difference between a and t?
  2. Why does this the type of boolFcn cannot be simplified to:

    (Num a, Eq a) => a -> a -> Bool

a and a1 have the same typeclasses, so why can't I just simply write them using one a? When I check type of the function:

let sumthis x y = if x > y then x+y else x-y

I get a result:

(Ord a, Num a) => a -> a -> a

Why doesn't it result in:

(Ord a, Num a, Ord a1, Num a1) => a -> a1 -> a

I'm sorry if the question is trivial, though I would gladly hear any explanations/hints to this problem.

解决方案

  1. Yes, a and t are essentially the same in these examples. The difference is just a side effect of the type inference algorithms.

  2. Well, in boolFcn, (Num a, Eq a) => a -> a -> Bool would not be general enough, because the first two arguments need not be the same type. Consider the following call:

    boolFcn (4::Int) (4::Double)

    This is valid, because Int and Double are both members of the Num and Eq typeclasses, but they are clearly not the same type. In your sumthis example, x and y must be the same type because they are used as inputs to functions that require arguments of the same type.

    We can see that by checking :t (+), which returns (+) :: Num a => a -> a -> a. Since the parameters of + must be the same type, x and y must be the same type, so sumthis must require arguments of the same type. Therefore

    sumthis (4::Int) (4::Double)

    would not be valid.

这篇关于了解功能类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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