Commit fd4250b4 authored by dterei's avatar dterei

Add tests for Safe Haskell Typeable instances feature

parent 96de88d5
{-# LANGUAGE DeriveDataTypeable, StandaloneDeriving #-}
-- | This shows how to use typeable to perform unsafe
-- casts. Basically its an example of what Safe Haskell
-- should disallow. SafeLang14 will do that.
module Main where
import SafeLang13_A
import Data.Typeable
data H = H String deriving (Typeable, Show)
data G = G Int deriving (Show)
instance Typeable G where
typeOf _ = typeOf (undefined :: H)
instance Typeable P where
typeOf _ = typeOf (undefined :: G)
{-
deriving instance Typeable G
deriving instance Typeable P
-}
main = do
let h = H "Hello World"
g = G 1
-- Just h' = (cast h) :: Maybe G
Just p' = (cast p) :: Maybe G
Just px = (cast $ incrG p') :: Maybe P
putStrLn $ show h
putStrLn $ show g
-- putStrLn $ show h'
putStrLn $ showP p
putStrLn $ show p'
putStrLn $ showP px
incrG :: G -> G
incrG (G n) = G $ n + 1
H "Hello World"
G 1
Should be 3 := 3
G 3
Should be 3 := 4
module SafeLang13_A (IsoInt, h, showH, P, p, showP) where
newtype IsoInt = I Int
h :: IsoInt
h = I 2
showH :: String
showH = let I n = h
in show n
data P = P Int
p :: P
p = P 3
showP :: P -> String
showP (P n) = "Should be 3 := " ++ show n
{-# LANGUAGE DeriveDataTypeable, StandaloneDeriving #-}
-- | This shows how to use typeable to perform unsafe
-- casts. Basically its an example of what Safe Haskell
-- should disallow. SafeLang14 will do that.
module Main where
import SafeLang13_A
import Data.Typeable
data H = H String deriving (Typeable, Show)
data G = G Int deriving (Show)
instance Typeable G where
typeOf _ = typeOf (undefined :: H)
instance Typeable P where
typeOf _ = typeOf (undefined :: G)
{-
deriving instance Typeable G
deriving instance Typeable P
-}
main = do
let h = H "Hello World"
g = G 1
-- Just h' = (cast h) :: Maybe G
Just p' = (cast p) :: Maybe G
Just px = (cast $ incrG p') :: Maybe P
putStrLn $ show h
putStrLn $ show g
-- putStrLn $ show h'
putStrLn $ showP p
putStrLn $ show p'
putStrLn $ showP px
incrG :: G -> G
incrG (G n) = G $ n + 1
[1 of 2] Compiling SafeLang13_A ( SafeLang13_A.hs, SafeLang13_A.o )
[2 of 2] Compiling Main ( SafeLang14.hs, SafeLang14.o )
SafeLang14.hs:14:10:
Can't create hand written instances of Typeable in Safe Haskell! Can only derive them
SafeLang14.hs:17:10:
Can't create hand written instances of Typeable in Safe Haskell! Can only derive them
module SafeLang13_A (IsoInt, h, showH, P, p, showP) where
newtype IsoInt = I Int
h :: IsoInt
h = I 2
showH :: String
showH = let I n = h
in show n
data P = P Int
p :: P
p = P 3
showP :: P -> String
showP (P n) = "Should be 3 := " ++ show n
{-# LANGUAGE DeriveDataTypeable, StandaloneDeriving #-}
-- | This shows how to use typeable to perform unsafe
-- casts. Basically its an example of what Safe Haskell
-- should disallow. SafeLang14 will do that.
module Main where
import SafeLang13_A
import Data.Typeable
data H = H String deriving (Typeable, Show)
data G = G Int deriving (Show)
deriving instance Typeable G
deriving instance Typeable P
main = do
let h = H "Hello World"
g = G 1
-- Just h' = (cast h) :: Maybe G
Just p' = (cast p) :: Maybe G
Just px = (cast $ incrG p') :: Maybe P
putStrLn $ show h
putStrLn $ show g
-- putStrLn $ show h'
putStrLn $ showP p
putStrLn $ show p'
putStrLn $ showP px
incrG :: G -> G
incrG (G n) = G $ n + 1
SafeLang15: SafeLang15.hs:22:9-37: Irrefutable pattern failed for pattern Data.Maybe.Just p'
module SafeLang13_A (IsoInt, h, showH, P, p, showP) where
newtype IsoInt = I Int
h :: IsoInt
h = I 2
showH :: String
showH = let I n = h
in show n
data P = P Int
p :: P
p = P 3
showP :: P -> String
showP (P n) = "Should be 3 := " ++ show n
......@@ -16,4 +16,7 @@ test('SafeLang09', exit_code(1), compile_and_run, [''])
test('SafeLang10', normal, compile_fail, ['--make -trust base'])
test('SafeLang11', req_interp, compile_and_run, ['--make -trust base'])
test('SafeLang12', normal, compile_fail, ['--make -trust base'])
test('SafeLang13', normal, compile_and_run, ['--make -trust base'])
test('SafeLang14', normal, compile_fail, ['--make -trust base -XSafe'])
test('SafeLang15', exit_code(1), compile_and_run, ['--make -trust base -XSafe'])
[1 of 2] Compiling BadImport02_A ( BadImport02_A.hs, BadImport02_A.o )
[2 of 2] Compiling Main ( BadImport03.hs, BadImport03.o )
BadImport03.hs:8:1:
base:Data.Typeable can't be safely imported! The module itself isn't safe.
BadImport03.hs:16:10:
Can't create hand written instances of Typeable in Safe Haskell! Can only derive them
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment