Transcript

Semantik und Analysevon funktionalen Programmen

Aufgabenblatt 4

Wintersemester 2007/08

Aufgabe 1Aufgabe 2Aufgabe 3

Aufgabe 1a

> data EXPR = Variable Var

> | Lambda Var EXPR

> | App EXPR EXPR

> | CTrue

> | CFalse

> | CCons EXPR EXPR

> | CNil

> | CPaar EXPR EXPR

> | CaseBool EXPR [Alt]

> | CaseList EXPR [Alt]

> | CasePaar EXPR [Alt]

> | Superkombinator SKName

> | Bot

> | Top

> deriving(Show,Eq)

> type Alt = (Pat,EXPR)

> type Pat = EXPR

> type Var = String

> type SKName = String

Professur KIST, WS 07/08 SAFP, Blatt 4

Aufgabe 1Aufgabe 2Aufgabe 3

Aufgabe 1b

N-arer Baum

> data NTree edge node = Leaf node

> | Node node [(edge, (NTree edge node))]

> deriving (Eq,Show)

Kalkulregeln

> data KalRegel = Reduktion NoReduktion

> | BotReduktion

> | CaseTop

> | Generalisierung

> deriving (Eq,Show)

> type NoReduktion = Bool

Tableau

> type Tableau = NTree KalRegel EXPR

Professur KIST, WS 07/08 SAFP, Blatt 4

Aufgabe 1Aufgabe 2Aufgabe 3

Aufgabe 1c

Tableau geschlossen?

> isClosed :: Tableau -> Bool

> isClosed tableau =

> all isClosedPath (allPathes tableau)

Ein Pfad geschlossen?

> isClosedPath (w,[])= isBot w

> isClosedPath (w,p) =

> let blatt = snd (last p)

> in isBot blatt -- Bot am Blatt

> || hasLoop blatt w

> isBot Bot = True

> isBot = False

Alle Pfade berechnen

> allPathes :: NTree e n -> [(n,[(e,n)])]

> allPathes (Leaf node) = [(node,[])]

> allPathes (Node node children) =

> concatMap

> (\(e,n) -> map

> (\(n,list) -> (node,(e,n):list))

> (allPathes n))

> children

Schleifenerkennung

> hasLoop blatt n [] = False

> hasLoop blatt n p@((e,n2):rest)

> | n == blatt = noRedExists p

> | otherwise = hasLoop blatt n2 rest

> noRedExists p = any isNoRed (map fst p)

> isNoRed (Reduktion a) = a

> isNoRed = False

Professur KIST, WS 07/08 SAFP, Blatt 4

Aufgabe 1Aufgabe 2Aufgabe 3

Aufgabe 2

g x y = caseBool x of

True -> g False x

False -> caseBool y of

False -> True

True -> caseBool (g x False) of

True -> False

False -> True

Analyse fur das erste Argument:

Professur KIST, WS 07/08 SAFP, Blatt 4

Aufgabe 1Aufgabe 2Aufgabe 3

Aufgabe 2

g x y = case1 x of

True -> g False x

False -> case2 y of

False -> True

True -> case3 (g x False) of

True -> False

False -> True

Analyse fur das erste Argument:

Professur KIST, WS 07/08 SAFP, Blatt 4

Aufgabe 1Aufgabe 2Aufgabe 3

Aufgabe 2

g x y = case1 x of

True -> g False x

False -> case2 y of

False -> True

True -> case3 (g x False) of

True -> False

False -> True

Analyse fur das erste Argument:

g ⊥ >

Professur KIST, WS 07/08 SAFP, Blatt 4

Aufgabe 1Aufgabe 2Aufgabe 3

Aufgabe 2

g x y = case1 x of

True -> g False x

False -> case2 y of

False -> True

True -> case3 (g x False) of

True -> False

False -> True

Analyse fur das erste Argument:

g ⊥ >

NO-Reduktion��

case1 ⊥ of . . .

Professur KIST, WS 07/08 SAFP, Blatt 4

Aufgabe 1Aufgabe 2Aufgabe 3

Aufgabe 2

g x y = case1 x of

True -> g False x

False -> case2 y of

False -> True

True -> case3 (g x False) of

True -> False

False -> True

Analyse fur das erste Argument:

g ⊥ >

NO-Reduktion��

case1 ⊥ of . . .

��⊥

Professur KIST, WS 07/08 SAFP, Blatt 4

Aufgabe 1Aufgabe 2Aufgabe 3

Aufgabe 2

Analyse fur das zweite Argument:

g > ⊥

Professur KIST, WS 07/08 SAFP, Blatt 4

g x y =

case1 x of

True -> g False x

False -> case2 y of

False -> True

True -> case3 (g x False) of

True -> False

False -> True

Aufgabe 1Aufgabe 2Aufgabe 3

Aufgabe 2

Analyse fur das zweite Argument:

g > ⊥

NO-Reduktion��case1 > of . . .

Professur KIST, WS 07/08 SAFP, Blatt 4

g x y =

case1 x of

True -> g False x

False -> case2 y of

False -> True

True -> case3 (g x False) of

True -> False

False -> True

Aufgabe 1Aufgabe 2Aufgabe 3

Aufgabe 2

Analyse fur das zweite Argument:

g > ⊥

NO-Reduktion��case1 > of . . .

**TTTTTTTT��xxpppppp

⊥ case1 True of . . . case1 False of . . .

Professur KIST, WS 07/08 SAFP, Blatt 4

g x y =

case1 x of

True -> g False x

False -> case2 y of

False -> True

True -> case3 (g x False) of

True -> False

False -> True

Aufgabe 1Aufgabe 2Aufgabe 3

Aufgabe 2

Analyse fur das zweite Argument:

g > ⊥

NO-Reduktion��case1 > of . . .

**TTTTTTTT��xxpppppp

⊥ case1 True of . . .

NO-Reduktion��

case1 False of . . .

NO-Reduktion��g False > case2 ⊥ of . . .

Professur KIST, WS 07/08 SAFP, Blatt 4

g x y =

case1 x of

True -> g False x

False -> case2 y of

False -> True

True -> case3 (g x False) of

True -> False

False -> True

Aufgabe 1Aufgabe 2Aufgabe 3

Aufgabe 2

Analyse fur das zweite Argument:

g > ⊥

NO-Reduktion��case1 > of . . .

**UUUUUUUU��wwooo

oooo

⊥ case1 True of . . .

NO-Reduktion��

case1 False of . . .

NO-Reduktion��g False >

NO-Reduktion��

case2 ⊥ of . . .

��case1 False of . . . ⊥

Professur KIST, WS 07/08 SAFP, Blatt 4

g x y =

case1 x of

True -> g False x

False -> case2 y of

False -> True

True -> case3 (g x False) of

True -> False

False -> True

Aufgabe 1Aufgabe 2Aufgabe 3

Aufgabe 2

Analyse fur das zweite Argument:

g > ⊥

NO-Reduktion��case1 > of . . .

**UUUUUUUU��wwooo

oooo

⊥ case1 True of . . .

NO-Reduktion��

case1 False of . . .

NO-Reduktion��g False >

NO-Reduktion��

case2 ⊥ of . . .

��case1 False of . . .

NO-Reduktion��

case2 > of . . .

Professur KIST, WS 07/08 SAFP, Blatt 4

g x y =

case1 x of

True -> g False x

False -> case2 y of

False -> True

True -> case3 (g x False) of

True -> False

False -> True

Aufgabe 1Aufgabe 2Aufgabe 3

Aufgabe 2

Analyse fur das zweite Argument:

g > ⊥

NO-Reduktion��case1 > of . . .

,,YYYYYYYYYYYYYYY��vvmmmmmmm

⊥ case1 True of . . .

NO-Reduktion��

case1 False of . . .

NO-Reduktion��g False >

NO-Reduktion��

case2 ⊥ of . . .

��case1 False of . . .

NO-Reduktion��

case2 > of . . .

))TTTTTTT��xxrrr

rrr

⊥ case2 True of . . . case2 False of . . .

Professur KIST, WS 07/08 SAFP, Blatt 4

g x y =

case1 x of

True -> g False x

False -> case2 y of

False -> True

True -> case3 (g x False) of

True -> False

False -> True

Aufgabe 1Aufgabe 2Aufgabe 3

Aufgabe 2

Analyse fur das zweite Argument:

g > ⊥

NO-Reduktion��case1 > of . . .

,,YYYYYYYYYYYYYYY��vvmmmmmmm

⊥ case1 True of . . .

NO-Reduktion��

case1 False of . . .

NO-Reduktion��g False >

NO-Reduktion��

case2 ⊥ of . . .

��case1 False of . . .

NO-Reduktion��

case2 > of . . .

))TTTTTTT��xxrrr

rrr

⊥ case2 True of . . .

NO-Reduktion��

case2 False of . . .

NO-Reduktion��. . . True

Professur KIST, WS 07/08 SAFP, Blatt 4

g x y =

case1 x of

True -> g False x

False -> case2 y of

False -> True

True -> case3 (g x False) of

True -> False

False -> True

Aufgabe 1Aufgabe 2Aufgabe 3

Aufgabe 2

Analyse fur das zweite Argument:

g > ⊥

NO-Reduktion��case1 > of . . .

,,YYYYYYYYYYYYYYY��vvmmmmmmm

⊥ case1 True of . . .

NO-Reduktion��

case1 False of . . .

NO-Reduktion��g False >

NO-Reduktion��

case2 ⊥ of . . .

��case1 False of . . .

NO-Reduktion��

case2 > of . . .

))TTTTTTT��xxrrr

rrr

⊥ case2 True of . . .

NO-Reduktion��

case2 False of . . .

NO-Reduktion��. . . True

Professur KIST, WS 07/08 SAFP, Blatt 4

Striktheit kann nicht mehrgefunden werden!

g x y =

case1 x of

True -> g False x

False -> case2 y of

False -> True

True -> case3 (g x False) of

True -> False

False -> True

Aufgabe 1Aufgabe 2Aufgabe 3

Aufgabe 3

Analyse fur das erste Argument:

f ⊥ >

Professur KIST, WS 07/08 SAFP, Blatt 4

f x y =

case1 x of

Zero -> y

Succ x’ -> case2 x’ of

Zero -> f (Pred x’) y

Succ x’’ -> f x’’ y

Pred x’’ -> f x y

Pred x’ -> f (Pred (Pred x)) y

Aufgabe 1Aufgabe 2Aufgabe 3

Aufgabe 3

Analyse fur das erste Argument:

f ⊥ >

NO-Reduktion

��case1 ⊥ of . . .

Professur KIST, WS 07/08 SAFP, Blatt 4

f x y =

case1 x of

Zero -> y

Succ x’ -> case2 x’ of

Zero -> f (Pred x’) y

Succ x’’ -> f x’’ y

Pred x’’ -> f x y

Pred x’ -> f (Pred (Pred x)) y

Aufgabe 1Aufgabe 2Aufgabe 3

Aufgabe 3

Analyse fur das erste Argument:

f ⊥ >

NO-Reduktion

��case1 ⊥ of . . .

��⊥

Professur KIST, WS 07/08 SAFP, Blatt 4

f x y =

case1 x of

Zero -> y

Succ x’ -> case2 x’ of

Zero -> f (Pred x’) y

Succ x’’ -> f x’’ y

Pred x’’ -> f x y

Pred x’ -> f (Pred (Pred x)) y

Aufgabe 1Aufgabe 2Aufgabe 3

Aufgabe 3

Analyse fur das zweite Argument:

f > ⊥

Professur KIST, WS 07/08 SAFP, Blatt 4

f x y =

case1 x of

Zero -> y

Succ x’ -> case2 x’ of

Zero -> f (Pred x’) y

Succ x’’ -> f x’’ y

Pred x’’ -> f x y

Pred x’ -> f (Pred (Pred x)) y

Aufgabe 1Aufgabe 2Aufgabe 3

Aufgabe 3

Analyse fur das zweite Argument:

f > ⊥

NO-Red.��case1 > of. . .

Professur KIST, WS 07/08 SAFP, Blatt 4

f x y =

case1 x of

Zero -> y

Succ x’ -> case2 x’ of

Zero -> f (Pred x’) y

Succ x’’ -> f x’’ y

Pred x’’ -> f x y

Pred x’ -> f (Pred (Pred x)) y

Aufgabe 1Aufgabe 2Aufgabe 3

Aufgabe 3

Analyse fur das zweite Argument:

f > ⊥

NO-Red.��case1 > of. . .

ssgggggggggggggguukkkkkkk

�� **VVVVVVVVV

⊥ case1 Zero of. . . case1 Succ > of. . . case1 Pred > of. . .

Professur KIST, WS 07/08 SAFP, Blatt 4

f x y =

case1 x of

Zero -> y

Succ x’ -> case2 x’ of

Zero -> f (Pred x’) y

Succ x’’ -> f x’’ y

Pred x’’ -> f x y

Pred x’ -> f (Pred (Pred x)) y

Aufgabe 1Aufgabe 2Aufgabe 3

Aufgabe 3

Analyse fur das zweite Argument:

f > ⊥

NO-Red.��case1 > of. . .

ssgggggggggggggguukkkkkkk

�� ++VVVVVVVVV

⊥ case1 Zero of. . .

NO-Red.��

case1 Succ > of. . .

NO-Red.��

case1 Pred > of. . .

NO-Red.��⊥ case2 > of. . . f (Pred (Pred >)) ⊥

Professur KIST, WS 07/08 SAFP, Blatt 4

f x y =

case1 x of

Zero -> y

Succ x’ -> case2 x’ of

Zero -> f (Pred x’) y

Succ x’’ -> f x’’ y

Pred x’’ -> f x y

Pred x’ -> f (Pred (Pred x)) y

Aufgabe 1Aufgabe 2Aufgabe 3

Aufgabe 3

Analyse fur das zweite Argument:

f > ⊥

NO-Red.��case1 > of. . .

ssgggggggggggggguukkkkkkk

�� --[[[[[[[[[[[[[[[[[[[[[[[[[[[[[

⊥ case1 Zero of. . .

NO-Red.��

case1 Succ > of. . .

NO-Red.��

case1 Pred > of. . .

NO-Red.��⊥ case2 > of. . .

uullllllllll�� ))SSSSSSSS

,,YYYYYYYYYYYYYYYY f (Pred (Pred >)) ⊥

Gen.��⊥ case2 Zero of. . . case2 Succ > of. . . case2 Pred > of. . . f > ⊥

Schleife

Professur KIST, WS 07/08 SAFP, Blatt 4

f x y =

case1 x of

Zero -> y

Succ x’ -> case2 x’ of

Zero -> f (Pred x’) y

Succ x’’ -> f x’’ y

Pred x’’ -> f x y

Pred x’ -> f (Pred (Pred x)) y

Aufgabe 1Aufgabe 2Aufgabe 3

Aufgabe 3

Analyse fur das zweite Argument:

f > ⊥

NO-Red.��case1 > of. . .

ssgggggggggggggguukkkkkkk

�� --[[[[[[[[[[[[[[[[[[[[[[[[[[[[[

⊥ case1 Zero of. . .

NO-Red.��

case1 Succ > of. . .

NO-Red.��

case1 Pred > of. . .

NO-Red.��⊥ case2 > of. . .

uullllllllll�� ))SSSSSSSS

,,YYYYYYYYYYYYYYYY f (Pred (Pred >)) ⊥

Gen.��⊥ case2 Zero of. . .

NO-Red.��

case2 Succ > of. . .

NO-Red.��

case2 Pred > of. . .

NO-Red.��

f > ⊥Schleife

f (Pred >) ⊥ f > ⊥Schleife

f > ⊥Schleife

Professur KIST, WS 07/08 SAFP, Blatt 4

f x y =

case1 x of

Zero -> y

Succ x’ -> case2 x’ of

Zero -> f (Pred x’) y

Succ x’’ -> f x’’ y

Pred x’’ -> f x y

Pred x’ -> f (Pred (Pred x)) y

Aufgabe 1Aufgabe 2Aufgabe 3

Aufgabe 3

Analyse fur das zweite Argument:

f > ⊥

NO-Red.��case1 > of. . .

ssgggggggggggggguukkkkkkk

�� --[[[[[[[[[[[[[[[[[[[[[[[[[[[[[

⊥ case1 Zero of. . .

NO-Red.��

case1 Succ > of. . .

NO-Red.��

case1 Pred > of. . .

NO-Red.��⊥ case2 > of. . .

uullllllllll�� ))SSSSSSSS

,,YYYYYYYYYYYYYYYY f (Pred (Pred >)) ⊥

Gen.��⊥ case2 Zero of. . .

NO-Red.��

case2 Succ > of. . .

NO-Red.��

case2 Pred > of. . .

NO-Red.��

f > ⊥Schleife

f (Pred >) ⊥

Gen.��

f > ⊥Schleife

f > ⊥Schleife

f > ⊥Schleife

Professur KIST, WS 07/08 SAFP, Blatt 4

f x y =

case1 x of

Zero -> y

Succ x’ -> case2 x’ of

Zero -> f (Pred x’) y

Succ x’’ -> f x’’ y

Pred x’’ -> f x y

Pred x’ -> f (Pred (Pred x)) y

Aufgabe 1Aufgabe 2Aufgabe 3

Erweiterter Striktheitskalkul

Subsumptionsordnung

1 s ≤sub s fur alle s.

2 s ≤sub > fur alle s.

3 ⊥ ≤sub s fur alle s.

4 Sei C[] ein Kontext und s ≤sub t. Dann soll auch C[s] ≤sub C[t] gelten.

Professur KIST, WS 07/08 SAFP, Blatt 4

Aufgabe 1Aufgabe 2Aufgabe 3

Erweiterter Striktheitskalkul

Regeln

(Improvement-Anwendung): An Blatt mit Markierung t kann Blatt mitMarkierung t′ angehangt werden, wenn t′ � t

Zusatzliche Abschlussregeln:

Blatt B mit Markierung t ist abgeschlossen, wenn es einenVorganger N mit Markierung t′ gibt und t ≤sub t′, und wennzwischen N und B eine abstrakte N.O.-ReduktionDas Blatt B mit Markierung tB ist abgeschlossen, wenn es einenVorganger N mit Markierung tN gibt und einen echten Untertermt′B von tB , so dass t′B in einem Reduktionskontext ist (Erweiterung:an einer strikten Position). D.h. t′B wird auf jeden Fall reduziert,wenn tB zu WHNF reduziert wird. Zusatzlich muss t′B ≤sub tN sein.

Die case-Fallunterscheidung wird eingeschrankt: der ⊥-Fall muss nichtmehr betrachtet werden, er wird durch die Subsumtionsordnung erledigt:er ist immer redundant.

Professur KIST, WS 07/08 SAFP, Blatt 4

Aufgabe 1Aufgabe 2Aufgabe 3

Erweiterte Abstrakte Werte - Tailstriktheit

Inf := 〈⊥,> : inf〉

Zerlegung

C[Inf ]

&&MMMMMMMMMM

zzuuuuuuuuu

C[⊥] C[> : Inf ]

nur sinvoll, wenn C = case [·] . . .

Professur KIST, WS 07/08 SAFP, Blatt 4

Aufgabe 1Aufgabe 2Aufgabe 3

Erweiterte Abstrakte Werte - Tailstriktheit

Inf := 〈⊥,> : inf〉

Zerlegung

C[Inf ]

&&MMMMMMMMMM

zzuuuuuuuuu

C[⊥] C[> : Inf ]

nur sinvoll, wenn C = case [·] . . .

Professur KIST, WS 07/08 SAFP, Blatt 4

Aufgabe 1Aufgabe 2Aufgabe 3

Erweiterte Abstrakte Werte - Hyperstriktheit

HS := 〈⊥, c1 HS > . . . >, c1 > HS, . . . >, . . . , c1 > . . . > HS. . .cn HS > . . . >, cn > HS, . . . >, . . . , cn > . . . > HS〉

Zur Vereinfachung bei der Zerlegung, betrachten wir ungetypte Falle nicht:

caseList HS of. . .

**UUUUUUUUUUUUUUUU

uukkkkkkkkkkkkkkk

��caseList ⊥ of. . . caseList HS : > of. . . caseList > : HS of. . .

Professur KIST, WS 07/08 SAFP, Blatt 4


Recommended