问题
Looking at exercise 9.2 from Type-Driven Development with Idris:
data Last : List a -> a -> Type where
LastOne : Last [value] value
LastCons : (prf : Last xs value) -> Last (x :: xs) value
Uninhabited (Last [] value) where
uninhabited LastOne impossible
uninhabited (LastCons _) impossible
notLast : Not (x = value) -> Last [x] value -> Void
notLast prf LastOne impossible
notLast prf (LastCons _) impossible
isLast : DecEq a => (xs : List a) -> (value : a) -> Dec (Last xs value)
isLast [] value = No absurd
isLast (x::[]) value = case decEq x value of
Yes Refl => Yes LastOne
No contra => No (notLast contra)
isLast (x::y::ys) value = ?rhs
I'm getting a compile-time error on the notLast prf LastOne
case:
*section1> :r
Type checking ./section1.idr
section1.idr:20:9:notLast prf LastOne is a valid case
Holes: Main.rhs, Main.notLast
Why does the compiler find it to be a valid case?
回答1:
Idris is not quite able to see that Not (value = value)
is isomorphic to Void
so you need to help it by explaining what the issue is like so:
notLast : Not (x = value) -> Last [x] value -> Void
notLast prf LastOne = absurd (prf Refl)
notLast prf (LastCons _) impossible
来源:https://stackoverflow.com/questions/44865052/implementing-islast-with-idris