Suppose I have a function (it really does what the name says):
filter : ∀ {A n} → (A → Bool) → Vec A n → ∃ (λ m → Vec A m)
Now, I’d like to somehow work with the dependent pair I return. I wrote simple head function:
head :: ∀ {A} → ∃ (λ n → Vec A n) → Maybe A
head (zero , _ ) = nothing
head (succ _ , (x :: _)) = just x
which of course works perfectly. But it made me wonder: is there any way I can make sure, that the function may only be called with n ≥ 1?
Ideally, I’d like to make function head : ∀ {A} → ∃ (λ n → Vec A n) → IsTrue (n ≥ succ zero) → A; but that fails, because n is out of scope when I use it in IsTrue.
Thanks for your time!
IsTrue is something like:
data IsTrue : Bool → Set where
check : IsTrue true
I think this is a question about uncurrying. The standard library provides uncurrying
functions for products, see uncurry.
For your situation, it would be more beneficial to have a uncurry function where the first
argument is hidden, since a head function would normally take the length index as an implicit argument.
We can write an uncurry function like that:
The function that returns the head of a vector if there is one does not seem to exist in the standard library,
so we write one:
Now your desired function is just a matter of uncurrying the
maybe-head function with the first-argument-implicit-uncurrying
function defined above:
Conclusion: dependent products gladly curry and uncurry like their non-dependent versions.
Uncurrying aside, the function you want to write with type signature
Can be written as: