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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With