Agda: Simulate Coq's rewrite tactic

584 views Asked by At

I have some experience using Coq and am now in the process of learning Agda. I'm working on a correctness proof of insertion sort and have reached a point where I would like to perform something similar to Coq's rewrite tactic. Currently, I have:

open import Data.Nat
open import Relation.Binary.PropositionalEquality
open import Data.Sum

data list : Set where
  nil : list
  cons : ℕ -> list -> list

data sorted (n : ℕ) : list -> Set where
  nilSorted : sorted n nil
  consSorted : ∀ hd tl -> hd ≥ n -> sorted hd tl -> sorted n (cons hd tl)

leMin : ∀ x y -> x ≤ y -> (x ⊓ y) ≡ x
leMin zero zero p = refl
leMin zero (suc y) p = refl
leMin (suc x) zero ()
leMin (suc x) (suc y) (s≤s p) = cong suc (leMin x y p)

insert : ℕ → list → list
insert x l = {!!}

intDec : ∀ x y → x ≤ y ⊎ x > y
intDec x y = {!!}

insertCorrect : ∀ {n} -> ∀ x l -> sorted n l -> sorted (n ⊓ x) (insert x l)
insertCorrect {n} x nil p with intDec n x
insertCorrect {n} x nil p | inj₁ x₁ with (leMin n x x₁) 
... | c = {c }0

My proof context looks like:

Goal: sorted (n ⊓ x) (cons x nil)
————————————————————————————————————————————————————————————
p  : sorted n nil
c  : n ⊓ x ≡ n
x₁ : n ≤ x
x  : ℕ
n  : ℕ

I have tried breaking up the c in hopes of replacing the occurrences of (n ⊓ x) with n, however, I get the following error:

I'm not sure if there should be a case for the constructor refl,
because I get stuck when trying to solve the following unification
problems (inferred index ≟ expected index):
n₁ ⊓ x₂ ≟ n₁
when checking that the expression ? has type
sorted (n ⊓ x) (cons x nil)

Basically, I would like to be able to perform a rewrite so I can get to the following point:

Goal: sorted n (cons x nil)
————————————————————————————————————————————————————————————
p  : sorted n nil
x₁ : n ≤ x
x  : ℕ
n  : ℕ

Any ideas on how to proceed?

1

There are 1 answers

0
Cactus On BEST ANSWER

You can use the Agda keyword rewrite to apply a propositional equivalence on your goal:

insertCorrect : ∀ {n} -> ∀ x l -> sorted n l -> sorted (n ⊓ x) (insert x l)
insertCorrect {n} x nil p with intDec n x
insertCorrect {n} x nil p | inj₁ x₁ rewrite leMin n x x₁ = ?

Here, the goal in the ? is, as you hoped for:

Goal: sorted n (cons x nil)
————————————————————————————————————————————————————————————
p  : sorted n nil
x₁ : n ≤ x
x  : ℕ
n  : ℕ