必须手动打开量化类型 class 证据?

Having to open quantified type class evidence manually?

是否必须像prj'那样手动打开证据,或者是否有更直接的方法来引导实例求解器?

{-# LANGUAGE QuantifiedConstraints #-}
{-# LANGUAGE UndecidableInstances #-}

module CatLib.SO.SO_TCMatchingDict where

class OneSide a where oneSide :: a

class (OneSide a {- , ManyOtherTC a -}) => TC a
instance (OneSide a {- , ManyOtherTC a -}) => TC a

class Other f where other :: forall a. f a

instance Other f => OneSide (f x) where oneSide = other


prj :: forall f a. (forall x. TC (f x)) => f a
prj = oneSide @(f a) -- Could not deduce (Other f) arising from a use of ‘oneSide’

prj' :: forall f g tag a b. (forall x. TC (f x)) => f a
prj' = case Dict :: Dict (TC (f a)) of
  Dict -> oneSide @(f a) -- that's ok

data Dict c = c => Dict

怎么样

prj :: forall f a. (forall x. TC (f x)) => f a
prj = go
  where
    go :: TC (f a) => f a
    go = oneSide @(f a)

或者,在一行中:

prj :: forall f a. (forall x. TC (f x)) => f a
prj = oneSide :: TC (f a) => f a