1 / 53

Deel I: Functionele Programmeertalen

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.

etta
Download Presentation

Deel I: Functionele Programmeertalen

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Deel I: FunctioneleProgrammeertalen Hoofdstuk 5: Implementatie-technieken

  2. @ @ 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 Voorstelling abstract concreet

  3. Syntaxisboom (x.x*x)(5+5) @ @  x @ @ 5 x @ + 5 * x

  4. Ingeblikte voorstelling

  5. Oningeblikte voorstelling

  6. 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

  7. Lazy uitvoering • Strikte talen: ML en Hope • Lazy talen: SASL, KRC, LML, Miranda, Orwell, Ponder, Haskell en Hugs

  8. Een l-expressie is in zwakke hoofdnormaalvorm als en slechts als ze van de vorm is, met F een veranderlijke, een gegevensobject, een ingebouwde functie, ofwel een l-abstractie en zodanig dat FE1…Em geen b-redex is voor m n Zwakke hoofdnormaalvorm

  9. Zwakke hoofdnormaalvorm • Gegevensobject: constante of constructor • Gevolg: ZHN: geen kopredices • Voorbeelden: v, 19, +3, ly.x+ 3

  10. zwakke hoofdnormaalvorm normaalvorm Overzicht -expressie reductie van kopredices normale reductieorde

  11. Voorbeeld

  12. Voorbeeld: normale reductievolgorde

  13. Voorbeeld: reductie van kopredices

  14. @ @ M3 @ M2 M M1 Kopredices @ Mn

  15. Vier gevallen • M is een gegevensobject (constante, CONS-cel, enz) n= 0: ZHN n> 0: fatale fout • M is een ingebouwde functie met k argumenten k>n: ZHN k n: kopredex =MM1… Mk

  16. Vier gevallen • M is een l-abstractie n> 0: kopredex =MM1 n= 0: ZHN • M is een veranderlijke n= 0: ZHN n> 0: fatale fout

  17. Ruggengraadstapel

  18. @ @ M3 @ M2 M M1 Kopredices @ ... Mn

  19. Reductie • Argumenten niet substitueren, maar verwijzingen gebruiken • Applicatieknopen worden vervangen door hun waarde

  20. Graafreductie @ (x.x*x)(5+5) @  x @ @ 5 x @ + 5 * x

  21. Graafreductie (5+5)*(5+5) @ @  @ * x @ @ 5 x @ + 5 * x

  22. Graafreductie 10*10 @ @ 10  * x @ @ 5 x @ + 5 * x

  23. Graafreductie 100 @ 10  * x @ @ 5 x @ + 5 * x

  24. Het overschrijven van de redex

  25. @ @ @ f a f a Het overschrijven van de redex @ (x.x)(fa) @  x x f a

  26. De SECD-machine De virtuele machine met vier stapels, • 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 continuaties • D: de dump die gebruikt wordt om gegevens te bewaren tijdens de oproep van een andere functie

  27. De G-machine Vier gegevensstructuren • een graaf • G-programma • expressiestapel • een controlestapel

  28. Voorbeeld PUSH 1 ; dupliceer f MKAP ; maak f x PUSH 1 ; dupliceer f MKAP ; maak f (f x) SLIDE 1 ; gooi argument f weg

  29. f(fx) fx f(fx) Voorbeeld f x f f

  30. 68030 Code move.l (4,a6),-(a6) ; dupliceer f move.l (4,a6),-(a5) ; x move.l (a6)+,-(a5) ; f move.l AP,-(a5) ; typekenteken move.l a5,(a6) ; overschrijf "x" move.l (4,a6),-(a6) ; dupliceer f move.l (4,a6),-(a5) ; f x move.l (a6)+,-(a5) ; f move.l AP,-(a5) ; typekenteken move.l a5,(a6) ; overschrijf "f x" move.l (a6)+,(a6) ; overschrijf "f"

  31. Voorbeeld

  32. Optimale code move.l (a6)+,-(a5) ; x, en gooi "x" weg move.l (a6),-(a5) ; f move.l AP,-(a5) ; typekenteken move.l a5,-(a5) ; f x move.l (a6),-(a5) ; f move.l AP,-(a5) ; typekenteken move.l a5,(a6) ; overschrijf "f” ; met "f (f x)"

  33. Compilatieschema GLOBSTART naam, n ... UPDATE n+1 POP n UNWIND

  34. Stapelorganisatie

  35. Compilatie • Getallen PUSHINT getal • Combinatoren en ingebouwde functiesPUSHGLOBAL naam • Veranderlijken PUSH verschuiving • Applicaties MN (Applicatieknoop) Code voor N Code voor M MKAP

  36. Voorbeeld GLOBSTART kwadraat, 1 PUSH 0 ; push x PUSH 1 ; push x PUSHGLOBAL * ; push * MKAP ; maak * x MKAP ; maak (* x) x UPDATE 2 ; overschrijf redex POP 1 ; gooi "x" weg UNWIND ; zoek volgende redex

  37. Voorbeeld GLOBSTART prog, 0 PUSHINT 5 PUSHINT 5 PUSHGLOBAL + MKAP ; + 5 MKAP ; (+ 5) 5 PUSHGLOBAL kwadraat MKAP ; kwadraat ((+ 5) 5) UPDATE 1 ; overschrijf redex UNWIND ; zoek volgende redex

  38. Voorbeeld BEGIN ; initialiseer PUSHGLOBAL prog ; vast beginpunt EVAL ; start de uitvoering PRINT ; print resultaat ; van stapel END ; einde van het programma

  39. Code: detail GLOBSTART kwadraat, 1 PUSH 0 ; push x PUSH 1 ; push x PUSHGLOBAL * ; push * MKAP ; maak * x MKAP ; maak * x x UPDATE 2 ; overschrijf redex POP 1 ; gooi "x" weg UNWIND ; zoek volgende redex

  40. Code: detail GLOBSTART *, 2 PUSH 1 ; push y EVAL ; reduceer naar ZHNV PUSH 1 ; push x EVAL ; reduceer naar ZHNV MULTIPLY ; voer de vermenig- ; vuldiging uit UPDATE 3 ; overschrijf redex POP 2 ; gooi de argumenten weg RETURN ; zoek volgende redex

  41. Uitvoering van EVAL

  42. Optimalisatie 1: combinatorsubstitutie GLOBSTART kwadraat, 1 PUSH 0 ; push x EVAL PUSH 1 ; push x EVAL MULTIPLY ; voer de vermenig- ; vuldiging uit UPDATE 2 ; overschrijf redex POP 1 ; gooi "x" weg UNWIND ; zoek volgende redex

  43. Optimalisatie 2: resultaatrecuperatie GLOBSTART kwadraat, 1 PUSH 0 ; push x EVAL PUSH 0 ; push de waarde van x MULTIPLY ; voer de vermenig- ; vuldiging uit UPDATE 2 ; overschrijf redex POP 1 ; gooi "x" weg RETURN ; herstel dump

  44. Optimalisatie 3: geheugenrecuperatie GLOBSTART kwadraat, 1 EVAL PUSH 0 ; push de waarde van x MULTIPLY ; voer de vermenig- ; vuldiging uit UPDATE 1 ; overschrijf redex RETURN ; herstel dump

  45. Optimalisatie 4: combinatorsubstitutie GLOBSTART prog, 0 PUSHINT 5 PUSHINT 5 ADD PUSHGLOBAL kwadraat MKAP ; kwadraat (+ 5 5) UPDATE 1 ; overschrijf redex UNWIND ; zoek volgende redex

  46. Optimalisatie 5:constante expressies GLOBSTART prog, 0 PUSHINT 10 PUSHGLOBAL kwadraat MKAP ; kwadraat 10 UPDATE 1 ; overschrijf redex UNWIND ; zoek volgende redex

  47. Optimalisatie 6: striktheid GLOBSTART kwadraat, 1 PUSH 0 ; push de waarde van x MULTIPLY ; voer de vermenig- ; vuldiging uit UPDATE 1 ; overschrijf redex RETURN ; herstel dump GLOBSTART prog, 0 PUSHINT 10 PUSHGLOBAL kwadraat MKAP ; kwadraat 10 UPDATE 1 ; overschrijf redex UNWIND ; zoek volgende redex

  48. Staartrecursie GLOBSTART F,2 ..code voor E3 ..code voor E2 ..code voor E1 PUSHGLOBAL W MKAP MKAP MKAP UPDATE 3 POP 2 UNWIND

  49. Staartrecursie: stapelbeeld

  50. Voorbeeld GLOBSTART FAC,2 PUSH 0 ; n EVAL ; waarde van n PUSHINT 0 ; nul EQUAL ; gelijk ? JTRUE POSITIEF ; indien n>0 PUSH 1 ; a EVAL ; evalueer UPDATE 3 ; overschrijf redex van fac POP 2 ; argumenten van fac weg RETURN ; reeds in ZHN vorm dank ; zij eval

More Related