I've seen an example of the inspect function in my last question Using the value of a computed function for a proof in agda , but I'm still having trouble wrapping my head around that.
Here's a simple example:
Given the function crazy
,
crazy : ℕ -> ℕ
crazy 0 = 10
crazy 1 = 0
crazy 2 = 0
crazy 3 = 1
crazy 4 = 0
crazy xxx = xxx
I want to create a safe
function such that safe : {nn : ℕ} -> (id nn) ≢ 0 -> Fin (id nn)
. In other words it will return one number mod crazy, if you give it a proof crazy is 0. (I know that the example is a little contrived and I would probably be better off using the suc
in the function signature)
My first solution is
safebad : {nn : ℕ} -> (crazy nn) ≢ 0 -> Fin (crazy nn)
safebad {1} hh with hh refl
... | ()
safebad {2} hh with hh refl
... | ()
safebad {4} hh with hh refl
... | ()
safebad {0} hh = # 0
safebad {3} hh = # 0
safebad {suc (suc (suc (suc (suc _))))} _ = # 0
But this is long and messy. So I tried to emulate the example in Using the value of a computed function for a proof in agda but could only get so far
safegood : (nn : ℕ) -> (crazy nn) ≢ 0 -> Fin (crazy nn)
safegood nn nez with crazy nn | inspect crazy nn
... | 0 | [ proof ] = ⊥-elim ???
... | _ | _ = # 0
inspect uses Hidden to hide a record of the function application in the type signature, I think. This can then be retrieved with reveal.
This is what I think I understand:
Reveal_is_
seems to hold the value of the hidden f
, and x
; and the result of x
applied to f
. [_]
will result in the proof of that equality.
⊥-elim
takes a proof of contradiction and returns a contradiction.
What do I put into ???
for this to work?