typeclass - 使用接口在 Type 上定义偏函数
问题描述
我试图在 Idris 中定义一些东西,这将提供一种以印刷方式表示类型的方法。想一想Show
,但Show
我不想显示类型的元素,而是显示类型本身。这将具有相同的实际效果,即在Type
(给出类型的表示)上定义用户也可以为其类型扩展的部分函数。
考虑到“用户可以扩展功能”的要求,我的第一个想法是使用接口。所以看起来像这样:
interface Representable a where
representation : String
一些可能的实现是
Representable Nat where
representation = "Nat"
但是,我遇到了一个问题。假设我想定义函数类型的表示。那将是其域的表示、箭头和其范围的表示。所以像
(Representable a, Representable b) => Representable (a -> b) where
representation = representation ++ " -> " ++ representation
现在问题很明显了。编译器无法推断representation
右侧的 's 类型。
我想出的一个替代方案是创建一个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
争论——显然,因为它是type的表示,而不是它的元素。
有没有人对如何解决这个特定错误有任何建议,或者更好的是,如何以一些不那么可怕的方式实现我的想法?
解决方案
您可以从 Haskell 中获取想法并使用代理将令牌传递给representation
没有术语级别的内容:
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})
推荐阅读
- python - Selenium with Python - 在选定元素之后查找元素
- mysql - 为什么会出现这个sql查询错误?有没有另一种写法?
- c++ - 我的代码以奇怪的字体将数字保存到 data.txt 文件中
- azure-devops - 从 Azure 管道使用 AWS CLI
- jquery - jQuery Datatable 中的列重叠
- java - 如何将给定的数字列表划分为 N 个数组,其中每个数组的总和最接近特定数字 M?
- javascript - 如何将货币添加到 javascript 变量中?
- javascript - 如何自动增加本地存储的javascript项目值?
- c - 为什么一个程序会依赖glibc而不调用它的任何功能?
- xamarin - 在 xamarin 表单应用程序中导航时未调用后退按钮的自定义 NavigationRenderer