Using PolyKinds and OverlappingInstances, the instance record for (t :: k) is fully applied to the arguments k

This seems to be impossible, but here is an example of what I'm working on:

{-# LANGUAGE PolyKinds , MultiParamTypeClasses , FlexibleInstances , OverlappingInstances #-}

data Proxy a = Proxy

class Test pt t where
    test :: pt -> t -> IO ()

instance Test (Proxy t) t where
    test _ _ = putStrLn "MATCHES"

-- I would like to combine these:
instance Test (Proxy t) (t a) where
    test _ _ = putStrLn "MATCHES2"
instance Test (Proxy t) (t a b) where
    test _ _ = putStrLn "MATCHES3"
--etc.

instance Test (Proxy t) x where
    test _ _ = putStrLn "FAIL"

C PolyKinds, and our instances above ours tin Proxy tmay be arity * -> *or * -> * -> *, and the code works correctly, however, support for ta higher arity requires the addition of an arbitrary number of additional instances. Is there a way to combine these two instances into one instance, which means that " tfully applies to any arguments k"?

+4
source share
1 answer

: , "" , , , , :

{-# LANGUAGE PolyKinds , MultiParamTypeClasses , FlexibleInstances , OverlappingInstances 
 , FlexibleContexts  -- these are new
 , ScopedTypeVariables
 #-}

data Proxy a = Proxy

class Test pt t where
    test :: pt -> t -> IO ()

-- ...also, as you can see I've had to make the second argument a `Proxy` since I can't
-- do the type-chopping thing to real values. I don't think that should be too much of
-- an issue for my use case though.
instance Test (Proxy t) (Proxy t) where
    test _ _ = putStrLn "MATCHES"
-- we need this extra instance for an explicit match that is more specific than the instance below:
instance Test (Proxy (t a)) (Proxy (t a)) where
    test _ _ = putStrLn "MATCHES"

instance (Test (Proxy t) (Proxy ta))=> Test (Proxy t) (Proxy (ta b)) where
    test p _ = test p (Proxy :: Proxy ta)

instance Test (Proxy t) x where
    test _ _ = putStrLn "FAIL"

.

: proxy-kindness, - .

+1

All Articles