试图抽象掉typeclass,但是类型变量转义了 [英] Trying to abstract away typeclass, but a type variable escapes

查看:97
本文介绍了试图抽象掉typeclass,但是类型变量转义了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些类及其实例.该示例显示了一些荒谬的类.它们的确切性质并不重要.

I have some classes and their instances. The example shows some nonsensical classes. Their exact nature is not important.

{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE TypeSynonymInstances #-}
{-# LANGUAGE FlexibleInstances #-}


class Foo a where
   foo :: a -> Int
class Bar a where
   bar :: a -> String

instance Foo Int where
   foo x = x
instance Foo String where
   foo x = length x

instance Bar Int where
   bar x = show x
instance Bar String where
   bar x = x

好的,现在我想创建一些将这些类隐藏在某些数据类型外观后面的存在性类型,因此我不必处理约束. (我知道存在类型被视为反模式,请不要向我解释这一点.)

OK, now I want to create some existential types that hide these classes behind some datatype façade, so I don't have to deal with constraints. (I know existential types are considered an anti-pattern, please don't explain this to me).

data TFoo = forall a. Foo a => TFoo a
instance Foo TFoo where
  foo (TFoo x) = foo x
data TBar = forall a. Bar a => TBar a
instance Bar TBar where
  bar (TBar x) = bar x

显然那里有一些样板.我想把它抽象出来.

Obviously there's some boilerplate in there. I want to abstract it away.

{-# LANGUAGE ConstraintKinds #-}
data Obj cls = forall o. (cls o) => Obj o

因此,我只有一个由类型类参数化的存在性类型,而不是几个存在性类型.到目前为止一切顺利.

So instead of several existential types I have just one, parametrised by a typeclass. So far so good.

现在如何在Obj a上执行操作?显而易见的尝试

Now how do I perform operations on Obj a? The obvious attempt

op f (Obj a) = f a

失败,因为类型变量可以转义.

fails because the type variable could escape.

existential.hs:31:18: error:
    • Couldn't match expected type ‘o -> p1’ with actual type ‘p’
        because type variable ‘o’ would escape its scope
      This (rigid, skolem) type variable is bound by
        a pattern with constructor:
          Obj :: forall (cls :: * -> Constraint) o. cls o => o -> Obj cls,
        in an equation for ‘call’
        at existential.hs:31:9-13
    • In the expression: f k
      In an equation for ‘call’: call f (Obj k) = f k
    • Relevant bindings include
        k :: o (bound at existential.hs:31:13)
        f :: p (bound at existential.hs:31:6)
        call :: p -> Obj cls -> p1 (bound at existential.hs:31:1)
   |
31 | call f (Obj k) = f k
   |                  ^^^
Failed, no modules loaded.

我有点理解为什么会这样.但是对于像call foocall bar这样的真正调用,类型变量将不会逃脱.我可以说服编译器吗?也许我能以某种方式表示类型u -> v where v does not mention u(实际上应该是f的类型)?如果没有,还有什么其他方法可以处理这种情况?我想我可以使用TemplateHaskell生成某些内容,但是仍然无法将其包裹住.

I kinda understand why this happens. But with real invocations like call foo and call bar the type variable wouldn't escape. Can I convince the compiler of it? Perhaps I somehow can express the type u -> v where v does not mention u (which really should be the type of f)? If not, what other ways to deal with the situation are there? I guess I could generate something with TemplateHaskell but I still cannot wrap my head around it.

推荐答案

您的代码可以正常工作;编译器仅需要有关其类型的帮助.

Your code works fine; the compiler just needs some help with its type.

Obj隐藏其内容的类型,这意味着op的参数f必须是多态的(也就是说,它不能检查其参数).开启RankNTypes:

Obj hides the type of its contents, which means that op's argument f must be polymorphic (that is, it can't scrutinise its argument). Turn on RankNTypes:

op :: (forall a. cls a => a -> r) -> Obj cls -> r
op f (Obj x) = f x

您必须提供完整的类型签名,因为GHC无法推断更高级别的类型.

You have to give the type signature in full because GHC can't infer higher-rank types.

对这样的类进行现有的量化是通常不是设计给定程序的最佳方法.

Existentially quantifying over a class like this is usually not the best way to design a given program.

这篇关于试图抽象掉typeclass,但是类型变量转义了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆