In Haskell we can do the following without getting a runtime error (ref):
mytake 0 _ = []
mytake _ [] = []
mytake n (x:xs) = x : mytake (n-1) xs
print( mytake 0 (undefined::[Int]) )
In Idris, we can do a similar definition, but the behavior is different:
mytake : Integer -> List a -> List a
mytake 0 _ = []
mytake _ [] = []
mytake n (x::xs) = x :: mytake (n-1) xs
printLn( mytake {a = Nat} 0 ?undefined )
In this case we get ABORT: Attempt to evaluate hole Main.undefined
. I know Idris is not a lazy language, but my impression was that pattern matching parameters might be separate from the logic for evaluation of data structures (which can be sidestepped in Idris, e.g. with Stream
).
In addition to understanding if there is a way around this, I'd also appreciate knowing why Idris behaves in this way.