0

I noticed that the signature for Vec.filter is

filter : ∀ {n} → Vec A n → Vec≤ A n

In order to go back to a Vec can I only use

padRight : ∀ {n} → A → Vec≤ A n → Vec A n

or

padLeft : ∀ {n} → A → Vec≤ A n → Vec A n

?

Ideally I would like to get back a Vec that has its length equal to the initial length of the Vec minus the number of elements that have been filtered out (instead of padding the spots where the elements have been filtered out).

Thanks!

fsuna064
  • 195
  • 1
  • 7

1 Answers1

1

If you look at the definition of Vec≤, you will see that it is already exactly what you are asking for:

record Vec≤ (A : Set a) (n : ℕ) : Set a where
  constructor _,_
  field {length} : ℕ
        vec      : Vec A length
        .bound   : length ≤ n

So vec of a Vec≤ A n is a "normal" Vec A length, for some length less than n.

Cactus
  • 27,075
  • 9
  • 69
  • 149
  • Thanks! However, if I apply `filter` then I don't know what the length of the new `Vec` will be. I don't think the compiler will be happy with that. Does that mean I would need to stay in `Vec≤` or is there still a way to downcast back to a `Vec` having the new length? – fsuna064 Nov 05 '20 at 14:09
  • for example: `DeleteNat : ∀ {n m} → (Vec ℕ n) → ℕ → (Vec ℕ m) DeleteNat nats nat = Vec≤.vec (v.filter (λ e → e ≟ nat) nats)` gives the error `Vec≤.length (v.filter (λ e → e ≟ nat) nats) != m of type ℕ` – fsuna064 Nov 05 '20 at 14:19