Lambda Calculus and Functional Programming
Total Page:16
File Type:pdf, Size:1020Kb
Global Journal of Researches in Engineering Vol. 10 Issue 2 (Ver 1.0) June 2010 P a g e | 47 Lambda Calculus and Functional Programming Anahid Bassiri1Mohammad Reza. Malek2 GJRE Classification (FOR) 080299, 010199, 010203, Pouria Amirian3 010109 Abstract-The lambda calculus can be thought of as an idealized, Basis concept of a Turing machine is the present day Von minimalistic programming language. It is capable of expressing Neumann computers. Conceptually these are Turing any algorithm, and it is this fact that makes the model of machines with random access registers. Imperative functional programming an important one. This paper is programming languages such as FORTRAN, Pascal etcetera focused on introducing lambda calculus and its application. As as well as all the assembler languages are based on the way an application dikjestra algorithm is implemented using a Turing machine is instructed by a sequence of statements. lambda calculus. As program shows algorithm is more understandable using lambda calculus in comparison with In addition functional programming languages, like other imperative languages. Miranda, ML etcetera, are based on the lambda calculus. Functional programming is a programming paradigm that I. INTRODUCTION treats computation as the evaluation of mathematical ambda calculus (λ-calculus) is a useful device to make functions and avoids state and mutable data. It emphasizes L the theories realizable. Lambda calculus, introduced by the application of functions, in contrast with the imperative Alonzo Church and Stephen Cole Kleene in the 1930s is a programming style that emphasizes changes in state. formal system designed to investigate function definition, Lambda calculus provides a theoretical framework for function application and recursion in mathematical logic and describing functions and their evaluation. Though it is a computer science. It has emerged as a useful tool in the mathematical abstraction rather than a programming investigation of problems in computability or recursion language, it forms the basis of almost all functional theory, and forms the basis of a paradigm of computer programming languages today. Modern functional programming called functional programming. languages can be viewed as embellishments to the lambda As lambda calculus is capable of expressing any algorithm calculus. (ref2) the lambda calculus can be thought of as an idealized, In the next section first we introduce functional minimalistic programming language. Based on these programming and after that functional and non-functional capabilities lambda calculus became an important model of programming are compared. functional programming. Functional programs are stateless II. FUNCTIONAL PROGRAMMING and deal exclusively with functions that accept and return data (including other functions), but they produce no side Functional programming languages, especially purely effects in 'state' and thus make no alterations to incoming functional ones, have largely been emphasized in academia data. Modern functional languages, building on the lambda rather than in commercial software development. However, calculus, include Erlang, Haskell, Lisp, ML, Scheme and notable functional programming languages used in industry Microsoft has in the past couple years has turned its and commercial applications include Erlang, OCaml, attention towards functional programming with introduction Haskell, Scheme (since 1986) and domain-specific of .NET based functional programming language called programming languages like R (statistics), Mathematica F#.(ref1) (symbolic math), J and K (financial analysis), and XSLT The lambda calculus continues to play an important role in (XML). mathematical foundations, through the Curry-Howard Many non-functional programming languages such as C, correspondence.(ref1) C++ and C# can be made to exhibit functional behaviors Church (1936) invented a formal system called the lambda using function pointers, the <functional> library and lambda calculus and defined the notion of computable function via functions respectively. this system. Turing (1936, 1937) invented a class of A functional program consists of an expression E machines (later to be called Turing machines) and defined (representing both the algorithm and the input). This the notion of computable function via these machines. In expression E is subject to some rewrite rules. 1936 Turing proved that both models are equally strong in Reduction consists of replacing a part P of E by another the sense that they define the same class of computable expression P‘ according to the given rewrite rules. In functions. schematic notation _______________________________ About*-GIS Department, Faculty Of Geodesy And Geomatic Eng., Provided that P -> P‘ is according to the rules. This process K.N.Toosi University Of Technology, Mirdamad Cross, Valiasr St., Tehran, of reduction will be repeated until the resulting expression IRAN has no more parts that can be rewritten. This so called About- (e-mail;[email protected]) 2 normal form E* of the expression E consists of the output of About- (e-mail; [email protected]) About-(e-mail;[email protected]) the given functional program. P a g e | 48 Vol. 10 Issue 2 (Ver 1.0) June 2010 Global Journal of Researches in Engineering Iteration (looping) in functional languages is usually A non-strict evaluator may recognize that a sub-expression accomplished via recursion. Recursive functions invoke does not need to be evaluated. For example, given the themselves, allowing an operation to be performed over and definitions over. Recursion may require maintaining a stack, but tail Multiply (0, x) = 0; recursion can be recognized and optimized by a compiler Multiply (n, x) = x + multiply (n-1, x); into the same code used to implement iteration in imperative F (0) = 1; languages. The Scheme programming language standard F (n) = n * f (n-1); requires implementations to recognize and optimize tail recursion. Multiply (0, f (1000000)) a strict evaluator would (strictly Functional languages can be categorized by whether they speaking) need to take (on the order of) 1,000,000 steps to use strict or non-strict evaluation, concepts that refer to how find the value of f (1000000). A non-strict evaluator may function arguments are processed when an expression is use the definition of multiply first, reducing the whole being evaluated. expression to 0 before even trying to compute f (1000000). In brief, strict evaluation always fully evaluates function Non-strict evaluation can use the above to allow arguments before invoking the function. Non-strict "infinite" data structures. evaluation is free to do otherwise To illustrate, consider the following two functions f and g: III. COMPARISON OF FUNCTIONAL AND IMPERATIVE f(x) := x^2 + x + 1 PROGRAMMING g(x, y) := x + y Functional programming is very different from imperative Under strict evaluation, we would have to evaluate function programming. The most significant differences stem from arguments first, for example: the fact that functional programming avoids side effects, f(g(1, 4)) which are used in imperative programming to implement = f(1 + 4) state and I/O. Pure functional programming disallows side = f(5) effects completely. Disallowing side effects provides for = 5^2 + 5 + 1 referential transparency, which makes it easier to verify, = 31 optimize, and parallelize programs, and easier to write automated tools to perform those tasks. This means that pure By contrast, non-strict evaluation need not fully evaluate the functions have several useful properties, many of which can arguments; in particular it may send the arguments be used to optimize the code: unevaluated to the function, perhaps evaluating them later. If the result of a pure expression is not used, it can For example, one non-strict strategy (call-by-name) might be removed without affecting other expressions. work as follows: If a pure function is called with parameters that f(g(1, 4)) cause no side-effects, the result is constant with = g(1, 4)^2 + g(1, 4) + 1 respect to that parameter list (sometimes called = (1 + 4)^2 + (1 + 4) + 1 referential transparency), i.e. if the pure function is = 5^2 + 5 + 1 again called with the same parameters, the same = 31 result will be returned (this can enable caching optimizations). A key property of strict evaluation is that when an argument If there is no data dependency between two pure expression fails to terminate, the whole expression fails to expressions, then their order can be reversed, or they can be terminate. With non-strict evaluation, this need not be the performed in parallel and they cannot interfere with one case, since argument expressions need not be evaluated at another (in other terms, the evaluation of any pure all. expression is thread-safe). Advantages of strict-evaluation can be categorized into two If the entire language does not allow side-effects, categories as it denoted in below: then any evaluation strategy can be used; this gives Parameters are usually passed around as (simple) atomic the compiler freedom to reorder or combine the units, rather than as (rich) expressions. (For example, the evaluation of expressions in a program (for integer 5 can be passed on a register, whereas the expression example, using lazy evaluation). 1+4 will require several memory locations). This has a While most compilers for imperative programming direct implementation on standard hardware. languages detect pure functions, and perform common- The order of evaluation