有没有一种方法可以轻松地将类型堆栈'(f1 (f2 (f3 .... fn t))) a'封装为'F t a'?
Is there a way to easily encapsulate stack of types '(f1 (f2 (f3 .... fn t))) a' as 'F t a'?
我用头撞墙已经有一段时间了。我有一堆类型,它们表示对基本类型的转换(更具体地说,XMonad 中的布局修饰符)。
长话短说,这些类型都有种类 (* -> *) -> * -> *
。
出于我不想在这里讨论的原因,我想做的是获取这些转换的堆栈,并将它们表示为基类型(* -> *
类型)的单个转换.
我的第一个想法是定义一个类型组合运算符
newtype ((f :: (* -> *) -> * -> *) :. (g :: (* -> *) -> * -> *)) l a
= Compose (f (g l) a)
而且大部分情况下它都有效。但是,给定一个值,比如 v :: f1 (f2 (f3 (... (fn l))) a
,我必须对其应用 Compose
n-1
次才能得到 v' :: (f1 :. f2 :. ... :. fn) l a
,这不是很漂亮而且有点烦人。
所以,问题是,有没有办法自动应用 Compose
直到我得到我想要的?
F.ex.,现在我做这样的事情:
modifyLayout $ Compose . Compose . Compose . Mirror . avoidStruts . minimize . smartBorders
我想做的事情:
modifyLayout' $ Mirror . avoidStruts . minimize . smartBorders
where modifyLayout' = modifyLayout . magicCompose
一个相关问题:也许有更好的方式来表达相同的概念?
供参考,modifyLayout
是
modifyLayout :: (CC m Window)
=> (forall l. (LayoutClass l Window) => l Window -> m l Window)
-> ConfigMonad
澄清(编辑):
使用类型组合的整个想法是这样的。
考虑两个布局修饰符,
m1 :: LayoutClass l a => l a -> M1 l a
和
m2 :: LayoutClass l a => l a -> M2 l a
如果我组合这两个,我得到
m1m2 :: (LayoutClass l a, LayoutClass (M2 l) a) => l a -> M1 (M2 l) a
m1m2 = m1 . m2
我们可以假设有一个 instance LayoutClass l a => LayoutClass (M2 l) a
。同时,还假设有 CC M1 Window
和 CC M2 Window
.
的实例
如果我现在尝试将其输入上面定义的 modifyLayout
:
modifyLayout m1m2
GHC 立即对嵌套类型感到困惑并抱怨:
Couldn't match type ‘l’ with ‘M2 l’
‘l’ is a rigid type variable bound by
a type expected by the context:
LayoutClass l Window => l Window -> M1 l Window
Expected type: l Window -> M1 l Window
Actual type: l Window -> M1 (M2 l) Window
使用类型组合,我可以纠正这一点,因为 GHC 在 modifyLayout
签名中将 M1 :. M2
与 m
匹配,并避免整个嵌套混淆。类型同义词显然不会有这个 属性.
更新:
经过一番摸索,我找到了部分解决方案(不知道为什么我没有早点想到它,但是哦好吧)
可以像这样定义类型类
class S l f t | f l -> t where
sq :: (l a -> f a) -> (l a -> t l a)
函数依赖确保编译器能够select自己实例化。
那么,就可以这样写实例了
instance S l (m1 l) m1 where
sq = id
instance S l (m1 (m2 l)) (m1 :. m2) where
sq = sq . (Compose .)
instance S l (m1 (m2 (x l))) ((m1 :. m2) :. x) where
sq = sq . (Compose .)
instance S l (m1 (m2 (m3 (x l)))) (((m1 :. m2) :. m3) :. x) where
sq = sq . (Compose .)
-- etc
这部分回答了我的问题:sq
封装了一个转换堆栈,前提是为给定的嵌套级别定义了一个实例。
但是,这些实例似乎需要递归实例定义。到目前为止,我还无法弄清楚那到底是什么样子。因此,欢迎任何见解。
感谢 Adam Vogt (@aavogt),我终于得出了一个令人满意的结论。
我在 S
class 的实例中走在了正确的轨道上。事实证明,反转实例依赖性允许类型检查器推断其他实例。但是,递归终止(即基本情况)需要 IncoherentInstances
扩展。
代码如下:
instance {-# INCOHERENT #-} S l (m l) m where
sq = id
instance S l ((f :. g) l') t => S l (f (g l')) t where
sq = squash . (Compose .)
我用头撞墙已经有一段时间了。我有一堆类型,它们表示对基本类型的转换(更具体地说,XMonad 中的布局修饰符)。
长话短说,这些类型都有种类 (* -> *) -> * -> *
。
出于我不想在这里讨论的原因,我想做的是获取这些转换的堆栈,并将它们表示为基类型(* -> *
类型)的单个转换.
我的第一个想法是定义一个类型组合运算符
newtype ((f :: (* -> *) -> * -> *) :. (g :: (* -> *) -> * -> *)) l a
= Compose (f (g l) a)
而且大部分情况下它都有效。但是,给定一个值,比如 v :: f1 (f2 (f3 (... (fn l))) a
,我必须对其应用 Compose
n-1
次才能得到 v' :: (f1 :. f2 :. ... :. fn) l a
,这不是很漂亮而且有点烦人。
所以,问题是,有没有办法自动应用 Compose
直到我得到我想要的?
F.ex.,现在我做这样的事情:
modifyLayout $ Compose . Compose . Compose . Mirror . avoidStruts . minimize . smartBorders
我想做的事情:
modifyLayout' $ Mirror . avoidStruts . minimize . smartBorders
where modifyLayout' = modifyLayout . magicCompose
一个相关问题:也许有更好的方式来表达相同的概念?
供参考,modifyLayout
是
modifyLayout :: (CC m Window)
=> (forall l. (LayoutClass l Window) => l Window -> m l Window)
-> ConfigMonad
澄清(编辑):
使用类型组合的整个想法是这样的。
考虑两个布局修饰符,
m1 :: LayoutClass l a => l a -> M1 l a
和
m2 :: LayoutClass l a => l a -> M2 l a
如果我组合这两个,我得到
m1m2 :: (LayoutClass l a, LayoutClass (M2 l) a) => l a -> M1 (M2 l) a
m1m2 = m1 . m2
我们可以假设有一个 instance LayoutClass l a => LayoutClass (M2 l) a
。同时,还假设有 CC M1 Window
和 CC M2 Window
.
如果我现在尝试将其输入上面定义的 modifyLayout
:
modifyLayout m1m2
GHC 立即对嵌套类型感到困惑并抱怨:
Couldn't match type ‘l’ with ‘M2 l’
‘l’ is a rigid type variable bound by
a type expected by the context:
LayoutClass l Window => l Window -> M1 l Window
Expected type: l Window -> M1 l Window
Actual type: l Window -> M1 (M2 l) Window
使用类型组合,我可以纠正这一点,因为 GHC 在 modifyLayout
签名中将 M1 :. M2
与 m
匹配,并避免整个嵌套混淆。类型同义词显然不会有这个 属性.
更新:
经过一番摸索,我找到了部分解决方案(不知道为什么我没有早点想到它,但是哦好吧)
可以像这样定义类型类
class S l f t | f l -> t where
sq :: (l a -> f a) -> (l a -> t l a)
函数依赖确保编译器能够select自己实例化。
那么,就可以这样写实例了
instance S l (m1 l) m1 where
sq = id
instance S l (m1 (m2 l)) (m1 :. m2) where
sq = sq . (Compose .)
instance S l (m1 (m2 (x l))) ((m1 :. m2) :. x) where
sq = sq . (Compose .)
instance S l (m1 (m2 (m3 (x l)))) (((m1 :. m2) :. m3) :. x) where
sq = sq . (Compose .)
-- etc
这部分回答了我的问题:sq
封装了一个转换堆栈,前提是为给定的嵌套级别定义了一个实例。
但是,这些实例似乎需要递归实例定义。到目前为止,我还无法弄清楚那到底是什么样子。因此,欢迎任何见解。
感谢 Adam Vogt (@aavogt),我终于得出了一个令人满意的结论。
我在 S
class 的实例中走在了正确的轨道上。事实证明,反转实例依赖性允许类型检查器推断其他实例。但是,递归终止(即基本情况)需要 IncoherentInstances
扩展。
代码如下:
instance {-# INCOHERENT #-} S l (m l) m where
sq = id
instance S l ((f :. g) l') t => S l (f (g l')) t where
sq = squash . (Compose .)