Com S 641 meeting -*- Outline -*- * Type and Effect Systems (1.6) ** goals To fit program analysis into the syntax-directed machinery of type checking. To extend type systems with information about effects. ** idea Q: What's the basic idea? Use types to express the analysis. Two techniques: - annotated type systems - effect systems ** annotated type system example *** annotated base types For assigned variables analysis, we seek sets of variables that may have been assigned up to a program point. ------------------------------------------ ANNOTATED TYPE SYSTEM EXAMPLE [ass] [x := a]^l : AV1 -> AV2 if AV2 = AV1 \cup {x} [skip] [skip]^l : AV -> AV S1 : AV1 -> AV2, S2: AV2 -> AV3 [seq] -------------------------------- S1; S2 : AV1 -> AV3 S1 : AV1 -> AV2, S2: AV1 -> AV2 [if] -------------------------------- if [b]^l then S1 else S2 : AV1 -> AV2 S : AV1 -> AV2 [wh] -------------------------------- while [b]^l then S : AV1 -> AV2 S : AV2 -> AV3 [sub] -------------- if AV1 \subseteq AV2, S : AV1 -> AV4 AV3 \subseteq AV4 ------------------------------------------ Q: What's the meaning of the basic types? set of variables that might be assigned Q: What's the meaning of the arrow types, like AV1 -> AV4? if the input flows to the statement have assigned to a subset of AV1, then the statement assigns to no more than the AV4 (see the sub rule) Q: How would you explain the assignment rule? Q: How would you explain the if rule? Q: Why don't we have to check the type of the condition in an if? the grammar does that Q: How would you explain the while rule? Q: How would you explain the sub rule? submption, says the meaning of arrow types (as above) **** type checking ------------------------------------------ EXAMPLE [q := 0]^1; [r := x]^2; while [r >= y]^3 do ([r := r-y]^4; [q := q+1]^5); assert [0<=r and r AV1 \cup {q}, [ass] [r := x]^2: AV2 -> AV2 \cup {r} [ass] ___________________________________ [seq] ([q := 0]^1;[r := x]^2) : AV1 -> AV3 if AV3 = AV2 \cup {r}, AV2 = AV1 \cup {q} [r := r-y]^4: AV4 -> AV4 \cup {r}, [ass] [q := q+1]^5: AV5 -> AV5 \cup {q} [ass] ___________________________________ [seq] ([r := r-y]^4;[q := q+1]^5) : AV4 -> AV6 if AV6 = AV5 \cup {q} AV5 = AV4 \cup {r} ___________________________________[sub] ([r := r-y]^4;[q := q+1]^5) : AV6 -> AV6 if AV6 \subseteq AV4 ___________________________________[wh] while [r >= y]^3 do ([r := r-y]^4;[q := q+1]^5) : AV6 -> AV6 so by the seq rule, ------------------------------------------ ([q := 0]^1;[r := x]^2); while [r >= y]^3 do ([r := r-y]^4;[q := q+1]^5) : AV1 -> AV6 if AV3 = AV6 And with seq again (([q := 0]^1;[r := x]^2); while [r >= y]^3 do ([r := r-y]^4;[q := q+1]^5)); assert [0<=r and r AV7 if AV6 = AV7 Q: So what are all the constraints ------------------------------------------ CONSTRAINTS AV2 = AV1 \cup {q} AV3 = AV2 \cup {r}, AV5 = AV4 \cup {r} AV6 = AV5 \cup {q} AV6 \subseteq AV4 AV3 = AV6 AV6 = AV7 ------------------------------------------ Q: So, what's a solution? If A1 = {}, we have A2 = {q} A3 = {q,r} A6 = {q,r} A4 = {q,r} A5 = {q,r} A7 = {q,r} Can solve constraints as previously. Can accumulate constraints in different orders. *** annotated type constructors Ideas: - express modification to analysis results (deltas) - annotate the type constructors instead of using analysis results as the types. - simultaneously do definitely/possibly (must/may) analyses ------------------------------------------ EXAMPLE Judgments: XMust S : Sigma -------------> Sigma YMay Where XMust and YMay are sets of variables. {x} [ass] [x := a]^l : Sigma ----> Sigma {x} {} [skip] [skip]^l : Sigma -----> Sigma {} X1 S1 : Sigma -----> Sigma, Y1 X2 S2 : Sigma -----> Sigma Y2 [seq] -------------------------------- X3 S1; S2 : Sigma ----> Sigma Y3 if X3 = X1 \cup X2, Y3 = Y1 \cup Y2 X1 S1 : Sigma -----> Sigma, Y1 X2 S2 : Sigma -----> Sigma Y2 [if] -------------------------------- if [b]^l then S1 else S2 X3 : Sigma ----> Sigma Y3 if X3 = X1 \cap X2, Y3 = Y1 \cup Y2 X S : Sigma -----> Sigma Y [wh] -------------------------------- while [b]^l then S {} : Sigma ----> Sigma Y X S : Sigma ----> Sigma Y [sub] ---------------------- X' S : Sigma -----> Sigma Y' if X' \subseteq X, Y \subseteq Y' ------------------------------------------ Q: Why does the assignment rule make sense? Q: How do you explain the if rule? Q: How do you explain the while rule? Q: How would you deal with assert statements? Q: How would you deal with a try statement? ------------------------------------------ TYPE CHECKING EXAMPLE TYPE CHECKING Idea: accumulate constraints. {q} [q := 0]^1: Sigma ---> Sigma , [ass] {q} {r} [r := x]^2: Sigma ---> Sigma [ass] {r} _________________________________ [seq] ([q := 0]^1;[r := x]^2) {q,r} : Sigma ----> Sigma {q,r} {r} [r := r-y]^4: Sigma ---> Sigma, [ass] {r} {q} [q := q+1]^5: Sigma ---> Sigma [ass] {q} _________________________________ [seq] ([r := r-y]^4;[q := q+1]^5) {q,r} : Sigma ----> Sigma {q,r} ___________________________________[wh] while [r >= y]^3 do ([r := r-y]^4;[q := q+1]^5) {} : Sigma ----> Sigma {q,r} so by the seq rule, ------------------------------------------ ([q := 0]^1;[r := x]^2); while [r >= y]^3 do ([r := r-y]^4;[q := q+1]^5) {q,r} : Sigma ----> Sigma {q,r} And with seq again (([q := 0]^1;[r := x]^2); while [r >= y]^3 do ([r := r-y]^4;[q := q+1]^5)); assert [0<=r and r Sigma {q,r} Q: How is this different than with annotated base types? It presents the changes (deltas) instead of the accumulations. This is "higher-order". ** effect systems These are similar to im annotated base types, except that in functional languages, we have latent effects associated with functions. *** example: call tracking analysis we want to know what functions may be called by a given expression ------------------------------------------ EXAMPLE Judgments: Gamma |- e : t & phi where Gamma : Var -> Type e : Expression t : Type phi : Effect phi Type = int | bool | t1 ---> t2 phi : Powerset(FunName) [var] Gamma |- x : t & {}, if Gamma(x) = t Gamma[x |-> tx] |- e : t & phi [fn] -------------------------------- Gamma |- fn_pi x => e phi2 : tx ------> t & {} if phi2 = phi \cup {pi} phi Gamma |- e1 : t2 ---> t & phi1, Gamma |- e2 : t2 & phi2 [app] -------------------------------- Gamma |- e1 e2 : t & phi3 if phi3 = phi1 \cup phi2 \cup phi ------------------------------------------ Q: What do the judgments mean? Q: What do the arrow types mean? Q: How would you explain the fn rule? Q: How would you explain the app rule? One advantage of this is that "effect systems are often implemented as extensions of type inference algorithms."