150

# Inspective Equality @ Iowa

An intentional type theory that is incompatible with extensional equality, but allows propositional equality to be decided.

October 11, 2013

## Transcript

1. ### Deciding intensional equality of total-inductive functions Larry Diehl Portland State

University U. Iowa Mini-Symposium on Programming Languages October 2013 Monday, March 3, 14
2. ### Outline • Background • motivation • equality • Examples /

Quiz Time • when two functions are equal • Inspective Type Theory • the implementation • example neutral terms Monday, March 3, 14

4. ### Topic • Deciding intensional, or syntactic, equality • i.e. Martin-Löf’s

propositional equality, which internalizes deﬁnitional equality of the metalanguage into the object language Monday, March 3, 14
5. ### Restrictions • Total language • Terminating functions • Covered functions

• Inductive types • Intensional propositional equality • Syntactic equality of functions • Denies extensionality Monday, March 3, 14
6. ### Restrictions • Total language • Terminating functions • Covered functions

• Inductive types • Intensional propositional equality • Syntactic equality of functions •Denies extensionality Monday, March 3, 14
7. ### Motivation • Homogenous tactic and programming language • Allows tactics

to match on functions stuck in particular ways to perform rewrites by representing the context and goal using dependent functions and pairs Monday, March 3, 14
8. ### lemma : (n : ℕ) ! Fin (n + 0)

! Fin n _≟_ : {A : Set} (x y : A) ! Dec (x ≡ y) Context : Set Context = Σ Set (λ A ! A) tactic : Context ! Context tactic (Σ ℕ F , (n , i)) with F ≟ (λ n ! Fin (n + 0)) tactic (Σ ℕ .(λ m ! Fin (m + 0)) , n , i) | yes refl = (Σ ℕ Fin , (n , lemma n i)) tactic ((Σ ℕ F) , (n , i)) | no p = (Σ ℕ F , (n , i)) tactic (A , a) = (A , a) Monday, March 3, 14
9. ### Simpler Problem • Today we will consider a simply typed

language • Have already proven some meta-theory • Still a work in progress • I don’t see any major obstacles to modifying this to work with dependent types Monday, March 3, 14
10. ### Deﬁnitional Equality • Can be decided (used in type checking)

• Part of meta-theory • β-equality via syntactic comparison of canonical terms • False just means terms are not equal now, not that further case analysis may make them equal later Monday, March 3, 14
11. ### Propositional Equality • Internalizes deﬁnitional equality of the metalanguage into

the constructive [dependently typed] object language • x ≡ y • x ≢ y ≔ x ≡ y → ⊥ • Inconsistent to use deﬁnitional equality and simply lift it to propositional equality • danger lies in the unequal case Monday, March 3, 14
12. ### data Dec (P : Set) : Set where yes :

P ! Dec P no : (P ! ⊥) ! Dec P Monday, March 3, 14
13. ### primitive _==_ : {A : Set} ! A ! A

! Bool _≟_ : {A : Set} (x y : A) ! Dec (x ≡ y) x ≟ y with x == y ... | true = yes trustMe ... | false = no whatever where postulate whatever : _ Monday, March 3, 14
14. ### kt : Bool ! Bool kt b = if b

then true else true evil? : (b : Bool) ! Dec (true ≡ kt b) evil? b = true ≟ kt b Monday, March 3, 14
15. ### Binding • Type checking in a dependently typed language requires

β-reducing, or partially evaluating, terms • Deciding propositional equality must address when it is safe to compare neutral terms • If it is unsafe to compare neutral terms, the comparison operation itself must be a neutral term! Monday, March 3, 14
16. ### High-level Solution • Comparing a neutral term with another must

result in a neutral term • Comparing neutral bodies of two functions can reduce by comparing “neutral” terms stuck on the variable bound by the function • Comparing two functions can partially evaluate based on bound neutral terms, but get stuck when comparing some inner “free” neutral term Monday, March 3, 14
17. ### Free variables • Consider a variable “free” if it is

not bound at the point of the comparison of two functions! Monday, March 3, 14

19. ### (λ (n : ℕ) ! n) == (λ (n :

ℕ) ! n) Monday, March 3, 14
20. ### (λ (n : ℕ) ! n) == (λ (n :

ℕ) ! n) true Monday, March 3, 14
21. ### (λ b₁ ! (λ b₂ ! b₂) == (λ b₂

! if (if b₁ true b₂) b₂ true) ) true Monday, March 3, 14
22. ### (λ b₁ ! (λ b₂ ! b₂) == (λ b₂

! if (if b₁ true b₂) b₂ true) ) true true Monday, March 3, 14
23. ### (λ b₁ ! (λ b₂ ! if b₂ b₁ true)

== (λ b₂ ! if b₂ true b₁) ) false Monday, March 3, 14
24. ### (λ b₁ ! (λ b₂ ! if b₂ b₁ true)

== (λ b₂ ! if b₂ true b₁) ) false false Monday, March 3, 14
25. ### (λ b₁ ! (λ b₂ ! if b₂ b₁ true)

== (λ b₂ ! if b₂ true b₁) ) true Monday, March 3, 14
26. ### (λ b₁ ! (λ b₂ ! if b₂ b₁ true)

== (λ b₂ ! if b₂ true b₁) ) true true Monday, March 3, 14
27. ### (λ f ! f == (λ (n : ℕ) !

n) ) (λ (n : ℕ) ! n) Monday, March 3, 14
28. ### (λ f ! f == (λ (n : ℕ) !

n) ) (λ (n : ℕ) ! n) true Monday, March 3, 14
29. ### (λ (b : Bool) ! (λ n ! if b

zero n) == (λ n ! n) ) == (λ (b : Bool) ! (λ n ! if b zero n) == (λ n ! n) ) Monday, March 3, 14
30. ### (λ (b : Bool) ! (λ n ! if b

zero n) == (λ n ! n) ) == (λ (b : Bool) ! (λ n ! if b zero n) == (λ n ! n) ) true Monday, March 3, 14

32. ### data Expr (Γ : Context) : Type ! Set where

`tt : Expr Γ `⊤ `true `false : Expr Γ `Bool `zero : Expr Γ `ℕ `suc : Expr Γ `ℕ ! Expr Γ `ℕ `λ : ∀{A B} ! Expr (Γ , A % Sem) B ! Expr Γ (A `! B) `var : ∀{A} ! Var Sem Γ A ! Expr Γ A `if : ∀{C} ! Expr Γ `Bool ! Expr Γ C ! Expr Γ C ! Expr Γ C _`\$_ : ∀{A B} ! Expr Γ (A `! B) ! Expr Γ A ! Expr Γ B _`==_ : ∀{A} ! Expr Γ A ! Expr Γ A ! Expr Γ `Bool `fold : ∀{C} ! Expr Γ `ℕ ! Expr Γ C ! Expr Γ (C `! C) ! Expr Γ C ⟦_⟧ : ∀{Γ A} ! Expr Γ A ! Value Γ A ⟦ `tt ⟧ = `tt ⟦ `true ⟧ = `true ⟦ `false ⟧ = `false ⟦ `zero ⟧ = `zero ⟦ `suc n ⟧ = `suc ⟦ n ⟧ ⟦ `λ f ⟧ = `λ ⟦ f ⟧ ⟦ `var i ⟧ = `neutral _ (`var i) ⟦ `if b c₁ c₂ ⟧ = ⟦if⟧ ⟦ b ⟧ ⟦ c₁ ⟧ ⟦ c₂ ⟧ ⟦ f `\$ a ⟧ = ⟦ f ⟧ ⟦\$⟧ ⟦ a ⟧ ⟦ x₁ `== x₂ ⟧ = ⟦ x₁ ⟧ ⟦==⟧ ⟦ x₂ ⟧ ⟦ `fold n cz cs ⟧ = ⟦fold⟧ ⟦ n ⟧ ⟦ cz ⟧ ⟦ cs ⟧ Monday, March 3, 14
33. ### data Expr (Γ : Context) : Type ! Set where

_`==_ : ∀{A} ! Expr Γ A ! Expr Γ A ! Expr Γ `Bool ⟦_⟧ : ∀{Γ A} ! Expr Γ A ! Value Γ A ⟦ x₁ `== x₂ ⟧ = ⟦ x₁ ⟧ ⟦==⟧ ⟦ x₂ ⟧ Monday, March 3, 14
34. ### data Value (Γ : Context) : Type ! Set data

Neutral (M : Mode) (Γ : Context) : Type ! Set data Value Γ where `tt : Value Γ `⊤ `true `false : Value Γ `Bool `zero : Value Γ `ℕ `suc : Value Γ `ℕ ! Value Γ `ℕ `λ : ∀{A B} ! Value (Γ , A % Sem) B ! Value Γ (A `! B) `neutral : ∀{A} M ! Neutral M Γ A ! Value Γ A data Neutral M Γ where `var : ∀{A} ! Var M Γ A ! Neutral M Γ A `if : ∀{C} ! Neutral M Γ `Bool ! Value Γ C ! Value Γ C ! Neutral M Γ C `fold : ∀{C} ! Neutral M Γ `ℕ ! Value Γ C ! Value Γ (C `! C) ! Neutral M Γ C _`==_ : ∀{A} ! Neutral M Γ A ! Value Γ A ! Neutral M Γ `Bool `surmise : ∀{A} ! Neutral M (Γ , A % Syn) `Bool ! Neutral M Γ `Bool _`\$_ : ∀{A B} ! Neutral M Γ (A `! B) ! Value Γ A ! Neutral M Γ B Monday, March 3, 14
35. ### data Value (Γ : Context) : Type ! Set data

Neutral (M : Mode) (Γ : Context) : Type ! Set data Value Γ where `λ : ∀{A B} ! Value (Γ , A % Sem) B ! Value Γ (A `! B) `neutral : ∀{A} M ! Neutral M Γ A ! Value Γ A data Neutral M Γ where _`==_ : ∀{A} ! Neutral M Γ A ! Value Γ A ! Neutral M Γ `Bool `surmise : ∀{A} ! Neutral M (Γ , A % Syn) `Bool ! Neutral M Γ `Bool Monday, March 3, 14
36. ### _⟦==⟧_ : ∀{Γ A} ! Value Γ A ! Value

Γ A ! Value Γ `Bool _⟦==ˢ⟧_ : ∀{Γ A} ! Neutral Syn Γ A ! Neutral Syn Γ A ! Value Γ `Bool `λ f₁ ⟦==⟧ `λ f₂ = ⟦surmise⟧ (⟦quote⟧ f₁ ⟦==⟧ ⟦quote⟧ f₂) `neutral Syn x₁ ⟦==⟧ `neutral Syn x₂ = x₁ ⟦==ˢ⟧ x₂ `neutral Sem x₁ ⟦==⟧ x₂ = `neutral Sem (x₁ `== x₂) x₁ ⟦==⟧ `neutral Sem x₂ = `neutral Sem (x₂ `== x₁) ⟦quote⟧ : ∀{Γ A B} ! Value (Γ , A % Sem) B ! Value (Γ , A % Syn) B ⟦surmise⟧ : ∀{Γ A} ! Value (Γ , A % Syn) `Bool ! Value Γ `Bool ⟦surmise⟧ `true = `true ⟦surmise⟧ `false = `false ⟦surmise⟧ (`neutral M x) = `neutral M (`surmise x) Monday, March 3, 14
37. ### Larger Collection of Canonical Terms • Now includes syntactic neutral

terms • Due to presence of surmise, and used during partial evaluation • Comparing any two semantically closed terms reduces to a canonical term Monday, March 3, 14
38. ### standard-expr : Expr ∅ ((`Bool `! `Bool) `! `Bool) standard-expr

= `λ f ! ( f `== (`λ x ! x) ) Monday, March 3, 14
39. ### standard-val : Value ∅ ((`Bool `! `Bool) `! `Bool) standard-val

= `λ f ! ( `neutral Sem ( f `== (`λ x ! `neutral Sem x) ) ) Monday, March 3, 14
40. ### surm-expr : Expr ∅ (`Bool `! `Bool) surm-expr = `λ

x ! ( (`λ y ! x) `== (`λ y ! y) ) Monday, March 3, 14
41. ### surm-val : Value ∅ (`Bool `! `Bool) surm-val = `λ

x ! ( `neutral Sem ( `surmise y ! ( x `== `neutral Syn y ) ) ) Monday, March 3, 14

43. ### Proven Metatheory ⟦sound⟧ : ∀{Γ A} ! Closed Γ !

(x y : Value Γ A) ! x ⟦≡⟧ y ! x ≡ y ⟦complete⟧ : ∀{Γ A} ! Closed Γ ! (x y : Value Γ A) ! x ≡ y ! x ⟦≡⟧ y _⟦≡⟧_ : ∀{Γ A} (x y : Value Γ A) ! Set x ⟦≡⟧ y = (x ⟦==⟧ y) ≡ `true Monday, March 3, 14
44. ### Corollaries ⟦refl⟧ : ∀{Γ A} ! Closed Γ ! (x

: Value Γ A) ! x ⟦≡⟧ x ⟦sym⟧ : ∀{Γ A} ! Closed Γ ! (x y : Value Γ A) ! x ⟦≡⟧ y ! y ⟦≡⟧ x ⟦trans⟧ : ∀{Γ A} ! Closed Γ ! (x y z : Value Γ A) ! x ⟦≡⟧ y ! y ⟦≡⟧ z ! x ⟦≡⟧ z Monday, March 3, 14
45. ### Corollaries ⟦subst⟧ : ∀{Γ A} ! Closed Γ ! (P

: Value Γ A ! Set) ! (x y : Value Γ A) ! x ⟦≡⟧ y ! P x ! P y ⟦cong⟧ : ∀{Γ A B} ! Closed Γ ! (f : Value Γ A ! Value Γ B) ! (x y : Value Γ A) ! x ⟦≡⟧ y ! f x ⟦≡⟧ f y ⟦dec⟧ : ∀{Γ A} ! Closed Γ ! (x y : Value Γ A) ! Dec (x ⟦≡⟧ y) Monday, March 3, 14
46. ### Remaining Metatheory ⟦canon⟧ : ∀{A} M → Closed Γ →

Neutral M Γ A → ⊥ ⟦sound2⟧ : ∀{Γ A} → Closed Γ → (x y : Value Γ A) → x ⟦≢⟧ y → x ≢ y ⟦complete2⟧ : ∀{Γ A} → Closed Γ → (x y : Value Γ A) → x ≢ y → x ⟦≢⟧ y _⟦≢⟧_ : ∀{Γ A} (x y : Value Γ A) ! Set x ⟦≢⟧ y = (x ⟦==⟧ y) ≡ `false Monday, March 3, 14

48. ### data Neutral M Γ where `surmise : ∀{A B} {f

g : Value (Γ , A % Sem) B} ! Neutral M (Γ , A % Syn) (`Dec (quote f `≡ quote g)) ! Neutral M Γ (`Dec (`λ f `≡ `λ g)) Monday, March 3, 14