47
CST223 1_introduction 1 Programming Languages • A programming language is a language that is intended to be used by a person to express a process by which a computer can solve a problem. • ?? ? – Is a calculator a programming language? – Is a spreadsheet a programming language? – Is HTML a programming language?

CST2231_introduction1 Programming Languages A programming language is a language that is intended to be used by a person to express a process by which

Embed Size (px)

Citation preview

CST223 1_introduction 1

Programming Languages

• A programming language is a language that is intended to be used by a person to express a process by which a computer can solve a problem.

• ?? ?– Is a calculator a programming language?– Is a spreadsheet a programming language?– Is HTML a programming language?

CST223 1_introduction 2

Computational Theory

• Anything that could be computed could, in theory, be computed on a simple device, a Turing Machine.

• If our language can simulate a Turing Machine, then in theory, it can compute anything that’s computable.

• If so, we have a programming language.

• ???

CST223 1_introduction 3

Computational Theory

• Turing Tarpit Argument– ““Anything your language can do, my language can Anything your language can do, my language can

do as well, so there!do as well, so there!””

• All languages have exactly the same power.

• Why do we need more than one language then?

• This argument is a tarpit because it is deceptively simple, but gets you nowhere.

• Everything is possible, but nothing is easy.

CST223 1_introduction 4

A simpler definition

• A language is a Programming Language iff it has the power for expressing 3 fundamental constructs in the problem solving process:– Sequence (sequential execution of statements)– Conditional (if, switch/case)– Loops (while, for)

• A programming language is said to be “Turing Complete”.

CST223 1_introduction 5

Language and Thought

• Instead of theoretical computation power, we are concerned with ease of use.

• How easily is it to communicate what we want the computer to do in some language?

• Structure of a language defines the boundaries of thought.

• Most of what we express is expressed in language, and must suffer the constraints of the medium.

CST223 1_introduction 6

Language and Thought

• Languages predispose us to viewing the world in a certain way.

• The same thing is true in programming languages, to an even greater extent.

• Once someone has learned their first programming language, they tend to approach all problems from within that language.

CST223 1_introduction 7

Why study PLs?

• Improve problem-solving ability.

• Make better use of a language.

• Choose an appropriate language.

• Easier to learn a new language.

• Better language designers.

• Increased capacity to express ideas.

CST223 1_introduction 8

Programming Domains• Scientific applications

– Large numbers of floating point computations; use of arrays– Fortran

• Business applications– Produce reports, use decimal numbers and characters– COBOL

• Artificial intelligence– Symbols rather than numbers manipulated; use of linked lists– LISP

• Systems programming– Need efficiency because of continuous use– C

• Web Software– Eclectic collection of languages: markup (e.g., XHTML), scripting

(e.g., PHP), general-purpose (e.g., Java)

CST223 1_introduction 9

Language Evaluation Criteria• Readability: the ease with which

programs can be read and understood• Writability: the ease with which a

language can be used to create programs

• Reliability: conformance to specifications (i.e., performs to its specifications)

• Cost: the ultimate total cost

CST223 1_introduction 10

Evaluation Criteria: Readability• Overall simplicity

– A manageable set of features and constructs– Minimal feature multiplicity – Minimal operator overloading

• Orthogonality – A relatively small set of primitive constructs can be

combined in a relatively small number of ways– Every possible combination is legal

• Data types– Adequate predefined data types

• Syntax considerations– Identifier forms: flexible composition – Special words and methods of forming compound

statements– Form and meaning: self-descriptive constructs,

meaningful keywords

CST223 1_introduction 11

Evaluation Criteria: Writability• Simplicity and orthogonality

– Few constructs, a small number of primitives, a small set of rules for combining them

• Support for abstraction– The ability to define and use complex structures or

operations in ways that allow details to be ignored

• Expressivity– A set of relatively convenient ways of specifying

operations– Strength and number of operators and predefined

functions

CST223 1_introduction 12

Evaluation Criteria: Reliability• Type checking

– Testing for type errors

• Exception handling– Intercept run-time errors and take corrective measures

• Aliasing– Presence of two or more distinct referencing methods

for the same memory location

• Readability and writability– A language that does not support “natural” ways of

expressing an algorithm will require the use of “unnatural” approaches, and hence reduced reliability

CST223 1_introduction 13

Evaluation Criteria: Cost• Training programmers to use the

language• Writing programs (closeness to particular

applications)• Compiling programs• Executing programs• Language implementation system:

availability of free compilers• Reliability: poor reliability leads to high

costs• Maintaining programs

CST223 1_introduction 14

Evaluation Criteria: Others

• Portability– The ease with which programs can be

moved from one implementation to another

• Generality– The applicability to a wide range of

applications

• Well-definedness– The completeness and precision of the

language’s official definition

CST223 1_introduction 15

Influences on Language Design

• Computer Architecture– Languages are developed around the prevalent

computer architecture, known as the von Neumann architecture

• Programming Methodologies– New software development methodologies (e.g.,

object-oriented software development) led to new programming paradigms and by extension, new programming languages

CST223 1_introduction 16

Computer Architecture Influence

• Well-known computer architecture: Von Neumann • Imperative languages, most dominant, because of von

Neumann computers– Data and programs stored in memory

– Memory is separate from CPU

– Instructions and data are piped from memory to CPU

– Basis for imperative languages• Variables model memory cells

• Assignment statements model piping

• Iteration is efficient

CST223 1_introduction 17

The von Neumann Architecture

CST223 1_introduction 18

The von Neumann Architecture

• Fetch-execute-cycle (on a von Neumann architecture computer)

initialize the program counter

repeat forever

fetch the instruction pointed by the counter

increment the counter

decode the instruction

execute the instruction

end repeat

CST223 1_introduction 19

Programming Methodologies Influences

• 1950s and early 1960s: Simple applications; worry about machine efficiency

• Late 1960s: People efficiency became important; readability, better control structures– structured programming

– top-down design and step-wise refinement

• Late 1970s: Process-oriented to data-oriented– data abstraction

• Middle 1980s: Object-oriented programming– Data abstraction + inheritance + polymorphism

CST223 1_introduction 20

Language Categories• Imperative

– Central features are variables, assignment statements, and iteration

– Include languages that support object-oriented programming– Include scripting languages– Include the visual languages– Examples: C, Java, Perl, JavaScript, Visual BASIC .NET, C++

• Functional– Main means of making computations is by applying functions

to given parameters– Examples: LISP, Scheme

• Logic– Rule-based (rules are specified in no particular order)– Example: Prolog

• Markup/programming hybrid – Markup languages extended to support some programming– Examples: JSTL, XSLT

CST223 1_introduction 21

Language Design Trade-Offs• Reliability vs. cost of execution

– Example: Java demands all references to array elements be checked for proper indexing, which leads to increased execution costs

• Readability vs. writabilityExample: APL provides many powerful operators (and a

large number of new symbols), allowing complex computations to be written in a compact program but at the cost of poor readability

• Writability (flexibility) vs. reliability– Example: C++ pointers are powerful and very flexible

but are unreliable

CST223 1_introduction 22

Implementation Methods

• Compilation– Programs are translated into machine language

• Pure Interpretation– Programs are interpreted by another program known as an

interpreter

• Hybrid Implementation Systems– A compromise between compilers and pure interpreters

CST223 1_introduction 23

Genealogy of

Common Languages

CST223 1_introduction 24

Brief History of PL

• Early Languages– Fortran– Cobol– Algol60– Lisp– APL– Basic

CST223 1_introduction 25

Algol-based Languages

• PL/I

• Simula 67

• ALGOL68

• Pascal

CST223 1_introduction 26

Languages of the 80’s

• Prolog

• Smalltalk

• C/C++

• Modula-2

• Ada

CST223 1_introduction 27

Language of the 90’s and beyond

• Domain-Specific Languages (www, database, GUI, etc.)

• Visual Programming Languages

• Visual Environments

• Scripting languages

• Libraries

CST223 1_introduction 28

Learning a new language

• Questions to ask:– What problem domain is the language intended for?

– What is the programming model?

– What are the basic objects (data structures) manipulated in the language?

– What are the things that you can do with these objects?

– How is control flow managed in the language?

– How is information flow managed in the language?• Parameter passing, scope rules, declaration statements, etc.

CST223 1_introduction 29

Learning a new language

• Problem-oriented.

• Syntax is unimportant at the conceptual stage.

• Syntax is the least interesting part of learning a new language.

CST223 1_introduction 30

Programming Models

• Paradigms– Imperative– Functional– Object-Oriented– Logical– Distributed/Parallel– Constraints– Declarative– ....

CST223 1_introduction 31

Imperative Paradigm

• Dictatorship:– Processor in control– Stuff things in new places in the processor memory.– State modification - program consists of a sequence

of modification to the memory.– Somewhere in the state modification lies the answer.

CST223 1_introduction 32

Imperative Example

cin >> i;

X = x + i;

y++;

Z = x / y;

for (int j=0; j<i; j++)

a = a + Z;

i

X

Y

Z

j

a

CST223 1_introduction 33

Functional Paradigm

• Mathematician: – Express the process in an abstract way.– Programming has to do with specifying definitions

much like math functions.– Program describe, in an abstract way, the operations

that must be performed to solve the problem.

CST223 1_introduction 34

Functional Example

• QuickSort a list of items

QuickSort(empty) = empty

QuickSort(head, tail) = QuickSort(list < head) +

head +

QuickSort(list >= head)

* + concatenation

CST223 1_introduction 35

Object-Oriented Paradigm

• Committee– Send messages to ask members to do stuff– Objects have states.– Only objects can change their own states. – Divide up the responsibilities.

CST223 1_introduction 36

Logical Paradigm

• Philosopher– People ask questions– Program is a logical description of the problem

expressed as facts and rules.– Output or answers are derived from the facts.

CST223 1_introduction 37

Problem Example

Dexter wants to send flowers to Mimi

Dexter Mimi

CST223 1_introduction 38

Imperative Solution

• Allocate space for $$$ & flower• Get money from bank• Store money• Retrieve money• Give money to florist• Get flower from florist• Store flower• Arrive at destination• Retrieve flower• Deliver flower

$$$

CST223 1_introduction 39

OO Solution

Dexter Mimi

Bank Teller

Florist

Delivery Person

CST223 1_introduction 40

Functional Solution

• What needs to be done here?– Cash Withdraw

– Buy Flowers

– Delivery Flowers

• What order do they need to happen?– Cash -> Flowers -> Deliver

• Functions have parameters and return values

Deliver( Buy (Withdraw()))

CST223 1_introduction 41

Logical Solution

• Ask the Question:

ReceiveFlower(Mimi)?

• Let’s see the rules and facts.

CST223 1_introduction 42

Rules

ReceiveFlower(x): DeliverFlower(y, x)

DeliverFlower(y,x):

Florist(y) & OrderFlower(z, y) & Person(x)

OrderFlower(x,y): Florist(y) & Person(x) & HasMoney(x) & GiveMoney(x, y)

CST223 1_introduction 43

Facts

HasMoney(Dexter)

Florist(Flo)

GiveMoney(Dexter, Flo)

Now ask the question: ReceiveFlower(Mimi)?

???

What’s missing?

CST223 1_introduction 44

Facts

Dog (Dexter)

Dog(Mimi)

ReceiveFlower(Mimi)?

NO

CST223 1_introduction 45

Problem Example

Dexter wants to send flowers to Mimi

CST223 1_introduction 46

Other Paradigms

• Distributed / Parallel

• Constraints– Formulas

• Other names for functional– Declarative– Applicative

CST223 1_introduction 47

Declarative Paradigm

• Specify the WHAT instead of HOW

• Definitional and not operational

• Includes functional, logical, constraints