Transcript ppt

1
Introduction to Programming
Languages and Evolution of the
Major Programming Languages
886340
Principles of Programming Languages
1 / 51
Course Goals
• To gain an understanding of the basic structure of
programming languages:
- Data types, control structures, naming conventions,...
• To learn the principles underlying all programming
languages:
- So that it is easier to learn new languages
• To study different language paradigms:
- Functional (Scheme)
- Imperative (C)
- Object-Oriented (C++, Java)
- Logic (Prolog)
• So that you can select an appropriate language for a task
2 / 51
What is Programming Language?
3 / 51
Definition of Programming Language I
“a programming language is a notation for
communication to a computer what we want to do”
• Before the middle of the 1940s, computer operators
“hardwired” their programs.
• In the late 1940s, John von Neumann had the idea.
• Data and programs stored in memory
• Memory is separate from CPU
• Instructions and data are piped from memory to CPU
4 / 51
The von Neumann Architecture
5 / 51
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
6 / 51
Machine Language and
the First Stored Programs
• At this point, computer operators became the first
true programmers.
• developed software
• the machine code
• to solve problems with computers
7 / 51
Machine Language
• In this program, each line of
code contains 16 bits.
• A line of 16 bits represents For example, A machine language program
either a single machine
language instruction or a
single data value.
• Program execution begins
with the first line of code,
which is fetched from
memory, decoded
(interpreted), and executed.
8 / 51
Assembly language
• mnemonic symbols for the instruction codes and
memory locations
• A program called an assembler translates the
symbolic assembly language code to binary machine
code.
For example, An LC-3 assembly language program that adds two numbers
9 / 51
Assembly language
• But….
• The programmer must still do the hard work of
translating the abstract ideas of a problem domain to
the concrete and machine-dependent notation of a
program.
• Computer hardware architecture has its own machine
language instruction set, and thus requires its own
dialect of assembly language.
•Low-Level Language
10 / 51
Definition of Programming Language II
“A programming language is a notation system for
describing computation in machine-readable and
human-readable form”
11 / 51
Computation
•กระบวนการ (process) ใดๆที่
คอมพิวเตอร ์สามารถทาได้
•การคานวณทางคณิ ตศาสตร ์
•การจัดการกับข้อมู ล (data
manipulation)
•การประมวลผลข้อความ (text
processing)
•การจัดเก็บและการค้นข้อมู ล
(information storage and retrieval)12 / 51
Machine readability
่ ยบง่ าย
•มีโครงสร ้างทีเรี
่
่ านการแปลแล้วต้องมีความ
•ภาษาเครืองที
ผ่
ช ัดเจนไม่กากวม
่ แปลต้องมี complexity อยู ่ใน
• algorithm ทีใช้
่
่
ระด ับทียอมร
ับได้ เพือให้
การแปลมีประสิทธิภาพ
้ ภาษาสาหร ับเขียนโปรแกรมจึงต้องมี
• ด ังนัน
รู ปแบบของไวยากรณ์แน่ นอน เรียกว่า
context free language
13 / 51
Human readability
•การทา abstraction หรือการแทน
รายละเอียดของการทางานของคอมพิวเตอร ์ที่
ซ ับซ ้อน
่
่
•เพือให้
ผูเ้ ขียนโปรแกรมเน้นความสนใจไปทีงาน
่ องการแก้ปัญา
ทีต้
้
•ด ังนันภาษาส
าหร ับเขียนโปรแกรมควรมี
โครงสร ้างและวิธก
ี ารทางานเป็ นแบบเดียวกับ
ภาษาธรรมชาติ (natural language)
14 / 51
Abstractions in
Programming Languages
15 / 51
Abstractions in Programming Languages
•Data abstractions
•การซ่อนรายละเอียดของการจัดเก็บข้อมู ลให้
อยู ่ในรู ปแบบทีง่่ ายต่อการใช้งาน เช่น
character string, numbers หรือ binary
tree เป็ นต้น
•Control abstractions
•การซ่อนรายละเอียดของการส่งผ่านการ
่ มี
่ เงื่อนไขในทาง
ควบคุม เช่น loops, คาสังที
ปฏิบต
ั ิ และการเรียกใช้โปรแกรมย่อย เป็ นต้น
16 / 51
Data abstractions
Basic abstraction
้
•การ abstract การจัดเก็บข้อมู ลพืนฐานใน
หน่ วยความจา
•การ abstract วิธก
ี ารดาเนิ นการทาง
้
คณิ ตศาสตร ์พืนฐานกับข้
อมู ล
่ เก็บข้อมู ลใน
•การ abstract ตาแหน่ งทีใช้
หน่ วยความจา
่
ตัวแปร คือ ชือของต
าแหน่ งในหน่ วยความจา และ
่ ดขึนจากการประกาศ
้
ชนิ ดของข้อมู ล ทีเกิ
(declaration) เช่น
17 / 51
Data abstractions
Structure abstraction
่
•การ abstract กลุ่มของข้อมู ลทีมี
ความสัมพันธ ์กัน (data structure)
่
•type declaration กาหนดชือของชนิ
ดข้อมู ล
่ นเลข
•เช่น การกาหนดกลุ่มของข้อมู ลทีเป็
จานวนเต็มเป็ น array เช่น
a : array [ 1 .. 10 ] of integer;
ใน
ภาษา Pascal หรือ
int a[10];
ในภาษา C
18 / 51
Data abstractions
Structure abstraction
•กาหนดให้มข
ี อ
้ มู ลชนิ ด record เช่น
type
date = record
day
: 1..31;
month : 1..12;
year : 2000..2100;
end;
ภาษา Pascal
struct date {
int
day;
int
month;
int
year;
};
typedef struct date date;
ภาษา C
19 / 51
Data abstractions
Unit abstractions
่ สร ้างและการใช้
• การแยกโปรแกรมในส่วนทีใช้
งานโครงสร ้างข้อมู ลออกไว้เป็ นสัดส่วน
•Data encapsulation
•module และ package
20 / 51
Control abstractions
• Basic abstractions
่
่
่
• การรวมคาสังภาษาเครื
องหลายค
าสัง่ เข้าเป็ นคาสังใน
่ ยว
ภาษาระดับสู งคาสังเดี
่ าหนดค่า (assignment statement) ใน
• เช่น คาสังก
ภาษา Pascal เช่น
x := x + 3;
่
• มีความหมายในคาสังภาษา
Assembly ของ 80x86
่ าหนดให้ x เป็ นตัวแปรขนาด 16 บิต
ดังต่อไปนี ้ เมือก
และมีตาแหน่ งในหน่ วยความจาเป็ น 1000
mov
dx, 1000
mov
ax, [dx]
mov
bx, 3
add ax, bx
21 / 51
Control abstractions
• Structured abstractions
่
่
• การซ ้อน (nested) คาสังหรื
อกลุ่มของคาสังไว้
ภายใต้เงื่อนไขการทดสอบ เช่น คาสัง่ if, switch
while, for ในภาษา
C
Ifcase,
x > 0.0 then
IF x > 0.0 THEN
begin
numSolns := 2;
r1 := sqrt(x);
r2 := -r1;
end
else
numSolns := 0;
numSolns := 2;
r1 := sqrt(x);
r2 := -r1;
ELSE
numSolns := 0;
END;
ภาษา Modula-2
if ( x > 0.0 ) {
numSolns = 2;
r1 = sqrt(x);
r2 = -r1;
} else
numSolns = 0;
ภาษา C
ภาษา Pascal
22 / 51
Control abstractions
• Structured abstractions
•การทาโปรแกรมย่อย เรียกว่า procedure หรือ
subprogram หรือ subroutine หรือ function
่
่ าด้วยกัน
•การรวมคาสังในภาษาหลายค
าสังเข้
่ ยว
เป็ นเสมือนหนึ่งคาสังเดี
•มีองค ์ประกอบ 2 ประการ
•procedure declaration
•procedure activation
23 / 51
Control abstractions
• Structured abstractions
• ตัวอย่าง เช่น
function gcd(u, v : integer) : integer;
var
t : integer;
begin
while v > 0 do begin
t := v;
v := u mod v;
u := t;
end;
gcd := u;
end;
ภาษา Pascal
int gcd ( int u, int v )
{
int
tmp;
while ( v > 0 ) {
t = v;
v = u % v;
u = t;
}
return u;
}
ภาษา C
24 / 51
Control abstractions
• Unit abstractions
่
•ภาษาโดยทัวไปจะมี
กลไกในการรวมโปรแกรม
่ การทางานคล้ายคลึงกันเข้าเป็ นกลุ่ม
ย่อยทีมี
่
เพือสะดวกต่
อการใช้งาน
•เช่น การ module ในภาษา Modular-2 และ
package ในภาษา Ada function ในภาษา C
เป็ นต้น
•ในภาษา C, header file เป็ นส่วนกาหนด
โครงสร ้างและชนิ ดของข้อมู ล
25 / 51
Computational Paradigms
26 / 51
What is a Paradigm?
• A way of looking at a problem and seeing a program
– What kind of parts do you look for?
Problem: Print a “large X” of size n.
– E.g., size 5 is
X
X
X X
X
X X
X
X
27 / 51
Paradigm
•Imperative Paradigm
•Functional Paradigm
•Logic Programming Paradigm
•Object-Oriented Paradigm
28 / 51
Imperative Paradigm
• A program is: A sequence of state-changing actions
• Manipulate an abstract machine with:
– Variables that name memory locations
– Arithmetic and logical operations
– Reference, evaluate, assign operations
– Explicit control flow statements
• Fits the Von Neumann architecture closely
• Key operations: Assignment and “Go To”
29 / 51
Example: Imperative Paradigm
30 / 51
Functional Paradigm
• A program is: Composition of operations on data
• Characteristics (in pure form):
– Name values, not memory locations
– Value binding through parameter passing
– Recursion rather than iteration
• Key operations: Function Assignment, Function
Application, and Function Abstraction– Based on
the Lambda Calculus
31 / 51
Example: Functional Paradigm
32 / 51
Logic Programming Paradigm
• A program is: Formal logical specification of problem
• Characteristics (in pure form):
– Programs say what properties the solution must
have, not how to find it
– Solutions are obtained through a specialized
form of theorem-proving
• Key
operations: Unification and NonDeterministic Search
– Based on First Order Predicate Logic
33 / 51
Example: Logic Programming Paradigm
34 / 51
Object-Oriented Paradigm
• A program is: Communication between abstract
objects
• Characteristics:
– “Objects” collect both the data and the
operations
– “Objects” provide data abstraction
– Can be either imperative or functional (or
logical)
• Key operation: Message Passing or Method Invocation
35 / 51
Example: Object-Oriented Paradigm
Java
36 / 51
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., HTML),
scripting (e.g., PHP), general-purpose (e.g., Java)
37 / 51
History of Programming
Languages
Click here
http://oreilly.com/pub/a/oreilly/news/languageposter_0504.html
38 / 51
History of Programming Languages
Prehistory
• 300 B.C. Greece, Euclid invented the greatest common divisor
algorithm - oldest known algorithm
• ~1820-1850 England, Charles Babbage invented two mechanical
computational devices
- difference engine
- analytical engine
- Countess Ada Augusta of Lovelace, first computer programmer
• Precursors to modern machines
- 1940’s United States, ENIAC developed to calculate trajectories
- 1950’s United States, first high-level PLs invented
39 / 51
History of Programming Languages
• Fortran 1954-57, John Backus (IBM on 704) designed
for numerical scientific computation
• fixed format for punched cards
• implicit typing
• only counting loops, if test versus zero
• only numerical data
• 1957 optimizing Fortran compiler translates into
code as efficient as hand-coded
40 / 51
History of Programming Languages
• Algol60 1958-60, designed by international
committee for numerical scientific computation
[Fortran]
• block structure with lexical scope
• free format, reserved words
• while loops, recursion
• explicit types
• BNF developed for formal syntax definition
• Cobol 1959-60, designed by committee in US,
manufacturers and DoD for business data processing
• Records
• focus on file handling
• English-like syntax
41 / 51
History of Programming Languages
• APL 1956-60 Ken Iverson, (IBM on 360, Harvard) designed
for array processing
• functional programming style
• LISP 1956-62, John McCarthy (MIT on IBM704,Stanford)
designed for non-numerical computation
• uniform notation for program and data
• recursion as main control structure
• garbage collection
• Snobol 1962-66, Farber, Griswold, Polansky (Bell
Labs)designed for string processing
• powerful pattern matching
42 / 51
History of Programming Languages
• PL/I 1963-66, IBM designed for general purpose
computing [Fortran, Algol60, Cobol]
• user controlled exceptions
• multi-tasking
• Simula67 1967, Dahl and Nygaard (Norway) designed as
a simulation language [Algol60]
• data abstraction
• inheritance of properties
• Algol68 1963-68, designed for general purpose
computing [Algol60]
• orthogonal language design
• interesting user defined types
43 / 51
History of Programming Languages
• Pascal 1969, N. Wirth(ETH) designed for teaching
programming [Algol60]
• 1 pass compiler
• call-by-value semantics
• Prolog 1972, Colmerauer and Kowalski designed for
Artificial Intelligence applications
• theorem proving with unification as basic operation
• logic programming
44 / 51
History of Programming Languages
Recent
• C 1974, D. Ritchie (Bell Labs) designed for systems
programming
• allows access to machine level within high-level PL
• efficient code generated
• Clu 1974-77, B. Liskov (MIT) designed for
simulation [Simula]
• supports data abstraction and exceptions
• precise algebraic language semantics
• attempt to enable verification of programs
45 / 51
History of Programming Languages
• Smalltalk mid-1970s, Alan Kay (Xerox Parc),
considered first real object-oriented PL, [Simula]
• encapsulation, inheritance
• easy to prototype applications
• hides details of underlying machine
• Scheme mid-1970s, Guy Steele, Gerald Sussman (MIT)
• Static scoping and first-class functions
46 / 51
History of Programming Languages
• Modula 1977 N. Wirth (ETH), designed language for
large software development [Pascal]
• to control interfaces between sets of procedures or
modules
• real-time programming
• Ada 1979, US DoD committee designed as general
purpose PL
• explicit parallelism – rendezvous
• exception handling and generics (packages)
47 / 51
History of Programming Languages
• C++ 1985, Bjorne Stroustrup (Bell Labs) general purpose
• goal of type-safe object-oriented PL
• compile-time type checking
• templates
• Java ~1995, J. Gosling (SUN)
• aimed at portability across platform through use of JVM
- abstract machine to implement the PL
• aimed to fix some problems with previous OOPLs¨
multiple inheritance¨ static and dynamic objects
• ubiquitous exceptions
• thread objects
48 / 51
Why learn more than one PL?
• So you can choose the right language for a
given problem
– If all you have is a hammer, every problem looks
like a nail
49 / 51
Why learn more than one PL?
• So you can learn a new language more easily later
– As your job changes, you may need to used
different languages
– As our understanding of programming improves,
new languages are created
• To learn new ways of thinking about problems
– Different languages encourage you to think
about problems in different ways
– “Paradigms”
50 / 51
Conclusion
and
Question
51 / 51