A Universe of Binding and Computation
1
Dan Licata and Robert Harper
Carnegie Mellon University
1
A Universe of Binding and Computation Dan Licata and Robert Harper - - PowerPoint PPT Presentation
A Universe of Binding and Computation Dan Licata and Robert Harper Carnegie Mellon University 1 1 Goal Functional programming with binding and scope Applications: Domain-specific logics for reasoning about code Mechanized metatheory 2 2
1
1
2
Functional programming with binding and scope Domain-specific logics for reasoning about code Mechanized metatheory Applications:
2
2
Functional programming with binding and scope Domain-specific logics for reasoning about code Mechanized metatheory Applications: Two important ingredients...
2
3
Represent bound variables: lam(x.e) , ∀x:τ.A , hypothetical judgements
3
3
E.g. type exp representing syntax of λ-terms: app : exp ⇒ exp ⇒ exp lam : (exp ⇒ exp) ⇒ exp Represent bound variables: lam(x.e) , ∀x:τ.A , hypothetical judgements
3
3
E.g. type exp representing syntax of λ-terms: app : exp ⇒ exp ⇒ exp lam : (exp ⇒ exp) ⇒ exp Represent bound variables: lam(x.e) , ∀x:τ.A , hypothetical judgements
weak function space representing binding: means “an exp in the presence of a new exp”
3
4
normalize : exp ⊃ exp normalize (lam x.e) = ... normalize (app e1 e2) = …
pattern-matching recursive function
4
5
5
5
⇒ (binding) and ⊃ (computation) [unlike Parametric & Weak HOAS / Hybrid ]
5
5
⇒ (binding) and ⊃ (computation) [unlike Parametric & Weak HOAS / Hybrid ]
[unlike Twelf/Delphin/Beluga]
5
5
⇒ (binding) and ⊃ (computation) [unlike Parametric & Weak HOAS / Hybrid ]
[unlike Twelf/Delphin/Beluga]
[unlike nominal logic / FreshML / cαml]
5
6
Treat variables as pronouns, not nouns: reference to a designated binding site y : exp , z : exp ⊢ lam (x.app(z , x)) : exp
6
6
Treat variables as pronouns, not nouns: reference to a designated binding site y : exp , z : exp ⊢ lam (x.app(z , x)) : exp
Can test equality of variables as pointers, not as names
6
7
Contextual types <Ψ> A track scoping of variables: E.g. < x1 : exp … xn : exp > exp
free vars in x1 … xn
7
7
Contextual types <Ψ> A track scoping of variables: E.g. < x1 : exp … xn : exp > exp normalize : <⋅>exp ⊃ <⋅>exp normalize/open : ∀ Ψ. <Ψ>exp ⊃ <Ψ>exp Permit precise types for computations:
free vars in x1 … xn
7
8
Implement a framework as a universe in Agda Examples and comparisons with Twelf/Delphin/Beluga/FreshML Datatype-generic implementations of the structural properties
8
9
Implement framework as a universe in Agda Examples and comparisons with Twelf/Delphin/Beluga/FreshML Datatype-generic implementations of the structural properties
9
10
Datatype of codes naming a user-defined collection
data Code where … Interpretation function maps codes to Agda Sets: Elements : Code → Set Ingredients:
10
10
Datatype of codes naming a user-defined collection
data Code where … Interpretation function maps codes to Agda Sets: Elements : Code → Set Ingredients: This work: universe of contextual types
10
11
data Ctx Ψ ::= ⋅ | Ψ , D data Code A ::= A list | A ⊃ B | D | Ψ ⇒ A | ∀ Ψ. A Datatype of codes for contextual types: Interpretation <Ψ>A: <_>_ : Ctx → Code → Set
11
12
<Ψ> (A list) = List <Ψ>A <Ψ> (A ⊃ B) = <Ψ>A → <Ψ>B <Ψ> (Ψ’ ⇒ A) = <Ψ,Ψ’>A <Ψ> (∀ Ψ’. A) = (Ψ’ : Ctx) → <Ψ>(A Ψ’) <Ψ> D = …
Context Code for a Contextual Type Agda Set
12
13
<Ψ> exp = Expr Ψ where data Expr : Ctx → Set where lam : <Ψ>(exp ⇒ exp) → Expr Ψ app : <Ψ>(exp * exp) → Expr Ψ var : (exp ∈ Ψ) → Expr Ψ app : (exp * exp) ⇒ exp lam : (exp ⇒ exp) ⇒ exp
13
14
data _∈_ : Datatype → Ctx → Set where i0 : D ∈ (Ψ , D) iS : (D ∈ Ψ) → D ∈ (Ψ , D’) var : (exp ∈ Ψ) → Expr Ψ
14
15
Implement framework as a universe in Agda Examples and comparisons with Twelf/Delphin/ Beluga/FreshML Datatype-generic implementations of the structural properties
15
16
Normalize syntactic λ-terms by interpreting them as computational functions ⊃ in the metalanguage
16
16
Normalize syntactic λ-terms by interpreting them as computational functions ⊃ in the metalanguage norm : <⋅> (exp ⊃ exp) norm e = reify (eval e) where eval : <⋅> (exp ⊃ sem) reify : <⋅> (sem ⊃ exp)
16
16
Normalize syntactic λ-terms by interpreting them as computational functions ⊃ in the metalanguage norm : <⋅> (exp ⊃ exp) norm e = reify (eval e) where eval : <⋅> (exp ⊃ sem) reify : <⋅> (sem ⊃ exp)
maps closed expressions to closed expressions
16
17
17
17
First cut: sem = μs. s ⊃ s eval : <⋅> exp ⊃ sem eval (app e1 e2) = (unroll (eval e1)) (eval e2)
17
17
First cut: sem = μs. s ⊃ s eval : <⋅> exp ⊃ sem eval (app e1 e2) = (unroll (eval e1)) (eval e2) But how do you write reify : <⋅> sem ⊃ exp ?
17
17
First cut: sem = μs. s ⊃ s eval : <⋅> exp ⊃ sem eval (app e1 e2) = (unroll (eval e1)) (eval e2) But how do you write reify : <⋅> sem ⊃ exp ? Requires a slightly different target type...
17
18
napp : neu ⇒ sem ⇒ neu neut : neu ⇒ sem slam : (sem ⊃ sem) ⇒ sem
Semantic S ::= slam φ | neut(R) Neutral R ::= x | napp(R,S)
sem ⊃ sem
18
18
napp : neu ⇒ sem ⇒ neu neut : neu ⇒ sem slam : (sem ⊃ sem) ⇒ sem
Semantic S ::= slam φ | neut(R) Neutral R ::= x | napp(R,S)
sem ⊃ sem however, it’s not enough that φ works in the current context Ψ
18
19
napp : neu ⇒ sem ⇒ neu neut : neu ⇒ sem slam : (∀ Ψ. Ψ ⇒ (sem ⊃ sem)) ⇒ sem
semantic function that anticipates extensions of the context sem ⊃ sem
Semantic S ::= slam φ | neut(R) Neutral R ::= x | napp(R,S)
19
19
napp : neu ⇒ sem ⇒ neu neut : neu ⇒ sem slam : (∀ Ψ. Ψ ⇒ (sem ⊃ sem)) ⇒ sem
semantic function that anticipates extensions of the context sem ⊃ sem
Semantic S ::= slam φ | neut(R) Neutral R ::= x | napp(R,S)
19
20
eval: <⋅> (exp ⊃ sem)
20
21
eval: <⋅> ∀ Ψe, Ψs. env Ψe Ψs ⊃ [Ψe]exp ⊃ [Ψs]sem
21
21
eval: <⋅> ∀ Ψe, Ψs. env Ψe Ψs ⊃ [Ψe]exp ⊃ [Ψs]sem
<Ψ> ([Ψ’] A) = <Ψ’> A
21
21
eval: <⋅> ∀ Ψe, Ψs. env Ψe Ψs ⊃ [Ψe]exp ⊃ [Ψs]sem
<Ψ> ([Ψ’] A) = <Ψ’> A
env Ψe Ψs = [Ψe](exp#) ⊃ [Ψs]sem Environment type:
21
21
eval: <⋅> ∀ Ψe, Ψs. env Ψe Ψs ⊃ [Ψe]exp ⊃ [Ψs]sem
<Ψ> ([Ψ’] A) = <Ψ’> A
env Ψe Ψs = [Ψe](exp#) ⊃ [Ψs]sem
<Ψ> (D#) = D ∈ Ψ
Environment type:
21
22
eval σ (var x) = σ x eval σ (app e1 e2) = appsem (eval σ e1) (eval σ e2) eval σ (lam e) = ? eval: <⋅> ∀ Ψe, Ψs. env Ψe Ψs ⊃ [Ψe]exp ⊃ [Ψs]sem
22
23
eval{Ψe}{Ψs} σ (lam e) = slam φ eval: <⋅> ∀ Ψe, Ψs. env Ψe Ψs ⊃ [Ψe]exp ⊃ [Ψs]sem
23
24
eval{Ψe}{Ψs} σ (lam e) = slam φ where φ : <Ψs> ∀ Ψs’. Ψs’ ⇒ (sem ⊃ sem) φ = ? eval: <⋅> ∀ Ψe, Ψs. env Ψe Ψs ⊃ [Ψe]exp ⊃ [Ψs]sem
24
25
eval{Ψe}{Ψs} σ (lam e) = slam φ where φ : <Ψs> ∀ Ψs’. Ψs’ ⇒ (sem ⊃ sem) φ Ψs’ s’ = eval{Ψe, exp}{Ψs,Ψs’} σ’ e eval: <⋅> ∀ Ψe, Ψs. env Ψe Ψs ⊃ [Ψe]exp ⊃ [Ψs]sem
25
26
eval{Ψe}{Ψs} σ (lam e) = slam φ where φ : <Ψs> ∀ Ψs’. Ψs’ ⇒ (sem ⊃ sem) φ Ψs’ s’ = eval{Ψe, exp}{Ψs,Ψs’} σ’ e where σ’ : ([Ψe, exp]exp# ⊃ [Ψs,Ψs’]sem σ’ = extend σ with s’ eval: <⋅> ∀ Ψe, Ψs. env Ψe Ψs ⊃ [Ψe]exp ⊃ [Ψs]sem
26
27
eval{Ψe}{Ψs} σ (lam e) = slam φ where φ : <Ψs> ∀ Ψs’. Ψs’ ⇒ (sem ⊃ sem) φ Ψs’ s’ = eval{Ψe, exp}{Ψs,Ψs’} σ’ e where σ’ : ([Ψe, exp]exp# ⊃ [Ψs,Ψs’]sem σ’ i0 = s’ σ’ (iS x) = weaken (σ x) with Ψs’ eval: <⋅> ∀ Ψe, Ψs. env Ψe Ψs ⊃ [Ψe]exp ⊃ [Ψs]sem
27
28
σ’ (iS x) = weaken (σ x) with Ψs’
28
28
σ’ (iS x) = weaken (σ x) with Ψs’
has type <Ψs>sem
28
28
σ’ (iS x) = weaken (σ x) with Ψs’
weaken : <Ψs> sem ⊃ (Ψs’ ⇒ sem) has type <Ψs>sem
28
28
σ’ (iS x) = weaken (σ x) with Ψs’
weaken : <Ψs> sem ⊃ (Ψs’ ⇒ sem) has type <Ψs>sem
In raw Agda, would need to implement for each
28
29
Implement framework as a universe in Agda Examples and comparisons with Twelf/Delphin/ Beluga/FreshML Datatype-generic implementations of the structural properties
29
30
Weakening: ∀Ψ. Ψ ⇒ (A ⊃ (D ⇒ A)) Substitution: ∀Ψ. Ψ ⇒ (D ⇒ A) ⊃ (D ⊃ A) Exchange: ∀Ψ. Ψ ⇒(D1 ⇒ D2 ⇒ A) ⊃ (D2 ⇒ D1 ⇒ A) Contraction: ∀Ψ. Ψ ⇒(D ⇒ D ⇒ A) ⊃ (D ⇒ A) Strengthening: ∀Ψ. Ψ ⇒(D ⇒ A) ⊃ A
30
31
In mixed, pronominal setting, structural properties do not hold at all types: Computational functions express side conditions on the current context (Ψ ⊢ J) ⊃ 0 Ψ ⊢ J’
31
31
In mixed, pronominal setting, structural properties do not hold at all types: Computational functions express side conditions on the current context (Ψ ⊢ J) ⊃ 0 Ψ ⊢ J’
“it is not the case that Ψ ⊢ J” Proof cannot be weakened with J because Ψ,J ⊢ J
31
31
In mixed, pronominal setting, structural properties do not hold at all types: Computational functions express side conditions on the current context (Ψ ⊢ J) ⊃ 0 Ψ ⊢ J’
“it is not the case that Ψ ⊢ J” Proof cannot be weakened with J because Ψ,J ⊢ J
But there are sufficient conditions under which they do hold…
31
32
Instances of weakening:
32
32
exp ⊃ (exp ⇒ exp) Instances of weakening:
32
32
exp ⊃ (exp ⇒ exp) Instances of weakening:
OK
32
32
exp ⊃ (exp ⇒ exp) (exp ⇒ exp) ⊃ (exp ⇒ (exp ⇒ exp)) Instances of weakening:
OK
32
32
exp ⊃ (exp ⇒ exp) (exp ⇒ exp) ⊃ (exp ⇒ (exp ⇒ exp)) Instances of weakening:
OK OK
32
32
exp ⊃ (exp ⇒ exp) (exp ⇒ exp) ⊃ (exp ⇒ (exp ⇒ exp)) (exp ⊃ exp) ⊃ (exp ⇒ (exp ⊃ exp)) Instances of weakening:
OK OK
32
32
exp ⊃ (exp ⇒ exp) (exp ⇒ exp) ⊃ (exp ⇒ (exp ⇒ exp)) (exp ⊃ exp) ⊃ (exp ⇒ (exp ⊃ exp)) Instances of weakening:
OK OK ?
32
33
Weakening: (exp ⊃ exp) ⊃ (exp ⇒ (exp ⊃ exp)) Given, for example, normalize : <⋅> (exp ⊃ exp) normalize (lam x.e) = … normalize (app e1 e2) = … normalize (var x) = impossible
33
34
Weakening: (exp ⊃ exp) ⊃ (exp ⇒ (exp ⊃ exp)) Must create normalize’ : <⋅> (exp ⇒ (exp ⊃ exp)) normalize’ y.(lam x.e) = … normalize’ y.(app e1 e2) = … normalize’ y.(var y) = ???
34
35
Structural properties are not built-in (unlike LF): OK that they only hold at certain types Implemented datatype-generically but conditionally: “free” when conditions hold Conditions discharged automatically weaken: A ⊃ (D ⇒ A) if canWkn(D,A)
possible when D does not appear to the left of an ⊃ in A
35
36
Implemented a framework for mixing binding and computation in a pronominal setting Datatype-generic implementations of the structural properties by recursion over universe See paper for more examples and comparisons with Twelf/Delphin/Beluga/FreshML
36
37
Positivity check for signature + termination Equational behavior of structural properties Named syntax for variables; More-convenient syntax for structural properties Derived induction principles for structural props Dependent types!
37
38