Polyvariant Flow Analysis with Higher-ranked Polymorphic Types and - - PowerPoint PPT Presentation

polyvariant flow analysis with higher ranked polymorphic
SMART_READER_LITE
LIVE PREVIEW

Polyvariant Flow Analysis with Higher-ranked Polymorphic Types and - - PowerPoint PPT Presentation

[ Faculty of Science Information and Computing Sciences] Polyvariant Flow Analysis with Higher-ranked Polymorphic Types and Higher-order Effect Operators Jurriaan Hage Joint work with Stefan Holdermans (Vector Fabrics) Dept. of Information


slide-1
SLIDE 1

[Faculty of Science Information and Computing Sciences]

Polyvariant Flow Analysis with Higher-ranked Polymorphic Types and Higher-order Effect Operators

Jurriaan Hage Joint work with Stefan Holdermans (Vector Fabrics)

  • Dept. of Information and Computing Sciences

Utrecht University The Netherlands E-mail: jur@cs.uu.nl

September 27, 2010

slide-2
SLIDE 2

[Faculty of Science Information and Computing Sciences] 2

Type based program analysis

◮ Compilers for strongly typed functional languages need to

implement the intrinsic type system of the language.

◮ In TBPA:

◮ Other analyses take advantage of standardised concepts,

vocabulary, and implementation.

◮ Moreover, the (underlying) types lend structure to the

analysis.

slide-3
SLIDE 3

[Faculty of Science Information and Computing Sciences] 3

Control-flow analysis

◮ Control-flow analysis:

Determine for every expression, the locations where its value may have been produced.

◮ In type and effect systems: annotate types with analysis

information.

◮ bool{ℓ1,ℓ2} describes

◮ a boolean value ◮ produced at either program location ℓ1 or ℓ2.

◮ (bool{ℓ1} → bool{ℓ1,ℓ3}){ℓ2} describes

◮ a boolean-valued function produced at location ℓ2 ◮ that takes a value produced at ℓ1 and ◮ returns a value produced at ℓ1 or ℓ3.

slide-4
SLIDE 4

[Faculty of Science Information and Computing Sciences] 4

An imprecise control-flow analysis

h f = if f falseℓ1 then f trueℓ2 else falseℓ3 id x = x main = h id

◮ h can have type (bool{ℓ1,ℓ2} → bool{ℓ1,ℓ2}) → bool{ℓ1,ℓ2,ℓ3}

slide-5
SLIDE 5

[Faculty of Science Information and Computing Sciences] 4

An imprecise control-flow analysis

h f = if f falseℓ1 then f trueℓ2 else falseℓ3 id x = x main = h id

◮ h can have type (bool{ℓ1,ℓ2} → bool{ℓ1,ℓ2}) → bool{ℓ1,ℓ2,ℓ3} ◮ id can have type bool{ℓ1,ℓ2} → bool{ℓ1,ℓ2} ◮ Unacceptable:

◮ analysis is not modular: all uses of id must be known. ◮ other uses of id poisoned by effect of passing id to h

slide-6
SLIDE 6

[Faculty of Science Information and Computing Sciences] 5

Let-polyvariance to the rescue

id x = x h f = if f falseℓ1 then f trueℓ2 else falseℓ3, main = h id

◮ Let-defined and top-level identifiers identifiers can obtain a

context-sensitive, polyvariant type.

◮ h can now have type

∀β.(bool{ℓ1,ℓ2} → boolβ) → boolβ∪{ℓ3}

◮ For h id, instantiate β to {ℓ1,ℓ2} to obtain bool{ℓ1,ℓ2,ℓ3}. ◮ Improvement visible for h ctrue where ctrue z = trueℓ4:

bool{ℓ3,ℓ4} instead of bool{ℓ1,ℓ2,ℓ3,ℓ4}.

◮ Moreover, type of h independent of other calls to h.

slide-7
SLIDE 7

[Faculty of Science Information and Computing Sciences] 5

Let-polyvariance to the rescue

id x = x h f = if f falseℓ1 then f trueℓ2 else falseℓ3, main = h id

◮ Let-defined and top-level identifiers identifiers can obtain a

context-sensitive, polyvariant type.

◮ h can now have type

∀β.(bool{ℓ1,ℓ2} → boolβ) → boolβ∪{ℓ3}

◮ For h id, instantiate β to {ℓ1,ℓ2} to obtain bool{ℓ1,ℓ2,ℓ3}. ◮ Improvement visible for h ctrue where ctrue z = trueℓ4:

bool{ℓ3,ℓ4} instead of bool{ℓ1,ℓ2,ℓ3,ℓ4}.

◮ Moreover, type of h independent of other calls to h. ◮ But there is still some poisoning left.

slide-8
SLIDE 8

[Faculty of Science Information and Computing Sciences] 6

Higher-ranked polyvariance to finish the job

id x = x h f = if f falseℓ1 then f trueℓ2 else falseℓ3, main = h id

◮ Type of main is bool{ℓ1,ℓ2,ℓ3} ◮ But: the value of ℓ1 never flows to result of h. ◮ Poisoning still applies to different uses of f in h. ◮ Why?

slide-9
SLIDE 9

[Faculty of Science Information and Computing Sciences] 6

Higher-ranked polyvariance to finish the job

id x = x h f = if f falseℓ1 then f trueℓ2 else falseℓ3, main = h id

◮ Type of main is bool{ℓ1,ℓ2,ℓ3} ◮ But: the value of ℓ1 never flows to result of h. ◮ Poisoning still applies to different uses of f in h. ◮ Because f has to be assigned a monovariant type. ◮ If f could have type ∀β.boolβ → boolβ, then

◮ β = {ℓ1} for condition: does not propagate to result h id ◮ β = {ℓ2} for then-part: propagates to result h id

slide-10
SLIDE 10

[Faculty of Science Information and Computing Sciences] 7

Central question

But can such types, annotated with flow-sets, be inferred?

slide-11
SLIDE 11

[Faculty of Science Information and Computing Sciences] 7

Central question

But can such types, annotated with flow-sets, be inferred?

◮ Unassisted inference for higher-ranked polymorphism is

undecidable.

slide-12
SLIDE 12

[Faculty of Science Information and Computing Sciences] 7

Central question

But can such types, annotated with flow-sets, be inferred?

◮ Unassisted inference for higher-ranked polymorphism is

undecidable.

◮ For control-flow analysis we much prefer not to assist.

slide-13
SLIDE 13

[Faculty of Science Information and Computing Sciences] 7

Central question

But can such types, annotated with flow-sets, be inferred?

◮ Unassisted inference for higher-ranked polymorphism is

undecidable.

◮ For control-flow analysis we much prefer not to assist. ◮ But note that our types are not higher-ranked, only the

annotations are.

slide-14
SLIDE 14

[Faculty of Science Information and Computing Sciences] 8

Our contributions

◮ Undecidability of inference for higher-order polymorphism

  • n types does not imply undecidability of inference for

higher-ranked annotations on (ordinary) types.

◮ Inspired by Dussart, Henglein and Mossin

◮ Type inference algorithm is remarkably like Damas and

Milner’s algorithm W.

◮ Enabling technology of fully flexible types

◮ Modularity helps.

◮ The algorithm computes the best analysis for a given fully

flexible type derivation.

slide-15
SLIDE 15

[Faculty of Science Information and Computing Sciences] 9

The source language

◮ Simple monomorphic language:

◮ Producers: lambda-abstractions and boolean literals ◮ Consumers: applications, fix and conditional ◮ Variables propagate.

◮ Each expression is labelled to express its location.

t ::= x | pℓ | cℓ p ::= false | true | λx : τ.t1 c ::= if t1 then t2 else t3 | t1 t2 | fix t1.

slide-16
SLIDE 16

[Faculty of Science Information and Computing Sciences] 10

Types and type environments

Types, taken from Ty, are given by τ ::= bool | τ1 → τ2. Type environments are given by Γ ∈ TyEnv = Var→fin Ty .

slide-17
SLIDE 17

[Faculty of Science Information and Computing Sciences] 11

Control-flow annotations

◮ Associate with each term t a triple

τψ &ϕ

◮ ψ is an annotation, a set of labels describing the

production sites of the values of t.

◮ ϕ is an effect value that describes the flow (ℓ,ψ) that may

result from evaluating t: values produced at ℓ1 ∈ ψ may flow to ℓ.

τ is an annotated type that may contain further annotations:

  • τ

::= bool | τ1ψ1 ϕ − → τ2ψ2 | ...

◮ We extend to annotated type environments:

  • Γ

  • TyEnv

= Var→fin ( Ty×Ann) .

slide-18
SLIDE 18

[Faculty of Science Information and Computing Sciences] 12

Your first fully flexible (annotated) type

(λx : bool.(if x then falseℓ1 else trueℓ2)ℓ3)ℓ4. which may result in (∀β.boolβ {(ℓ3,β)} − − − − − →bool{ℓ1,ℓ2}){ℓ4} &{ },

◮ Produces a result constructed at ℓ1 or ℓ2. ◮ A lambda has no effect, and produces itself. ◮ No need to restrict the annotation of the argument x.

◮ Always annotate with an annotation variable.

◮ For every use of the expression we may choose a different

instance for β.

◮ Whatever is passed in is consumed by the conditional, ℓ3.

slide-19
SLIDE 19

[Faculty of Science Information and Computing Sciences] 13

Fully flexible types

◮ Types in which all argument positions are labelled with a

quantified annotation variable.

◮ Our algorithm only infers fully flexible types.

slide-20
SLIDE 20

[Faculty of Science Information and Computing Sciences] 14

From fully flexible types to effect operators

(λf : bool → bool.(f trueℓ5)ℓ6)ℓ7,

◮ To be fully flexible f has annotation βf . ◮ All functions passed into f are fully flexible: give f type

∀β.boolβ ϕ − →boolψ.

◮ In general, the latent effect of f and the flow of the result

  • f f depend on β.

◮ Let’s make that explicit: ∀β.boolβ ϕ0 β

− − →boolψ0 β

◮ Now, ϕ0 and ψ0 have become effect operators.

slide-21
SLIDE 21

[Faculty of Science Information and Computing Sciences] 15

Delivery time for the motivating example

(λf : bool → bool. (if (f falseℓ1)ℓ2 then (f trueℓ3)ℓ4 else falseℓ5)ℓ6)ℓ7 has fully flexible annotated type ∀βf .∀δ0.∀β0.(∀β.boolβ δ0 β − − →bool(β0 β))βf

{(ℓ2,βf )}∪{(ℓ4,βf )}∪δ0 {ℓ1}∪δ0 {ℓ3}∪{(ℓ6,β0 {ℓ1})}

− − − − − − − − − − − − − − − − − − − − − − − − − − − − − − → bool(β0 {ℓ3}∪{ℓ5}), Instantiating it to prepare it for receiving (λx : bool.x)ℓ8 gives (∀β.boolβ { } − →boolβ)

{(ℓ2,ℓ8),(ℓ4,ℓ8),(ℓ6,ℓ1)}

− − − − − − − − − − − − − →bool{ℓ3,ℓ5}. Finally commit to particular choices: βf = {ℓ8},δ0 = λβ.{ } and β0 = λβ.β.

slide-22
SLIDE 22

[Faculty of Science Information and Computing Sciences] 16

Further remarks

◮ Analysis of a function is parameterised over the analysis of

its argument.

◮ The relation between those is captured by the

annotation/effect operators.

slide-23
SLIDE 23

[Faculty of Science Information and Computing Sciences] 16

Further remarks

◮ Analysis of a function is parameterised over the analysis of

its argument.

◮ The relation between those is captured by the

annotation/effect operators.

◮ Changes are not without consequences.

◮ Unification of types now needs beta-reduction of

expressions over annotations and effects.

◮ And a notion of well-typedness (sorting) for such

expressions.

slide-24
SLIDE 24

[Faculty of Science Information and Computing Sciences] 17

The ubiquitous deduction rules

◮ See the paper. ◮ Includes

◮ definitions for sorting the annotations and effects, ◮ definitional equivalence for annotations and effects, ◮ definition of type well-formedness, ◮ and metatheoretic properties.

slide-25
SLIDE 25

[Faculty of Science Information and Computing Sciences] 18

The algorithm

◮ Remarkably like Algorithm W. ◮ Traverse t to perform “unifications”, and generates

constraints that describe the actual flow.

◮ Solving is a bit more complicated due to beta-reduction for

annotations and effects.

◮ Compared to Algorithm W:

◮ Solve occurs for each lambda-abstraction (vs. let-definition) ◮ Instantiation performed in the application rule (vs.

identifier).

slide-26
SLIDE 26

[Faculty of Science Information and Computing Sciences] 19

Summary

◮ Full annotated-type inference in the presence of

higher-ranked polymorphism for annotations.

◮ Allows to parameterise functions over the analysis of their

arguments,

◮ which provides context-sensitivity for lambda-bound

identifiers.

slide-27
SLIDE 27

[Faculty of Science Information and Computing Sciences] 20

Future work, lots of it

◮ Short term: asymptotic complexity estimate ◮ Scale to realistic language. ◮ Apply to other optimising analyses. ◮ Backwards variant

◮ For every value, where may it flow to.

◮ Extend to validating analyses, e.g., dimension analysis. ◮ Minimal typing derivations. ◮ Comparison with let-polyvariance:

◮ How much does additional precision buy us practically?

◮ Comparison with intersection types.

◮ Currently available implementations of intersection types?

slide-28
SLIDE 28

[Faculty of Science Information and Computing Sciences] 21

Thank you for your attention

slide-29
SLIDE 29

[Faculty of Science Information and Computing Sciences] 22

Algorithm

◮ Algorithm W style constraint based algorithm.

◮ R(

Γ,t) returns ( τ,β,δ,C).

τ is the annotated type.

◮ β is an annotation variable representing the top-level

annotation of τ.

◮ δ is an effect variable. ◮ Constraint set C to constrain these.

slide-30
SLIDE 30

[Faculty of Science Information and Computing Sciences] 23

Algorithm - the case of lambda

R( Γ,(λx : τ1.t1)ℓ) = let ( τ1,χi :: si) = C(τ1,ε) β1,β,δ be fresh ( τ2,β2,δ0,C1) = R( Γ[x → ( τ1,β1)],t1) X = {β1}∪{χi}∪ffv( Γ) (ψ2,ϕ0) = S(C1,X,β2,δ0)

  • τ = ∀β1 :: ann.∀χi :: si.

τ1β1 ϕ0 − → τ2ψ2 in ( τ,β,δ,{{ℓ} ⊆ β })

◮ Completion function C annotates type τ1 freshly. ◮ Solve to obtain actual flows before generalisation. ◮ Solver S treats active variables as annotation constants. ◮ Active = free in

Γ or exposed via τ.

slide-31
SLIDE 31

[Faculty of Science Information and Computing Sciences] 24

Algorithm - the case of application

R( Γ,(t1 t2)ℓ) = let ( τ1,β1,δ1,C1) = R( Γ,t1) ( τ2,β2,δ2,C2) = R( Γ,t2)

  • τ′

2 β ′

2

ϕ′

− → τ′ψ′ = I( τ1) θ = [β ′

2 → β2]◦M([ ],

τ2, τ′

2)

β,δ be fresh C = {δ1 ⊆ δ }∪{δ2 ⊆ δ }∪{{(ℓ,β1)} ⊆ δ }∪{θ ϕ′

0 ⊆ δ }∪

{θ ψ′ ⊆ β }∪C1 ∪C2 in (θ τ′,β,δ,C)

◮ I freshes all annotation variables. ◮ M performs matching (one-sided unification).

◮ Works because the second argument is the result of I.

slide-32
SLIDE 32

[Faculty of Science Information and Computing Sciences] 24

Algorithm - the case of application

R( Γ,(t1 t2)ℓ) = let ( τ1,β1,δ1,C1) = R( Γ,t1) ( τ2,β2,δ2,C2) = R( Γ,t2)

  • τ′

2 β ′

2

ϕ′

− → τ′ψ′ = I( τ1) θ = [β ′

2 → β2]◦M([ ],

τ2, τ′

2)

β,δ be fresh C = {δ1 ⊆ δ }∪{δ2 ⊆ δ }∪{{(ℓ,β1)} ⊆ δ }∪{θ ϕ′

0 ⊆ δ }∪

{θ ψ′ ⊆ β }∪C1 ∪C2 in (θ τ′,β,δ,C)

◮ δ1 ⊆ δ,δ2 ⊆ δ: flow of evaluating application includes the

effects of evaluating the function and argument.

◮ θ ϕ′ 0 ⊆ δ: effect of the body is included too. ◮ (ℓ,β1)} ⊆ δ: the application consumes the function. ◮ θ ψ′ ⊆ β: body result flows to the application result.