Typinferenzalgorithmus
Typinferenzalgorithmus
mit Kontrollflussanalyse
Robert Kufner
26.06.2009
Typinferenzalgorithmus
Inhaltsverzeichnis
1 Der WU L Algorithmus Idee
Beispiel
2 Der UU LAlgorithmus Idee
Beispiel
3 CFA Algorithmus Kontrollflussanalyse Unification
Beispiel
Constraints
Typinferenzalgorithmus
Unterschiede zwischen τ und ˆ τ
Dekl. Typsystem Algorithmus ULτ Typen:
Keine Variablen od. Annotationen
erweiterte Typen: Variablen aber keine Annotationen CFA ˆτ Annotierte Typen:
keine Variablen aber aber Annotata- tionen
Einfache Typen: Variablen & Annota- tionsvariablen
Typinferenzalgorithmus
Unterschiede zwischen τ und ˆ τ
Dekl. Typsystem Algorithmus ULτ Typen:
Keine Variablen od. Annotationen
erweiterte Typen:
Variablen aber keine Annotationen CFA ˆτ Annotierte Typen:
keine Variablen aber aber Annotata- tionen
Einfache Typen:
Variablen & Annota- tionsvariablen
Typinferenzalgorithmus DerWU LAlgorithmus
Idee
Algorithmus erh¨alt Typumgebung Γ und Ausdruck e
Falls erfolgreich: Liefert einen erweiterten Typτ und Substitution θ
Typinferenzalgorithmus DerWU LAlgorithmus
Idee
Algorithmus erh¨alt Typumgebung Γ und Ausdruck e Falls erfolgreich: Liefert einen erweiterten Typτ und Substitution θ
Typinferenzalgorithmus DerWU LAlgorithmus
Idee
Auszug aus den Spezifikationen
WU L(Γ, c) = (τc, id) WU L(Γ, x) = (Γ(x), id) WU L(Γ, f nπx⇒e) = let αx be fresh
(τ, θ) =WU L(Γ[x7→αx], e) in ((θαx)→τ, θ) WU L(Γ, f unπf x⇒e0) = let αx, α0 be fresh
(τ0, θ0) =WU L(Γ[f 7→αx →α0][x7→αx], e0) θ1=UU L(τ0, θ0α0)
in (θ1(θ0αx)→θ1τ0, θ1◦θ0)
Typinferenzalgorithmus DerWU LAlgorithmus
Idee
Auszug aus den Spezifikationen
WU L(Γ, e1e2) = let (τ1, θ1) =WU L(Γ, e1) (τ2, θ2) =WU L(θ1Γ, e2) α be fresh
θ3=UU L(θ2τ1, τ2→α) in (θ3α, θ3◦θ2◦θ1)
Typinferenzalgorithmus DerWU LAlgorithmus
Beispiel
e = (f n X x ⇒ x) ( f n Y y ⇒ y)
Welche Teile vonWU L werden aufgerufen?
1 WU L([ ], (f nX x⇒x) (f nY y⇒ y))
2 WU L([ ], f nX x⇒ x)
3 WU L([ ], f nY y⇒ y)
4 UU L(´a→´a, (´b →´b)→´c)
Typinferenzalgorithmus DerWU LAlgorithmus
Beispiel
e = (f n X x ⇒ x) ( f n Y y ⇒ y)
Welche Teile vonWU L werden aufgerufen?
1 WU L([ ], (f nX x⇒x) (f nY y⇒ y))
2 WU L([ ], f nX x⇒ x)
3 WU L([ ], f nY y⇒ y)
4 UU L(´a→´a, (´b →´b)→´c)
Typinferenzalgorithmus DerWU LAlgorithmus
Beispiel
e = (f n X x ⇒ x) ( f n Y y ⇒ y)
Welche Teile vonWU L werden aufgerufen?
1 WU L([ ], (f nX x⇒x) (f nY y⇒ y))
2 WU L([ ], f nX x⇒ x)
3 WU L([ ], f nY y⇒ y)
4 UU L(´a→´a, (´b →´b)→´c)
Typinferenzalgorithmus DerWU LAlgorithmus
Beispiel
e = (f n X x ⇒ x) ( f n Y y ⇒ y)
Welche Teile vonWU L werden aufgerufen?
1 WU L([ ], (f nX x⇒x) (f nY y⇒ y))
2 WU L([ ], f nX x⇒ x)
3 WU L([ ], f nY y⇒ y)
4 UU L(´a→´a, (´b →´b)→´c)
Typinferenzalgorithmus DerWU LAlgorithmus
Beispiel
e = (f n X x ⇒ x) ( f n Y y ⇒ y)
Welche Teile vonWU L werden aufgerufen?
1 WU L([ ], (f nX x⇒x) (f nY y⇒ y))
2 WU L([ ], f nX x⇒ x)
3 WU L([ ], f nY y⇒ y)
4 UU L(´a→´a, (´b →´b) →´c)
Typinferenzalgorithmus DerUU LAlgorithmus
Idee
Algorithmus erh¨alt zwei erweiterte Typen τ1 und τ2
Gibt Art der Substitution zur¨uck falls erfolgreich
Bricht ab bei top-level Konstruktoren (Hier: int, bool, →) oder wenn versucht wird eine Typvariable mit einem Funktionstyp zu vereinigen der die Typvariable enth¨alt
Typinferenzalgorithmus DerUU LAlgorithmus
Idee
Algorithmus erh¨alt zwei erweiterte Typen τ1 und τ2
Gibt Art der Substitution zur¨uck falls erfolgreich
Bricht ab bei top-level Konstruktoren (Hier: int, bool, →) oder wenn versucht wird eine Typvariable mit einem Funktionstyp zu vereinigen der die Typvariable enth¨alt
Typinferenzalgorithmus DerUU LAlgorithmus
Idee
Algorithmus erh¨alt zwei erweiterte Typen τ1 und τ2
Gibt Art der Substitution zur¨uck falls erfolgreich
Bricht ab bei top-level Konstruktoren (Hier: int, bool, →) oder wenn versucht wird eine Typvariable mit einem Funktionstyp zu vereinigen der die Typvariable enth¨alt
Typinferenzalgorithmus DerUU LAlgorithmus
Idee
Auszug aus den Spezifikationen
UU L(τ1 →τ2, τ10 →τ20) = let θ1 =UU L(τ1, τ10) θ2 =UU L(θ1τ2, θ1τ20) inθ2◦θ1
UU L(τ, α) =
( [α7→τ] fallsαnicht inτ auftaucht oderα=τ ist
fail sonst UU L(α, τ) = s.o.
UU L(τ1, τ2) = fail
Typinferenzalgorithmus DerUU LAlgorithmus
Beispiel
Beispiel von zuvor: e = (f nX x⇒ x) (f nY y⇒ y)
=UU L(´a→´a, ( ´b→´b) →´c)
Welche Teile vonUU L werden aufgerufen und was wird zur¨uckgegeben?
1 UU L(´a→´a, ( ´b →´b) →´c)
2 UU L(´a, ´b→´b) = [´a7→´b→´b]
3 UU L(´b→´b,´c) = [´c7→´b→´b]
Endergebnis: [´a7→ ´b→ ´b][´c7→ ´b→ ´b]
Typinferenzalgorithmus DerUU LAlgorithmus
Beispiel
Beispiel von zuvor: e = (f nX x⇒ x) (f nY y⇒ y)
=UU L(´a→´a, ( ´b→´b) →´c)
Welche Teile vonUU L werden aufgerufen und was wird zur¨uckgegeben?
1 UU L(´a→´a, ( ´b→´b) →´c)
2 UU L(´a, ´b→´b) = [´a7→´b→´b]
3 UU L(´b→´b,´c) = [´c7→´b→´b]
Endergebnis: [´a7→ ´b→ ´b][´c7→ ´b→ ´b]
Typinferenzalgorithmus DerUU LAlgorithmus
Beispiel
Beispiel von zuvor: e = (f nX x⇒ x) (f nY y⇒ y)
=UU L(´a→´a, ( ´b→´b) →´c)
Welche Teile vonUU L werden aufgerufen und was wird zur¨uckgegeben?
1 UU L(´a→´a, ( ´b→´b) →´c)
2 UU L(´a, ´b→´b) = [´a7→´b→´b]
3 UU L(´b→´b,´c) = [´c7→´b→´b]
Endergebnis: [´a7→ ´b→ ´b][´c7→ ´b→ ´b]
Typinferenzalgorithmus DerUU LAlgorithmus
Beispiel
Beispiel von zuvor: e = (f nX x⇒ x) (f nY y⇒ y)
=UU L(´a→´a, ( ´b→´b) →´c)
Welche Teile vonUU L werden aufgerufen und was wird zur¨uckgegeben?
1 UU L(´a→´a, ( ´b→´b) →´c)
2 UU L(´a, ´b→´b) = [´a7→´b→´b]
3 UU L(´b→´b,´c) = [´c7→´b→´b]
Endergebnis: [´a7→ ´b→ ´b][´c7→ ´b→ ´b]
Typinferenzalgorithmus DerUU LAlgorithmus
Beispiel
Beispiel von zuvor: e = (f nX x⇒ x) (f nY y⇒ y)
=UU L(´a→´a, ( ´b→´b) →´c)
Welche Teile vonUU L werden aufgerufen und was wird zur¨uckgegeben?
1 UU L(´a→´a, ( ´b→´b) →´c)
2 UU L(´a, ´b→´b) = [´a7→´b→´b]
3 UU L(´b→´b,´c) = [´c7→´b→´b]
Typinferenzalgorithmus CFA Algorithmus
Kontrollflussanalyse
Was ist Kontrollflussanalyse ?
Kontrollflussanalyse stellt folgende Kontrollflussanomalien im Code fest:
nicht erreichbarer Quelltext duplizierter Quelltext GOTO-Anweisungen
Typinferenzalgorithmus CFA Algorithmus
Kontrollflussanalyse
Was ist Kontrollflussanalyse ?
Kontrollflussanalyse stellt folgende Kontrollflussanomalien im Code fest:
nicht erreichbarer Quelltext duplizierter Quelltext GOTO-Anweisungen
Typinferenzalgorithmus CFA Algorithmus
Kontrollflussanalyse
Was ist Kontrollflussanalyse ?
Kontrollflussanalyse stellt folgende Kontrollflussanomalien im Code fest:
nicht erreichbarer Quelltext
duplizierter Quelltext GOTO-Anweisungen
Typinferenzalgorithmus CFA Algorithmus
Kontrollflussanalyse
Was ist Kontrollflussanalyse ?
Kontrollflussanalyse stellt folgende Kontrollflussanomalien im Code fest:
nicht erreichbarer Quelltext duplizierter Quelltext
GOTO-Anweisungen
Typinferenzalgorithmus CFA Algorithmus
Kontrollflussanalyse
Was ist Kontrollflussanalyse ?
Kontrollflussanalyse stellt folgende Kontrollflussanomalien im Code fest:
nicht erreichbarer Quelltext duplizierter Quelltext GOTO-Anweisungen
Typinferenzalgorithmus CFA Algorithmus
Kontrollflussanalyse
Wie implementieren wir eine
Kontrollflussanalyse in unseren Algorithmus ?
Bisher :
WU L(f nXx⇒x)(f nYy ⇒y) = [´a7→´b→´b][´c7→´b→´b]
Jetzt :
WU L(f nXx⇒x)(f nYy ⇒y) = [´a7→´b−→´2 ´b][´c7→´b−→´2 ´b]
Typinferenzalgorithmus CFA Algorithmus
Kontrollflussanalyse
Wie implementieren wir eine
Kontrollflussanalyse in unseren Algorithmus ?
Bisher :
WU L(f nXx⇒x)(f nYy ⇒y) = [´a7→´b→´b][´c7→´b→´b]
Jetzt :
WU L(f nXx⇒x)(f nYy ⇒y) = [´a7→´b−→´2 ´b][´c7→´b−→´2 ´b]
Typinferenzalgorithmus CFA Algorithmus
Kontrollflussanalyse
Wie implementieren wir eine
Kontrollflussanalyse in unseren Algorithmus ?
Bisher :
WU L(f nXx⇒x)(f nYy⇒y) = [´a7→´b→´b][´c7→´b→´b]
Jetzt :
WU L(f nXx⇒x)(f nYy ⇒y) = [´a7→´b−→´2 ´b][´c7→´b−→´2 ´b]
Typinferenzalgorithmus CFA Algorithmus
Kontrollflussanalyse
Wie implementieren wir eine
Kontrollflussanalyse in unseren Algorithmus ?
Bisher :
WU L(f nXx⇒x)(f nYy⇒y) = [´a7→´b→´b][´c7→´b→´b]
Jetzt :
WU L(f nXx⇒x)(f nYy ⇒y) = [´a7→´b−→´2 ´b][´c7→´b−→´2 ´b]
Typinferenzalgorithmus CFA Algorithmus
Kontrollflussanalyse
Wie implementieren wir eine
Kontrollflussanalyse in unseren Algorithmus ?
Bisher :
WU L(f nXx⇒x)(f nYy⇒y) = [´a7→´b→´b][´c7→´b→´b]
Jetzt :
WU L(f nXx⇒x)(f nYy ⇒y) = [´a7→´b−→´2 ´b][´c7→´b−→´2 ´b]
Typinferenzalgorithmus CFA Algorithmus
Unification
Beispiel: U
CF A(´ a −
´1→ ´ a, (´ b −
´2→ ´ b) −
´3→´ c)
Aufruf Ergebnis
UCF A(´a−→´´1 a,(´b−→´2 ´b)−→´´3 c) [´37→´1] UCF A(´a,´b−→´2 ´b) [´a,´b−→´2 ´b] UCF A(´b−→´2 ´b,´c) [´c,´b−→´2 ´b]
Endergebnis: [´3 7→ ´1][´ a, ´ b −
´2→ ´ b][´ c, ´ b −
´2→ ´ b]
Typinferenzalgorithmus CFA Algorithmus
Unification
Beispiel: U
CF A(´ a −
´1→ ´ a, (´ b −
´2→ ´ b) −
´3→´ c)
Aufruf Ergebnis
UCF A(´a−→´´1 a,(´b−→´2 ´b)−→´´3 c) [´37→´1]
UCF A(´a,´b−→´2 ´b) [´a,´b−→´2 ´b]
UCF A(´b−→´2 ´b,´c) [´c,´b−→´2 ´b]
Endergebnis: [´3 7→ ´1][´ a, ´ b −
´2→ ´ b][´ c, ´ b −
´2→ ´ b]
Typinferenzalgorithmus CFA Algorithmus
Unification
Beispiel: U
CF A(´ a −
´1→ ´ a, (´ b −
´2→ ´ b) −
´3→´ c)
Aufruf Ergebnis
UCF A(´a−→´´1 a,(´b−→´2 ´b)−→´´3 c) [´37→´1]
UCF A(´a,´b−→´2 ´b) [´a,´b−→´2 ´b]
UCF A(´b−→´2 ´b,´c) [´c,´b−→´2 ´b]
Endergebnis: [´3 7→ ´1][´ a, ´ b −
´2→ ´ b][´ c, ´ b −
´2→ ´ b]
Typinferenzalgorithmus CFA Algorithmus
Constraints
Einf¨ uhrung der Constraints - aber warum ?
Problem:
einfache Typen :: = int|bool|τˆ1
−→β τˆ2 |α wobei :β::= ´1 |´2|´3|... annotierte Typen :: = int |bool|τˆ1
−→ϕ τˆ2
wobei :ϕ::={π} | ϕ1∪ϕ2 | ∅ | β
Deswegen: Einf¨uhrung von Constraints der Artβ ⊇ϕ
Typinferenzalgorithmus CFA Algorithmus
Constraints
Einf¨ uhrung der Constraints - aber warum ?
Problem:
einfache Typen :: = int|bool|τˆ1
−→β τˆ2 |α wobei :β::= ´1 |´2 |´3|...
annotierte Typen :: = int |bool|τˆ1
−→ϕ τˆ2
wobei :ϕ::={π} | ϕ1∪ϕ2 | ∅ | β
Deswegen: Einf¨uhrung von Constraints der Artβ ⊇ϕ
Typinferenzalgorithmus CFA Algorithmus
Constraints
Einf¨ uhrung der Constraints - aber warum ?
Problem:
einfache Typen :: = int|bool|τˆ1
−→β τˆ2 |α wobei :β::= ´1 |´2 |´3|...
annotierte Typen :: = int |bool|τˆ1
−→ϕ τˆ2
wobei :ϕ::={π} | ϕ1∪ϕ2 | ∅ | β
Typinferenzalgorithmus CFA Algorithmus
Der Algorithmus
Form: WCF A(ˆΓ, e) = (ˆτ , θ, C) Auszug aus den Spezifikationen
WCF A(ˆΓ, c) = (τc, id,∅) WCF A(ˆΓ, x) = (ˆΓ(x), id,∅) WCF A(ˆΓ, f nπx⇒e0) = let αx be fresh
( ˆτ0, θ0, C0) =WCF A(ˆΓ[x7→αx], e0) β0 be fresh
in ((θ0αx)−→β0 τ0, θ0, C0∪ {β0 ⊇ {π}})
Typinferenzalgorithmus CFA Algorithmus
Der Algorithmus
Auszug aus den Spezifikationen
WCF A(ˆΓ, f unπf x⇒e0) = letαx, α0, β0 be fresh ( ˆτ0, θ0, C0) =WCF A(ˆΓ[f 7→αx
β0
−→α0][x7→αx], e0) θ1=UCF A( ˆτ0, θ0α0)
in (θ1(θ0αx)−−−−−→θ1(θ0β0) θ1τˆ0, θ1◦θ0), (θ1C0)∪ {θ1(θ0β0)⊇ {π}})
WCF A(ˆΓ, e1e2) = let ( ˆτ1, θ1, C1) =WCF A(ˆΓ, e1) ( ˆτ2, θ2, C2) =WCF A(θ1Γ, eˆ 2)
α, β be fresh
Typinferenzalgorithmus CFA Algorithmus
Der Algorithmus
W
CF A([ ], (f n
Xx ⇒ x) ( f n
Yy ⇒ y))
Aufruf Effekt
WCF A([ ], (f nX x⇒ x)) (´a−→´a,´1 id,{´1⊇ {X}}) WCF A([ ], (f nY y⇒ y)) (´b−→´b,´2 id,{´2⊇ {Y}})
UCF A(´a−→´´1 a,(´b−→´2 ´b)−→´´3 c) [´37→´1][´a7→´b−→´2 ´b][´c7→´b−→´2 ´b]
Endergebnisse = Typ : ´b−→´2 ´b
Substitution : [´37→´1][´a7→´b−→´2 ´b][´c7→´b−→´2 ´b] Set :{´1⊇ {X},´2⊇ {Y}}
Typinferenzalgorithmus CFA Algorithmus
Der Algorithmus
W
CF A([ ], (f n
Xx ⇒ x) ( f n
Yy ⇒ y))
Aufruf Effekt
WCF A([ ], (f nX x⇒ x)) (´a−→´a,´1 id,{´1⊇ {X}}) WCF A([ ], (f nY y⇒y)) (´b−→´b,´2 id,{´2⊇ {Y}})
UCF A(´a−→´´1 a,(´b−→´2 ´b)−→´´3 c) [´37→´1][´a7→´b−→´2 ´b][´c7→´b−→´2 ´b]
Endergebnisse = Typ : ´b−→´2 ´b
Substitution : [´37→´1][´a7→´b−→´2 ´b][´c7→´b−→´2 ´b] Set :{´1⊇ {X},´2⊇ {Y}}
Typinferenzalgorithmus CFA Algorithmus
Der Algorithmus
W
CF A([ ], (f n
Xx ⇒ x) ( f n
Yy ⇒ y))
Aufruf Effekt
WCF A([ ], (f nX x⇒ x)) (´a−→´a,´1 id,{´1⊇ {X}}) WCF A([ ], (f nY y⇒y)) (´b−→´b,´2 id,{´2⊇ {Y}})
UCF A(´a−→´´1 a,(´b−→´2 ´b)−→´´3 c) [´37→´1][´a7→´b−→´2 ´b][´c7→´b−→´2 ´b]
Endergebnisse = Typ : ´b−→´2 ´b
Substitution : [´37→´1][´a7→´b−→´2 ´b][´c7→´b−→´2 ´b]
Set :{´1⊇ {X},´2⊇ {Y}}