了解功能类型 [英] Understanding function types
问题描述
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的类型不能被简化为:
(Num a,Eq a)=> a - > a - > Bool
为什么boolFcn的类型由
a,a1
文字,而三角形的类型包含 t,t1
文字? a
和 t
? 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
如果问题不大,我很抱歉,但我很乐意听到任何解释/提示这个问题。
-
是,
a
和t
在这些例子中基本相同。 -
那么,在
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:
- Why does the type of boolFcn consists of
a, a1
literals whilst the type of triangles consists oft,t1
literals? Is there any difference betweena
andt
? 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.
Yes,
a
andt
are essentially the same in these examples. The difference is just a side effect of the type inference algorithms.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
andDouble
are both members of theNum
andEq
typeclasses, but they are clearly not the same type. In yoursumthis
example,x
andy
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
andy
must be the same type, sosumthis
must require arguments of the same type. Thereforesumthis (4::Int) (4::Double)
would not be valid.
这篇关于了解功能类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!