Disclaimer The lecture slides for this semester are based upon the - - PDF document

disclaimer
SMART_READER_LITE
LIVE PREVIEW

Disclaimer The lecture slides for this semester are based upon the - - PDF document

Disclaimer The lecture slides for this semester are based upon the slides used by Dr. Adam Webber. Credit Programming Paradigms and thanks are due to Dr. Webber; mistakes are mine. Primary Language Classification 01 Introduction to Language


slide-1
SLIDE 1

1

01 Introduction to Language Paradigms CS631 Fall 2000 1

Programming Paradigms

Primary Language Classification

01 Introduction to Language Paradigms CS631 Fall 2000 2

Disclaimer

The lecture slides for this semester are based upon the slides used by Dr. Adam Webber. Credit and thanks are due to Dr. Webber; mistakes are mine.

01 Introduction to Language Paradigms CS631 Fall 2000 3

Related Reading

Chapter 1 Programming Languages Concepts and Constructs, Ravi Sethi

01 Introduction to Language Paradigms CS631 Fall 2000 4

Overview

Why bother studying programming languages? Classification of programming languages Strengths and weaknesses of each paradigm Best uses for each of the paradigms

“Don’t fight the paradigm” “Use the right tool for the job”

01 Introduction to Language Paradigms CS631 Fall 2000 5

Why Study Programming Languages?

Computer science is a young, evolving field. Today’s cool language is tomorrow’s ‘has been’ “Everything old is new again” The more you know about programming

languages, the easier it will be to learn the “next great language.”

It is an active research area.

01 Introduction to Language Paradigms CS631 Fall 2000 6

Classification: Four Language Families

Imperative Object-oriented Functional Logic
slide-2
SLIDE 2

2

01 Introduction to Language Paradigms CS631 Fall 2000 7

The Imperative Language Paradigm

An imperative program is a sequence of

statements.

Each statement is a directive to change the

“environment” (memory, devices, etc.).

Changes are cumulative, so the sequence must

be done in the order specified.

Imperative languages are tightly modeled to the

computer hardware

01 Introduction to Language Paradigms CS631 Fall 2000 8

Imperative Example: Factorial in C

Imperative characteristics: assignment statements (= and *=) loop depends on side-effects sequence of statements with implied ordering void main(int argc, char *argv[]) { int n = atoi(argv[1]) ; int sofar = 1; while (n > 1) sofar *= n--; printf (“%d\n”, sofar); }

01 Introduction to Language Paradigms CS631 Fall 2000 9

Imperative Languages

All the most popular languages

– COBOL – BASIC – C – Fortran – Ada – Pascal – and many others

01 Introduction to Language Paradigms CS631 Fall 2000 10

Imperative Paradigm Strengths

People seem to be able to program in this mode

most easily.

Since it is derived from the way computer

hardware works, it is easy to translate into efficient machine language.

01 Introduction to Language Paradigms CS631 Fall 2000 11

Imperative Language Drawback #1: Difficult to Reason About

Informally, programmers try to prove to

themselves that the program works.

Formally, the holy grail for many researchers is

to find a way to formally prove that a program is correct.

But imperative languages make it difficult: you

can’t reason about any part of the program independently of the other parts. . .

a = f(x,y); b = f(x,y); // Does a equal b? Or f might change a: int f(int x, int y) { a = 0; return x + y; } f might not be referentially transparent: int f(int x, int y) { return system_clock; } Or a and x might be aliased: int a; int& x = a; ... a = f(x,y); b = f(x,y);

Example

slide-3
SLIDE 3

3

01 Introduction to Language Paradigms CS631 Fall 2000 13

If a computer has multiple processors, you can

speed up your program if you can make use of more than one processor.

This is only possible if we can identify parts that

do not depend on being executed in the standard

  • rder.
Side effects and implied sequencing in

imperative languages make this difficult to do automatically.

Imperative Lang. Drawback #2 (1): Hard to Parallelize

01 Introduction to Language Paradigms CS631 Fall 2000 14

Imperative Lang. Drawback #2 (2): Parallelization, Example 1

a = 0; b = a; b = a; a = 0; For each group of C statements below, is it possible for a non-standard order of execution to give different results from the standard order? a = 0; a = 1; a = 0; b = 0;

01 Introduction to Language Paradigms CS631 Fall 2000 15

*p = 1; *q = 2; *p = 0; *q = 0; For each group of C statements below, is it possible for a non-standard order of execution to give different results from the standard order?

Imperative Lang. Drawback #2 (3): Parallelization, Example 2

01 Introduction to Language Paradigms CS631 Fall 2000 16

for (int i = 0; i < 100; i++) a[i] = i * 2; for (int i = 1; i < 100; i++) a[i] = a[i-1] * 2; For each group of C statements below, is it possible for a non-standard order of execution to give different results from the standard order?

Imperative Lang. Drawback #2 (4): Parallelization, Example 3

01 Introduction to Language Paradigms CS631 Fall 2000 17

The Object-Oriented Paradigm

A object oriented program is a set of objects. Each object has private state, and presents a

strictly-enforced interface to be used by other

  • bjects.
A computation is the interaction of a collection
  • f such objects.

class MyInt

{

private int value;

public MyInt(int value) { this.value = value; } public int getValue() { return value; }

public MyInt getFactorial()

{

return new MyInt(fact(value));

}

private int fact(int n)

{

int sofar = 1; while (n > 1) sofar *= n--; return sofar;

} }

“I am a MyInt object”

OO Example Factorial in Java

“I hold an int value” “I can be created” “I can tell you my value” “I can return a MyInt object

for my factorial... ”

“…but how I calculate my

factorial is my own business ”

slide-4
SLIDE 4

4

01 Introduction to Language Paradigms CS631 Fall 2000 19

An object has a state that is altered via method

calls (side effects).

Objects execute code that looks like ordinary

imperative code

The Object-Oriented Paradigm Is it really just imperative?

Object-oriented programming is really a variation

  • n the theme of imperative programming.

01 Introduction to Language Paradigms CS631 Fall 2000 20

Object-oriented Languages

Simula Smalltalk C++ Java Eiffel

01 Introduction to Language Paradigms CS631 Fall 2000 21

Object Oriented Benefits (1)

Better Organization of Large Programs

Encapsulation: objects hide implementation

details from other objects

Inheritance: objects can inherit functionality

from other objects (makes code reuse simpler) Encapsulation and inheritance also help moderate the drawbacks of imperative programs...

01 Introduction to Language Paradigms CS631 Fall 2000 22

Easier to reason about:

– OOP helps since private properties of objects can be

reasoned about in isolation from the rest of the program.

Easier to parallelize:

– OOP helps since the strict interfaces among objects

make good places at which to divide up the computation among machines.

Object Oriented Benefits (2)

Mitigate Drawbacks of Imperative

01 Introduction to Language Paradigms CS631 Fall 2000 23

OO Drawbacks

OOP adds an extra discipline applied to

imperative programs, but at a cost:

– Programs can be longer – Languages can be more complex – Paradigm can take longer to master

Bad OOP code is often worse than bad imperative code

01 Introduction to Language Paradigms CS631 Fall 2000 24

The Functional Paradigm

A functional program is a collection of function

definitions.

Each function is free of side-effects: it has no

detectable permanent effect on memory, devices, etc.

Modeled on functions in mathematics.
slide-5
SLIDE 5

5

01 Introduction to Language Paradigms CS631 Fall 2000 25

Functional Example Factorial in ML

fun fact x = if x = 0 then 1 else x * fact(x-1); Functional characteristics: No assignment statements Recursion (not necessarily present) Referential transparency

01 Introduction to Language Paradigms CS631 Fall 2000 26

Functional Languages

Lisp-y languages

– Common Lisp, Scheme, T

More recent functional languages

– Haskell, Gofer, ML

“Pure” functional

– FP

01 Introduction to Language Paradigms CS631 Fall 2000 27

Functional Benefits (1) Easier to Parallelize

Side-effects are not allowed. Any execution order is safe.

We know these can be evaluated simultaneously, without even seeing the definitions of g and h. f( g(a), h(a) )

01 Introduction to Language Paradigms CS631 Fall 2000 28

Functional Benefits (2) Easier to Reason About

When all functions exhibit referential

transparency we know that calling the same function with the same parameters always produces the same result.

( ) ( ) ( )

2 2 1 1 2 1 2 1

, , y x f y x f y y x x = ⇒ = ∧ =

01 Introduction to Language Paradigms CS631 Fall 2000 29

Functional Paradigm Drawbacks

No side effects is both a blessing and a

curse

– Pro: You can reason about the program – Con: I/O is technically not allowed

Paradigm can be difficult to learn

– Recursion can be a very difficult concept – No assignment statements

01 Introduction to Language Paradigms CS631 Fall 2000 30

The Logic Paradigm

A logic program is a collection of formal logical

assertions and rules of inference.

Execution means proving a new assertion (the

“goal”) from the part already given.

Modeled on mathematical first-order logic.
slide-6
SLIDE 6

6

01 Introduction to Language Paradigms CS631 Fall 2000 31

Factorial Written in Prolog

fact(0,1). fact(X,Y) :- X>0, R is X-1, fact(R, Z), Y is X*Z. This is a logical definition of the relation, “Y is the factorial of X”. ?- fact(4,10). No You can then ask, “can you prove that 10 is the factorial of 4?” (No!) ?- fact(4,X). X = 24 Or you can ask, “can you prove that X is the factorial of 4?” (Yes, if X is 24!)

01 Introduction to Language Paradigms CS631 Fall 2000 32

Logic Programming Languages

Basically, just Prolog Plus some dialects (such as Datalog for database

queries)

01 Introduction to Language Paradigms CS631 Fall 2000 33

Logic Programming Benefits

Logic languages give neat solutions for

certain kinds of problems:

– dynamic database of facts – pattern-matching, backtracking search

More declarative in nature

– You express the problem logically, without

saying exactly how to solve it

Higher-level languages

01 Introduction to Language Paradigms CS631 Fall 2000 34

Logic Programming Drawbacks

Not entirely declarative

– Side effects such as I/O require forcing imperative

paradigms onto logic paradigm

Hard to judge runtime behavior

– Is the program efficient? – Will a program terminate?

Difficult paradigm to learn

01 Introduction to Language Paradigms CS631 Fall 2000 35

Complexity Results

All these high-level programming

languages have equivalent power.

And all have the same power as various

mathematical models of computation: Turing machines, lambda calculus, etc.

01 Introduction to Language Paradigms CS631 Fall 2000 36

Which Paradigm is Best?

No right answer. Certain problems are better suited for certain

paradigms than others.

No hard and fast rules--decision to use a

language may be based on non-paradigm issues. But match the paradigm to the language--don’t fight the paradigm. Forcing the wrong paradigm on the language results in bad code.

slide-7
SLIDE 7

7

01 Introduction to Language Paradigms CS631 Fall 2000 37

Fighting the Paradigm: Imperative ML

fun fact n = let val i = ref 1; val xn = ref n in while !xn>1 do ( i := !i * !xn; xn := !xn - 1 ); !i end; ML makes it hard to use assignment and side-effects. But it is still possible: Don’t write your ML code this way!

01 Introduction to Language Paradigms CS631 Fall 2000 38

Fighting the Paradigm: Imperative Java

Java, more than C++, tries to encourage you to adopt an

  • bject-oriented mode. But you can still put your whole

program into static methods of a single class: class Fubar { public static void main (String[] args) { // Put the whole program here and/or // use nothing but static methods } }

01 Introduction to Language Paradigms CS631 Fall 2000 39

Fighting the Paradigm: Functional Pascal

procedure ForLoop(Low,High: Integer): Boolean; begin if Low <= High then begin {for-loop body here} ForLoop(Low+1, High) end end;

Any imperative language that supports recursion can be used as a functional language:

01 Introduction to Language Paradigms CS631 Fall 2000 40

Conclusions

Four basic programming language paradigms Each language has its own set of strengths and

weaknesses

The best way to express an algorithmic idea

depends on the language in which you are trying to express it. Don’t fight the paradigm--use the right paradigm for the language.

01 Introduction to Language Paradigms CS631 Fall 2000 41

Review Questions

What are the 4 programming language

paradigms?

What makes the paradigm unique? What are the strengths and weaknesses of each

paradigm?