Evolution of programming languages Machine language Assembly language

Evolution of programming languages – Machine language – Assembly language – Sub-routines and loop (Fortran) – Procedures and recursion (Algol, Pascal, C) – Modules (Modula-2, Ada) – Objects (Simula, Smalltalk, C++, Java) – Declarative programming languages (Prolog, CLP, Lisp, ML, Haskall) by Neng-Fa Zhou 1

Why are there so many languages 4 Evolution – Procedural structural object-oriented 4 New paradigms and applications – Logic languages (Prolog, CLP) for complex data and knowledge processing – Functional languages (Lisp, ML, Haskell) for symbolic computation – Scripting languages (Java. Script, Pearl, Tcl, Python, Ruby, XSLT) for Web-based data processing 4 Personal preferences by Neng-Fa Zhou 2

What makes a language successful 4 Expressiveness 4 Availability of implementations 4 Efficiency 4 Productivity 4 Industrial sponsorships by Neng-Fa Zhou 3

Why study programming languages 4 Understand language features and concepts at a higher level 4 Improve the ability to choose appropriate languages 4 Increase the ability to learn new languages 4 Simulate useful features by Neng-Fa Zhou 4

Why study language implementation 4 Understand how languages are specified and implemented 4 Understand obscure phenomena 4 Write better-style and efficient programs 4 Design and implement domain-specific languages by Neng-Fa Zhou 5

Programming language spectrum 4 Declarative – Logic and constraint-based (Prolog, CLP(FD)) – Functional (Lisp/Scheme, ML, Haskell) – Dataflow (Id, Val) – Template-based (XSLT) – Database (SQL) 4 Imperative – von Neumann (C, Ada, Fortran, Pascal, …) – Scripting (Perl, Python, PHP, …) – Object-oriented (Smalltalk, Effel, C++, Java, C#) by Neng-Fa Zhou 6

Imperative 4 Features – Variables are mnemonics of memory locations – Assignment statements – goto – Iterative constructs by Neng-Fa Zhou 7

Stack in C typedef struct Node. Struct { int val; struct Node. Struct *next; } Node, *Node. Ptr, *List; typedef struct Stack. Struct { int size; List elms; } Stack, *Stack. Ptr; by Neng-Fa Zhou 8

Stack in C (Cont. ) void stack_push(Stack. Ptr s, int x){ s->size++; lst_add(&s->elms, x); } int stack_pop(Stack. Ptr s){ if (s->size==0){ error("empty stack"); } else { s->size--; return lst_remove(&s->elms); } by Neng-Fa Zhou } 9

Object-oriented 4 Features – Abstract data types – Inheritance and overriding – Polymorphism – Dynamic binding by Neng-Fa Zhou 10

Stack in Java import java. util. Linked. List; class My. Stack { private Linked. List<Integer> elms; public My. Stack() { elms = new Linked. List<Integer>(); } public void push(int x) { elms. add. First(x); } public int pop() { if (elms. size()==0){ throw new Runtime. Exception("Empty stack"); } else return elms. remove. First(); } } by Neng-Fa Zhou 11

Stack in C# using System; using System. Collections. Generic; class My. Stack { private Linked. List<int> elms; public My. Stack() { elms = new Linked. List<int>(); } public void push(int x) { elms. Add. First(x); } public int pop() { if (elms. Count==0){ throw new System. Exception("stack underflow"); } else { int tmp = elms. First. Value; elms. Remove. First(); return tmp; by Neng-Fa Zhou } } 12

Stack in C++ class Stack { public: Stack(); void push(int); int pop(); private: list<int> elms; }; Stack: : Stack(){ } void Stack: : push(int x){ elms. push_front(x); } int Stack: : pop(){ assert(!elms. empty()); int x = elms. front(); elms. pop_front(); return x; } by Neng-Fa Zhou 13

Functional 4 Features – Single assignment variables (no side effects) – Recursion – Rule-based and pattern matching (ML, Haskell) – High-order functions – Lazy evaluation (Haskell) – Meta-programming (Scheme) by Neng-Fa Zhou 14

Stack in Scheme (define stack_push (lambda (s x) (cons x s))) (define stack_peek (lambda (s) (if (eq? s ()) (raise "empty stack") (car s)))) (define stack_pop (lambda (s) (if (eq? s ()) (raise "empty stack") (cdr s)))) by Neng-Fa Zhou 15

Stack in Haskell stack_push s x = (x: s) stack_peek (x: _) = x stack_pop (_: s) = s by Neng-Fa Zhou 16

Stack in SML/NJ fun stack_push s x = (x: : s) fun stack_peek (x: : s) = x fun stack_pop (_: : s) = s by Neng-Fa Zhou 17

F# let stack_push s x = x : : s let stack_peek s = match s with | x : : _ -> x let stack_pop s = match s with | _ : : s 1 -> s 1 by Neng-Fa Zhou 18

Logic & constraint-based 4 Features – Logic variables – Recursion – Unification – Backtracking – Meta-programming by Neng-Fa Zhou 19
![Stack in Prolog stack_push(S, X, [X|S]). stack_pop([X|S], X, S). by Neng-Fa Zhou 20 Stack in Prolog stack_push(S, X, [X|S]). stack_pop([X|S], X, S). by Neng-Fa Zhou 20](http://slidetodoc.com/presentation_image_h/2f8bd5672569bbfcfe450800f3ccf181/image-20.jpg)
Stack in Prolog stack_push(S, X, [X|S]). stack_pop([X|S], X, S). by Neng-Fa Zhou 20
![Stack in Picat stack_push(S, X) = [X|S]. stack_peek([X|_]) = X. stack_pop([_|S]) = S. by Stack in Picat stack_push(S, X) = [X|S]. stack_peek([X|_]) = X. stack_pop([_|S]) = S. by](http://slidetodoc.com/presentation_image_h/2f8bd5672569bbfcfe450800f3ccf181/image-21.jpg)
Stack in Picat stack_push(S, X) = [X|S]. stack_peek([X|_]) = X. stack_pop([_|S]) = S. by Neng-Fa Zhou 21

Implementation methods 4 Compilation – Translate high-level program to machine code • Slow translation • Fast execution 4 Pure interpretation – No translation • Slow execution • Becoming rare 4 Hybrid implementation systems – Small translation cost – Medium execution speed by Neng-Fa Zhou 22

Review questions 4 Why are there so many programming languages? 4 What makes a programming language successful? 4 Why is it important to study programming languages? 4 Name two languages in each of the following paradigms: procedural, OOP, logic, and functional. 4 What are the features of OOP languages? 4 What are the features of functional languages? 4 What are the features of logic languages? by Neng-Fa Zhou 23
- Slides: 23