Skip to content

Instantly share code, notes, and snippets.

@aaronlevin
Created February 14, 2015 05:50
Show Gist options
  • Select an option

  • Save aaronlevin/7c1ba0731ac26b2ddafe to your computer and use it in GitHub Desktop.

Select an option

Save aaronlevin/7c1ba0731ac26b2ddafe to your computer and use it in GitHub Desktop.
Using Equality Constraints to put Constraints on elemts in a Type Family's CoDomain
-- | I wanted to encode the json key for a payload type as type-level strings
-- one can use equality constraints to resolve type family mappings
data SomeData = SomeData
type family Key (a :: *) :: Symbol where
Key SomeData = "some_data"
-- | here we use equality constrains to resolve `k` to the type-level mapping
-- of SomeData a. then we can restrict `k` to some other constraint (in this
-- case to be a `KnownSymbol`.
data Payload a where
Payload :: (k ~ SomeData a, KnownSymbol k)
=> Proxy (Key a)
-> a
-> Payload a
instance (ToJSON a) => ToJSON (Payload a) where
toJSON (Payload p a) = object [ "payload_type" .= symbolVal p
, "data" .= toJSON a
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment