为Agda功能提供证据

时间:2017-07-30 14:27:13

标签: agda

鉴于以下片段(取自Ulf Norell的教程 和James Chapman),我理解查询的来电者必须提供所需项目的索引 ix 有效但我无法找到合适的证明形成这样做。

data Proven-n : Set where
record Proven-y : Set where

-- using the name 'isProven' instead of 'isTrue' of the tutorial
isProven : Bool -> Set 
isProven true = Proven-y
isProven false = Proven-n

lookup : {A : Set}(xs : List A)(ix : Nat) -> isProven (n < length xs) -> A

我已经设法解决了下面的特殊情况,但我尝试更广泛地应用查找,其中向量和查找索引是在另一个函数中生成的,失败。我想了解传递证据的概念。请帮忙。

aList : List Bool
aList = true :: false :: []

aTest: Bool 
aTest = lookup aList 0 (record {})

1 个答案:

答案 0 :(得分:0)

编辑:以下内容更有意义,作为对编辑here的问题的回复。

我猜你想要aTest : Nat -> Maybe Bool之类的东西,如果给定的索引超出范围,则返回nothing。在这种情况下,以下内容将:

data Decide (P : Set) : Set where
  yes : P -> Decide P
  no : (P -> Proven-n) -> Decide P

okay? : ∀ {A : Set} (xs : List A) (ix : Nat) -> Decide (isProven (ix < length xs))
okay? [] ix = no λ ()
okay? (_ ∷ xs) zero = yes record {}
okay? (_ ∷ xs) (suc ix) = okay? xs ix

data Maybe (A : Set) : Set where
  nothing : Maybe A
  just : A -> Maybe A

aTest : Nat -> Maybe Bool
aTest ix with okay? aList ix
... | yes ix<len = just (lookup aList ix ix<len)
... | no ix≥len = nothing

在上文中,aTest使用okay?来确定索引是否在边界内。如果是,则证明(由Decide携带)传递给lookup