Compiling Functional Programs Mooly Sagiv Chapter 7

Slides:



Advertisements
Similar presentations
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 14 Functional Programming It is better to.
Advertisements

Introduction to Compilation of Functional Languages Wanhe Zhang Computing and Software Department McMaster University 16 th, March, 2004.
1 Scheme and Functional Programming Aaron Bloomfield CS 415 Fall 2005.
CMSC 330: Organization of Programming Languages Tuples, Types, Conditionals and Recursion or “How many different OCaml topics can we cover in a single.
ML Lists.1 Standard ML Lists. ML Lists.2 Lists  A list is a finite sequence of elements. [3,5,9] ["a", "list" ] []  ML lists are immutable.  Elements.
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
Compiler Construction
Chapter 15 Other Functional Languages. Copyright © 2007 Addison-Wesley. All rights reserved. Functional Languages Scheme and LISP have a simple syntax.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 14 Functional Programming It is better to.
Compiler construction in4020 – lecture 13 Koen Langendoen Delft University of Technology The Netherlands.
CSC321: Programming Languages14-1 Programming Languages Tucker and Noonan Chapter 14: Functional Programming 14.1 Functions and the Lambda Calculus 14.2.
Expressions (chapter 7 of textbook) Semantics of expressions depends on: –operator evaluation order operator precedence operator associativity –operand.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
Functional Programming Professor Yihjia Tsai Tamkang University.
Type Inference: CIS Seminar, 11/3/2009 Type inference: Inside the Type Checker. A presentation by: Daniel Tuck.
Cse536 Functional Programming 1 7/14/2015 Lecture #2, Sept 29, 2004 Reading Assignments –Begin Chapter 2 of the Text Home work #1 can be found on the webpage,
Gary MarsdenSlide 1University of Cape Town Functional programming in Clean Gary Marsden Semester 2 – 2000.
Functional Languages. Why? Referential Transparency Functions as first class objects Higher level of abstraction Potential for parallel execution.
Functional Programming in Scheme
Haskell. 2 GHC and HUGS Haskell 98 is the current version of Haskell GHC (Glasgow Haskell Compiler, version 7.4.1) is the version of Haskell I am using.
Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
ISBN Chapter 15 Functional Programming Languages.
1 COMP313A Functional Programming (1). 2 Main Differences with Imperative Languages Say more about what is computed as opposed to how Pure functional.
1 COMP 3438 – Part II-Lecture 1: Overview of Compiler Design Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
CSC 580 – Theory of Programming Languages, Spring, 2009 Week 9: Functional Languages ML and Haskell, Dr. Dale E. Parson.
Chapter 9: Functional Programming in a Typed Language.
Functional Programming With examples in F#. Pure Functional Programming Functional programming involves evaluating expressions rather than executing commands.
Chapter Fifteen: Functional Programming Languages Lesson 12.
Function Design in LISP. Program Files n LISP programs are plain text –DOS extensions vary; use.lsp for this course n (load “filename.lsp”) n Can use.
© Kenneth C. Louden, Chapter 11 - Functional Programming, Part III: Theory Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
TIVDM2Functional Programming Language Concepts 1 Concepts from Functional Programming Languages Peter Gorm Larsen.
COMP313A Functional Programming (1)
CS535 Programming Languages Chapter - 10 Functional Programming With Lists.
1 Alex Proctor and Brian Lee for CSCI 431 at UNCA, Fall 2002 ML (Meta Language) a brief introduction Alex Proctor and Brian Lee For CSCI 431 at the University.
CMSC 330: Organization of Programming Languages Functional Programming with OCaml.
Advanced Functional Programming Tim Sheard 1 Lecture 17 Advanced Functional Programming Tim Sheard Oregon Graduate Institute of Science & Technology Lecture:
CS412/413 Introduction to Compilers Radu Rugina Lecture 13 : Static Semantics 18 Feb 02.
Haskell Basics CSCE 314 Spring CSCE 314 – Programming Studio Using GHC and GHCi Log in to unix.cse.tamu.edu (or some other server) From a shell.
Haskell Chapter 5, Part II. Topics  Review/More Higher Order Functions  Lambda functions  Folds.
Cs7120 (Prasad)L1-FP-HOF1 Functional Programming Basics Correctness > Clarity > Efficiency.
An introduction to functional programming using Haskell CENG242 –Recitation 1.
Operational Semantics Mooly Sagiv Reference: Semantics with Applications Chapter 2 H. Nielson and F. Nielson
Chapter 1: Preliminaries Lecture # 2. Chapter 1: Preliminaries Reasons for Studying Concepts of Programming Languages Programming Domains Language Evaluation.
Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L05-1 September 21, 2006http:// Types and Simple Type.
Functional Programming
Functional Programming
Programming Languages and Compilers (CS 421)
Functional Programming
Principles of programming languages 12: Functional programming
Types CSCE 314 Spring 2016.
ML: a quasi-functional language with strong typing
Constructing Precedence Table
A lightening tour in 45 minutes
More Functional Programming
Haskell.
Important Concepts from Clojure
Important Concepts from Clojure
Objective caml Daniel Jackson MIT Lab for Computer Science 6898: Advanced Topics in Software Design March 18, 2002.
CSE-321 Programming Languages Introduction to Functional Programming
Recursion, Higher-order-functions
Important Concepts from Clojure
Compiler Construction
Functional Programming
Functional Programming and Haskell
Compiler Construction
Review Previously in: Lots of language features: functions, lists, records, tuples, variants, pattern matching Today: No new language features New idioms.
Functional Programming and Haskell
Presentation transcript:

Compiling Functional Programs Mooly Sagiv Chapter 7

Subjects A short tour of Haskel Comparison functional programs Compiling functional languages Polymorphic type checking Desugering Graph reduction Code generation Optimizing functional programs Summary

Main features of Haskel No side effects –Referential Transparency List comprehension Pattern matching Higher order functions –Curried notions Polymorphic typing Lazy evaluation

Factorial in Haskel vs. C fac 0 = 1 fac n = n * fac (n -1) int fac(int n) { int product = 1; while (n > 0) { product *= n ; n --; } return product; }

Function Application Concise syntax No argument parenthesizes –f Function application is left associative and has higher precedence than any operator –g g n = (g g) n –g n + 1 = (g n) + 1

Offside rule Layout characters matter to parsing divide x 0 = inf divide x y = x / y Everything below and right of = in equations defines a new scope Applied recursively fac n = if (n ==0) then 1 else prod n (n-1) where prod acc n = if (n == 0) then acc else prod (acc * n) (n -1) Lexical analyzer maintains a stack

Lists Part of all functional programs since Lisp Empty list [] = Nil [1] [1, 2, 3, 4] [4, 3, 7, 7, 1] [“red”, “yellow”, “green”] [1.. 10] Can be constructed using “:” infix operator –[1, 2, 3] = (1 : (2 : ( 3 : []))) –range n m = if n > m then [] else ( n: range (n+1) m)

List Comprehension Inspired by set comprehension S = {n 2 | n  {1, …, 100}  odd n} Haskel code s = [n^2 | n <- [1..100], odd n] “n square such that n is an element of [1..100] and n is odd” Qsort in Haskel qsort [] = [] qsort (x: xs) = qsort [y | y = x]

Pattern Matching Convenient way to define recursive functions A simple example fac 0 = 1 fac n = n * fac (n-1) Equivalent code fac n = if (n == 0) then 1 else n * fac (n -1) Another example length [] = 0 length (x: xs) = 1 + length xs Equivalent code length list = if (list == []) then 0 else let x = head list xs = tail list in 1 + length xs

Polymorphic Typing Polymorphic expression has many types Benefits: –Code reuse –Guarantee consistency The compiler infers that in length [] = 0 length (x: xs) = 1 + length xs –length has the type [a] -> int length :: [a] -> int Example expressions –length [1, 2, 3] + length [“red”, “yellow”, “green”] –length [1, 2, “green” ] // invalid list The user can optionally declare types Every expression has the most general type “boxed” implementations

Referential Transparency Expressions in Haskel have no side effects Usually requires more space add_one [] = [] add_one (x xs) = x +1 : add_one xs Can be tolerated by garbage collection and smart compilers Input/Output operations can be also be defined using Monads

Higher Order Functions Functions are first class objects –Passed as parameters –Returned as results

Example Higher Order Function The differential operator Df = f’ where f’(x) = lim h  o (f(x+h)-f(x))/h In Haskel diff f = f_ where f_ x = (f (x +h) – f x) / h h = diff :: (float -> float) -> (float -> float) (diff square) 0 = (diff square) = (diff (diff square)) 0 = 2

Currying Functions can be created by partially applying a function to some of its arguments deriv f x = (f (x + h) – f x) / h where h = deriv f x == (diff f) x Non semantic difference by Unary and n-ary functions f e 1 e 2 … e n = ( n ((f e 1 ) e 2 ) … e n ) Complicates compilation

Lazy vs. Eager Evaluation When to evaluate expressions A simple example let const c x = c in const 1 (2 + 3) In eager evaluation const 1 (2 + 3)  const 1 5 = 1 In lazy Evaluation (Haskel) const 1 (2 + 3)  1 Another example let const c x = c in const 1 (2 / 0)

Benefits of Lazy Evaluation Define streams main = take 100 [1.. ] deriv f x = lim [(f (x + h) – f x) / h | h <- [1/2^n | n <- [1..]]] where lim (a: b: lst) = if abs(a/b -1) < eps then b else lim (b: lst) eps = 1.0 e-6 Lower asymptotic complexity Language extensibility –Domain specific languages But some costs

Functional Programming Languages PLtypesevaluationSide-effect schemeWeakly typedEageryes ML OCAML F# Polymorphic strongly typed EagerReferences HaskelPolymorphic strongly typed LazyNone

Compiling Functional Programs Compiler PhaseLanguage Aspect Lexical AnalyzerOffside rule ParserList notation List comprehension Pattern matching Context HandlingPolymorphic type checking Run-time systemReferential transparency Higher order functions Lazy evaluation

Structure of a functional compiler High-level language Functional core Polymorphic type inference De-sugaring: 1.Pattern matching 2.List to pairs 3.List comprehension 4.Lambda lifting Optimizations Code generation C code Runtime system

Graph Reduction The runtime state of a lazy functional program can be represented as a direct graph –Nodes represent arguments in expressions –Edges between functions and argument An execution is simulated with a graph reduction Supports laziness and higher order functions

Function Application f e 1 e 2 … e n = ( n ((f e 1 ) e 2 ) … e n ) = ( n e 1 e 2 ) … e enen... e1e1 e2e2

A Simple Example let const c x = c in const 1 (2 const123 1 c x

Another Example let twice f x = f (f x) square n = n * n in twice @x 3 x f

Another Example (cont1) let twice f x = f (f x) square n = n * n in twice square n * 3

Another Example (cont2) let twice f x = f (f x) square n = n * n in twice square @ 3 * 3

Another Example (cont3) let twice f x = f (f x) square n = n * n in twice square *

Reduction Order At every point in execution redexes can be selected Start at the root If the root is not application node print the result If the root is an application node  its value is needed –Traverse the application spine to the left to find the function, say f –Check if the application spine contains all the arguments for f No a Curried function is detected Yes search and apply the redex

The reduction Engine Usually implemented in C Apply redexes Use a stack to match arguments Use Eval to apply redexes using function pointers Built in functions are part of the runtime system User defined functions are mapped into C in a straightforward way using Eval function Significant runtime overhead

C Header file typedef enum {FUNC, NUM, NIL, CONS, APPL} node_type typedef struct node *Pnode typedef Pnode (*unary) (Pnode *arg) struct function_descriptor { int arity; const char * name; unary code ; }; struct node_type { node_type tag; union { struct function_descriptor func; int num; struct {Pnode hd, Pnode tl ;} cons struct {Pnode fun; Pnode arg;} appl; } nd; } extern Pnode Func(int arity, const char *name, unary code); extern Pnode Nil(int num); …

Reducing the cost of graph reduction Shortcut reductions Detect lazy expressions which are always executed –Strict

Optimizing the functional core Boxing analysis Tail call elimination Accumulator translation Strictness analysis

Strictness Analysis A function is strict in an argument a if it needs the value of a in all executions Example safe_div a b = if (b == 0) then 0 else a / b –strict in b Can be computed using dataflow equations

Limitations Usually the generated C code can be reasonably efficient Current strictness analysis works poorly for user-defined data structures and higher order functions

Summary Functional programs provide concise coding Compiled code compares with C code Successfully used in some commercial applications –F#, ERLANG Ideas used in imperative programs Less popular than imperative programs