Just getting started in Idris (it's Idris 2 if it matters), and stumble upon this problem. I'm trying to implement a function that returns a vector of all the Fibonacci numbers up to given n. Here's the definition so far (it doesn't compile):
fibs : (n : Nat) -> Vect (n + 1) Int
fibs Z = [0]
fibs (S Z) = [0, 1]
fibs (S k) =
case (fibs k) of
(x :: y :: xs) => (x + y) :: x :: y :: xs
The error I get is as follows:
Can't solve constraint between:
S (S ?len)
and
plus ?_ (fromInteger 1)
The error points to expression (x :: y :: xs)
.
I understand I need to prove to Idris the length of fibs k
will be at least 2, but I fail to understand how to do that, and why it's not obvious from existing definitions. For me it looks like the k
in fibs (S k)
must definitely be >= 1, because otherwise either fibs Z
or fibs (S Z)
would match. The length of fibs k
where k >= 1
must therefore be >= 2, by definition of fibs : (n : Nat) -> Vect (n + 1) Int
. What am I missing and how to express this in Idris?