0

I have the following function:

natListLast : List ℕ → ℕ
natListLast nats = v.last (v.fromList nats)

and I currently get this error:

l.foldr (λ _ → suc) 0 nats != suc _n_126 of type ℕ
when checking that the expression fromList nats has type
Vec ℕ (1 + _n_126)

I would like to know how I can call last on v.fromList nats when v.fromList returns Vec A (length xs). How can I tell the compiler that length xs is equal to 1 + _n_126?

Thanks!

I also tried doing:

natListLast : List ℕ → ℕ
natListLast nats = v.last {l.length nats} (v.fromList nats)

since last has this signature:

last : ∀ {n} → Vec A (1 + n) → A

I thought that I could pass the length of nats in as the implicit argument for last but I get this error:

ℕ !=< Level
when checking that the inferred type of an application
  ℕ
matches the expected type
  Level
fsuna064
  • 195
  • 1
  • 7

1 Answers1

2

How can I tell the compiler that length xs is equal to 1 + _n_126?

Think about this question for a second. How can you tell that length xs is equal to 1 + _n_126? What if xs = [] and thus length xs = 0? The answer is you can't, so don't expect Agda to do so either!

To fix the problem, you'll need to pattern match on xs to make sure it is non-empty:

natListLast : List ℕ → ℕ
natListLast nats@(x ∷ xs) = v.last (v.fromList nats)
natListLast []            = {!!}

I'll leave it up to you what to return in the second case.


For the second problem, you'll need to check the full type of last (using C-c C-d in Emacs):

{A.a : Agda.Primitive.Level} {A : Set A.a} {n : ℕ} →
v.Vec A (suc n) → A

As you can see, there are two hidden arguments that come before the length n (these have been inserted by automatic variable generalization). To pass the argument n directly without passing those previous two arguments, you can name the argument, for example last {n = l.length nats}.

Jesper
  • 2,781
  • 13
  • 14