Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Curry–Howard correspondence wikipedia , lookup
Anonymous function wikipedia , lookup
Closure (computer programming) wikipedia , lookup
Standard ML wikipedia , lookup
C Sharp (programming language) wikipedia , lookup
Falcon (programming language) wikipedia , lookup
Lambda calculus wikipedia , lookup
Common Lisp wikipedia , lookup
Lisp (programming language) wikipedia , lookup
Lambda lifting wikipedia , lookup
5/17/2011 University of Petra Some Programming Paradigms Faculty of Information Technology Dr. techn. Dipl. Inform. Bassam Haddad Associate Professor of Computer Science Faculty of Information Technology Petra University Office: 7312 Ext. 340 haddad@uop edu jo [email protected] Programming Language Design PART V Functional Programming: Lisp Course No.: 602386 Prerequisite:602311 ( IP) Term 2010-I Based on: 1) Concept of Programming Languages R. W. Sebesta, Seventh Edition, Pearson Ed., 2005 Procedural examples: C, Pascal, Basic, Fortran Functional examples: Lisp, ML Object-oriented examples: C++, Java, Smalltalk Rule-based (or Logic) example: p Prolog g Models of Computation RAM machine – procedural directed acyclic graphs – Smalltalk model of O-O partial recursive functions – Lisp and ML • Logic g based – Prolog 2) Programming Languages Design and Implementation T. W. Pratt and M. V. Zelkowitz, Pentice-Hall International Inc, 2002 © Bassam Haddad 2010 © Bassam Haddad 2010 2 1 5/17/2011 Some Programming Paradigms Many of other Languages There are many programming languages Lots of other PL-like objects – document languages, e.g. LaTeX, Postscript – command languages, e.g. bash, MATLAB – markup languages, e.g. HTML and XML – specification languages, e.g. UML Functional Programming The Functional Programming Paradigm is one of the major programming paradigms. – FPL iss a type ype oof dec declarative ve programming p og g paradigm – Also known as applicative programming and value-oriented programming Idea: everything is a function Based on sound theoretical frameworks (e.g., the lambda calculus) Examples of FP languages – First (and most popular) FPL language: Lisp – Other important FPLs: ML, Haskell, Miranda, Scheme, Logo The design of the imperative languages is based directly on the von Neumann architecture (Efficiency is the primary concern, rather than the suitability of the language for software development) The design of the functional languages is based on mathematical functions (A solid theoretical basis that is also closer to the user, but relatively unconcerned with the architecture of the machines on which programs will run like PROLOG) © Bassam Haddad 2010 3 © Bassam Haddad 2010 4 2 5/17/2011 Lambda functions Mathematical Functions Def: A mathematical function is a mapping of members of one set, called the domain set, to another set, called the range set. The lambda calculus is a formal mathematical system devised by Alonzo Church to investigate functions, function application and recursion. A lambda expression specifies the parameter(s) and the mapping of a function in the following form λx .x*x*x Nameless Function for the function cube (x) = x * x * x Lambda expressions: describe nameless functions Lambda expressions are applied to parameter(s) by placing the parameter(s) after the expression (λ x . x * x * x) 3 => 3*3*3 => 27 Lambda functions Lisp uses a mechanism known as lambda abstraction to create new function. function In Lisp, lambda abstraction is the process of creating a function from a list of variables and terms ( lambda variables-list term) (lambda (X Y) ( CONS X (list Y))) Variables list (( lambda (Y X) ( CONS X (list Y))) 1 9) (λ x,y . (x-y)*(y-x)) (3,5) => (3-5)*(5-3) (3 5) (5 3) => -44 > ( 9 1) © Bassam Haddad 2010 5 © Bassam Haddad 2010 6 3 5/17/2011 Characteristics of Pure FPLs Pure FPL languages tend to Have no assignment statements Often have no variables! Be built on a small, concise framework Have a simple, uniform syntax Be implemented via interpreters rather than compilers Be mathematicallyy easier to handle © Bassam Haddad 2010 7 Importance of FP In their pure form FPLs do without with the notion of assignment – it's it' easier i to t program in i them th – easier to reason about programs written in them • FPLs encourage thinking at higher levels of abstraction – support modifying and combining existing programs – thus, FPLs encourage programmers to work in units larger than statements of conventional languages: "programming in the large" • FPLs provide a paradigm for parallel computing – absence of assignment (or single assignment) – independence of evaluation order (for parallel) – ability to operate on entire data structures ( functional programming) FPLs are very useful for AI and other applications which require extensive symbol manipulation. Functional Programming is tied to CS theory – provides framework for viewing decidability questions (both programming and computers) – Good introduction to Denotational Semantics (functional in form) © Bassam Haddad 2010 8 4 5/17/2011 Expressions "Can programming be liberated from the von Neumann style?“ John Backus Key purpose of functional programming: to extend the advantages of expressions (over statements) to an entire programming language Backus has said that expressions and statements come from two different worlds: expressions: (a + b) * c (a + b) = 0 ¬(a ∨ b) Arithmetic Relational Boolean statements: the usual assortment with assignment singled out assignments alter the state of a computation (ordering is important) e.g. a:= a * i; i:= i + 1 • In contrast, ordering of expressions is not side-effecting and therefore not order dependent © Bassam Haddad 2010 9 We can formally model the process of evaluating an expression as the application of one or more reduction rules. • E.g., lambda-calculus includes the beta-reduction rule to evaluate the application of a lambda abstraction to an argument expression. – A copy of the body of the lambda abstraction is made and occurrences of the bound variable replaced by the argument. – E.g. (λ x . x+1) 4 => 4+1 Example of some FPLs LISP – the h fi first FPL, FPL ~1958 1958 Pure FPLs have no side effects Haskell and Miranda are the two most popular examples • Some FPLs try to be more practical and do allow some side effects – Lispp and its dialects (e.g. ( g Scheme)) – ML (Meta Language) and SML (Standard ML) © Bassam Haddad 2010 10 5 5/17/2011 Applications of Functional Languages Lisp is used for artificial intelligence applications Knowledge representation Machine learningg Natural language processing Modeling of speech and vision Embedded Lisp interpreters add programmability to some systems, such as Emacs • Scheme is used to teach introductory programming at many universities (not in Jordan!!) • FPLs FPL are often ft usedd where h quick i k prototyping t t i is i desired. d i d • Pure FPLs like Haskell are useful in contexts requiring some degree of program verification Defined by John McCarthy (1958) as a language for AI. Originally, LISP was a typeless language with only two data types: atom and list LISP’s lists LISP’ li t are stored t d iinternally t ll as single-linked i l li k d lists li t Lambda notation was used to specify functions. Function definitions, function applications, and data all have the same form: If the list (A B C) is interpreted as data, it is a simple list of three atoms,, A,, B,, and C but if interpreted p as a function application, it means that the function named A is applied to the two parameters, B and C. (+ x y) Imperative PLs vs FPLs Imperative Languages: – Efficient execution – Complex semantics – Complex syntax – Concurrency is programmer designed Functional Languages: – Simple i l semantics i – Simple syntax – Inefficient execution – Programs can automatically be made concurrent © Bassam Haddad 2010 Lisp 11 Function name • Common Lispp is the ANSI standard Lispp specification p © Bassam Haddad 2010 12 6 5/17/2011 Lisp Lisp LISP was first designed and implemented by John McCarthy at MIT around 1960 (1958) LISP widely used for artificial intelligence research y g First language based on applicative execution LISP has equivalence of form between programs and data, which allows data structures to be executed as programs and programs to be modified as data In the late 1970s, Gerald Sussman and Guy Steele developed a variant named Scheme. This is the version that had the most impact on university use of LISP. In 1992 a standard for Common LISP was finally written LISP data structures Internal representation of two LISP lists Concept of Programming Languages R. W. Sebesta source: Programming Languages Design and Implementation, T. W. Pratt and M. V. Zelkowitz, © Bassam Haddad 2010 13 7 5/17/2011 Lisp Basic LISP operations Operators: (car ( 1 2 3 )) = 1 ⇐ head of list (cdr ( 1 2 3 ) ) = ( 2 3 ) ⇐ tail of list (cons a ( b c d ) ) = ( a b c d ) ⇐ join operation (cons ( car ( cdr (a b c))) ( c d)) = (b c d) But: (cons (a b ) ( c d ) ) = ( ( a b ) c d ) Examples: The result of several CONS operations Concept of Programming Languages R. W. Sebesta > (+ 1 2) 3 > (* 7 9) 63 >(- (+ 3 4 ) 7) 0 >(=(+ 2 3) 5) t >(list 1 2 3 4 5) (1 2 3 4 5) >(nth 0 ’(a b c d)) a >(nth 2 (list 1 2 3 4 5)) 3 > (length ‘ (a b c d)) 4 © Bassam Haddad 2010 16 8 5/17/2011 Example: Cond (<condition1><action1>) (<condition2><action2>) …. (<condition n> <action n>) > (quote (a b c)) (a b c) >(quote (+ 1 3)) (+ 1 3) > ‘ (a b c) ( a b c) > ‘ (+ 1 3) (+ 1 3) >(eval ( quote (+ 2 3))) Example: (defun absulot-value (X) ((cond (( < X 0)) (-X)) ( )) (( >= X 0 ) X))) Define Function (defun <function name> (<formal parameter>) <function body> Example: Like defun, cond does not evaluate all of its arguments. instead it evaluate the conditions in order until one of them return a nun nil value. ( defun d f square (X) (* X X)) © Bassam Haddad 2010 17 © Bassam Haddad 2010 18 9 5/17/2011 Local Variables One of the most frequently used operators in FL is let. • This allows local variables to be used in a function. • A let expression e pression has two t o parts. parts – First comes a list of instructions for creating variables, each of the form var or (var expression). Local variables are valid within the body of the let. – After the list of variables and values comes the body of expressions, which are evaluated in order. > (let ((x 100) (y 200)) (print (+ x y)) (setq x 200) (print (+ x y)) ‘foo) 300 400 FOO © Bassam Haddad 2010 19 10