190 likes | 208 Views
This post discusses the concepts of call-by-name and call-by-need in programming languages, with a focus on Haskell. It also explores the future of programming languages and the potential of call-by-name/need in modern language design.
E N D
CSE-321 Programming LanguagesCall-by-name/need 박성우 POSTECH June 9, 2006
Rule App • Evaluates argument e' only when it is necessary to do so
Call-by-need • Semantically equivalent to call-by-name • Never evaluates the same expression more than once
Graph Representation • let square = fn x => mult x x let main = square (square 3) @ @ @ @ x square mult square 3 x mult x x square (square 3)
square under Call-by-need • let square = fn x => mult x x let main = square (square 3) @ @ @ x @ x mult mult x ² call-by-name call-by-need
Graph Reduction • let square = fn x => mult x x let main = square (square 3) square= @ @ @ @ x square mult square 3 ²
@ square 3 Graph Reduction • let square = fn x => mult x x let main = square (square 3) square= @ @ mult ²
@ @ 3 mult ² Graph Reduction • let square = fn x => mult x x let main = square (square 3) @ @ mult ²
Graph Reduction • let square = fn x => mult x x let main = square (square 3) @ @ 9 mult ²
Outline • Introduction to call-by-need V • Haskell • don't think operationally, i.e., how. • think 'declaratively, i.e., what. • Future of programming languages
"Hello, Haskell" inc :: Integer -> Integer inc n = n + 1 length :: [a] -> Integer length [] = 0 length (x:xs) = 1 + length xs head :: [a] -> a head (x:xs) = x tail :: [a] -> [a] tail (x:xs) = xs
List Comprehension • List of all f x such that x is drawn from xs. [f x | x <- xs] • Quick sort quicksort [] = [] quicksort (x:xs) = quicksort [ y | y <- xs, y < x] ++ [x] ++ quicksort [y | y <- xs, y >= x]
Infinite Data Structures • An infinite list of ones ones = 1 : ones • Infinite list of successive integers beginning with n numsFrom n = n : numsFrom (n + 1) • Infinite list of squares squares = map (^2) (numsFrom 0)
Infinite Fibonacci! fib = 1 : 1 : [ a + b | (a, b) <- zip fib (tail fib) ] fib = 1 : 1 : 2 : 3 : 5 : 8 : 13 : ... tail fib = 1 : 2 : 3 : 5 : 8 : 13 : ... zip ... = (1,1) : (1, 2) : (2, 3) : (3, 5) : ... a + b 2 3 5 8
Outline • Introduction to call-by-need V • Haskell V • Future of programming languages
Functional vs. Imperative • Functional languages are in the minority • not because they are ill-designed • not because they are difficult to learn • but because ... • Imperative languages are in the majority • not because they are elegant • not because they are easy to learn • but because of their popularity in the past
Future of Programming Languages • Criteria of little importance • memory requirement • speed • integrated programming environments • ... • Criteria of ultimate importance • specification • specification • specification • ...
PostechML • Call-by-value + call-by-need • next-generation functional languages • Mechanized safety proof • automatic proof of type safety from language definition • Scientific computation • dimension analysis • network communication • ...