Disadvantage of unlifted type products?
In Haskell, lifted type products mean that there's a semantic difference between (a,b,c) and (a, (b, c)).
If all pattern matches of all products was always irrefutable, then there would be no difference, and (a, b, c) could be syntactic sugar for (a, (b, c)).
Why did Haskell choose to lift type products?
One reason is that implementing seq
for an unlifted product requires parallel/interleaved computation, since seq (a, b) True
would be supposed to be True
if and only if at least one of a
and b
is non-bottom. You might not find this reason terribly convincing, depending on how you feel about seq
, but of course polymorphic seq
is by definition a part of Haskell...
Why did Haskell choose to lift type products?
You can justify this design choice without appealing to laziness or refutable patterns. The same design choice is made ML for reasons of supporting polymorphism. Consider
fst (x, y) = x
snd (x, y) = y
Now if (a, (b, c))
is syntactic sugar for (a, b, c)
, it's quite difficult to see how to specialize fst
and snd
to take this type as an argument. But
fst :: (a, (b, c)) -> a
snd :: (a, (b, c)) -> (b, c)
are perfectly reasonable. Because polymorphic functions like fst
and snd
are so incredibly useful, both Haskell and ML give the programmer the ability to distinguish (a, (b, c))
and ((a, b), c)
from (a, b, c)
.
(For people who care about costs, the type structure is also a reasonable guide to the size of the type and the number of indirections (loads) needed to get its elements. Some programmers need or want to know about such things and to have some small degree of control over them.)
You can produce a semantic difference if you want to in connection with type classes.
(a, b, c)
and (a, (b, c))
can instantiate classes differently. Just think of
show (1, 2, 3)
and
show (1, (2, 3))
I'd consider it counter-intuitive to have both yielding the same output.
链接地址: http://www.djcxy.com/p/7496.html上一篇: 在Haskell中,为什么不是
下一篇: 未提升型产品的缺点?