[Haskell-beginners] phantom types and record syntax

Dimitri DeFigueiredo defigueiredo at ucdavis.edu
Wed Jun 17 00:43:38 UTC 2015


Hi All,

I am a little suprised that this program compiles in GHC:

----
data ReqTime = ReqTime
data AckTime = AckTime

data Order a = Order { price  ::Double, volume ::Int, timestamp ::Int }

convertToReq :: Order AckTime -> Order ReqTime
convertToReq o = o{price = 1}

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 to avoid this:

----
data Order a = Order { price  ::Double, volume ::Int, timestamp ::Int, 
myType :: a }

convertToReq :: Order AckTime -> Order ReqTime
convertToReq o = o{price = 1}  -- fail!!
----

Could somebody sprinkle some insight into why this is "converted 
automatically" for phantom types? Can I avoid this behavior?

Thanks!

Dimitri


More information about the Beginners mailing list