[ 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
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. [ Faculty of Science Information and Computing Sciences] 2
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 . [ Faculty of Science Information and Computing Sciences] 3
An imprecise control-flow analysis = if f false ℓ 1 then f true ℓ 2 else false ℓ 3 h f id x = x main = h id ◮ h can have type ( bool { ℓ 1 ,ℓ 2 } → bool { ℓ 1 ,ℓ 2 } ) → bool { ℓ 1 ,ℓ 2 ,ℓ 3 } [ Faculty of Science Information and Computing Sciences] 4
An imprecise control-flow analysis = if f false ℓ 1 then f true ℓ 2 else false ℓ 3 h f 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 [ Faculty of Science Information and Computing Sciences] 4
Let-polyvariance to the rescue id x = x = if f false ℓ 1 then f true ℓ 2 else false ℓ 3 , h f 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 . [ Faculty of Science Information and Computing Sciences] 5
Let-polyvariance to the rescue id x = x = if f false ℓ 1 then f true ℓ 2 else false ℓ 3 , h f 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. [ Faculty of Science Information and Computing Sciences] 5
Higher-ranked polyvariance to finish the job id x = x = if f false ℓ 1 then f true ℓ 2 else false ℓ 3 , h f 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? [ Faculty of Science Information and Computing Sciences] 6
Higher-ranked polyvariance to finish the job id x = x = if f false ℓ 1 then f true ℓ 2 else false ℓ 3 , h f 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 [ Faculty of Science Information and Computing Sciences] 6
Central question But can such types, annotated with flow-sets, be inferred? [ 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. [ 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. [ 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. [ Faculty of Science Information and Computing Sciences] 7
Our contributions ◮ Undecidability of inference for higher-order polymorphism on 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. [ Faculty of Science Information and Computing Sciences] 8
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. x | p ℓ | c ℓ :: = t :: = false | true | λ x : τ . t 1 p c :: = if t 1 then t 2 else t 3 | t 1 t 2 | fix t 1 . [ Faculty of Science Information and Computing Sciences] 9
Types and type environments Types, taken from Ty , are given by τ :: = bool | τ 1 → τ 2 . Type environments are given by ∈ = Var → fin Ty . Γ TyEnv [ Faculty of Science Information and Computing Sciences] 10
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: τ 1 ψ 1 ϕ τ 2 ψ 2 | ... � :: = bool | � − → � τ ◮ We extend to annotated type environments: � � Var → fin ( � ∈ = Ty × Ann ) . Γ TyEnv [ Faculty of Science Information and Computing Sciences] 11
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 . [ Faculty of Science Information and Computing Sciences] 12
Fully flexible types ◮ Types in which all argument positions are labelled with a quantified annotation variable. ◮ Our algorithm only infers fully flexible types. [ Faculty of Science Information and Computing Sciences] 13
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 of f depend on β . ◮ Let’s make that explicit: ∀ β . bool β ϕ 0 β → bool ψ 0 β − − ◮ Now, ϕ 0 and ψ 0 have become effect operators. [ Faculty of Science Information and Computing Sciences] 14
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 β { } { ( ℓ 2 ,ℓ 8 ) , ( ℓ 4 ,ℓ 8 ) , ( ℓ 6 ,ℓ 1 ) } → bool β ) → bool { ℓ 3 ,ℓ 5 } . − − − − − − − − − − − − − − Finally commit to particular choices: β f = { ℓ 8 } , δ 0 = λ β . { } and β 0 = λ β . β . [ Faculty of Science Information and Computing Sciences] 15
Recommend
More recommend