Type inference interferes with referential transparency

前端 未结 7 1314
醉酒成梦
醉酒成梦 2021-02-12 18:07

What is the precise promise/guarantee the Haskell language provides with respect to referential transparency? At least the Haskell report does not mention this notion.

C

7条回答
  •  死守一世寂寞
    2021-02-12 18:32

    Probably another type-inference and referential-transparency related thing is the „dreaded“ Monomorphism restriction (its absence, to be exact). A direct quote:

    An example, from „A History of Haskell“:
    Consider the genericLength function, from Data.List

    genericLength :: Num a => [b] -> a

    And consider the function:

    f xs = (len, len) where len = genericLength xs

    len has type Num a => a and, without the monomorphism restriction, it could be computed twice.

    Notice that in this case types of both expressions are the same. Results are too, but the substitution isn't always possible.

提交回复
热议问题