假设我有一个这样的函数:
{-# LANGUAGE ScopedTypeVariables #-}
class C a where
foo :: forall f a b. (C (f a), C (f b)) => f a -> f b
foo = _
现在,如果我想移动范围a
and b
在类型中类型类约束的右侧foo
(假设,因为我想用foo
实现需要多态的类型类方法a
and b
),可以使用一些跑腿工作来完成Data.Constraint.Forall
:
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE ConstraintKinds, TypeOperators #-}
import Data.Constraint
import Data.Constraint.Forall
foo' :: forall f. (ForallF C f) => forall a b. f a -> f b
foo' = helper
where
helper :: forall a b. f a -> f b
helper = case (instF :: ForallF C f :- C (f a)) of
Sub Dict -> case (instF :: ForallF C f :- C (f b)) of
Sub Dict -> foo
现在,我的问题是,假设我将函数更改为涉及类型相等的函数:
{-# LANGUAGE TypeFamilies, ScopedTypeVariables #-}
type family F a :: * -> *
bar :: forall f g a b. (F (f a) ~ g a, F (f b) ~ g b) => f a -> f b
bar = _
有没有办法使上述技术适应这一点?
这是我尝试过的:
{-# LANGUAGE TypeFamilies, ScopedTypeVariables #-}
{-# LANGUAGE ConstraintKinds, TypeOperators #-}
import Data.Constraint
import Data.Constraint.Forall
type F'Eq f g x = F (f x) ~ g x
bar' :: forall f g. (Forall (F'Eq f g)) => forall a b. f a -> f b
bar' = helper
where
helper :: forall a b. f a -> f b
helper = case (inst :: Forall (F'Eq f g) :- F'Eq f g a) of
Sub Dict -> case (inst :: Forall (F'Eq f g) :- F'Eq f g b) of
Sub Dict -> bar
但是(毫不奇怪)由于不饱和类型同义词,这会失败:
类型同义词‘F'Eq’
应该有 3 个参数,但已给出 2 个
在表达式类型签名中:Forall (F'Eq f g) :- F'Eq f g a
在表达式中:(inst :: Forall (F'Eq f g) :- F'Eq f g a)