2017-04-04 24 views
1

考慮:理解`decEq`

*section3> :module Data.Vect  
*section3> :let e = the (Vect 0 Int) [] 
*section3> :let xs = the (Vect _ _) [1,2] 

*section3> decEq xs e 
(input):1:7:When checking argument x2 to function Decidable.Equality.decEq: 
     Type mismatch between 
       Vect 0 Int (Type of e) 
     and 
       Vect 2 Integer (Expected type) 

     Specifically: 
       Type mismatch between 
         0 
       and 
         2 

爲什麼一定要Nat參數彼此相當的DecEq?

注 - 張貼在https://groups.google.com/forum/#!topic/idris-lang/qgtImCLka3I原本

+3

'的Vect 0 Int'和'的Vect 1 Int':

您可以先檢查它們的長度,然後委託給同質版本寫相同的元素類型的Vect ORS自己的異質平等決勝局與「Vect n Int」和「Vect n Float」是不同的類型。 – gallais

回答

4

decEq同質命題平等:

||| Decision procedures for propositional equality 
interface DecEq t where 
    ||| Decide whether two elements of `t` are propositionally equal 
    total decEq : (x1 : t) -> (x2 : t) -> Dec (x1 = x2) 

正如你所看到的,x1x2都是t類型。在你的情況下,你有x1 : Vect 2 Integerx2 : Vect 0 Int。這是兩種不同的類型。

import Data.Vect 

vectLength : {xs : Vect n a} -> {ys : Vect m a} -> xs = ys -> n = m 
vectLength {n = n} {m = n} Refl = Refl 

decEqVect : (DecEq a) => (xs : Vect n a) -> (ys : Vect m a) -> Dec (xs = ys) 
decEqVect {n = n} {m = m} xs ys with (decEq n m) 
decEqVect xs ys | Yes Refl = decEq xs ys 
decEqVect xs ys | No notEq = No (notEq . vectLength)