CSC304 Week 1 Slides
CSC304 Week 1 Slides
Programming is like …
1st View: Imperative & Procedural
• Computers take commands and do operations
• Thus, programming is like …
issuing procedural commands to the computer
– Example: a factorial function in C
int fact(int n) {
int sofar = 1;
while (n>0) sofar *= n--;
return sofar;
}
• Since almost all computers today use the von
Neumann architecture → PL mimic the
architecture.
The von Neumann Architecture
Program counter
(Fig. 1.1)
The von Neumann Architecture
• Key features:
– Data and programs stored in memory
– Instructions and data are piped from memory to
CPU
– Fetch-execute-cycle for each machine instruction
initialize the program counter (PC)
repeat forever
fetch the instruction pointed by PC
add A,B,C 0100 1001
increment the counter
sub C,D,E 0110 1010
decode the instruction br LOOP 1011 0110
execute the instruction … …
end repeat Assembly Machine
code code
Imperative Language and Architecture
• Example: a factorial function in C
int fact(int n) {
int sofar = 1;
while (n>0) sofar *= n--;
return sofar;
}
– Indicates that data n,
sofar, and program code
are stored in memory
– Program code instructs
CPU to do operations
Imperative Language and Architecture
sofar
n
int fact(int n) {
int sofar = 1;
while (n>0) sofar *= n--;
return sofar; }
Program counter
Imperative Languages
• Imperative languages, e.g., C, C++, Java, which
dominate programming, mimic von Neumann
architecture
– Variables → memory cells
– Assignment statements → data piping between
memory and CPU
– Operations and expressions → CPU executions
– Explicit control of execution flows → prog. counter
• Allow efficient mapping between language and
hardware for good execution performance, but
limited by von Neumann bottleneck
Layers of Abstraction/Translation
High Level Language temp = v[k];
Program
v[k] = v[k+1];
Compiler v[k+1] = temp;
Assembly Language lw $15, 0($2)
Program
lw $16, 4($2)
Assembler sw$16, 0($2)
sw$15, 4($2)
Machine Language 0000 1001 1100 0110 1010 1111 0101 1000
Program 1010 1111 0101 1000 0000 1001 1100 0110
1100 0110 1010 1111 0101 1000 0000 1001
0101 1000 0000 1001 1100 0110 1010 1111
Machine
Interpretation
All imperative!
Control Signal
Specification
ALUOP[0:3] <= InstReg[9:11] & MASK
2nd View: Functional
• Programming is like …
solving mathematical functions, e.g.,
z = f(y, g(h(x)))
– A program, and its subprograms, are just
implementations of mathematical functions
– Example: a factorial function in ML
fun fact x = Input Input
if x <= 0
then 1
else x * fact(x-1); Program Function
Output Output
Another Functional Language: LISP
• Example: a factorial function in Lisp
(defun fact (x)
(if (<= x 0) 1 (* x (fact (- x 1)))))
– Computations by applying functions to parameters
– No concept of variables (storage) or assignment
• Single-valued variables: no assignment, not storage
– Control via recursion and conditional expressions
• Branches → conditional expressions
• Iterations → recursion
– Dynamically allocated linked lists
• 2nd-oldest general-purpose PL still in use (1958)
3rd View: Logic
• Programming is like …
logic induction
– Program expressed as rules in formal logic
– Execution by rule resolution
– Example: relationship among people
fact: mother(joanne,jake).
father(vern,joanne).
rule: grandparent(X,Z) :- parent(X,Y),
parent(Y,Z).
goal: grandparent(vern,jake).
Logic Programming
• Non-procedural
– Only supply relevant facts (predicate calculus) and
inference rules (resolutions)
– System then infer the truth of given queries/goals
• Highly inefficient, small application areas
(database, AI)
– Example: a factorial function in Prolog
fact(X,1) :- X =:= 1.
fact(X,Fact) :-
X > 1, NewX is X - 1,
fact(NewX,NF),
Fact is X * NF.
Summary: Language Categories
Imperative Functional
Logic Language
Language Language
Memory
ALU Control