Skip to content

Loss of referential transparency #12

@Nick-Chapman

Description

@Nick-Chapman

The doc for HKey.withKey states:

"The implementation actually creates a key, but because the key cannot escape the given function f, there is no way to observe that if we run withKey f twice, that it will get a different key the second time."

But in the following we see it is possible.

Nick

{-#LANGUAGE ExistentialQuantification, RankNTypes #-}
module Main(main) where

import Data.HMap as HMap

data Wrapped = forall x. Wrapped (HKey x ())

create :: () -> Wrapped
create () = HMap.withKey Wrapped

x,y,z :: Wrapped
x = create ()
y = create ()
z = y

comp :: Wrapped -> Wrapped -> Bool
comp (Wrapped key1) (Wrapped key2) = HMap.unique key1 == HMap.unique key2

main :: IO ()
main = do
  print ("x~y",comp x y)
  print ("x~z",comp x z)
  print ("y~z",comp y z)

{- Prints...
("x~y",False)
("x~z",False)
("y~z",True)
-}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions