| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Explaining how to use the inspect idiom and elaborating on the way |
| 5 |
-- it is implemented in the standard library. |
| 6 |
------------------------------------------------------------------------ |
| 7 |
|
| 8 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 9 |
|
| 10 |
module README.Inspect where |
| 11 |
|
| 12 |
open import Data.Nat.Base |
| 13 |
open import Data.Nat.Properties |
| 14 |
open import Data.Product.Base using (_×_; _,_) |
| 15 |
open import Relation.Binary.PropositionalEquality.Core using (_≡_; refl) |
| 16 |
open import Relation.Binary.PropositionalEquality using (inspect; [_]) |
| 17 |
|
| 18 |
------------------------------------------------------------------------ |
| 19 |
-- Using inspect |
| 20 |
|
| 21 |
-- We start with the definition of a (silly) predicate: `Plus m n p` states |
| 22 |
-- that `m + n` is equal to `p` in a rather convoluted way. Crucially, it |
| 23 |
-- distinguishes two cases: whether `p` is 0 or not. |
| 24 |
|
| 25 |
Plus-eq : (m n p : ℕ) → Set |
| 26 |
Plus-eq m n zero = m ≡ 0 × n ≡ 0 |
| 27 |
Plus-eq m n p@(suc _) = m + n ≡ p |
| 28 |
|
| 29 |
-- A sensible lemma to prove of this predicate is that whenever `p` is literally |
| 30 |
-- `m + n` then `Plus m n p` holds. That is to say `∀ m n → Plus m n (m + n)`. |
| 31 |
-- To be able to prove `Plus-eq m n (m + n)`, we need `m + n` to have either |
| 32 |
-- the shape `zero` or `suc _` so that `Plus-eq` may reduce. |
| 33 |
|
| 34 |
-- We could follow the way `_+_` computes by mimicking the same splitting |
| 35 |
-- strategy, thus forcing `m + n` to reduce: |
| 36 |
|
| 37 |
plus-eq-+ : ∀ m n → Plus-eq m n (m + n) |
| 38 |
plus-eq-+ zero zero = refl , refl |
| 39 |
plus-eq-+ zero (suc n) = refl |
| 40 |
plus-eq-+ (suc m) n = refl |
| 41 |
|
| 42 |
-- Or we could attempt to compute `m + n` first and check whether the result |
| 43 |
-- is `zero` or `suc p`. By using `with m + n` and naming the result `p`, |
| 44 |
-- the goal will become `Plus-eq m n p`. We can further refine this definition |
| 45 |
-- by distinguishing two cases like so: |
| 46 |
|
| 47 |
-- plus-eq-with : ∀ m n → Plus-eq m n (m + n) |
| 48 |
-- plus-eq-with m n with m + n |
| 49 |
-- ... | zero = {!!} |
| 50 |
-- ... | suc p = {!!} |
| 51 |
|
| 52 |
-- The problem however is that we have abolutely lost the connection between the |
| 53 |
-- computation `m + n` and its result `p`. Which makes the two goals unprovable: |
| 54 |
|
| 55 |
-- 1. `m ≡ 0 × n ≡ 0`, with no assumption whatsoever |
| 56 |
-- 2. `m + n ≡ suc p`, with no assumption either |
| 57 |
|
| 58 |
-- By using the `with` construct, we have generated an auxiliary function that |
| 59 |
-- looks like this: |
| 60 |
-- `plus-eq-with-aux : ∀ m n p → Plus-eq m n p` |
| 61 |
-- when we would have wanted a more precise type of the form: |
| 62 |
-- `plus-eq-aux : ∀ m n p → m + n ≡ p → Plus-eq m n p`. |
| 63 |
|
| 64 |
-- This is where we can use `inspect`. By using `with f x | inspect f x`, |
| 65 |
-- we get both a `y` which is the result of `f x` and a proof that `f x ≡ y`. |
| 66 |
-- Splitting on the result of `m + n`, we get two cases: |
| 67 |
|
| 68 |
-- 1. `m ≡ 0 × n ≡ 0` under the assumption that `m + n ≡ zero` |
| 69 |
-- 2. `m + n ≡ suc p` under the assumption that `m + n ≡ suc p` |
| 70 |
|
| 71 |
-- The first one can be discharged using lemmas from Data.Nat.Properties and |
| 72 |
-- the second one is trivial. |
| 73 |
|
| 74 |
plus-eq-with : ∀ m n → Plus-eq m n (m + n) |
| 75 |
plus-eq-with m n with m + n | inspect (m +_) n |
| 76 |
... | zero | [ m+n≡0 ] = m+n≡0⇒m≡0 m m+n≡0 , m+n≡0⇒n≡0 m m+n≡0 |
| 77 |
... | suc p | [ m+n≡1+p ] = m+n≡1+p |
| 78 |
|
| 79 |
|
| 80 |
------------------------------------------------------------------------ |
| 81 |
-- Understanding the implementation of inspect |
| 82 |
|
| 83 |
-- So why is it that we have to go through the record type `Reveal_·_is_` |
| 84 |
-- and the ̀inspect` function? The fact is: we don't have to if we write |
| 85 |
-- our own auxiliary lemma: |
| 86 |
|
| 87 |
plus-eq-aux : ∀ m n → Plus-eq m n (m + n) |
| 88 |
plus-eq-aux m n = aux m n (m + n) refl where |
| 89 |
|
| 90 |
aux : ∀ m n p → m + n ≡ p → Plus-eq m n p |
| 91 |
aux m n zero m+n≡0 = m+n≡0⇒m≡0 m m+n≡0 , m+n≡0⇒n≡0 m m+n≡0 |
| 92 |
aux m n (suc p) m+n≡1+p = m+n≡1+p |
| 93 |
|
| 94 |
-- The problem is that when we write ̀with f x | pr`, `with` decides to call `y` |
| 95 |
-- the result `f x` and to replace *all* of the occurences of `f x` in the type |
| 96 |
-- of `pr` with `y`. That is to say that if we were to write: |
| 97 |
|
| 98 |
-- plus-eq-naïve : ∀ m n → Plus-eq m n (m + n) |
| 99 |
-- plus-eq-naïve m n with m + n | refl {x = m + n} |
| 100 |
-- ... | p | eq = {!!} |
| 101 |
|
| 102 |
-- then `with` would abstract `m + n` as `p` on *both* sides of the equality |
| 103 |
-- proven by `refl` thus giving us the following goal with an extra, useless, |
| 104 |
-- assumption: |
| 105 |
|
| 106 |
-- 1. `Plus-eq m n p` under the assumption that `p ≡ p` |
| 107 |
|
| 108 |
-- So how does `inspect` work? The standard library uses a more general version |
| 109 |
-- of the following type and function: |
| 110 |
|
| 111 |
record MyReveal_·_is_ (f : ℕ → ℕ) (x y : ℕ) : Set where |
| 112 |
constructor [_] |
| 113 |
field eq : f x ≡ y |
| 114 |
|
| 115 |
my-inspect : ∀ f n → MyReveal f · n is (f n) |
| 116 |
my-inspect f n = [ refl ] |
| 117 |
|
| 118 |
-- Given that `inspect` has the type `∀ f n → Reveal f · n is (f n)`, when we |
| 119 |
-- write `with f n | inspect f n`, the only `f n` that can be abstracted in the |
| 120 |
-- type of `inspect f n` is the third argument to `Reveal_·_is_`. |
| 121 |
|
| 122 |
-- That is to say that the auxiliary definition generated looks like this: |
| 123 |
|
| 124 |
plus-eq-reveal : ∀ m n → Plus-eq m n (m + n) |
| 125 |
plus-eq-reveal m n = aux m n (m + n) (my-inspect (m +_) n) where |
| 126 |
|
| 127 |
aux : ∀ m n p → MyReveal (m +_) · n is p → Plus-eq m n p |
| 128 |
aux m n zero [ m+n≡0 ] = m+n≡0⇒m≡0 m m+n≡0 , m+n≡0⇒n≡0 m m+n≡0 |
| 129 |
aux m n (suc p) [ m+n≡1+p ] = m+n≡1+p |
| 130 |
|
| 131 |
-- At the cost of having to unwrap the constructor `[_]` around the equality |
| 132 |
-- we care about, we can keep relying on `with` and avoid having to roll out |
| 133 |
-- handwritten auxiliary definitions. |