Deel I: Functionele Programmeertalen

Post on 19-Mar-2016

54 views 6 download

description

Deel I: Functionele Programmeertalen. Hoofdstuk 5: Implementatie-technieken. @. @. Applicatie. f x. f x. . . Abstractie. v M. v M. :. Constructor. :. x y. x y. Constante. N. 25. 25. Operator. B. +. +. Veranderlijke. V. v. v. - PowerPoint PPT Presentation

Transcript of Deel I: Functionele Programmeertalen

Deel I: FunctioneleDeel I: FunctioneleProgrammeertalenProgrammeertalen

Hoofdstuk 5:Implementatie-

technieken

Voorstelling abstract concreet

Constante N 2525Operator B ++Veranderlijke V vv

Applicatie @@

f x

Abstractie

v M

f x

v M

Constructor :x y

:

x y

Syntaxisboom@

@

5@

+ 5

x @

@ x

* x

(x.x*x)(5+5)

Ingeblikte voorstelling

Oningeblikte voorstelling

Lazy uitvoering

Twee voorwaarden Argumenten van functies worden

slechts geëvalueerd op het ogenblik dat men de waarde werkelijk nodig heeft (soms ook call by need genoemd)

Argumenten van functies mogen ten hoogste eenmaal geëvalueerd worden

Lazy uitvoering

Strikte talen: ML en Hope

Lazy talen: SASL, KRC, LML, Miranda, Orwell, Ponder, Haskell en Hugs

Zwakke hoofdnormaalvorm

0 ,1 nEFE n

is, met F een veranderlijke, een gegevensobject, een ingebouwde functie, ofwel een -abstractie en zodanig dat FE1…Em geen -redex is voor m n

Een -expressie is in zwakke hoofdnormaalvorm als en slechts als ze van de vorm

Zwakke hoofdnormaalvorm

Gegevensobject: constante of constructor

Gevolg: ZHN: geen kopredices

Voorbeelden: v, 19, 3, y.x 3

Overzicht

-expressiereductie van kopredices

normale reductieordezwakke hoofdnormaalvorm

normaalvorm

Voorbeeld

])6,5,4[ ]3,2,1[ som( hdprog som:)():( ):( som

] [] [ ] [ som21212211

llaalala

Voorbeeld: normale reductievolgorde

5])))) [:9(:7(:5( hd])))) [ ] [ som(:9(:7(:5( hd

])))) [ ] [ som(:)63((:7(:5( hd])))6[ ]3[ som(:7(:5( hd

])))6[ ]3[ som(:)52((:5( hd]))6,5[ ]3,2[ som(:5( hd

]))6,5[ ]3,2[ som(:)41(( hd])6,5,4[ ]3,2,1[ som( hdprog

Voorbeeld: reductie van kopredices

541

]))6,5[ ]3,2[ som(:)41(( hd])6,5,4[ ]3,2,1[ som( hdprog

Kopredices@

Mn

@

M M1

@

M2

@

M3

Vier gevallen

M is een gegevensobject (constante, CONS-cel, enz)n 0: ZHNn 0: fatale fout

M is een ingebouwde functie met k argumentenk n: ZHNk n: kopredex MM1… Mk

Vier gevallen

M is een -abstractien 0: kopredex MM1

n 0: ZHNM is een veranderlijken 0: ZHNn 0: fatale fout

Ruggengraadstapel

Kopredices@

Mn

@

M M1

@

M2

@

M3

...

Reductie

Argumenten niet substitueren, maar verwijzingen gebruiken

Applicatieknopen worden vervangen door hun waarde

Graafreductie@

@

5@

+ 5

x @

@ x

* x

(x.x*x)(5+5)

Graafreductie@

@

5@

+ 5

x @

@ x

* x

(5+5)*(5+5)

@

*

Graafreductie@

10

5@

+ 5

x @

@ x

* x

10*10

@

*

Graafreductie100

10

5@

+ 5

x @

@ x

* x

@

*

Het overschrijven van de redex

Het overschrijven van de redex@

x

(x.x)(fa)

x

@

f a

@

f a

@@

f a

De SECD-machine

S: de evaluatiestapel, gebruikt om de tijdelijke resultaten van het evaluatieproces bij te houden

E: de omgeving die de waarden van de veranderlijken bijhoudt

C: controlestapel met de continuatiesD: de dump die gebruikt wordt om

gegevens te bewaren tijdens de oproep van een andere functie

De virtuele machine met vier stapels,

een graaf

G-programma

expressiestapel

een controlestapel

De G-machineVier gegevensstructuren

Voorbeeld

2))(.( fxfxf

PUSH 1 ; dupliceer fMKAP ; maak f xPUSH 1 ; dupliceer fMKAP ; maak f (f x)SLIDE 1 ; gooi argument f weg

Voorbeeld

fxffxf

f(fx)f(fx)

68030 Codemove.l (4,a6),-(a6) ; dupliceer f move.l (4,a6),-(a5) ; xmove.l (a6)+,-(a5) ; fmove.l AP,-(a5) ; typekentekenmove.l a5,(a6) ; overschrijf "x" move.l (4,a6),-(a6) ; dupliceer f

move.l (4,a6),-(a5) ; f xmove.l (a6)+,-(a5) ; fmove.l AP,-(a5) ; typekentekenmove.l a5,(a6) ; overschrijf "f x"

move.l (a6)+,(a6) ; overschrijf "f"

Voorbeeld

Optimale code

move.l (a6)+,-(a5) ; x, en gooi "x" wegmove.l (a6),-(a5) ; fmove.l AP,-(a5) ; typekentekenmove.l a5,-(a5) ; f xmove.l (a6),-(a5) ; fmove.l AP,-(a5) ; typekentekenmove.l a5,(a6) ; overschrijf "f” ; met "f (f x)"

Compilatieschema

Mxx n 1naam

GLOBSTART naam, n...UPDATE n+1POP nUNWIND

Stapelorganisatie

CompilatieGetallen PUSHINT getalCombinatoren en ingebouwde functies

PUSHGLOBAL naamVeranderlijken PUSH verschuivingApplicaties MN (Applicatieknoop)

Code voor NCode voor MMKAP

Voorbeeld

)55( kwadraatprogkwadraat

xxx

GLOBSTART kwadraat, 1PUSH 0 ; push xPUSH 1 ; push xPUSHGLOBAL * ; push *MKAP ; maak * xMKAP ; maak (* x) xUPDATE 2 ; overschrijf redexPOP 1 ; gooi "x" wegUNWIND ; zoek volgende redex

Voorbeeld

GLOBSTART prog, 0 PUSHINT 5 PUSHINT 5 PUSHGLOBAL +MKAP ; + 5MKAP ; (+ 5) 5 PUSHGLOBAL kwadraat MKAP ; kwadraat ((+ 5) 5)UPDATE 1 ; overschrijf redexUNWIND ; zoek volgende redex

Voorbeeld

BEGIN ; initialiseerPUSHGLOBAL prog ; vast beginpuntEVAL ; start de uitvoeringPRINT ; print resultaat ; van stapelEND ; einde van het programma

Code: detail

GLOBSTART kwadraat, 1PUSH 0 ; push xPUSH 1 ; push xPUSHGLOBAL * ; push *MKAP ; maak * xMKAP ; maak * x xUPDATE 2 ; overschrijf redexPOP 1 ; gooi "x" wegUNWIND ; zoek volgende redex

Code: detail

GLOBSTART *, 2PUSH 1 ; push yEVAL ; reduceer naar ZHNVPUSH 1 ; push xEVAL ; reduceer naar ZHNVMULTIPLY ; voer de vermenig- ; vuldiging uitUPDATE 3 ; overschrijf redexPOP 2 ; gooi de argumenten wegRETURN ; zoek volgende redex

Uitv

oerin

g va

n EVAL

Optimalisatie 1: combinatorsubstitutie

GLOBSTART kwadraat, 1PUSH 0 ; push xEVALPUSH 1 ; push xEVALMULTIPLY ; voer de vermenig- ; vuldiging uitUPDATE 2 ; overschrijf redexPOP 1 ; gooi "x" wegUNWIND ; zoek volgende redex

Optimalisatie 2: resultaatrecuperatie

GLOBSTART kwadraat, 1PUSH 0 ; push xEVALPUSH 0 ; push de waarde van xMULTIPLY ; voer de vermenig- ; vuldiging uitUPDATE 2 ; overschrijf redexPOP 1 ; gooi "x" wegRETURN ; herstel dump

Optimalisatie 3: geheugenrecuperatie

GLOBSTART kwadraat, 1EVALPUSH 0 ; push de waarde van xMULTIPLY ; voer de vermenig- ; vuldiging uitUPDATE 1 ; overschrijf redexRETURN ; herstel dump

Optimalisatie 4: combinatorsubstitutie

GLOBSTART prog, 0 PUSHINT 5 PUSHINT 5 ADDPUSHGLOBAL kwadraat MKAP ; kwadraat (+ 5 5)UPDATE 1 ; overschrijf redexUNWIND ; zoek volgende redex

Optimalisatie 5:constante expressies

GLOBSTART prog, 0 PUSHINT 10 PUSHGLOBAL kwadraat MKAP ; kwadraat 10UPDATE 1 ; overschrijf redexUNWIND ; zoek volgende redex

Optimalisatie 6: striktheidGLOBSTART kwadraat, 1PUSH 0 ; push de waarde van xMULTIPLY ; voer de vermenig- ; vuldiging uitUPDATE 1 ; overschrijf redexRETURN ; herstel dump

GLOBSTART prog, 0PUSHINT 10PUSHGLOBAL kwadraatMKAP ; kwadraat 10UPDATE 1 ; overschrijf redexUNWIND ; zoek volgende redex

Staartrecursie

zyx

EEEyx W

W F 321

GLOBSTART F,2..code voor E3..code voor E2..code voor E1PUSHGLOBAL WMKAPMKAPMKAPUPDATE 3POP 2UNWIND

Staartrecursie: stapelbeeld

Voorbeeld))( )1( (fac )0( if fac annanan

GLOBSTART FAC,2PUSH 0 ; nEVAL ; waarde van nPUSHINT 0 ; nulEQUAL ; gelijk ?JTRUE POSITIEF ; indien n>0PUSH 1 ; aEVAL ; evalueerUPDATE 3 ; overschrijf redex van facPOP 2 ; argumenten van fac wegRETURN ; reeds in ZHN vorm dank ; zij eval

LABEL POSITIEFPUSH 1 ; aPUSH 1 ; nMULTIPLY ; n*aPUSH 1 ; nPUSHINT 1 ; 1MINUS ; n-1PUSHGLOBAL FAC ; fac (n-1) (n*a)SQUEEZE 3 2 ; overschrijf originele ; argumenten DISPATCH 2 ; staartrecursie

Voorbeeld

Prestatienadelen van lazyness

laxlax :)():( telop

De uitdrukking

zal herleid worden tottelop 1 (telop 2 (telop 3 [0]))

[1 + 2 + 3 + 0]

Oplossing:accumulator + striktheid

lalbaaa

xx

)1( acculengte):( acculengte] [ acculengte

0acculengtelengte