To set the scene, here are a bunch of language extensions we'll use, and some simplified definitions from CLaSH:
{-# LANGUAGE GADTs, StandaloneDeriving #-}
{-# LANGUAGE TypeOperators, DataKinds, PolyKinds #-}
{-# LANGUAGE TypeFamilyDependencies, FlexibleContexts, FlexibleInstances #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE QuantifiedConstraints #-}
data Signal dom a
instance Functor (Signal dom) where
instance Applicative (Signal dom) where
class Bundle a where
type Unbundled dom a = res | res -> dom a
bundle :: Unbundled dom a -> Signal dom a
unbundle :: Signal dom a -> Unbundled dom a
I would like to make Bundle
instances for an n-ary product type. The type itself is defined as follows:
import Control.Monad.Identity
data ProductF (f :: * -> *) (ts :: [*]) where
NilP :: ProductF f '[]
ConsP :: f a -> ProductF f ts -> ProductF f (a : ts)
deriving instance (Show (f t), Show (ProductF f ts)) => Show (ProductF f (t : ts))
headPF :: ProductF f (t : ts) -> f t
headPF (ConsP x xs) = x
tailP :: ProductF f (t : ts) -> ProductF f ts
tailP (ConsP x xs) = xs
-- Utilities for the simple case
type Product = ProductF Identity
infixr 5 ::>
pattern (::>) :: t -> Product ts -> Product (t : ts)
pattern x ::> xs = ConsP (Identity x) xs
headP :: Product (t : ts) -> t
headP (x ::> xs) = x
What I would like to write is a Bundle
instance that simply replaces Identity
with Signal dom
. Unfortunately, we can't do that in one go:
instance Bundle (Product ts) where
type Unbundled dom (Product ts) = ProductF (Signal dom) ts
bundle NilP = pure NilP
bundle (ConsP x xs) = (::>) <$> x <*> bundle xs
unbundle = _ -- Can't implement this, since it would require splitting on ts
Here, unbundle
needs to do something different for ts ~ []
and for ts ~ t : ts'
. OK, so let's try writing it in two instances:
instance Bundle (Product '[]) where
type Unbundled dom (Product '[]) = ProductF (Signal dom) '[]
bundle NilP = pure NilP
unbundle _ = NilP
instance (Bundle (Product ts), forall dom. Unbundled dom (Product ts) ~ ProductF (Signal dom) ts) => Bundle (Product (t : ts)) where
type Unbundled dom (Product (t : ts)) = ProductF (Signal dom) (t : ts)
bundle (ConsP x xs) = (::>) <$> x <*> bundle xs
unbundle xs = ConsP (headP <$> xs) (unbundle $ tailP <$> xs)
And so it is in that second instance that the problem arises. Even though we have a (quantified) type equality forall dom. Unbundled dom (Product ts) ~ ProductF (Signal dom) ts
in the instance constraints, GHC 8.6.3 doesn't use it during typechecking:
For bundle
:
• Couldn't match type ‘Unbundled dom (Product ts)’ with ‘ProductF (Signal dom) ts’ Expected type: Unbundled dom (Product ts) Actual type: ProductF (Signal dom) ts1 • In the first argument of ‘bundle’, namely ‘xs’ In the second argument of ‘(<*>)’, namely ‘bundle xs’ In the expression: (::>) <$> x <*> bundle xs
For unbundle
:
• Couldn't match expected type ‘ProductF (Signal dom) ts’ with actual type ‘Unbundled dom (ProductF Identity ts)’ • In the second argument of ‘ConsP’, namely ‘(unbundle $ tailP <$> xs)’ In the expression: ConsP (headP <$> xs) (unbundle $ tailP <$> xs) In an equation for ‘unbundle’: unbundle xs = ConsP (headP <$> xs) (unbundle $ tailP <$> xs)
A possible workaround
Of course, we can just take the long road instead: make our own class specifically for Product
and delegate all the real work to that. I am presenting that solution here, but I am specifically interested in something that is less verbose and ad-hoc than this.
class IsProduct (ts :: [*]) where
type UnbundledProd dom ts = (ts' :: [*]) | ts' -> dom ts
bundleProd :: Product (UnbundledProd dom ts) -> Signal dom (Product ts)
unbundleProd :: Signal dom (Product ts) -> Product (UnbundledProd dom ts)
instance (IsProduct ts) => Bundle (Product ts) where
type Unbundled dom (Product ts) = Product (UnbundledProd dom ts)
bundle = bundleProd
unbundle = unbundleProd
and then IsProduct
has the advantage that it can actually be implemented:
type (:::) (name :: k) (a :: k1) = (a :: k1)
instance IsProduct '[] where
type UnbundledProd dom '[] = dom ::: '[]
bundleProd NilP = pure NilP
unbundleProd _ = NilP
instance (IsProduct ts) => IsProduct (t : ts) where
type UnbundledProd dom (t : ts) = Signal dom t : UnbundledProd dom ts
bundleProd (x ::> xs) = (::>) <$> x <*> bundleProd xs
unbundleProd xs = (headP <$> xs) ::> (unbundleProd $ tailP <$> xs)