使用接口在 Type 上定义部分函数

Using interfaces to define partial functions on Type

我正在尝试在 Idris 中定义 something,它将提供一种以印刷方式表示类型的方法。想想 Show,但我不想 Showing 类型的元素,而是想显示类型 本身 。这与在 Type 上定义部分函数(给出类型的表示)具有相同的实际效果,用户也可以为他们的类型扩展。

考虑到“用户可以扩展功能”的要求,我的第一个想法是使用接口。所以这看起来像这样:

interface Representable a where
  representation : String

一些可能的实现是

Representable Nat where
  representation = "Nat"

但是,我遇到了一个问题。假设我想定义函数类型的表示。那将是其域的表示,箭头和范围的表示。所以像

(Representable a, Representable b) => Representable (a -> b) where
  representation = representation ++ " -> " ++ representation

现在问题很明显了。编译器无法推断右侧 representation 的类型。

我想出的一个替代方案是创建一个 Representation 类型,它会带有一个“人工”参数:

data Representation : Type -> Type where
  Repr : (a : Type) -> String -> Representation a

这会导致

interface Representable a where
  representation : Representation a

然后

Representable Nat where
  representation = Repr Nat "Nat"

(Representable d, Representable r) => Representable (d -> r) where
  representation = Repr (d -> r) $
    (the (Representation d) representation)
    ++ " -> "
    ++ (the (Representation r) representation)

但是,当然,我得到了错误

Representations.idr:13:20-127:
   |
13 |   representation = Repr (d -> r) $ (the (Representation d) representation) ++ " -> " ++ (the (Representation r) representation)
   |                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
When checking right hand side of Representations.Representations.d -> r implementation of Representations.Representable, method representation with expected type
        Representation (d -> r)

When checking argument a to constructor Representations.Repr:
        No such variable d

但是,我希望 representation 是无参数的——显然,因为它是 类型 的表示,而不是它的元素。

有没有人对如何修复这个特定错误有任何建议,或者更好的是,如何以一些不那么可怕的方式实现我的想法?

您可以借鉴 Haskell 的想法并使用 proxy 将令牌传递给 representation 而没有 term-level 内容:

data Proxy a = MkProxy

interface Representable a where
  representation : Proxy a -> String

Representable Nat where
  representation _ = "Nat"

(Representable a, Representable b) => Representable (a -> b) where
  representation {a = a} {b = b} _ = representation (MkProxy {a = a}) ++ " -> " ++ representation (MkProxy {a = b})