idris - 使用依赖对时类型检查失败

标签 idris dependent-type

我写下这个:

data IsTag : String -> Type where
    NumIsTag : IsTag "num"
    StrIsTag : IsTag "str"

arr1 : List (tag : String ** (IsTag tag, (case tag of "num" => Int; "str" => String)))
arr1 = [("num" ** (NumIsTag, 1)), ("str" ** (StrIsTag, "hello"))]

并收到以下使用错误消息:

When checking right hand side of arr1 with expected type
        List (tag : String **
              (IsTag tag, case tag of   "num" => Int "str" => String))

When checking argument b to constructor Builtins.MkPair:
        case "num" of
          "num" => Int
          "str" => String is not a numeric type

但我不明白,为什么 case "num"of "num"=> Int; "str"=> String 不是数字类型?它不等于Int吗?

最佳答案

事实并非如此,因为 Idris 在类型检查期间不会减少部分(非全部)函数。

如果您的字符串不等于 "num"/"str" 时有一个很好的默认类型,那么您可以使用如下所示的内容:

total
tagToType : String -> Type
tagToType "num" = Int
tagToType "str" = String
tagToType _ = ?defaultType

arr1 : List (tag : String ** (IsTag tag, tagToType tag))
arr1 = [("num" ** (NumIsTag, 1)), ("str" ** (StrIsTag, "hello"))]

另一种选择是像这样定义它:

total
tagToType : IsTag s -> Type
tagToType NumIsTag = Int
tagToType StrIsTag = String

arr1 : List (tag : String ** istag : IsTag tag ** tagToType istag)
arr1 = [("num" ** NumIsTag ** 1), ("str" ** StrIsTag ** "hello")]

关于idris - 使用依赖对时类型检查失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48154108/

相关文章:

standard-library - 如何获取 Idris 标准库的源代码?

idris - 为什么重写在这种情况下不改变表达式的类型?

verification - 流仿函数定律的证明

coq - 将临时列表转换为 Coq 中的依赖类型列表

haskell - Haskell 中异构列表的序列

scala - 当由 Scala 宏生成时,依赖类型似乎为 “not work”

Idris:强制向量和相等

list - 如何在 Idris REPL 中创建空列表?

haskell - 使 "The Singletons Paper"保持最新。如何使用单例库实现 AChar 数据类型

idris 中的快速排序