Skip to content

Unsound type #13

@Nick-Chapman

Description

@Nick-Chapman

Related to #12
The same wrapping trick can get us an unsound type.
In the following we show that Char 'a' can be unsafely cast to Int 97.
Changing Int to (Int,Int) causes a seg fault.

Nick

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

import Data.HMap(HKey,HMap)
import qualified Data.HMap as HMap

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

w :: Wrapped a
w = HMap.withKey Wrap

hmap :: HMap
hmap = case w of Wrap k -> HMap.insert k 'a' HMap.empty

res :: Maybe Int -- replace Int with (Int,Int) for seg fault!
res = case w of Wrap k -> HMap.lookup k hmap

main :: IO ()
main = do
  print res

{- Prints...
Just 97
-}

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