Skip to content

Instantly share code, notes, and snippets.

@dcastro
Last active October 1, 2018 10:39
Show Gist options
  • Save dcastro/4335f898e1ff2ee53d1f97beb4df16e5 to your computer and use it in GitHub Desktop.
Save dcastro/4335f898e1ff2ee53d1f97beb4df16e5 to your computer and use it in GitHub Desktop.
Generic functor, abstracting over a constraint
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE ExplicitForAll #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE TypeSynonymInstances #-}
{-# LANGUAGE FlexibleInstances #-}
module Playground where
import Data.Kind
import qualified Data.Set as Set
-- typeclass
class GFunctor (c :: * -> Constraint) (f :: * -> *) | f -> c where
gfmap :: forall a b. c b => (a -> b) -> (f a -> f b)
-- instance for sets
instance GFunctor Ord Set.Set where
gfmap = Set.map
-- instance for lists
type Const (a :: Constraint) (b :: k) = a
instance GFunctor (Const ()) [] where
-- error:
-- • The type synonym ‘Const’ should have 2 arguments, but has been given 1
-- • In the instance declaration for ‘GFunctor (Const ()) []’
-- |
-- 35 | instance GFunctor (Const ()) [] where
-- | ^^ ^^
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment