[Haskell-cafe] Newbie question (again!) about phantom types

peterv bf3 at telenet.be
Wed Aug 8 11:16:20 EDT 2007

I=92m having difficulty to understand what phantom types are good for. =
Is this
just for improving runtime performance?=20

I read the wiki, and it says "this is useful if you want to increase the
type-safety of your code", but the code below does not give a compiler =
for the function test1, I get a runtime error, just like test2.


-- CODE --
-- With phantom types
data T1 a =3D TI1 Int | TS1 String deriving Show

foo1 :: T1 String -> T1 String -> T1 String
foo1 (TS1 x) (TS1 y) =3D TS1 (x++y)

test1 =3D foo1 (TI1 1) (TI1 2) -- Shouldn't this give a compiler error =
of a runtime error?=20

-- Without phantom types
data T2 =3D TI2 Int | TS2 String deriving Show

foo2 :: T2 -> T2 -> T2
foo2 (TS2 x) (TS2 y) =3D TS2 (x++y)

test2 =3D foo2 (TI2 1) (TI2 2)

No virus found in this outgoing message.
Checked by AVG Free Edition.=20
Version: 7.5.476 / Virus Database: 269.11.8/941 - Release Date: =

More information about the Haskell-Cafe mailing list