evaluation strategies
play

Evaluation Strategies Call-me-maybe Moritz Flucht Institute for - PowerPoint PPT Presentation

Evaluation Strategies Call-me-maybe Moritz Flucht Institute for Software Engineering and Programming Languages Universitt zu Lbeck December 7th, 2015 Concepts of Programming Languages Introduction Taxonomy Lazy evaluation Eager


  1. Evaluation Strategies Call-me-maybe Moritz Flucht Institute for Software Engineering and Programming Languages Universität zu Lübeck December 7th, 2015 Concepts of Programming Languages

  2. Introduction Taxonomy Lazy evaluation Eager evaluation Optimistic evaluation Conclusion Motivation “At a conference on programming languages you might hear someone say, ‘The normal-order language Hassle has certain strict primitives. Other procedures take their arguments by lazy evaluation.’” — H. Abelson, The Structure and Interpretation of Computer Programs M. Flucht (Universität zu Lübeck) Evaluation Strategies 1/14

  3. Introduction Benefjts and drawbacks Evaluation Strategies M. Flucht (Universität zu Lübeck) Implementation & components Idea Optimistic evaluation 4 Lazy features in applicative-order languages Call-by-value vs. call-by-sharing Eager evaluation 3 Implementation Call-by-name vs. call-by-need Taxonomy Lazy evaluation 2 Strictness vs. non-strictness Applicative vs. normal-order Taxonomy 1 Contents Conclusion Optimistic evaluation Eager evaluation Lazy evaluation 3/28

  4. Introduction Taxonomy Lazy evaluation Eager evaluation Optimistic evaluation Conclusion Taxonomy M. Flucht (Universität zu Lübeck) Evaluation Strategies 1/7

  5. Introduction All parameters are evaluated before entering the body. Evaluation Strategies M. Flucht (Universität zu Lübeck) Found in functional languages like Haskell and Miranda . Parameter evaluation is deferred until they are used . Normal-order Found in traditional languages like C, Java, Ruby . Applicative-order Taxonomy Describing programming languages Semantics vs. Evaluators Conclusion Optimistic evaluation Eager evaluation Lazy evaluation 5/28

  6. Introduction Taxonomy Lazy evaluation Eager evaluation Optimistic evaluation Conclusion Semantics vs. Evaluators Describing programming languages Example: try-function in Scheme Consider M. Flucht (Universität zu Lübeck) Evaluation Strategies 3/14 (define (try a b) (if (= a 0) 1 b)) being called with (try 0 (/ 42 0))

  7. Introduction Taxonomy Lazy evaluation Eager evaluation Optimistic evaluation Conclusion Semantics vs. Evaluators Describing procedures and parameters Strictness A strict parameter is evaluated before the function is applied. Read: a function x is strict in its parameter y. Non-Strictness A non-strict parameter is evaluated at fjrst use . M. Flucht (Universität zu Lübeck) Evaluation Strategies 1/4

  8. Introduction Taxonomy Lazy evaluation Eager evaluation Optimistic evaluation Conclusion Semantics vs. Evaluators Describing evaluators Eager evaluation Expressions are evaluated when they are encountered . Lazy evaluation Expressions are evaluated when they are needed . M. Flucht (Universität zu Lübeck) Evaluation Strategies 2/7

  9. Introduction Taxonomy Lazy evaluation Eager evaluation Optimistic evaluation Conclusion Lazy evaluation M. Flucht (Universität zu Lübeck) Evaluation Strategies 9/28

  10. Introduction Taxonomy Evaluation Strategies M. Flucht (Universität zu Lübeck) 2 1 Example: unless in lazy Scheme 5/14 Deferring expression evaluation Lazy evaluation Conclusion Optimistic evaluation Eager evaluation Lazy evaluation � Class of strategies, not one single technique � Delay evaluation to the last possible moment � Unused expressions might never be evaluated (define (unless condition _then _else) (if condition _else _then))

  11. Introduction Call-by-name Evaluation Strategies M. Flucht (Universität zu Lübeck) Call-by-need Taxonomy 11/28 Call-by-name vs. call-by-need Lazy evaluation Conclusion Optimistic evaluation Eager evaluation Lazy evaluation � Expressions are evaluated every time their value is needed � Similar to macro expansion � The computed value is stored after fjrst usage � Call-by-name with memoization � Only practical with immutable values and pure functions

  12. Introduction Taxonomy Evaluation Strategies M. Flucht (Universität zu Lübeck) 2 1 Example: Infjnite lists in Haskell 3/7 Why bother?—Advantages of laziness Lazy evaluation Conclusion Optimistic evaluation Eager evaluation Lazy evaluation � More concise and intuitive code (have laziness take care of the fallout) � Generate-and-fjlter paradigm (intermediate lists) � Infjnite data structures Consider calling functions like length , sort and doubleList on magic :: Int -> Int -> [Int] magic m n = m : (magic n (m+n))

  13. getIt (1 : (magic 1 (1+1))) 3 � getIt (magic 1 (1+1)) (3-1) � getIt (1 : (magic (1+1) (1+(1+1)))) (3-1) � getIt (1 : (magic (1+1) (1+(1+1)))) 2 � getIt (magic (1+1) (1+(1+1))) (2-1) � getIt (magic 2 (1+2)) (2-1) � getIt (2 : magic (1+2) (2+(1+2))) (2-1) � getIt (2 : (magic (1+2) (2+(1+2))) 1 � 2 � M. Flucht (Universität zu Lübeck) Evaluation Strategies 13/28 1 2 3 4 Introduction Taxonomy Why bother?—Advantages of laziness Lazy evaluation Conclusion Optimistic evaluation Eager evaluation Lazy evaluation getIt :: [Int] -> Int -> Int getIt [] _ = undefined getIt (x:xs) 1 = x getIt (x:xs) n = getIt xs (n-1) getIt (magic 1 1) 3 �

  14. getIt (magic 1 (1+1)) (3-1) � getIt (1 : (magic (1+1) (1+(1+1)))) (3-1) � getIt (1 : (magic (1+1) (1+(1+1)))) 2 � getIt (magic (1+1) (1+(1+1))) (2-1) � getIt (magic 2 (1+2)) (2-1) � getIt (2 : magic (1+2) (2+(1+2))) (2-1) � getIt (2 : (magic (1+2) (2+(1+2))) 1 � 2 � M. Flucht (Universität zu Lübeck) Evaluation Strategies 13/28 1 2 3 4 Introduction Taxonomy Why bother?—Advantages of laziness Lazy evaluation Conclusion Optimistic evaluation Eager evaluation Lazy evaluation getIt :: [Int] -> Int -> Int getIt [] _ = undefined getIt (x:xs) 1 = x getIt (x:xs) n = getIt xs (n-1) getIt (magic 1 1) 3 � getIt (1 : (magic 1 (1+1))) 3 �

  15. getIt (1 : (magic (1+1) (1+(1+1)))) (3-1) � getIt (1 : (magic (1+1) (1+(1+1)))) 2 � getIt (magic (1+1) (1+(1+1))) (2-1) � getIt (magic 2 (1+2)) (2-1) � getIt (2 : magic (1+2) (2+(1+2))) (2-1) � getIt (2 : (magic (1+2) (2+(1+2))) 1 � 2 � M. Flucht (Universität zu Lübeck) Evaluation Strategies 13/28 1 2 3 4 Introduction Taxonomy Why bother?—Advantages of laziness Lazy evaluation Eager evaluation Optimistic evaluation Lazy evaluation Conclusion getIt :: [Int] -> Int -> Int getIt [] _ = undefined getIt (x:xs) 1 = x getIt (x:xs) n = getIt xs (n-1) getIt (magic 1 1) 3 � getIt (1 : (magic 1 (1+1))) 3 � getIt (magic 1 (1+1)) (3-1) �

  16. getIt (1 : (magic (1+1) (1+(1+1)))) 2 � getIt (magic (1+1) (1+(1+1))) (2-1) � getIt (magic 2 (1+2)) (2-1) � getIt (2 : magic (1+2) (2+(1+2))) (2-1) � getIt (2 : (magic (1+2) (2+(1+2))) 1 � 2 � 13/28 Evaluation Strategies M. Flucht (Universität zu Lübeck) 1 2 3 4 Introduction Taxonomy Why bother?—Advantages of laziness Lazy evaluation Eager evaluation Optimistic evaluation Conclusion Lazy evaluation getIt :: [Int] -> Int -> Int getIt [] _ = undefined getIt (x:xs) 1 = x getIt (x:xs) n = getIt xs (n-1) getIt (magic 1 1) 3 � getIt (1 : (magic 1 (1+1))) 3 � getIt (magic 1 (1+1)) (3-1) � getIt (1 : (magic (1+1) (1+(1+1)))) (3-1) �

  17. getIt (1 : (magic (1+1) (1+(1+1)))) 2 � getIt (magic 2 (1+2)) (2-1) � getIt (2 : magic (1+2) (2+(1+2))) (2-1) � getIt (2 : (magic (1+2) (2+(1+2))) 1 � 2 � 2 1 13/28 Evaluation Strategies M. Flucht (Universität zu Lübeck) 3 4 Taxonomy Introduction Why bother?—Advantages of laziness Lazy evaluation Lazy evaluation Eager evaluation Optimistic evaluation Conclusion getIt :: [Int] -> Int -> Int getIt [] _ = undefined getIt (x:xs) 1 = x getIt (x:xs) n = getIt xs (n-1) getIt (magic 1 1) 3 � getIt (1 : (magic 1 (1+1))) 3 � getIt (magic 1 (1+1)) (3-1) � getIt (1 : (magic (1+1) (1+(1+1)))) (3-1) � getIt (1 : (magic (1+1) (1+(1+1)))) 2 �

  18. getIt (magic 2 (1+2)) (2-1) � getIt (2 : magic (1+2) (2+(1+2))) (2-1) � getIt (2 : (magic (1+2) (2+(1+2))) 1 � 2 � Taxonomy 4 3 2 1 13/28 Evaluation Strategies M. Flucht (Universität zu Lübeck) Introduction Why bother?—Advantages of laziness Lazy evaluation Eager evaluation Optimistic evaluation Conclusion Lazy evaluation getIt :: [Int] -> Int -> Int getIt [] _ = undefined getIt (x:xs) 1 = x getIt (x:xs) n = getIt xs (n-1) getIt (magic 1 1) 3 � getIt (1 : (magic 1 (1+1))) 3 � getIt (magic 1 (1+1)) (3-1) � getIt (1 : (magic (1+1) (1+(1+1)))) (3-1) � getIt (1 : (magic (1+1) (1+(1+1)))) 2 � getIt (magic (1+1) (1+(1+1))) (2-1) �

  19. getIt (2 : magic (1+2) (2+(1+2))) (2-1) � getIt (2 : (magic (1+2) (2+(1+2))) 1 � 2 � Taxonomy M. Flucht (Universität zu Lübeck) Evaluation Strategies 13/28 1 2 Introduction 3 4 Why bother?—Advantages of laziness Lazy evaluation Conclusion Optimistic evaluation Eager evaluation Lazy evaluation getIt :: [Int] -> Int -> Int getIt [] _ = undefined getIt (x:xs) 1 = x getIt (x:xs) n = getIt xs (n-1) getIt (magic 1 1) 3 � getIt (1 : (magic 1 (1+1))) 3 � getIt (magic 1 (1+1)) (3-1) � getIt (1 : (magic (1+1) (1+(1+1)))) (3-1) � getIt (1 : (magic (1+1) (1+(1+1)))) 2 � getIt (magic (1+1) (1+(1+1))) (2-1) � getIt (magic 2 (1+2)) (2-1) �

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend