Programming Languages - Oregon Institute of Technology

Download Report

Transcript Programming Languages - Oregon Institute of Technology

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
1
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
2
Computational Theory
• Turing Tarpit Argument
– “Anything your language can do, my language can
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
3
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
4
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
5
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
6
Why study PLs?
•
•
•
•
•
•
CST223
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.
1_introduction
7
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
8
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
9
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
10
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
11
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
12
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
13
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
14
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
15
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
16
The von Neumann Architecture
CST223
1_introduction
17
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
18
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
19
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
20
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. writability
Example: 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
21
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
22
Genealogy
of
Common
Languages
CST223
1_introduction
23
Brief History of PL
• Early Languages
–
–
–
–
–
–
CST223
Fortran
Cobol
Algol60
Lisp
APL
Basic
1_introduction
24
Algol-based Languages
•
•
•
•
CST223
PL/I
Simula 67
ALGOL68
Pascal
1_introduction
25
Languages of the 80’s
•
•
•
•
•
CST223
Prolog
Smalltalk
C/C++
Modula-2
Ada
1_introduction
26
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
27
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
28
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
29
Programming Models
• Paradigms
–
–
–
–
–
–
–
–
CST223
Imperative
Functional
Object-Oriented
Logical
Distributed/Parallel
Constraints
Declarative
....
1_introduction
30
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
31
Imperative Example
i
cin >> i;
X = x + i;
y++;
Z = x / y;
for (int j=0; j<i; j++)
a = a + Z;
X
Y
Z
j
a
CST223
1_introduction
32
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
33
Functional Example
• QuickSort a list of items
QuickSort(empty) = empty
QuickSort(head, tail) = QuickSort(list < head) +
head +
QuickSort(list >= head)
* + concatenation
CST223
1_introduction
34
Object-Oriented Paradigm
• Committee
–
–
–
–
CST223
Send messages to ask members to do stuff
Objects have states.
Only objects can change their own states.
Divide up the responsibilities.
1_introduction
35
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
36
Problem Example
Dexter wants to send flowers to Mimi
Dexter
CST223
Mimi
1_introduction
37
Imperative Solution
•
•
•
•
•
•
•
•
•
•
CST223
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
1_introduction
$$$
38
OO Solution
Bank
Teller
Dexter
Mimi
Delivery
Person
Florist
CST223
1_introduction
39
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
40
Logical Solution
• Ask the Question:
ReceiveFlower(Mimi)?
• Let’s see the rules and facts.
CST223
1_introduction
41
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
42
Facts
HasMoney(Dexter)
Florist(Flo)
GiveMoney(Dexter, Flo)
Now ask the question: ReceiveFlower(Mimi)?
???
What’s missing?
CST223
1_introduction
43
Facts
Dog (Dexter)
Dog(Mimi)
ReceiveFlower(Mimi)?
NO
CST223
1_introduction
44
Problem Example
Dexter wants to send flowers to Mimi
CST223
1_introduction
45
Other Paradigms
• Distributed / Parallel
• Constraints
– Formulas
• Other names for functional
– Declarative
– Applicative
CST223
1_introduction
46
Declarative Paradigm
• Specify the WHAT instead of HOW
• Definitional and not operational
• Includes functional, logical, constraints
CST223
1_introduction
47