Implementing isLast with Idris

☆樱花仙子☆ 提交于 2019-12-10 17:27:27

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!