Programming Languages (1) -
Programming Languages (1) -
Tian-Li Yu
PL Generations
1st
2nd
Machine
Assembly
instructions
156C −−−−−−−−−−−−−−−−→ LD R5, Price
166D −−−−−−−−−−−−−−−−→ LD R6, ShippingCharge
5056 −−−−−−−−−−−−−−−−→ ADDI R0, R5, R6
306E −−−−−−−−−−−−−−−−→ ST R0, TotalCost
C000 −−−−−−−−−−−−−−−−→ HTL
Characteristics of assembly
- Machine dependent
- One-to-one mapping
- Assembler
High-level primitives
Machines independent (virtually)
One primitive to many MI mapping
Compiler & interpreter
Portability
- Theoretically: different compilers
- Reality: Minor modifications
Programming Paradigms
LISP ML Schema
Functional
C++ C#
Object-oriented
Smalltalk Visual Basic Java
Machine FORTRAN BASIC C Ada
Imperative
Languages COBOL ALGOL APL Pascal
GPSS Prolog
Declarative
1950 1960 1970 1980 1990 2000
Imperative paradigm
- Procedural
- Approaching a problem by finding an algorithm to solve the problem.
Declarative paradigm
- Implemented a general problem solver
- Approaching a problem by finding a formal description of the problem.
- Will talk more about this later.
Functional Paradigm
Inputs: Old_balance Credits Debits
Find_sum Find_sum
Find_diff
Output: New_balance
Object-Oriented Paradigm
References:
- http://www.codeproject.com/KB/architecture/OOP_Concepts_and_manymore.aspx
- http://en.wikipedia.org/wiki/Object-oriented_programming
Data Structure
Homogeneous array
Heterogeneous array
FORTRAN INTEGER a(6,3)
Pascal a: array[0..5,0..2] of integer;
C/C++
struct{
char Name[25];
int Age;
float SkillRating;
} Employee;
a ← b + 645;
- 645 is a literal
Operator precedence
Operator overloading
Control
Old-fashion: goto
goto 40
20 print "passed."
goto 70
40 if (grade < 60) goto 60
goto 20
60 print "failed."
70 stop
Control Structures
Condition Condition
true false
B?
S1 S2
if (B) S1
else S2;
Condition
false
B?
S1
S1;
False
Count < 4 ?
True
Comments
C/C++, Java
/**
/*
This is a
This is a
documentation
block comment
comment
*/
a = b + c; */
a = b + c;
Calling Procedures
Calling
program unit
Control is
transferred Procedure
to procedure
Calling program
unit continues
Control is returned to
calling enviroment when
procedure is completed.
Terminology
Starting the head with the term “void” is The former parameter list. Note
the way that a C programmer specifies that that C, as with many programming
the program unit is a procedure rather languages, requires that the data
than a function. We will learn about functions type of each parameter be specified.
shortly.
int Year;
Population[0] = 100.0;
for (Year = 0; Year =< 10; Year++)
Population[Year+1] = Population[Year] + (Population[Year]*GrowthRate);
Terminology (contd.)
Procedure’s header
Local vs. global variables
Formal vs. actual parameters
Passing parameters
- Call by value (passed by value)
- Call by reference (passed by reference)
- Call by address: variant of call-by-reference.
Call by Value
a. When the procedure is called, a copy of data
is given to the procedure
Calling environment Procedure’s environment
5 5
Demo(Actual);
Call by Reference
a. When the procedure is called, the formal parameter
becomes a reference to the actual parameter.
Calling environment Procedure’s environment
Actual Formal
5
procedure Demo(Formal )
Formal ← Formal + 1;
b. Thus, changes directed by the procedure are made
to the actual parameter
Demo(Actual); Calling environment Procedure’s environment
Actual Formal
6
C/C++
float Volume;
return Volume;
}
Terminate the function and return the Compute the volume of the cylinder
value of the variable Volume
This declares a local variable
named Volume.
Term
+
Term
Factor x
Factor
x
Starting: Expression
Nonterminals: Expression, Term, Factor
Terminals: x, y, z
Parse Tree
x +y ×z
Expression
+
Term Expression
Factor Term
x x
Factor Term
y Factor
Ambiguity
if B1 then if B2 then S1 else S2
Statement
Statement
B1
B1
if Boolean then
if Boolean then else
expression Statement
expression Statement Statement
B2 S1
B2 S1 S2
Code Generation
Strongly typed: no coercion, data types have to agree with each other.
Code optimization
- x = y + z;
- w = x + z;
- w = y + (z << 1);
OOP
Object
- Active program unit containing both data and procedures
Class
- A template from which objects are constructed
- An object is an instance of the class.
An Example of Class
Constructor assigns a value
to Remaining Power when
Instance variable an object is created.
class LaserClass
{ int RemainingPower;
LaserClass (InitialPower)
{ RemainingPower = InitialPower;
}
void turnRight ( )
{ ... }
void fire ( )
{ ... }
Encapsulation
Encapsulation
- A way of restricting access to the internal components of an object
- Bundling of data with the methods operating on that data.
Polymorphism
Polymorphism
- Allows method calls to be interpreted by the object that receives the call.
- Allows different data types to be handled using a uniform interface.
Circle circle;
Rectangle rect;
Circle();
Rectangle();
circle.draw();
rect.draw();
Inheritance
Inheritance
- Allows new classes to be defined in terms of previously defined classes.
Concurrency
Mutual Exclusion: A method for ensuring that data can be accessed by only one process at a time.
Monitor: A data item augmented with the ability to control access to itself
Calling
program unit
procedure is Procedure
activated.
Calling program
unit requests
procedure.
Both units
excute
simultaneiously.
Declarative Programming
Resolution
- Combining two or more statements to produce a new statement (that is a logical
consequence of the originals).
- (P OR Q) AND (R OR ¬Q) resolves to (P OR R)
- Resolvent: A new statement deduced by resolution
- Clause form: A statement whose elementary components are connected by OR
Unification
- Assigning a value to a variable so that two clauses would be the same.
- Unify(Father(Mark,John), Father(x,John)) results in x is Mark.
P OR Q R OR Q R P
P OR R
empty clause
Prolog
Variables: first letter capitalized (exactly contrary to common logics).
Constants: first letter uncapitalized.
Facts:
- Consists of a single predicate
- predicateName(arguments).
parent(bill, mary).
Rules:
- conclusion :- premise.
:- means “if”
- faster(X,Z) :- faster(X,Y), faster(Y,Z).
Operators:
“is”, ==,
=, <, >, +, -, *, /, =>, =<
Gnu Prolog
Comments
- /* */ or %
Chinese incompatible.
Prolog Examples
female(mary).
female(sue).
male(bill).
male(john).
parent(mary,john).
bill mary parent(bill,john).
parent(mary,sue).
parent(bill,sue).
mother(X,Y):−female(X),parent(X,Y).
john sue father(X,Y):−male(X),parent(X,Y).
son(X,Y):−male(X),parent(Y,X).
daughter(X,Y):−female(X),parent(Y,X).
sibling(X,Y):−X\=Y,parent(Z,X),parent(Z,Y).
Prolog Examples
Factorial again.
If we want Prolog to compute factorials, we need to tell it what factorials are.
factorial(0,1).
factorial(N,F) :−
| ?− factorial(5,W).
N>0,
W=120 ?
N1 is N−1,
factorial(N1,F1),
F is N * F1.
Fibonacci Revisited
Ordered Clauses
factorial(0,1).
factorial(N,F) :−
N>0, Try these commands:
factorial(N1,F1),
N1 is N−1, listing.
F is N * F1.
trace.
?−factorial(3,W).
notrace.