240 likes | 929 Views
Programming Languages and Paradigms. History on programming Why are there so many languages What makes a language successful Spectrum of programming languages. Brief History on Computer Programming. Programming with the first electronic computers in 1940s
E N D
Programming Languages and Paradigms History on programming Why are there so many languages What makes a language successful Spectrum of programming languages
Brief History on Computer Programming Programming with the first electronic computers in 1940s • Filling several rooms, costing millions of 1940s dollars • Computer’s time was more valuable than programmer’s • They programmed in machine languages (sequence of bits) 27bdffd0 afbf0014 0c1002a8 00000000 0c1002a8 afa2001c ... • Requiring a less error-prone notation, inventing assembly language addiu sp, sp, -32 sw ra, 20(sp) jal getint nop jal getint sw v0, 28(sp)
Brief History on Computer Programming • Translating from assembly language into machine language is straightforward due to one-to-one correspondence: assembler • But people began to wish for a machine-independent language • Frustrating to rewrite assembly program for every new machine • Difficult to maintain large assembly programs • Especially for numerical programs • Fortran was developed in mid-50s, followed by Lisp and Algol • Translating from high-level language into assembly language is more complicated than assemblers: compiler • Fortran program was slower at first than assembly program • But over time, performance gap narrowed, and eventually reversed (pipelining, ILP); optimizing compiler usually generates better code • Also, it is now important to economize programmer’s efforts in development and maintenance (time-to-market is more important)
Why Are There So Many Programming Languages? • Evolution: constantly finding better ways to do programming • e.g. structured programming: in 60s and early 70s, goto-based Fortran, Cobol, Basic gave way to while loops case statements • e.g. object-oriented programming: in late 80s, nested block-structured Pascal, Algol, Ada gave way to OO-structured C++, Smalltalk, Eiffel • Special purpose: many designed for specific domain • Lisp for manipulating symbolic data and complex data structures • C is good for low-level system programming (notably, for UNIX) • Prolog is good for reasoning about logical relationship among data • Snobol is good for manipulating character strings • Personal preference: different people like different things • Some people like the terseness of C or C++; some hate it • Some people like to think recursively; others prefer iteration • Some people like to work with pointers; others like implicit dereference
What Makes a Language Successful? • Expressive power: more “powerful” than others • In principle, all languages are equivalent (Church’s conjecture) • Still, language features have a huge impact in writing good code • Ease of use for novice: “learning curve” is important • Basic was successful due to its very low learning curve • Part of reasons for Java’s success • Ease of implementation: • Basic: implemented on tiny machines • Pascal: simple, portable implementation with free distribution (Pascal compiler, P-code, P-code interpreter) • Java: similar simple implementation
What Makes a Language Successful? • Excellent compilers • Fortran compilers have been extremely good (also its language design) • Common Lisp was successfully partly because its compiler and tools • Economics, patronage and inertia: non-technical factors • Powerful sponsor: Cobol and PL/1 to IBM, Ada to DoD • Huge base of installed software Need to consider viewpoints of both programmers and implementors • In early days, implementor’s viewpoint was important; PL is a means of telling a computer what to do (implementation efficiency) • But for programmers, PL is a means of expressing algorithms, constraining what can/cannot be expressed; so it affects what programmer can think (conceptual clarity) Knuth says “programming is art of telling another human what on wants computer to do” (compromise of both efficiency and clarity)
Programming Language Spectrum PLs can be classified based on their computation models • Declarative • Focus is what the computer is to do (programmer’s viewpoint) • subclassified into: • Functional: Lisp.Scheme, ML, Haskell • Logic : Prolog, VisiCalc • Dataflow : Id, Val • Imperative • Focus is how the computer should do it (implementor’s viewpoint) • Imperative predominates mainly for performance reasons • subclassified into • von Neumann : Fortran, Pascal, C, Basic, ... • Object-oriented: C++, Java, Smalltalk, Eiffel
Functional Language Computational model based on recursive definition of functions • Inspired by lambda calculus, a formal model by Church in 30’s • Program is considered a function from input to output • Lisp, Scheme, ML, Haskell Most scheme/Lisp interpreter repeats: read-evaluate-print (+ 3 4) 7 To create a function, one evaluates a lambda expression: (lambda (x) (* x x)) => function Calling a function ((lambda (x) (* x x)) 3) => 9
Scheme/Lisp Computational model based on recursive definition of functions (car ‘(2,3,4)) => 2 (cdr ‘(2,3,4)) => (3,4) (cons 2 ‘(3,4)) => (2,3,4) High-order functions (functions that take functions as arguments) (define compose (lambda (f g) (lambda (x) (f (g x))))) (compose car cdr) ‘(1,2,3)) => 2
Logic or Constraint-Based Language Computational model inspired by propositional logic (e.g. Prolog) • Programmer states a collection of axioms; user states a theorem (goal), and the system find axioms that satisfy the goal • Axioms are specified in Horn clauses H <- B1, B2, B3, .., Bn (meaning that H is true is B1, B2,..Bn are true) • New statements are derived through resolution C <- A, B D <- C ---------- D <- A,B • During resolution, free variables may get values via unification wet (X) <- rainy (X) rainy (Seoul) (Horn clause for a fact) ---------------- wet (Seoul)
Prolog • There are three types of Horn clauses • Fact (w/o RHS) rainy(Seattle). rainy(Seoul). cold(Seoul) • Rule snowy(X) <- rainy(X), cold(X) • Query (goal) ?- snowy(C) C = Seoul • List processing append( [], A, A). Append( [H | T], A, [H|L]) :- append(T, A, L) ?- append([a,b,c], [d,e], L) L = [a,b,c,d,e]
Dataflow Language Computation model based on data flows rather than control flows • Flow of information (tokens) among primitive functional nodes • Provides inherently parallel model of computation: nodes are triggered by arrivial of input tokens • Id, Val, Sisal
Von Neumann Language Computational model based on modification of variables • Computing via side-effects • While function PL are based on expressions that have values, von Neumann PL are based on statements (assignments) that influence subsequent computation by changing the value of memory • Fortran, C, Pascal, Basic, ...
An Example: GCD (a,b) • Imperative programmers says: To compute GCD of a and b, check if a and b are equal. If so, print one of them and stop. Otherwise, replace the larger one by their difference and repeat program gcd (input, output) var I, j: integer; begin read (I, j); while I <> j do if I > j then I = I - j else j = j - I; writeln (I); end
An Example: GCD (a,b) • Functional programmers says: The GCD of a and b is defined to be a when a and b are equal, and to be the GCD of c and d when a and b are unequal, where c is the smaller of a and b, and d is their difference. To compute the gcd of a given pair of numbers, expand and simplify this until finish • Logic programmer says The proposition gcd (a,b,g) is true if (1) a, b, g are all equal, or (2) there exists numbers c and d such that c is the minimum of a and b (I.e., min(a,b,c) is true), d is their difference (I.e., minus (a,b,d) is true), and gcd(c,d,g) is true. To compute the gcd of a given pair of numbers, search for a number g for which these two rules allow one to prove that gcd (a,b,g) is true
Object-Oriented Language Related to von Neumann but has a more structured and distributed model of both memory and computation • Rather than picture computation as a monolithic processor on a monolithic memory, OO picture it as interactions among semi-independent objects, each of which has its own state and executable functiosn to manage that state • C++, Java, Smalltalk, .. • We will now talk about the paradigm of object-oriented programming in detial