dependent-type - 如何在 Idris 中重写函数体,以便类型对应于函数签名并且整个事物都可以编译

标签 dependent-type idris

我想编译这个:

foo: Vect n String -> Vect n String
foo {n} xs = take n xs

这编译失败,因为编译器无法统一 nn + m .我知道这是因为take的签名对于 Vect,但我不知道如何向编译器展示它们可以统一,如果 m = 0 .

最佳答案

只是为了补充上一个答案,另一种可能性是使用现有的 plusZeroRightNeutral 进行内联重写。图书馆的引理:

foo: Vect n String -> Vect n String
foo {n} xs = let xs' : Vect (n + 0) String
                     = rewrite (plusZeroRightNeutral n) in xs in
                 take n xs'

Idris 在统一方面的困难是因为它不愿意推断 m在take的应用中:
take : (n : Nat) -> Vect (n + m) a -> Vect n a

你给了它一个 Vect n String它想要一个 Vect (n + m) a - 它愉快地统一了aString , 因为 Vect是类型构造器,但不愿统一nn + m因为,一般来说,它不能反转函数。你我都看得出来 m必须为零,但 idris 并不那么聪明。

关于dependent-type - 如何在 Idris 中重写函数体,以便类型对应于函数签名并且整个事物都可以编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24940018/

相关文章:

haskell - Haskell 中类型类的类型级别指示符函数

haskell - 为什么 Idris 不接受我的自定义折叠?

functional-programming - 所以 : what's the point?

dependent-type - 在Idris中进行秩N量化

agda - 类型中使用的agda命题-这是什么意思?

types - 由类型索引与在 idris 中包含类型

dependent-type - 如何证明一个类型与自身的 bool 不等式在 Idris 中是无人居住的?

haskell - 是否可以在构造演算上表达平衡无标记二叉树的类型?

parsing - Idris 解析器组合器 GADT

syntax - 是否可以在 idris 的函数定义中使用守卫?