compiler construction
play

Compiler Construction Compiler Construction 1 / 88 Mayer Goldberg \ - PowerPoint PPT Presentation

Compiler Construction Compiler Construction 1 / 88 Mayer Goldberg \ Ben-Gurion University Tuesday 31 st December, 2019 Mayer Goldberg \ Ben-Gurion University Chapter 8 Roadmap Compiler Construction 2 / 88 The expansion of letrec ,


  1. Compiler Construction Compiler Construction 1 / 88 Mayer Goldberg \ Ben-Gurion University Tuesday 31 st December, 2019 Mayer Goldberg \ Ben-Gurion University

  2. Chapter 8 Roadmap Compiler Construction 2 / 88 ▶ The expansion of letrec , revisited ▶ Recursion & Circularity ▶ Fixed-Point Theory ▶ Defjning circular structures with recursion ▶ Defjning circular structures with self-application Mayer Goldberg \ Ben-Gurion University

  3. RC&FPT Revisiting the expansion of letrec Compiler Construction (let () 3 / 88 This is the macro-expansion we presented for letrec : � (letrec ((f 1 ⟨ Expr 1 ⟩ ) = � (let ((f 1 'whatever) (f 2 ⟨ Expr 2 ⟩ ) (f 2 'whatever) · · · · · · (f n ⟨ Expr n ⟩ )) (f n 'whatever)) ⟨ expr 1 ⟩ · · · ⟨ expr m ⟩ ) � (set! f 1 ⟨ Expr 1 ⟩ ) (set! f 2 ⟨ Expr 2 ⟩ ) · · · (set! f n ⟨ Expr n ⟩ ) ⟨ expr 1 ⟩ · · · ⟨ expr m ⟩ )) � Mayer Goldberg \ Ben-Gurion University

  4. RC&FPT Revisiting the expansion of letrec ( continued ) disappointing that we needed to resort to side-efgects in order to implement an idea as fundamental to functional programming as recursion. without resorting to side-efgects, to defjne recursive functions Compiler Construction 4 / 88 ▶ When we introduced this expansion, we commented that it was ▶ At that time, we added that there is a purely functional way, ▶ This is the topic we are now entering Mayer Goldberg \ Ben-Gurion University

  5. Chapter 8 Roadmap Compiler Construction 5 / 88 🗹 The expansion of letrec , revisited ▶ Recursion & Circularity ▶ Fixed-Point Theory ▶ Defjning circular structures with recursion ▶ Defjning circular structures with self-application Mayer Goldberg \ Ben-Gurion University

  6. RC&FPT location the value at which is L … Compiler Construction of which is L … defjned in the top-level environment, a free varaiable the value (e.g., in machine language ) that references a free variable a closure, the code pointer L of which points to a some code to a location in the lexical environment of the closure, a which points to some code (e.g., in machine language ) that pointer circular data-structure 6 / 88 ▶ Recursive functions are an example of a statically-defjned, ▶ For local, recursive functions, the value of the recursive function is a closure ▶ Closures have 2 fjelds: A lexical environment, and a code ▶ That the function is recursive means that the code pointer L of references a bound variable the lexical address of which points ▶ For global, recursive functions, the value of the function is too Mayer Goldberg \ Ben-Gurion University

  7. RC&FPT recursive functions, is the linguistic ability to defjne a particular, circular data-structure statically, so that the compiler may know, at compile-time, that a cycle exists: label, to which to point from within the body of the procedure which to defjne circular data structures of other kinds too: Scheme, Prolog, C, assembly, etc kind of circular data-structures statically: In such languages, circular data-structures are created at run-time Compiler Construction 7 / 88 ▶ The linguistic ability of a programming language, to defjne ▶ The cycle is defjned by using the name of the procedure as a ▶ Some programming languages provide linguistic facilities with ▶ Other programming languages have no facility for defjning any Mayer Goldberg \ Ben-Gurion University

  8. RC&FPT by the compiler, statically: & many other operations can go into infjnite loops if circularity is not handled properly sometimes detect problems with termination conditions, infjnite loops, etc is free not to use a stack, either to pass arguments or to save the return address Compiler Construction 8 / 88 ▶ It is signifjcant that the circularity is defjned and be recognizable ▶ Circular data-structures require special handling: Input, output, ▶ When the circular structure is a function type, a compiler can ▶ When a functional type is not a circular structure, the compiler Mayer Goldberg \ Ben-Gurion University

  9. RC&FPT Static, circular data in C struct LL { int value ; struct LL *next ; } ; extern struct LL db4 ; struct LL db1 = {1, &db4 } ; struct LL db5 = {5, &db1 } ; struct LL db3 = {3, &db5 } ; struct LL db6 = {6, &db3 } ; struct LL db9 = {9, &db6 } ; struct LL db4 = {4, &db9 } ; Compiler Construction 9 / 88 Mayer Goldberg \ Ben-Gurion University

  10. RC&FPT Shape Compiler Construction Static, circular data in Scheme 10 / 88 #0=(1 . #1=((#1# . 2) 3 . #0#)) > '#0=(1 . #1=((#1# . 2) 3 . #0#)) Data pair int pair 1 pair pair int int 2 3 Mayer Goldberg \ Ben-Gurion University

  11. RC&FPT Static, circular data in other languages data is Prolog (up to the 1985 standard), Fortran (up to the 1977 standard) Compiler Construction 11 / 88 ▶ By far, the easiest language in which to defjne static, circular ▶ In Java, the only static, circular data-types that can be defjned are the method, class, and interface ▶ In Python, the only static, circular, data-types that can be defjned are the function, method, and class ▶ In some languages, the only recursive data-type is the function ▶ In PL/I, this must be declared with the keyword RECURSIVE ▶ In FORTH, mutual recursion can only be defjned at run-time ▶ Some languages don’t even permit recursive function: COBOL Mayer Goldberg \ Ben-Gurion University

  12. RC&FPT Writing fact without recursion ( define fact ( lambda (n) ( if (zero? n) 1 (* n (fact (- n 1)))))) that serves as an edge that closes a circular graph. Compiler Construction 12 / 88 ▶ We start with the recursive defjnition of fact : ▶ Examining the body of fact , we notice the free variable fact , Mayer Goldberg \ Ben-Gurion University

  13. RC&FPT cos x Compiler Construction outside the functional paradigm Writing fact without recursion 13 / 88 composition: ▶ Notice that fact is a free variable ▶ In the functional world, free variable are dead-weight: ▶ In the functional world, we change variables using functional f ( x ) = x 2 + 1 g ( x ) = cos ( x 2 + 1) ( f ◦ g )( x ) = ▶ Before the change ▶ After the change ▶ Free variables can only be changed using assignment, which is Mayer Goldberg \ Ben-Gurion University

  14. RC&FPT Writing fact without recursion variable fact , by enclosing the entire expression without (lambda (fact) ... ) . ( define Ffact ( lambda (fact) ( lambda (n) ( if (zero? n) 1 (* n (fact (- n 1))))))) Compiler Construction 14 / 88 ▶ Therefore, the fjrst step we take is to “close over” the free ▶ We call this expression Ffact : Mayer Goldberg \ Ben-Gurion University

  15. RC&FPT Writing fact without recursion meaning there is a simple derivation of the syntax of Ffact from the syntax of fact — As we mentioned earlier, we just surround the body of fact with (lambda (fact) ... ) to obtain Ffact mere syntax Compiler Construction 15 / 88 ▶ The relationship between fact & Ffact is a rich one: ▶ On the one hand, Ffact is related to fact syntactically, ▶ On the other hand, this relationship goes much deeper than Mayer Goldberg \ Ben-Gurion University

  16. RC&FPT Writing fact without recursion regardless of how it computes factorial, whether following the recursive defjnition ( fact ), or iteratively, or using some Regardless of how it is implemented, any implementation of the factorial function satisfjes two properties: function (leastness of fjxed-point) Compiler Construction 16 / 88 ▶ We claim that any implementation of the factorial function, mathematical wizardry such as the Gamma function, etc — ▶ It is a fjxed-point of Ffact ▶ Any other fjxed-point of Ffact can also compute the factorial Mayer Goldberg \ Ben-Gurion University

  17. Chapter 8 Roadmap Compiler Construction 17 / 88 🗹 The expansion of letrec , revisited 🗹 Recursion & Circularity ▶ Fixed-Point Theory ▶ Defjning circular structures with recursion ▶ Defjning circular structures with self-application Mayer Goldberg \ Ben-Gurion University

  18. Fixed Points 48.5427756667 Compiler Construction … 1 1 … 6.96726457562 2356.40106943 5552626 your screen: your mind wanders, and you begin to play with your calculator, punch her number on your handy pocket calculator… have handy paper & pencil with which to write it down, you 18 / 88 ▶ You meet a friend, ask for her phone number, and failing to ▶ Later on, sitting in some [non-Compiler-Construction] lecture, hitting the √ x key over and over… ▶ You notice how with each key-press, the numbers change on Mayer Goldberg \ Ben-Gurion University

  19. Fixed Points ( continued ) When you swap out of your day-dreaming, you realize some important facts: on your calculator screen of the sqare-root function Compiler Construction 19 / 88 ▶ The √ x key no longer has any efgect on the number displayed ▶ The number you’ve reached, 1, is what is known as a fjxed-point ▶ You’ve lost your friend’s phone number… Mayer Goldberg \ Ben-Gurion University

  20. simplify proofs and computations, so such techniques should be Fixed Points ( continued ) in your “bag of tricks”… Compiler Construction 20 / 88 ▶ For a function f : D → D , a point x 0 ∈ D is called a fjxed-point of f , if f ( x 0 ) = x 0 ▶ x 0 is a point that is not changed by the function ▶ The fjxed-points of a function tell us a lot about the function ▶ Techniques for working with fjxed-points can often greatly Mayer Goldberg \ Ben-Gurion University

Recommend


More recommend