The constraint Has c f means that given any value of type f a, we can determine
that there is an instance of c a. For example, Has Show Tag means that given any
x :: Tag a, we can conclude Show a. Most commonly, the type f will be a GADT,
where we can enumerate all the possible index types through pattern matching, and
discover that there is an appropriate instance in each case. In this sort of
situation, the c can be left entirely polymorphic in the instance for Has, and
this is the sort of instance that the provided Template Haskell code writes.
Methods
has :: f a -> (c a => r) -> rUse the
f ato show that there is an instance ofc a, and bring it into scope.The order of type variables is chosen to work with
-XTypeApplications.-- Hold a value of type a, along with a tag identifying the a. data SomeTagged tag where SomeTagged :: a -> tag a -> SomeTagged tag -- Use the stored tag to identify the thing we have, allowing us to call 'show'. Note that we -- have no knowledge of the tag type. showSomeTagged :: Has Show tag => SomeTagged tag -> String showSomeTagged (SomeTagged a tag) = has @Show tag $ show aargDict :: f a -> Dict (c a)Use an
f ato obtain a dictionary forc aargDict @Show I :: Dict (Show Int)