Elaborating dependent (co)pattern matching Andreas Abel Jesper Cockx Chalmers & Gothenburg University 23 March 2018
Type systems & proof assistants: science or faith? surface core language language 1 / 38
Type systems & proof assistants: science or faith? surface ⇒ core elaboration = = = = = = language language 1 / 38
Type systems & proof assistants: science or faith? science ↓ surface ⇒ core elaboration = = = = = = language language 1 / 38
Type systems & proof assistants: science or faith? faith science ↓ ↓ surface ⇒ core elaboration = = = = = = language language 1 / 38
Type systems & proof assistants: science or faith? faith science ↓ ↓ surface ⇒ core elaboration = = = = = = language language Goal: turn piece of faith into science. 1 / 38
Presenting. . . A core language with inductive data types, coinductive record types, an identity type, and typed case trees. 2 / 38
Presenting. . . A core language with inductive data types, coinductive record types, an identity type, and typed case trees. An elaboration algorithm from copattern matching to a well-typed case tree. 2 / 38
Presenting. . . A core language with inductive data types, coinductive record types, an identity type, and typed case trees. An elaboration algorithm from copattern matching to a well-typed case tree. A proof that elaboration preserves the first-match semantics of the clauses. 2 / 38
Dependent copattern matching Surface and core languages From clauses to a case tree Preservation of first-match semantics
Example: maximum max : N → N → N max zero = y y max x zero = x max (suc x ) (suc y ) = suc (max x y ) 3 / 38
Example: maximum max : N → N → N max zero = y y max x zero = x max (suc x ) (suc y ) = suc (max x y ) First-match semantics : We don’t have max x zero = x , but only max (suc x ) zero = suc x . 3 / 38
Example: conatural numbers record N ∞ : Set where iszero : B : iszero ≡ B false → N ∞ pred 4 / 38
Example: conatural numbers record N ∞ : Set where iszero : B : iszero ≡ B false → N ∞ pred suc : N ∞ → N ∞ zero : N ∞ zero . iszero = true suc n . iszero = false ∅ zero . pred suc n . pred = n inf : N ∞ inf . iszero = false inf . pred = inf 4 / 38
Example: C Streams record S : Set where head : N tail : ( m : N ) → head ≡ N suc m → S 5 / 38
Example: C Streams record S : Set where head : N tail : ( m : N ) → head ≡ N suc m → S timer : N → S timer n . head = n timer zero . tail m ∅ timer (suc m ) . tail m refl = timer m 5 / 38
Example based on #2896 data D : N → Set where c : ( n : N ) → D n foo : ( m : N ) → D (suc m ) → N foo m (c (suc n )) = m + n 6 / 38
Example based on #2896 data D : N → Set where c : ( n : N ) → D n foo : ( m : N ) → D (suc m ) → N foo m (c (suc n )) = m + n What does this even mean??? 6 / 38
Dependent copattern matching Surface and core languages From clauses to a case tree Preservation of first-match semantics
Term syntax (surface and core) A , B , u , v ::= ( x : A ) → B | Set ℓ | D ¯ u | R ¯ u | u ≡ A v | x ¯ e | f ¯ e | c ¯ u | refl ::= u | .π e ::= ϵ | ( x : A )∆ ∆ 7 / 38
Surface language decl ::= data D ∆ : Set ℓ where c ∆ | record self : R ∆ : Set ℓ where π : A | definition f : A where cls ::= ¯ q ֒ → u | ¯ q ֒ → impossible cls ::= p | .π q ::= x | c ¯ p | refl | ⌊ u ⌋ | ∅ p 8 / 38
Core language: typing rules ⊢ Γ Γ ⊢ A : Set ℓ Γ( x : A ) ⊢ B : Set ℓ ′ Γ ⊢ Set ℓ : Set ℓ +1 Γ ⊢ ( x : A ) → B : Set max( ℓ,ℓ ′ ) D : Set ℓ ∈ Σ R : Set ℓ ∈ Σ Γ ⊢ A : Set ℓ Γ ⊢ u : A Γ ⊢ v : A Γ ⊢ D : Set ℓ Γ ⊢ R : Set ℓ Γ ⊢ u ≡ A v : Set ℓ x : A ∈ Γ Γ | x : A ⊢ ¯ e : C f : A ∈ Σ Γ | f : A ⊢ ¯ e : C Γ ⊢ x ¯ e : C Γ ⊢ f ¯ e : C c ∆ c : D ∈ Σ Γ ⊢ ¯ v : ∆ c Γ ⊢ A Γ ⊢ u : A Γ ⊢ c ¯ v : D Γ ⊢ refl : u ≡ A u Γ ⊢ v : A Γ | u v : B [ v / x ] ⊢ ¯ e : C Γ | u : ( x : A ) → B ⊢ v ¯ e : C self : R ⊢ .π : A ∈ Σ Γ | u .π : A [ u / self ] ⊢ ¯ e : C Γ | u : R ⊢ .π ¯ e : C Γ | u : A ′ ⊢ ¯ Γ ⊢ u : A Γ ⊢ A = B Γ ⊢ A = A ′ e : C Γ ⊢ u : B Γ | u : A ⊢ ¯ e : C 9 / 38
Core language: case trees Q ::= u | λ x . Q | record { π 1 �→ Q 1 ; . . . ; π n �→ Q n } case x { c 1 ˆ ∆ 1 �→ Q 1 ; . . . ; c n ˆ | ∆ n �→ Q n } case x { refl �→ τ Q } | 10 / 38
Case tree typing Γ | f ¯ q : A ⊢ Q “The case tree Q gives a well-typed implementation of f applied to copatterns ¯ q ” 11 / 38
Case tree typing: v Γ ⊢ v : C Γ | f ¯ q : C ⊢ v Side effect: Σ := Σ , (Γ ⊢ f ¯ → v : C ) q ֒ 12 / 38
Case tree typing: λ x . Q Γ( x : A ) | f ¯ q x : B ⊢ Q Γ | f ¯ q : ( x : A ) → B ⊢ λ x . Q 13 / 38
Case tree typing: record { . . . } record self : R : Set ℓ where π i : A i ∈ Σ (Γ | f ¯ q .π i : A i [f ⌈ ¯ q ⌉ / self ] ⊢ Q i ) i =1 ... n Γ | f ¯ q : R ⊢ record { π 1 �→ Q 1 ; . . . ; π n �→ Q n } 14 / 38
Case tree typing: case x { . . . } D : Set ℓ where c i ∆ i ∈ Σ ρ i = [c i ˆ ∆ i / x ] Γ 1 ∆ i (Γ 2 ρ i ) | f ¯ q ρ i : C ρ i ⊢ Q i i =1 ... n Γ 1 ( x : D)Γ 2 | f ¯ q : C ⊢ case x { c 1 ˆ ∆ 1 �→ Q 1 ; . . . ; c n ˆ ∆ n �→ Q n } 15 / 38
Case tree typing: case x { refl �→ τ Q } Γ 1 ⊢ u = ? v : B ⇒ yes (Γ ′ 1 , ρ, τ ) Γ ′ 1 (Γ 2 ρ ) | f ¯ q ρ : C ρ ⊢ Q q : C ⊢ case x { refl �→ τ Q } Γ 1 ( x : u ≡ B v )Γ 2 | f ¯ Γ ′ 1 ⊢ u ρ = v ρ : A ρ Γ ′ 1 ⊢ τ ; ρ = 1 : Γ ′ 1 16 / 38
Case tree typing: case x {} Γ 1 ⊢ u = ? v : B ⇒ no Γ 1 ( x : u ≡ B v )Γ 2 | f ¯ q : C ⊢ case x {} 17 / 38
Dependent copattern matching Surface and core languages From clauses to a case tree Preservation of first-match semantics
From clauses to a case tree The clauses guide us in the construction of a well-typed case tree: 18 / 38
From clauses to a case tree The clauses guide us in the construction of a well-typed case tree: as we construct the case tree, we deconstruct the clauses. 18 / 38
From clauses to a case tree The clauses guide us in the construction of a well-typed case tree: as we construct the case tree, we deconstruct the clauses. Γ | f ¯ q : A ⊢ P � Q 18 / 38
From clauses to a case tree The clauses guide us in the construction of a well-typed case tree: as we construct the case tree, we deconstruct the clauses. Γ | f ¯ q : A ⊢ P � Q entails Γ | f ¯ q : A ⊢ Q 18 / 38
From clauses to a case tree The clauses guide us in the construction of a well-typed case tree: as we construct the case tree, we deconstruct the clauses. Γ | f ¯ q : A ⊢ P � Q entails Γ | f ¯ q : A ⊢ Q [ w ik / ? p ik ] ¯ { } → rhs i P = q i ֒ i =1 ... n 18 / 38
max : N → N → N zero ֒ → j j zero ֒ → i i → suc (max k l ) (suc k ) (suc l ) ֒ 19 / 38
( m : N ) | max m : N → N [ m / ? zero] ֒ → j j [ m / ? i ] zero ֒ → i [ m / ? suc k ] (suc l ) ֒ → suc (max k l ) 19 / 38
max zero : N → N [zero / ? zero] ֒ → j j [zero / ? i ] zero ֒ → i [zero / ? suc k ] (suc l ) ֒ → suc (max k l ) ( p : N ) | max (suc p ) : N → N [suc p / ? zero] → j j ֒ [suc p / ? i ] → i zero ֒ [suc p / ? suc k ] (suc l ) ֒ → suc (max k l ) 19 / 38
max zero : N → N ֒ → j j [zero / ? i ] zero ֒ → i ( p : N ) | max (suc p ) : N → N [suc p / ? i ] zero → i ֒ [ p / ? k ] → suc (max k l ) (suc l ) ֒ 19 / 38
( n : N ) | max zero n : N [ n / ? j ] ֒ → j [zero / ? i , n / ? zero] ֒ → i ( p : N )( n : N ) | max (suc p ) n : N [suc p / ? i , n / ? zero] ֒ → i [ p / ? k , n / ? suc l ] → suc (max k l ) ֒ 19 / 38
( n : N ) | max zero n ֒ → n : N ( p : N )( n : N ) | max (suc p ) n : N [suc p / ? i , n / ? zero] ֒ → i [ p / ? k , n / ? suc l ] ֒ → suc (max k l ) 19 / 38
( n : N ) | max zero n ֒ → n : N ( p : N ) | max (suc p ) zero : N [suc p / ? i ] ֒ → i ( p : N )( q : N ) | max (suc p ) (suc q ) : N [ p / ? k , q / ? l ] ֒ → suc (max k l ) 19 / 38
( n : N ) | max zero n ֒ → n : N ( p : N ) | max (suc p ) zero ֒ → suc p : N ( p : N )( q : N ) | max (suc p ) (suc q ) : N [ p / ? k , q / ? l ] ֒ → suc (max k l ) 19 / 38
( n : N ) | max zero n ֒ → n : N ( p : N ) | max (suc p ) zero ֒ → suc p : N ( p : N )( q : N ) | max (suc p ) (suc q ) → suc (max p q ) : N ֒ 19 / 38
Case tree for max zero �→ λ n . n suc p �→ λ m . case m zero �→ suc p λ n . case n suc q �→ suc (max p q ) 20 / 38
zero : N ∞ . iszero ֒ → true . pred ∅ ֒ → impossible 21 / 38
Recommend
More recommend