Created
September 21, 2022 10:06
-
-
Save Lysxia/7714c19ef9c17b487a46c804694fc0f9 to your computer and use it in GitHub Desktop.
Quantified constraint trick https://blog.poisson.chat/posts/2022-09-21-quantified-constraint-trick.html
This file contains 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
{-# LANGUAGE | |
ConstraintKinds, | |
FlexibleContexts, | |
FlexibleInstances, | |
MultiParamTypeClasses, | |
QuantifiedConstraints, | |
RankNTypes, | |
ScopedTypeVariables, | |
TypeApplications, | |
UndecidableInstances #-} | |
{-# OPTIONS_GHC -Wno-simplifiable-class-constraints #-} | |
module DG where | |
import GHC.Generics | |
class RepFmap a a' rep rep' where | |
repFmap :: (a -> a') -> rep -> rep' | |
gfmap :: forall f a a'. GFunctor f => (a -> a') -> f a -> f a' | |
gfmap f = | |
with @(RepFmapRep a a' f) -- Hand-holding for GHC <= 9.0 | |
(to @_ @() . repFmap f . from @_ @()) | |
with :: forall c r. (c => r) -> (c => r) | |
with x = x | |
class (forall a. Generic (f a), forall a a'. RepFmapRep a a' f) => GFunctor f | |
instance (forall a. Generic (f a), forall a a'. RepFmapRep a a' f) => GFunctor f | |
class RepFmap a a' (Rep (f a) ()) (Rep (f a') ()) => RepFmapRep a a' f | |
instance RepFmap a a' (Rep (f a) ()) (Rep (f a') ()) => RepFmapRep a a' f |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment