Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding code generated by Yesod Persistent TH

I've spent a while trying to understand the code generated by template haskell in this example taken from the Yesod book:

share [mkPersist sqlSettings, mkMigrate "migrateAll"] [persistLowerCase|
Person
    name String
    age Int
    deriving Show
Car
    color String
    make String
    model String
    deriving Show
|]

I feel like I mostly see what's going on (a lot of type marshalling), but one section still confuses me:

instance PersistEntity (PersonGeneric backend) where
  data instance Unique (PersonGeneric backend) =
  data instance EntityField (PersonGeneric backend) typ
      = typ ~ KeyBackend backend (PersonGeneric backend) => PersonId |
        typ ~ String => PersonName |
        typ ~ Int => PersonAge
  type instance PersistEntityBackend (PersonGeneric backend) =
      backend

The data instance instance EntityField (PersonGeneric backend) typ has three data constructors, which makes sense (one for each column in the database), but even after looking up what the tilde does in haskell, I can't understand what it's doing there. Why is the =>, normally used for universal quantification, used after something that doesn't seem to restict any types?

Please let me know if I can be more clear in some way.

like image 364
Andrew Thaddeus Martin Avatar asked Jul 31 '13 03:07

Andrew Thaddeus Martin


1 Answers

This syntax is used to declare GADT without GADT-syntax.

For instance,

data Z a b = (a ~ Int, b ~ Bool) => Z1 a b
           | (Show a, b ~ Float) => Z2 a b

is equivalent to

data Z a b where
    Z1 :: Int -> Bool -> Z Int Bool
    Z2 :: Show a => a -> Float -> Z a Float
like image 170
snak Avatar answered Nov 07 '22 12:11

snak