[Haskell-beginners] phantom types and record syntax

akash g akaberto at gmail.com
Wed Jun 17 04:14:03 UTC 2015


The phantom type doesn't appear on the actual constructors, only on the
type signatures.  In the function convertToReq, you are changing the
following
1. The type.
2. The value of the price record (actually returning a new value with the
price record changed to one and the rest of the fields being the same.



That said, I don't see the point of a phantom on a record structure like
this.  Phantoms are more useful when you have multiple data constructors
for a given type and you want to make sure you don't have constructions
that don't make sense.  Like in an interpreter, you don't want to create
expressions that can give rise to an expressions like add a integer to a
string and that sort of thing.

Try this:
http://www.scs.stanford.edu/11au-cs240h/notes/laziness-slides.html

In fact, that set of lecture notes as well as the 2014 class notes are
amazingly good.


On Wed, Jun 17, 2015 at 6:13 AM, Dimitri DeFigueiredo <
defigueiredo at ucdavis.edu> wrote:

> 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
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20150617/7a9fb655/attachment.html>


More information about the Beginners mailing list