Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Congruence for heterogenous equality

I'm trying to use heterogenous equality to prove statements involving this indexed datatype:

data Counter : ℕ → Set where
  cut : (i j : ℕ) → Counter (suc i + j)

I was able to write my proofs using Relation.Binary.HeterogenousEquality.≅-Reasoning, but only by assuming the following congruence property:

Counter-cong : ∀ {n n′} {k : Counter n} {k′ : Counter n′} →
               {A : ℕ → Set} → (f : ∀{n} → Counter n → A n) →
               k ≅ k′ → f k ≅ f k′
Counter-cong f k≅k′ = {!!}

However, I can't pattern match on k≅k′ being refl without getting the following error message from the type checker:

Refuse to solve heterogeneous constraint 
    k : Counter n =?= k′ : Counter n′

and if I try to do a case analysis on k≅k′ (i.e. by using C-c C-c from the Emacs frontend) to make sure all the implicit arguments are properly matched with respect to their constraints imposed by the refl, I get

Cannot decide whether there should be a case for the constructor
refl, since the unification gets stuck on unifying the 
inferred indices 
    [{.Level.zero}, {Counter n}, k] 
with the expected indices 
    [{.Level.zero}, {Counter n′}, k′]

(if you're interested, here are some non-relevant background: Eliminating subst to prove equality)

like image 322
Cactus Avatar asked Feb 16 '12 11:02

Cactus


1 Answers

What you can do is take an additional proof that the two indices are equal:

Counter-cong : ∀ {n n′} {k : Counter n} {k′ : Counter n′} →
               {A : ℕ → Set} → (f : ∀{n} → Counter n → A n) →
               n ≅ n′ → k ≅ k′ → f k ≅ f k′
Counter-cong f refl refl = refl

The original problem is that knowing Counter n ≅ Counter n′ doesn't imply n ≡ n′ because type constructors are not assumed to be injective (there's a flag --injective-type-constructors for this, which in fact makes the match go through, but it's known to be inconsistent with excluded middle), so while it can conclude that the two types are equal it won't rewrite n to n′ and so you get that error when it later checks if k and k′ are unifiable.

Since Counter n has exactly n elements, it's actually possible to prove Counter is injective using something like the pigeonhole principle (and maybe decidable equality for naturals), so you could do without the n ≅ n′ argument, though that'd be messy.

Edit: AFAICT the Het. equality behavior is still the same.

like image 199
Saizan Avatar answered Nov 09 '22 22:11

Saizan