[Haskell-cafe] phantom types and record syntax

Dimitri DeFigueiredo defigueiredo at ucdavis.edu
Thu Jun 18 02:45:34 UTC 2015


Hi All,

My apologies if this is not the right forum, but am not satisfied with 
my current understanding.

I am surprised that this program compiles in GHC:

-----
data UserSupplied  = UserSupplied -- i.e. unsafe
data Safe          = Safe

data Username a = Username { first :: String, last :: String}

sanitize :: Username UserSupplied -> Username Safe
sanitize name = name { first = "John" }

main = putStrLn "Hi!"
-----

My trouble is that it seems the record syntax is*implicitly* converting 
from one type to the other. It seems I would have to remove the phantom 
type by adding a tag to avoid this:

-----
data Username a = Username { first :: String, last :: String, tag :: a }

sanitize :: Username UserSupplied -> Username Safe
sanitize name = name { first = "John" } -- FAIL as expected!! :-)
-----

But this makes me unwilling to use phantom types for security as I would 
be worried of unwittingly making the conversion.

Could somebody sprinkle some insight into why this is "converted 
automatically" for phantom types?
Is there another way around this?


Thanks!


Dimitri


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20150617/b9a46540/attachment.html>


More information about the Haskell-Cafe mailing list