I'm trying to prove the (first part of the) subtyping lemma from Types and Programming Languages. Here's what I have so far:
data Type : Set where
_=>_ : Type → Type → Type
Top : Type
data _<:_ : Type → Type → Set where
s-refl : {S : Type} → S <: S
s-trans : {S T U : Type} → S <: U → U <: T → S <: T
s-top : {S : Type} → S <: Top
s-arrow : {S₁ S₂ T₁ T₂ : Type} → T₁ <: S₁ → S₂ <: T₂ → S₁ => S₂ <: T₁ => T₂
lemma-inversion₁ : {S T₁ T₂ : Type}
→ S <: T₁ => T₂
→ ∃[ (S₁ × S₂) ∈ (Type & Type) ] ((S ≡ (S₁ => S₂)) & (T₁ <: S₁) & (S₂ <: T₂))
lemma-inversion₁ (s-refl {T₁ => T₂}) = (T₁ × T₂) , (refl × s-refl × s-refl)
lemma-inversion₁ (s-arrow {S₁} {S₂} T₁<:S₁ S₂<∶T₂) = (S₁ × S₂) , (refl × T₁<:S₁ × S₂<∶T₂)
lemma-inversion₁ (s-trans {S} S<:U U<:T₁=>T₂) with lemma-inversion₁ U<:T₁=>T₂
... | (U₁ × U₂) , (U≡U₁=>U₂ × T₁<:U₁ × U₂<:T₂) with lemma-inversion₁ (subst (S <:_) U≡U₁=>U₂ S<:U)
... | (S₁ × S₂) , (S≡S₁=>S₂ × U₂<:S₁ × S₂<:U₂) = (S₁ × S₂) , (S≡S₁=>S₂ × s-trans T₁<:U₁ U₂<:S₁ × s-trans S₂<:U₂ U₂<:T₂)
This looks correct to me, but I get:
Termination checking failed for the following functions:
lemma-inversion₁
Problematic calls:
lemma-inversion₁ (s-trans S<:U U<:T₁=>T₂)
| lemma-inversion₁ U<:T₁=>T₂
lemma-inversion₁ U<:T₁=>T₂
lemma-inversion₁ (subst (_<:_ S) U≡U₁=>U₂ S<:U)
Looks like Agda can't infer termination because of the subst
? Is that right? Is there a workaround?
Turns out I needed to pattern-match on refl
, like this:
... | (U₁ × U₂) , (refl × T₁<:U₁ × U₂<:T₂) with lemma-inversion₁ S<:U
This lets Agda infer that U = U₁ => U₂
and the termination checker is happy.