Idris 可以推断顶级常量类型中的索引吗?

2023-12-28

例如,Agda 允许我这样写:

open import Data.Vec
open import Data.Nat

myVec : Vec ℕ _
myVec = 0 ∷ 1 ∷ 2 ∷ 3 ∷ []

and myVec将有类型Vec ℕ 4正如预期的那样。

但如果我在伊德里斯尝试同样的事情:

import Data.Vect

myVec : Vect _ Nat
myVec = [0, 1, 2, 3]

我从类型检查器收到一条错误消息:

 When checking right hand side of myVec with expected type
         Vect len Nat

 Type mismatch between
         Vect 4 Nat (Type of [0, 1, 2, 3])
 and
         Vect len Nat (Expected type)

 Specifically:
         Type mismatch between
                 4
         and
                 len

有没有办法定义myVec在 Idris 中无需手动指定索引Vect?


根据评论,伊德里斯的顶级漏洞被普遍量化,而不是通过术语推理来填补。

我相信(但是,最终开发团队中的某个人必须确认/否认)这样做部分是为了鼓励显式类型,从而鼓励类型导向的开发,部分是为了为无关值提供一个很好的语法接口实现如:

Uninhabited v => Uninhabited (_, v) where
    uninhabited (_, void) = uninhabited void

下划线的这种不关心的使用是从它在模式中的使用中采用的,而不是它在表达式中的使用。


对于这样的东西(这不完全是你想要的,但它对常量的改变是鲁棒的),你可以使用显式的存在:

fst : DPair t _ -> t
fst (x ** _) = x

snd : (s : DPair _ p) -> p (fst s)
snd (_ ** prf) = prf

myVecEx : (n ** Vect n Nat)
myVecEx = (_ ** [0, 1, 2, 3])

myVec : Vect (fst myVecEx) Nat
myVec = snd myVecEx

fst and snd可能在标准库中以不同的名称存在,但我在快速搜索中没有找到。

编辑:最近的一次投票再次引起了我对这个答案的注意。如果您使用 Idris 2,我相信您可以使用?代替_在顶层将其填充到我的 idris 中。_在顶层,仍然是一个被删除的、隐式的、未命名的参数。https://idris2.readthedocs.io/en/latest/implementation/overview.html#additional-type-in​​ference https://idris2.readthedocs.io/en/latest/implementation/overview.html#additional-type-inference

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Idris 可以推断顶级常量类型中的索引吗? 的相关文章

随机推荐