1
鑑於下列的數據類型,其編碼兩個自然數之間的驗證比較:故障在圖案匹配阿格達2.5.1.2
open import Agda.Builtin.Nat
data Compare : Nat -> Nat -> Set where
LT : {m : Nat} {n : Nat} -> Compare m (suc (m + n))
EQ : {m : Nat} -> Compare m m
GT : {m : Nat} {n : Nat} -> Compare (suc (m + n)) m
下面的代碼正確地進行比較的數字和計算它們的差值:
comparehelper : {m : Nat} {n : Nat} -> (Compare m n) -> Compare (suc m) (suc n)
comparehelper LT = LT
comparehelper EQ = EQ
comparehelper GT = GT
compare : (m : Nat) (n : Nat) -> Compare m n
compare 0 0 = EQ
compare (suc _) 0 = GT
compare 0 (suc _) = LT
compare (suc m) (suc n) = comparehelper (compare m n)
differencehelper : {m : Nat} {n : Nat} -> (Compare m n) -> Nat
differencehelper (LT {n = difflt1}) = suc difflt1
differencehelper EQ = 0
differencehelper (GT {n = difflt1}) = suc difflt1
difference : Nat -> Nat -> Nat
difference m n = differencehelper (compare m n)
但以下顯然功能相同的代碼沒有( 模式匹配失敗類型檢查):
compare' : (m : Nat) (n : Nat) -> Compare m n
compare' 0 0 = EQ
compare' (suc _) 0 = GT
compare' 0 (suc _) = LT
compare' (suc m) (suc n) with compare' m n
... | LT = LT
... | EQ = EQ
... | GT = GT
difference' : Nat -> Nat -> Nat
difference' m n with compare' m n
... | LT {n = difflt1} = suc difflt1
... | EQ = 0
... | GT {n = difflt1} = suc difflt1
行爲差異的解釋是什麼?正如問題所述,我正在運行Agda 2.5.1.2。
您的示例類型檢查當前的開發版本(Agda 2.5.2-017c22e)。 – asr