Idris - 无法评估类型中的函数应用程序

时间:2015-12-22 05:52:26

标签: idris unification type-level-computation

我遇到的问题是,我的值为fun afun是函数,a是在类型检查时无法计算的值当我强制它成为该函数应用程序的结果时抛出一个统一错误。

具体错误是:

When checking right hand side of testRec2 with expected type
         Record [("A", String), ("C", Nat)]

 Type mismatch between
         Record (projectLeft ["A", "C"]
                             [("A", String),
                              ("B", String),
                              ("C", Nat)]) (Type of hProjectByLabels_comp ["A",
                                                                           "C"]
                                                                          testRec1
                                                                          (getYes (isSet ["A",
                                                                                          "C"])))
 and
         Record [("A", String), ("C", Nat)] (Expected type)

 Specifically:
         Type mismatch between
                 projectLeft ["A", "C"]
                             [("A", String), ("B", String), ("C", Nat)]
         and
                 [("A", String), ("C", Nat)]

这是来自Idris中类似HList的记录的实现,具有以下示例:

testRec1 : Record [("A", String), ("B", String), ("C", Nat)]
-- testRec1's value is already defined    

testRec2 : Record [("A", String), ("C", Nat)]
testRec2 = hProjectByLabels_comp ["A", "C"] testRec1 (getYes $ isSet ["A", "C"])

......以下类型:

IsSet : List t -> Type

isSet : DecEq t => (xs : List t) -> Dec (IsSet xs)

LabelList : Type -> Type

IsLabelSet : LabelList lty -> Type

HList : LabelList lty -> Type

Record : LabelList lty -> Type

recToHList : Record ts -> HList ts

recLblIsSet : Record ts -> IsLabelSet ts

hListToRec : DecEq lty => {ts : LabelList lty} -> {prf : IsLabelSet ts} -> HList ts -> Record ts

IsProjectLeft : DecEq lty => List lty -> LabelList lty -> LabelList lty -> Type

IsProjectRight : DecEq lty => List lty -> LabelList lty -> LabelList lty -> Type

hProjectByLabelsHList : DecEq lty => {ts : LabelList lty} -> (ls : List lty) -> HList ts -> ((ls1 : LabelList lty ** (HList ls1, IsProjectLeft ls ts ls1)), (ls2 : LabelList lty ** (HList ls2, IsProjectRight ls ts ls2)))

projectLeft : DecEq lty => List lty -> LabelList lty -> LabelList lty

hProjectByLabelsLeftIsSet_Lemma2 : DecEq lty => {ls : List lty} -> {ts1, ts2 : LabelList lty} -> IsProjectLeft ls ts1 ts2 -> IsLabelSet ts1 -> IsLabelSet ts2

fromIsProjectLeftToComp : DecEq lty => {ls : List lty} -> {ts1, ts2 : LabelList lty} -> IsProjectLeft ls ts1 ts2 -> IsSet ls -> ts2 = projectLeft ls ts1

hProjectByLabels_comp : DecEq lty => {ts : LabelList lty} -> (ls : List lty) -> Record ts -> IsSet ls -> Record (projectLeft ls ts)

......以及以下(必要的)定义:

LabelList : Type -> Type
LabelList lty = List (lty, Type)

IsLabelSet : LabelList lty -> Type
IsLabelSet ts = IsSet (map fst ts) 

projectLeft : DecEq lty => List lty -> LabelList lty -> LabelList lty
projectLeft [] ts = []
projectLeft ls [] = []
projectLeft ls ((l,ty) :: ts) with (isElem l ls)
  projectLeft ls ((l,ty) :: ts) | Yes lIsInLs = 
    let delLFromLs = deleteElem ls lIsInLs
        rest = projectLeft delLFromLs ts
    in (l,ty) :: rest
  projectLeft ls ((l,ty) :: ts) | No _ = projectLeft ls ts

deleteElem : (xs : List t) -> Elem x xs -> List t
deleteElem (x :: xs) Here = xs
deleteElem (x :: xs) (There inThere) =
  let rest = deleteElem xs inThere
  in x :: rest 

getYes : (d : Dec p) -> case d of { No _ => (); Yes _ => p}
getYes (No _ ) = ()
getYes (Yes prf) = prf

hProjectByLabels_comp : DecEq lty => {ts : LabelList lty} -> (ls : List lty) -> Record ts -> IsSet ls -> Record (projectLeft ls ts)
hProjectByLabels_comp {ts} ls rec lsIsSet =
  let 
    isLabelSet = recLblIsSet rec
    hs = recToHList rec
    (lsRes ** (hsRes, prjLeftRes)) = fst $ hProjectByLabelsHList ls hs
    isLabelSetRes = hProjectByLabelsLeftIsSet_Lemma2 prjLeftRes isLabelSet
    resIsProjComp = fromIsProjectLeftToComp prjLeftRes lsIsSet
    recRes = hListToRec {prf=isLabelSetRes} hsRes
  in rewrite (sym resIsProjComp) in recRes

基本上,有一个函数projectLeft应用于2个列表并返回一个新列表。 hProjectByLabels_comp的类型在类型级别应用此函数。 为了实际构造结果列表,我有一个样式Pred l1 l2 l3的谓词和样式Pred l1 l2 l3 -> l3 = projectLeft l1 l2的引理。在hProjectByLabels_comp中,我将引理应用于谓词,并使用rewrite来获取正确的类型签名(重写l3,它隐含在实现中出现的谓词中,projectLeft l1 l2 1}},或在此特定情况下为projectLeft ls ts

我希望将hProjectByLabels_comp应用于记录会正确计算projectLeft ls ts。但是,在上面的示例中,它无法评估/计算projectLeft ["A", "C"] [("A", String), ("B", String), ("C", Nat)]。这看起来很奇怪,因为在REPL中评估函数应用程序给出了[("A", String), ("C", Nat)],这是类型所期望的,但是在类型检查时,Idris似乎无法计算这个函数。

我不确定某些引理/函数的实现是否与此有关,或者它是否仅仅是关于类型的内容。

我尝试使用更简单的示例(在Nats上使用谓词和函数)复制此错误,但是更简单的示例类型检查正确,因此我找不到另一种方法来复制此错误。

我正在使用Idris 0.9.20.2

修改:我尝试按以下方式重写projectLeft以查看是否有任何更改但仍然显示相同的错误

projectLeft : DecEq lty => List lty -> LabelList lty -> LabelList lty
projectLeft ls [] = []
projectLeft ls ((l,ty) :: ts) with (isElem l ls)
  projectLeft ls ((l,ty) :: ts) | Yes lIsInLs = 
    let delLFromLs = deleteElem ls lIsInLs
        rest = projectLeft delLFromLs ts
    in (l,ty) :: rest
  projectLeft ls ((l,ty) :: ts) | No _ = projectLeft ls ts

2 个答案:

答案 0 :(得分:1)

projectLeft是否具有全部功能?部分函数不会减少类型签名,也就是说,您只是看到它们应用于它们的参数,而不是应用程序的结果减少到的位置。

一个证明这一点的例子:

type : Int -> Type
type 0 = String

a : type 0
a = "Hello"

无法编译时出现类型错误,抱怨无法将type 0String匹配。尽管为所讨论的值定义了函数type,但Idris拒绝在类型签名中应用部分函数。不过,您仍然可以在repl中应用它。 type 0提供String : Typetype 1提供type 1 : Type(未减少)。

答案 1 :(得分:0)

显然,在更新到Idris 0.12后,此问题现已解决。没有改变任何东西,但它现在没有改变。