introduction to compilation of functional languages wanhe zhang computing and software department...

40
Introduction to Introduction to Compilation of Compilation of Functional Languages Functional Languages Wanhe Zhang Computing and Software Department McMaster University 16 th , March, 2004

Upload: daquan-wrather

Post on 01-Apr-2015

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to Compilation of Functional Languages Wanhe Zhang Computing and Software Department McMaster University 16 th, March, 2004

Introduction to Compilation of Introduction to Compilation of Functional LanguagesFunctional Languages

Wanhe ZhangComputing and Software Department

McMaster University16th, March, 2004

Page 2: Introduction to Compilation of Functional Languages Wanhe Zhang Computing and Software Department McMaster University 16 th, March, 2004

Functional ProgramsFunctional Programs

Based on the idea that a program is a function with one input parameter, its input and one result, its output.

Difference between functional and imperative language is efficiency consideration and readability. We can see that from the factorial function below:

Page 3: Introduction to Compilation of Functional Languages Wanhe Zhang Computing and Software Department McMaster University 16 th, March, 2004

Difference between Func and ImDifference between Func and Imperative Languageperative Language

Page 4: Introduction to Compilation of Functional Languages Wanhe Zhang Computing and Software Department McMaster University 16 th, March, 2004

Compilation of Functional Compilation of Functional LanguagesLanguages

A short tour of HaskellCompiling functional languagesPolymorphic type checkingDesugaring

Page 5: Introduction to Compilation of Functional Languages Wanhe Zhang Computing and Software Department McMaster University 16 th, March, 2004

Tour of HaskellTour of Haskell

Function application syntax:

f 11 13

1)No bracket around the arguments, allows currying to be expressed naturally.

2)Function application binds stronger than any operator.

g n+1 is (g n) + 1 rather than g (n+1)

Page 6: Introduction to Compilation of Functional Languages Wanhe Zhang Computing and Software Department McMaster University 16 th, March, 2004

Tour of HaskellTour of Haskell

Offside rule Lists List comprehension Pattern matching Polymorphic typing Referential transparency Higher-order functions Lazy evaluation

Page 7: Introduction to Compilation of Functional Languages Wanhe Zhang Computing and Software Department McMaster University 16 th, March, 2004

Offside ruleOffside rule

Divide x 0 = infDivide x y = x/y

An equation consists of a left-hand side, followed by the = token, followed by the right-hand side. No explicit token to denote the end of each equation. If treats line break as the terminator is inconvenient.

Offside rule controls the bounding box of anexpression.

Page 8: Introduction to Compilation of Functional Languages Wanhe Zhang Computing and Software Department McMaster University 16 th, March, 2004

Offside ruleOffside rule

Everything below and to the right of the = token is defined to be part of the expression making up the right-hand side.

The right-hand side terminates before the first token that is ‘offside’-to the left-of the = position.

Page 9: Introduction to Compilation of Functional Languages Wanhe Zhang Computing and Software Department McMaster University 16 th, March, 2004

ListsListsThe polymorphic typing of Haskell, does

not allow lists to contain elements of different.

[]

[1,2,3,4] = (1:(2:(3:(4:[]))))

[“red”, “yellow”]

[1..10]

Page 10: Introduction to Compilation of Functional Languages Wanhe Zhang Computing and Software Department McMaster University 16 th, March, 2004

List ComprehensionList ComprehensionSyntax that closely matches set notation.S = [n^2 | n <- [1..100], odd n]List comprehension generates lists rather

than sets: ordering is important and elements may occur multiple times in list comprehensions.

It is convenient to use when generating new lists from old ones.

Page 11: Introduction to Compilation of Functional Languages Wanhe Zhang Computing and Software Department McMaster University 16 th, March, 2004

List ComprehensionList Comprehensionqsort [] = []

qsort (x:xs) = qsort [y | y <- xs, y<x]

++ [x]

++ qsort [y | y <- xs, y >= x]

Page 12: Introduction to Compilation of Functional Languages Wanhe Zhang Computing and Software Department McMaster University 16 th, March, 2004

Pattern MatchingPattern Matching fac 0 = 1

fac n = n*fac (n-1) Function equations are matched from top to botto

m; the patterns in them are matched from left to right.

Pattern matching can be translated easily into equivalent definitions based on if-then-else constructs.

Fac n = if (n == 0) then 1 else n * fac (n-1)

Page 13: Introduction to Compilation of Functional Languages Wanhe Zhang Computing and Software Department McMaster University 16 th, March, 2004

Polymorphic TypingPolymorphic TypingAn expression is said to be polymorphic if

it ‘has many types’.List [] has many types: list of characters, list

of numbers and an infinite number of others.

The main advantage of polymorphic typing is that functions and data structures can be reused for any desired type instance.

Type checking will discussed later.

Page 14: Introduction to Compilation of Functional Languages Wanhe Zhang Computing and Software Department McMaster University 16 th, March, 2004

Referential TransparencyReferential Transparency A fixed relation between inputs and output:

f arg will produce the same output no matter what the overall state of the computation is.

Imperative languages, assignments to global variables and through pointers may cause two calls f arg to yield different result.

The advantage is that it simplifies program analysis and transformation. The bad thing is that it prevents the programmer from writing space-efficient programs that use in-space updates.

Add_one [] = []add_one (x:xs) = x+1 : add_one xs

In imperative language, we can update the input list in-place.

Page 15: Introduction to Compilation of Functional Languages Wanhe Zhang Computing and Software Department McMaster University 16 th, March, 2004

Higher-order FunctionsHigher-order Functions Higher-order function is defined as a function that takes a

function as an argument, or delivers one as a result. Imperative languages barely support higher-order function

s: functions may perhaps be passed as parameters, cannot create a new one.

Two way to create new functions:1) diff f = f_

where f_ x = (f ( x + h ) – f x) / hh = 0.0001

diff returns as its result a ‘new’ function that is composed out of already existing functions.2) diff f x = ( f ( x + h ) – f x) / h

where h = 0.0001Apply an existing function to a number of arguments that is less than the arity of the function.-------Currying

Page 16: Introduction to Compilation of Functional Languages Wanhe Zhang Computing and Software Department McMaster University 16 th, March, 2004

Lazy EvaluationLazy EvaluationLazy evaluation relaxes these constraints by

specifying that a subexpression will only be evaluated when its value is needed for the progress of the computation.

Page 17: Introduction to Compilation of Functional Languages Wanhe Zhang Computing and Software Department McMaster University 16 th, March, 2004

Compiling Functional LanguagesCompiling Functional Languages Below is the compiler phase handles which

aspect of Haskell:

Page 18: Introduction to Compilation of Functional Languages Wanhe Zhang Computing and Software Department McMaster University 16 th, March, 2004

Compiling Functional LanguagesCompiling Functional Languages

Page 19: Introduction to Compilation of Functional Languages Wanhe Zhang Computing and Software Department McMaster University 16 th, March, 2004

The Functional CoreThe Functional CoreIt must be high-level enough to serve as an

easy target for the front-end that compiles the syntactic sugar away.

It must be low-level enough to allow concise descriptions of optimizations, which are often expressed as a case analysis of all core constructs.

Page 20: Introduction to Compilation of Functional Languages Wanhe Zhang Computing and Software Department McMaster University 16 th, March, 2004

Functional Core of HaskellFunctional Core of Haskell Basic data types, including int, char, and bool; (user-defined) structured data types; Typed non-nesting functions; Local bindings as part of let-expressions; Expressions consisting of identifiers, arithmetic op

erators, if-then-else compounds, and function applications;

Higher-order functions; (cannot map onto C) Lazy evaluation semantics. (cannot map onto C)

Page 21: Introduction to Compilation of Functional Languages Wanhe Zhang Computing and Software Department McMaster University 16 th, March, 2004

Polymorphic Type checkingPolymorphic Type checking We illustrate this by an example:

map f [] = []

map f ( x : xs ) = f x : map f xs First equation: map :: a -> [b] -> [c] Second equation: map :: (b -> c) -> [b] -> [c]) For the second, x is an element of the list with typ

e [b] and that f x is a part of map’s result list with type [c], so the type of f is b -> c

Page 22: Introduction to Compilation of Functional Languages Wanhe Zhang Computing and Software Department McMaster University 16 th, March, 2004

Polymorphic Function ApplicationPolymorphic Function ApplicationMap :: ( a -> b ) -> [a] -> [b]

length :: [c] -> Intmap length

The type checker must unify the type of length, which is [c] -> Int, with the type of map’s first argument, a -> b. => a = [c], b = Int.

Map :: ( [c] -> Int ) -> [[c]] -> [Int]map length :: [[c]] -> [Int]

Page 23: Introduction to Compilation of Functional Languages Wanhe Zhang Computing and Software Department McMaster University 16 th, March, 2004

DesugaringDesugaringTransform a Haskell program into its

functional-core equivalent. We will focus on translating lists, pattern

matching, list comprehension, and nested functions to core constructs.

Page 24: Introduction to Compilation of Functional Languages Wanhe Zhang Computing and Software Department McMaster University 16 th, March, 2004

The Translation of ListsThe Translation of ListsThree forms of syntactic sugar : , .. The operator : constructs a node with three f

ields: a type tag Cons, an element, and a list.x : xs is transformed to (Cons x xs)[1,2] is transformed to (Cons 1(Cons 2 []))

[1..] is usually translated to calls of library functions that express these lists in terms of : and [].

Page 25: Introduction to Compilation of Functional Languages Wanhe Zhang Computing and Software Department McMaster University 16 th, March, 2004

The Translation of Pattern The Translation of Pattern MatchingMatching

Page 26: Introduction to Compilation of Functional Languages Wanhe Zhang Computing and Software Department McMaster University 16 th, March, 2004

The Translation of Pattern The Translation of Pattern MatchingMatching

A constant yields an equality test.A variable imposes no constraint at all.Constructor patterns, require additional

support to provide type information at run time. We must be able to verify that an argument matches the constructor specified in pattern

Page 27: Introduction to Compilation of Functional Languages Wanhe Zhang Computing and Software Department McMaster University 16 th, March, 2004

Constructor PatternsConstructor Patterns The run-time support to provide the _type_constr f

unction that returns the constructor tag of an arbitrary structured type element.

Reference the fields in the constructor type. The run-time assists us by providing the generic _t

ype_field n function that returns the nth field of any structured type.

We will illustrate above by an example below:

Page 28: Introduction to Compilation of Functional Languages Wanhe Zhang Computing and Software Department McMaster University 16 th, March, 2004

Constructor PatternsConstructor Patterns take 0 xs = []

take n [] = []

take n (x: xs) = x : take (n-1) xs

Page 29: Introduction to Compilation of Functional Languages Wanhe Zhang Computing and Software Department McMaster University 16 th, March, 2004

OptimizationOptimization

The code has already been type-checked at compile time, any second argument in a call of take is guaranteed to be a list. So the last equation need not verify that the argument matches the constructor pattern, and the error guard can be omitted too.

Page 30: Introduction to Compilation of Functional Languages Wanhe Zhang Computing and Software Department McMaster University 16 th, March, 2004

The Translation of List The Translation of List ComprehensionComprehension

[expression | qualifier, ..., qualifier]Qualifier is either a generator or a filter.A generator is of the form var <- list expres

sion; it introduces a variable iteration over a list.

A filter is a Boolean expression, which constrains the variables generated by earlier qualifiers.

Page 31: Introduction to Compilation of Functional Languages Wanhe Zhang Computing and Software Department McMaster University 16 th, March, 2004

The Translation of List The Translation of List ComprehensionComprehension

The transformation works by processing the qualifiers from left to right one at a time. This approach naturally leads to a recursive scheme as presented below.

Page 32: Introduction to Compilation of Functional Languages Wanhe Zhang Computing and Software Department McMaster University 16 th, March, 2004

The Translation of List The Translation of List ComprehensionComprehension

Transformation rule (1) covers the base case where no more qualifiers are present in the list comprehension.

The filter qualifier is handled in transformation rule(2), where F stands for the filter and Q stands the remaining sequence of qualifiers.

The generator qualifier e <- L is covered in rule (3) The generator produces zero or more elements e drawn from a list L. We must generate code to iterate over all elements e, Compute the remainder Q of the list comprehension for each value of e, and concatenate the – possible empty- result lists into a single list.

The key idea for rule (3) is a nested function takes element e and produces the list of values that Q can assume for e. The function then is called over all the elements in L.

Page 33: Introduction to Compilation of Functional Languages Wanhe Zhang Computing and Software Department McMaster University 16 th, March, 2004

The Translation of List The Translation of List ComprehensionComprehension

We need calling a function for all elements in a list;Concatenation the elements of the resulting lists into one.

Map function does not work.map f [] = []map f ( x : xs ) = f x : map f xsIt simply concatenates the results of function applications, and would yield a list of lists in this case.

Modified map:mappend :: (a -> [b]) -> [a] -> [b]mappend f [] = []mappend f (x:xs) = f x ++ mappend f xs

Page 34: Introduction to Compilation of Functional Languages Wanhe Zhang Computing and Software Department McMaster University 16 th, March, 2004

The Translation of List The Translation of List ComprehensionComprehension

Below we will illustrate the theory above: Pyth n = [(a, b, c) | a <- [1 .. n],

b <- [a .. n], c <- [b .. n],

a^2 + b^2 == c^2]

Transformation of Pyth: Pyth n = mappend f_bc2 [1..n]

where f_bc2 a = mappend f_2 [b .. n] where

f_2 c = if (a^2 + b^2 == c^2) then [(a, b, c)] else []

Page 35: Introduction to Compilation of Functional Languages Wanhe Zhang Computing and Software Department McMaster University 16 th, March, 2004

The Translation of Nested The Translation of Nested FunctionsFunctions

Since most target languages of functional compilers don’t support nested routines. The functional core excludes nested functions.

Using lexical pointers to activation records in combination with higher-order functions and lazy evaluation causes dynamic scope violations, since a call to a nested function may escape its lexical scope at run time, rendering its lexical pointer invalid.

For example, a nested function can be returned as the result of higher-order function; lazy evaluation can delay the execution of a call to the nested function until the caller has returned its value, contains a reference to the suspended call.

Page 36: Introduction to Compilation of Functional Languages Wanhe Zhang Computing and Software Department McMaster University 16 th, March, 2004

The Translation of Nested The Translation of Nested FunctionsFunctions

Example:

Sv_mul defines the multiplicaiton of a scalar and a vector.

Page 37: Introduction to Compilation of Functional Languages Wanhe Zhang Computing and Software Department McMaster University 16 th, March, 2004

Analysis of the ExampleAnalysis of the Example

Call map to apply the nested function s_mul to each element in the vector list.

At run time, the interpreted code for sv_mul returns a graph holding the unevaluated expression map s_mul vec.

If we return the routine value map s_mul vec, the activation record of sv_mul will be removed before the nested function s_mul is ever applied.

Page 38: Introduction to Compilation of Functional Languages Wanhe Zhang Computing and Software Department McMaster University 16 th, March, 2004

The Translation of Nested The Translation of Nested FunctionsFunctions

The functional core supports currying( partial parameterization).

Translating a nested routine f to a global routine is just a matter of extending it with additional parameters p1,p2… pa that capture the out-of-scope pointers; each usage of the nested function f must be replaced with a curried call: f p1…pa

Page 39: Introduction to Compilation of Functional Languages Wanhe Zhang Computing and Software Department McMaster University 16 th, March, 2004

The Translation of Nested The Translation of Nested FunctionsFunctions

Lift the nested s_mul into a global funciton sv_mul_dot_s_mul

Extend the function heading with an additional parameter named scal capuring the pointer to the scal parameter of the outer sv_mul function.

All calls of s_mul are replaced by the expression sv_mul_dot_s_mul scal.

Page 40: Introduction to Compilation of Functional Languages Wanhe Zhang Computing and Software Department McMaster University 16 th, March, 2004

ConclusionConclusion

Short tour of Haskell General concept of Compiler for Functional progr

ams Type checking Desugaring----The most important part

Questions?☺