[Haskell-beginners] phantom types and record syntax

Karl Voelker karl at karlv.net
Wed Jun 17 06:42:06 UTC 2015


On Tue, Jun 16, 2015, at 05:43 PM, Dimitri DeFigueiredo wrote:
> 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!"
> ----

I found it surprising, too. But, if you look in the Haskell report
(https://www.haskell.org/onlinereport/haskell2010/haskellch3.html#x8-540003.15.3),
record update is defined by a "desugaring" translation. So your
convertToReq desugars (roughly) to:

convertToReq o = case o of
    Order v1 v2 v3 -> Order 1 v2 v3

Unfortunately, the report does not have any commentary on why it is the
way it is.

-Karl


More information about the Beginners mailing list