Download PLD VII Haddad

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Tail call wikipedia , lookup

Currying wikipedia , lookup

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

Combinatory logic wikipedia , lookup

Lambda calculus definition wikipedia , lookup

Transcript
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