elaborating dependent co pattern matching
play

Elaborating dependent (co)pattern matching Andreas Abel Jesper - PowerPoint PPT Presentation

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 &


  1. Elaborating dependent (co)pattern matching Andreas Abel Jesper Cockx Chalmers & Gothenburg University 23 March 2018

  2. Type systems & proof assistants: science or faith? surface core language language 1 / 38

  3. Type systems & proof assistants: science or faith? surface ⇒ core elaboration = = = = = = language language 1 / 38

  4. Type systems & proof assistants: science or faith? science ↓ surface ⇒ core elaboration = = = = = = language language 1 / 38

  5. Type systems & proof assistants: science or faith? faith science ↓ ↓ surface ⇒ core elaboration = = = = = = language language 1 / 38

  6. Type systems & proof assistants: science or faith? faith science ↓ ↓ surface ⇒ core elaboration = = = = = = language language Goal: turn piece of faith into science. 1 / 38

  7. Presenting. . . A core language with inductive data types, coinductive record types, an identity type, and typed case trees. 2 / 38

  8. 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

  9. 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

  10. Dependent copattern matching Surface and core languages From clauses to a case tree Preservation of first-match semantics

  11. 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

  12. 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

  13. Example: conatural numbers record N ∞ : Set where iszero : B : iszero ≡ B false → N ∞ pred 4 / 38

  14. 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

  15. Example: C Streams record S : Set where head : N tail : ( m : N ) → head ≡ N suc m → S 5 / 38

  16. 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

  17. 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

  18. 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

  19. Dependent copattern matching Surface and core languages From clauses to a case tree Preservation of first-match semantics

  20. 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

  21. 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

  22. 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

  23. 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

  24. Case tree typing Γ | f ¯ q : A ⊢ Q “The case tree Q gives a well-typed implementation of f applied to copatterns ¯ q ” 11 / 38

  25. Case tree typing: v Γ ⊢ v : C Γ | f ¯ q : C ⊢ v Side effect: Σ := Σ , (Γ ⊢ f ¯ → v : C ) q ֒ 12 / 38

  26. Case tree typing: λ x . Q Γ( x : A ) | f ¯ q x : B ⊢ Q Γ | f ¯ q : ( x : A ) → B ⊢ λ x . Q 13 / 38

  27. 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

  28. 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

  29. 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

  30. Case tree typing: case x {} Γ 1 ⊢ u = ? v : B ⇒ no Γ 1 ( x : u ≡ B v )Γ 2 | f ¯ q : C ⊢ case x {} 17 / 38

  31. Dependent copattern matching Surface and core languages From clauses to a case tree Preservation of first-match semantics

  32. From clauses to a case tree The clauses guide us in the construction of a well-typed case tree: 18 / 38

  33. 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

  34. 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

  35. 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

  36. 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

  37. max : N → N → N zero ֒ → j j zero ֒ → i i → suc (max k l ) (suc k ) (suc l ) ֒ 19 / 38

  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

  39. 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

  40. 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

  41. ( 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

  42. ( 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

  43. ( 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

  44. ( 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

  45. ( 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

  46. 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

  47. zero : N ∞ . iszero ֒ → true . pred ∅ ֒ → impossible 21 / 38

Recommend


More recommend