Type inference interferes with referential transparency

前端 未结 7 1293
醉酒成梦
醉酒成梦 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:16

    I do not think there's any guarantee that evaluating a polymorphically typed expression such as 5 at different types will produce "compatible" results, for any reasonable definition of "compatible".

    GHCi session:

    > class C a where num :: a
    > instance C Int    where num = 0
    > instance C Double where num = 1
    > num + length []  -- length returns an Int
    0
    > num + 0          -- GHCi defaults to Double for some reason
    1.0
    

    This looks as it's breaking referential transparency since length [] and 0 should be equal, but under the hood it's num that's being used at different types.

    Also,

    > "" == []
    True
    > [] == [1]
    False
    > "" == [1]
    *** Type error
    

    where one could have expected False in the last line.

    So, I think referential transparency only holds when the exact types are specified to resolve polymorphism. An explicit type parameter application à la System F would make it possible to always substitute a variable with its definition without altering the semantics: as far as I understand, GHC internally does exactly this during optimization to ensure that semantics is unaffected. Indeed, GHC Core has explicit type arguments which are passed around.

提交回复
热议问题