Disadvantage of unlifted type products?

社会主义新天地 提交于 2019-12-04 16:20:14

问题


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?


回答1:


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...




回答2:


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.)




回答3:


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.



来源:https://stackoverflow.com/questions/2488228/disadvantage-of-unlifted-type-products

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!