Created
February 14, 2015 05:50
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- | 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