Fredrik Nordvall Forsberg (joint work with Dan …drg/bll/fredrik.pdfRestricted linear dependent...
Transcript of Fredrik Nordvall Forsberg (joint work with Dan …drg/bll/fredrik.pdfRestricted linear dependent...
Restricted linear dependent types for resource allocation
Fredrik Nordvall Forsberg(joint work with Dan Ghica)
BLL WorkshopFontainebleau, 4 December 2013
Resource analysis via types
Common theme: type systems with resource annotations.
Annotate terms with the amount of resources they need to executesuccessfully.
Resources taken from a semiring.
Type system generalisation of BLL.
1
Concrete example
δf
When compiling to hardware circuits, we need to enforce a pipeliningdiscipline for efficient results.
Here resources are “timing schedules”.
2
Data dependent resource requirements
Resource requirements are often data dependent.
Simple example:
Γ ` x : N Γ ` echeap : θ Γ ` eexpensive : θ
Γ ` ifzero x then echeap else eexpensive : θ
eexpensive
echeap
ifzero x then echeap else eexpensive
3
Data dependent resource requirements
Resource requirements are often data dependent.
Simple example:
Γ ` x : N Γ ` echeap : θ Γ ` eexpensive : θ
Γ ` ifzero x then echeap else eexpensive : θ
eexpensive
echeap
ifzero 0 then echeap else eexpensive
3
Data dependent resource requirements (cont.)
Possible if resources can depend on data:
eexpensive
echeap
ifzero x then echeap else eexpensive
We want resource annotations to appear in types, and resources todepend on data.
Hence, we need types to depend on data – dependent types.
4
Data dependent resource requirements (cont.)
Possible if resources can depend on data:
eexpensive
echeap
ifzero 0 then e else e′
We want resource annotations to appear in types, and resources todepend on data.
Hence, we need types to depend on data – dependent types.
4
Data dependent resource requirements (cont.)
Possible if resources can depend on data:
eexpensive
echeap
ifzero (n + 1) then e else e′
We want resource annotations to appear in types, and resources todepend on data.
Hence, we need types to depend on data – dependent types.
4
Data dependent resource requirements (cont.)
Possible if resources can depend on data:
eexpensive
echeap
ifzero (n + 1) then e else e′
We want resource annotations to appear in types, and resources todepend on data.
Hence, we need types to depend on data – dependent types.
4
Outline
A type system combining:
I The parametric resources from BLL over a general semiring (Ghica andSmith (2013), Brunel, Gaboardi, Mazza, and Zdancewic (2013)),
I the dependent linear types from d`PCF (Dal Lago, Gaboardi, Petit(2011 – 2013)).
A categorical semantics for it.
5
A type system for resources
Design goals
Type system with resource annotations and (restricted) dependenttypes.
Call by name to fit in with hardware compiler.
Decidable type inference (do not want to force programmer to dealwith complicated types).
6
DML-style dependent types
Following d`PCF, we use Dependent ML-style dependent types(“refinement types”).
Index language separate from term language.
Also means we can safely add non-terminating or side effect-inducingterms to the term language.
Linked through singleton types nat[i ]:
x : nat[i ] iff x = i
Here x is a term and i is an index term.
7
Overview
Four different judgements:
I Φ;χ ` i index
I Φ;χ ` J resource
I Φ;χ ` Θ type
I Φ;χ; Γ ` t : Θ
Φ index context χ index constraints Γ term context
For ease of presentation, we first introduce an affine system (i.e. nocontraction) without bounded exponentials.
8
Index terms
Like DML, we are almost completely parametric in the indexlanguage.
E.g. (N,+, 0, 1, <) plus a variable schema.
The theory of the index language needs to be decidable for typechecking.
9
Resources
Resources should form an ordered monoid (R,×, 1,≤).
Also resources depending on index terms, e.g.:
Φ;χ ` m, n index Φ;χ ` J,K resource
Φ;χ ` (if m = n then J else K ) resource
What happened to +?
“BLL over a semiring” is using + of the semiring for contraction. We willintroduce a different bounded exponential for that purpose later.
10
Types (without exponentials)
Generated by grammar
θ ::= nat | nat[M]
| θ ⊗ θ| (R · θ) ( θ
(M index term, R resource).
Formally derivations of the form Φ;χ ` θ type.
11
Terms
Term contexts are sets Γ = {x1 : R1 · θ1, . . . , xn : Rn · θn}.
Φ, χ ` θ type
Φ;χ; x : 1 · θ ` x : θ(axiom)
Γ ` t : θ′
Γ, x : R · θ ` t : θ′(weakening)
Γ, x : Q · θ ` t : θ′ Q ≤ R
Γ, x : R · θ ` t : θ′(resource weakening)
Γ ` s : θ Γ′ ` t : θ′
Γ, Γ′ ` 〈s, t〉 : θ ⊗ θ′(pairing)
12
Terms (cont.)
` n : nat[n]
Φ;χ; Γ ` b : nat[B] Φ;χ,B = 0; Γ′ ` s : θ Φ;χ,B > 0; Γ′ ` t : θ
Φ;χ; Γ, Γ′ ` ifzero b then s else t : θ
Γ, x : J · θ ` t : θ′
Γ ` λ x . t : J · θ ( θ′(abstraction)
Γ ` s : R · θ ( θ′ Γ′ ` t : θΓ,R · Γ′ ` s t : θ′
(application)
where
R · {x1 : S1 · θ1, . . . , xn : Sn · θn} :=
{x1 : (R × S1) · θ1, . . . , xn : (R × Sn) · θn}.
13
Example: short-circuit multiplication constant
∗ : nat→ nat→ nat
If the first argument is zero, no need to evaluate second argument.
Simple notion of resource: number of threads needed.
We can give ∗ type
∗ : 1 · nat[A] ( R · nat ( nat
where R ≡ if A = 0 then 0 else 1.
14
Example: a caching memory controller
Caching behaviour of a memory controller: if we request the sameaddress twice consecutively, the second roundtrip will be faster thanrequesting another address.
A family of constants
memi : Ri · (bool[Bi ]⊗ nat[Ai ]⊗ nat) ( nat
To write v at address a, we call
a←i v := memi (w, a, v)
and ignore the result (here w := ff).
To read the current value at address a, we call
!ia := memi(r, a, )
where is arbitrary (here r := tt).
15
Example: a caching memory controller (cont.)
memi : Ri · (bool[Bi ]⊗ nat[Ai ]⊗ nat) ( nat
The resource Ri depends on the data.
For simplicity, let’s use “cost 1” or “cost 0”.
R0 ≡ 1
Rn+1 ≡ if Bn+1 = r then (if An+1 = An then 0 else 1) else 1
16
Adding contraction
“BLL over a semiring” use the additive structure of the semiring forcontraction:
Γ, x : R1 · θ, y : R2 · θ ` t : θ′
Γ, x : (R1 + R2) · θ ` t{x/y} : θ′
Does not work for us because of higher-order dependent types:
Γ, f : R1 · (nat[A] ( nat), g : R2 · (nat[A] ( nat) ` f (0) + g(1) : nat
Γ, f : (R1 + R2) · (nat[A] ( nat) ` f (0) + f (1) : nat
would require 0 = A = 1.
17
Bounded exponentials
Following d`PCF, we use a bounded exponential ! i<N θ.
Here, i is an index term, and θ can depend on i .
Hence each argument can get its own index Ai :
f : ! i<2 (Ri · (nat[Ai ] ( nat))
In some sense a separate kind of resource (similar to the Nf 〈R〉construction).
Change contexts to be sets of the form
Γ = {x1 : ! i1<N1 (R1 · θ1), . . . , xn : ! in<Nn (Rn · θn)}.
18
Modified types
Types now given by the grammar
θ ::= nat | nat[N] | θ ⊗ θ| (! i<N (R · θ)) ( θ
Detailed rule for the function type:
Φ;χ ` N index Φ, i ;χ, i < N ` R resourceΦ;χ ` θ′ type
Φ, i ;χ, i < N ` θ type
Φ;χ ` (! i<N R · θ) ( θ′ type
Note dependency of R and θ on i < N.
Important point: R should only be allowed to depend on data Ai (asin nat[Ai ]), not on the index i itself (does not make sense in call byname setting).
19
Contraction rule
Φ;χ; Γ, x : ! i<N R · θ, y : ! i′<M ((R · θ){N + i ′/i}) ` t : θ′
Φ;χ; Γ, x : ! i<N+M R · θ ` t{x/y} : θ′(contraction)
Intuitively: split the N + M occurrences of x into the first N, followedby the next M.
(R · θ){0}, . . . , (R · θ){N − 1}︸ ︷︷ ︸! i<N R·θ
, (R · θ){N}, . . . (R · θ){N + M − 1}︸ ︷︷ ︸! i′<M ((R·θ){N+i ′/i})
Other rules needs to be adapted as well.
In particular, resource action R · Γ needs to be generalised to ! N R · Γ(following d`PCF).
20
Modified rules
Φ, i , χ, i < 1 ` θ type
Φ;χ; x : ! i<1 1 · θ ` x : θ{0/i} (axiom)
Γ, x : ! i<N Q · θ ` t : θ′ Q ≤ R
Γ, x : ! i<N R · θ ` t : θ′(resource weakening)
Γ, x : ! i<N (J · θ) ` t : θ′
Γ ` λ x . t : ! i<N (J · θ) ( θ′(abstraction)
Φ;χ; Γ ` f : ! i<N (R · θ) ( θ′ Φ, i ;χ, i < N; Γ′ ` t : θ
Φ;χ; Γ, ! N R · Γ′ ` f t : θ′(application)
21
Categorical semantics
Overview
Category B of index contexts.
Indexed category I : Bop → Cat of index terms.
Indexed category R : Bop → Cat of resources.
Indexed category C : Bop → Cat of computations.
22
Index contexts and index terms
Index contexts Φ;χ can be interpreted as the conjunction of theconstraints in χ.
Then interpreted in the standard categorical logic way in anyCartesian (well-powered) category B.
There is a morphism JΦ, χK→ JΦ′, χ′K if χ implies χ′ in the model.
Any indexed category I : Bop → Cat supporting the constants cannow interpret the index terms.
23
Resources
Similarly, resources can be interpreted in an indexed categoryR : Bop → Cat supporting the right constants.
In particular, R should be “indexed monoidal”, i.e. each fibre R(Φ, χ)should have a monoidal tensor (�, 1)Φ,χ (compatible with indexsubstitution).
Morphisms JRK→ JSK corresponds to inequalities R ≤ S .
24
Types and terms
Indexed symmetric monoidal closed category C : Bop → Cat (ie. eachfibre C(Φ, χ) symmetric monoidal closed).
Furthermore the unit IΦ,χ should be terminal in each fibre. Thisimplies that we have projections π : A⊗ B → A.
Types Φ;χ ` θ type are interpreted as objects JθK in C(Φ;χ).
Terms Φ;χ; Γ ` M : θ are interpreted as morphisms JMK : JΓK→ JθKin C(Φ;χ) where
JΓK = Jx1 : ! a1<N1 J1 · θ1, . . . , xn : ! an<Nn Jn · θnK:= ! N1 J1 · Jθ1K⊗ . . .⊗ ! Nn Jn · JθnK
with ! N J · X explained soon.
25
Resources acting on C
We further require a natural transformation · : R× C ·→ C, i.e.
·Φ;χ : R(Φ;χ)× C(Φ;χ)→ C(Φ;χ)
Satisfying the following equations (fibrewise):
1 ·Φ;χ θ = θ
(R � S) ·Φ;χ θ = R ·Φ;χ (S ·Φ;χ θ) .
26
Bounded exponential
The bounded exponential ! i<N should be a functor
! i<N : C(Φ, i ;χ, i < N)→ C(Φ;χ)
Which one? Kind of a bounded universal quantification. . .
Right adjoint to weakening!
We have a “projection” πΦ,χ,i ,i<N : JΦ, i ;χ, i < NK→ JΦ;χK in B.
Gives rise to wki<N := C(πΦ,χ,i ,i<N) : C(Φ;χ)→ C(Φ, i ;χ, i < N).
wki<N a ! N .
! N also needs to be monoidal.
27
Bounded exponential
The bounded exponential ! i<N should be a functor
! i<N : C(Φ, i ;χ, i < N)→ C(Φ;χ)
Which one? Kind of a bounded universal quantification. . .
Right adjoint to weakening!
We have a “projection” πΦ,χ,i ,i<N : JΦ, i ;χ, i < NK→ JΦ;χK in B.
Gives rise to wki<N := C(πΦ,χ,i ,i<N) : C(Φ;χ)→ C(Φ, i ;χ, i < N).
wki<N a ! N .
! N also needs to be monoidal.
27
The action ! N J · X
For each index term N, The natural transformation ! N− · − withcomponents
(! N− · −)Φ;χ : R(Φ, i ;χ, i < N)× C(Φ, i ;χ, i < N)→ C(Φ;χ)
can be defined by composing ! i<N and ·.
Also need a family of natural transformations δN,M corresponding tocontraction
δN,M,X : ! N+M (X )→ ! N (X )⊗ ! M (X{i/N + j})
and derelictionιX : ! 1 (X )→ X
(natural as usual).
28
The interpretation
JΓ ` t : θK : JΓK→ JθK
Jx : ! i<1 1 · θ ` x : θ{0/i}K = ι ◦ ε{0/i}JΓ, x : ! i<N R · θ ` t : θ′K = JΓ ` t : θ′K ◦ π
Jx : ! i<N R · θ ` t : θ′K= JΓ, x : ! i<N Q · θ ` t : θ′K ◦ (idΓ ⊗ ! idN
JQ ≤ RK · idθ)
JΓ, Γ′ ` 〈t, t ′〉 : θ ⊗ θ′K = JΓ ` t : θK⊗JΓ′ ` t ′ : θ′KJΓ ` λx .t : ! i<N R · θ ( θ′K = Λ(JΓ, x : ! i<N R · θ ` t : θ′K)
JΓ, ! N R · Γ′ ` f t : θ′K= eval ◦ JΓ `f : ! i<N R · θ ( θ′K⊗ ! N R·JΓ′ ` t : θK
JΓ, x : ! i<N+M R · θ ` t{x/y} : θ′K= JΓ, x : . . . ,y : . . . ` t : θ′K ◦ (idΓ ⊗ δN,M,R·θ)
29
Coherence
Just like for the non-dependent system, the use of weakening andcontraction introduces multiple derivations.
Coherence can be proven in the same way:
1 Transform derivations into a normal form, with weakening andcontractions performed as late as possible.
2 Show that the transformation is meaning-preserving.
3 Show that derivations in normal form are unique.
Crucial is the fact that resource dependency is uniform: it is possibleto depend on data Ai , but not on the index i itself.
30
Summary
Combining BLL over a semiring and restricted dependent linear types.
Coherent categorical semantics by using indexed categories.
Thank you!
31
The action ! NR · Γ′
Simplified:
! N R · (x : ! j<M (S · θ)) = x : ! j<N·M (R × S · θ)
Need N copies of the context, and each copy needs M occurrences ofx , so need N ·M copies.
For each copy, multiply resource requirements.
But this does not take index dependency into account; e.g. M candepend on i < N.
So use∑
i<N M instead of N ·M.
32
More dependencies
Simplified:
! N R · (x : ! j<M (S · θ)) = x : ! k<∑
i<N M (R × S · θ)
Similarly, dependencies of R on i < N and of S on j < M.
Each k <∑
i<N M can be written as k =∑
d<i M{d/i}+ j withi < N and j < M.
M(0) + M(1) + M(2) + M(3) + . . .+ M(N − 1)
∑d<i M(d) j
Multiply R(i) and S(j) for these i and j .
33
Formally
Given
Φ;χ ` N index
Φ, i ;χ, i < N ` R resource
Φ, i ;χ, i < N ` M index
Φ, i , j ;χ, i < N, j < M ` K resource
andA = ! j<M (K · θ{(
∑d<i
M{d/i}+ j)/k})
we define! N R · A = ! k<
∑i<N M (R × K ) · θ
34