• No results found

DILL syntactically separates non-linear variables Θ from linear variables ∆ in its typing judgment, but one could equally consider a typing judgment that annotated each variable as either linear or unrestricted. This presentation, which we call indexed resource modalities,

uses a single typing context Φ annotated with resource descriptors r:

Φ∶∶= ∅ ∣Φ, x∶rσ r∶∶=0∣1∣ω

The resource 1 stands for linear use,i.e.,the variable is used exactly once in a term, andω stands for unrestricted use. The resource 0 stands for an unused resource, so ifx does not appear in Φ, then Φ is equivalent to Φ, x∶0σ.

These resource descriptors form an algebraic structure known as a rig—a riNg without Negation:

0+r=r+0=r 0⋅r=r⋅0=0 1⋅r=r⋅1=r In addition, the unrestricted resource absorbs other resources.

1+1=ω ω+r=r+ω=ω ω⋅ω=ω

The first equation says that when a linear resource (denoted with the resource descriptor 1) is used more than once in a system, then it it is unrestricted in the combined system. With a different collection of resources, e.g., resources drawn from Z, we could produce a more

refined analysis; we discuss these more below. The second and third equations say that an unrestricted resource will always remain unrestricted.

We can extend the rig on resources to a semi-module on indexed typing contexts.

(∆1, x∶r1 σ) + (∆2, x∶r2 σ) ≡ (∆1+∆2), x∶r1+r2 σ

r⋅ (Γ, x∶r′ σ) ≡ (r⋅Γ), x∶rr′ σ

The typing judgment has the form Φ ⊢I e ∶ σ. Like in DILL, we want unrestricted

data annotated with ω to have implicit weakening and contraction, which we can obtain by modifying how contexts are split. Instead of restricting typing rules to disjoint typing contexts, we simply use context addition to determine the output typing context from the

input contexts. ω⋅Φ, x∶rσ⊢Ix∶σ i-var Φ⊢Ie∶σ Φ′, x∶rσ⊢Ie′∶τ (r⋅Φ) +Φ′⊢Iletx∶=ein e′∶τ i-let

In the variable rule, all the variables in ω⋅Φ are unrestricted, so they can be implicitly weakened. In theletrule, the resources Φ used to constructeare scaled by the number of times x is being used in the result.

The promotion rule says that any linear expression can be promoted, but the resources in the result are all scaled by ω, since the result could be used any number of times.

Φ⊢Ie∶σ ω⋅Φ⊢I!e∶!σ i-!-I Φ⊢Ie∶!σ Φ⊢Iderelicte∶σ i-!-E

Function types can be annotated with the resource corresponding to how many times the argument is used.

Φ, x∶rσ⊢Ie∶τ Φ⊢Iλx.eˆ ∶σ→rτ i--I Φ⊢Ie∶σ→rτ Φ′⊢Ie′∶σ Φ+r⋅Φ′⊢Iee′∶τ i--E

Related work. Resource annotations have often been extended to different substructural type systems. The style seems to have originated with bounded linear logic (Girard et al., 1992) annotating the exponential !nwith a numbernrecording the precise number of times it is used. The type system presented above can easily accommodate exponentials indexed by arbitrary resources: Φ⊢Ie∶σ r⋅Φ⊢I!e∶!rσ i-!r-I Φ⊢Ie∶!rσ Φ⊢Iderelicte∶σ i-!r-E

By including resources corresponding to affine or substructural use, resource annotations can express substructural typing systems, or coeffects like data flow, liveness analyses, or differential privacy (Petriceket al., 2014; Brunelet al., 2014; Reed and Pierce, 2010).

McBride (2016) uses resource annotations in a calculus for linear dependent types, where variables can be used in types with a resource annotation of x ∶0 σ. McBride indexes not

only variables, but also the typing judgment itself, with a resource: Φ⊢Ie∶rσ, which takes

the place of the exponential !r.

Bernardyet al.(2017) use resource annotations in a calculus that retrofits Haskell with linear types. Their typing judgment, though, has a unique interpretation: the typing judg- ment Φ⊢e∶σ in their system means that if the result of eis consumed exactly once, then the linear hypotheses in Φ will be consumed exactly once. However, any top-level expression can be consumed multiple times, to make the calculus backwards-compatible and facilitate code reuse between linear and non-linear types. This means that if a program wants to guarantee linear use of a piece of data, it must bind that data on the left-hand-side of a function type, as in σ→1τ. In practice this seems to result in a style of programming akin

to continuation-passing style.

Embedded indexed modalities. Like DILL, the presentation in terms of indexed modal- ities requires that both linear and non-linear resources share the same kind of type. But now we can define the type α→rβ as a wrapper for α→β when r is ω, and otherwise as an empty type.

data α→r β where

fun : (α→β)→ (α→ω β)

Thus, non-linear functionsf ∶α→β can be coerced into a linear expressionput(funf) of linear type α→ω β, but not into the type α→1 β, which can only be constructed via the

embedded ˆλconstructor. e∶LExpI (Φ, x∶rα) β ˆ λx.e∶LExpI Φ(α→rβ) e∶LExpI Φ (α→rβ) e ′LExp I Φ ′ α eˆe′∶LExpI (Φ+r⋅Φ ′) β

Non-linear functions from host-language libraries can now be applied to linear arguments. For example, consider thelookupoperation from the linear interface to mutable references discussed in Chapter 1.

lookup∶LExpI (ω⋅Φ) (LRef α→1α⊗LRef α)

We can lift arbitrary functions of type α→β to the result oflookup:

op∶ (α→β) →LExpI (ω⋅Φ) (LRefα→1β⊗LRef α)

op≡λf.ˆλr.let(x, r′) ∶=lookupr in (putfˆx, r′)

But what is the operational semantics ofput? Is(putf)a value? If so, then isput fˆ v a stuck term? What about put f ˆ put a?

These questions may not be insurmountable, but they are not straightforward from the theory of indexed resource modalities.