03-60-440 Principles of Programming Languages (2016F)cs.uwindsor.ca/~jlu/440/440overview2016.pdf ·...

23
03-60-440 Principles of Programming Languages (2016F) Jianguo Lu School of Computer Science University of Windsor

Transcript of 03-60-440 Principles of Programming Languages (2016F)cs.uwindsor.ca/~jlu/440/440overview2016.pdf ·...

Page 1: 03-60-440 Principles of Programming Languages (2016F)cs.uwindsor.ca/~jlu/440/440overview2016.pdf · 2016-09-11 · 03-60-440 Principles of Programming Languages (2016F) Jianguo Lu

03-60-440 Principles of Programming Languages (2016F)

Jianguo Lu School of Computer Science

University of Windsor

Page 2: 03-60-440 Principles of Programming Languages (2016F)cs.uwindsor.ca/~jlu/440/440overview2016.pdf · 2016-09-11 · 03-60-440 Principles of Programming Languages (2016F) Jianguo Lu

1

It is about programming languages…

•  This course involves several programming languages: – Scheme (Functional)

– Prolog (Logic)

– AspectJ – XSLT

– …

•  This course is more than excursions to various languages …

Page 3: 03-60-440 Principles of Programming Languages (2016F)cs.uwindsor.ca/~jlu/440/440overview2016.pdf · 2016-09-11 · 03-60-440 Principles of Programming Languages (2016F) Jianguo Lu

2

It is about the fundamental question

• What is a programming language?

Page 4: 03-60-440 Principles of Programming Languages (2016F)cs.uwindsor.ca/~jlu/440/440overview2016.pdf · 2016-09-11 · 03-60-440 Principles of Programming Languages (2016F) Jianguo Lu

3

What is a program

•  It is a sequence of statements!

– Imperative paradigm

•  It is a group of interacting objects!

– OO paradigm

•  It is a function! – Functional paradigm

•  It is a set of rules! – Logic paradigm

• …

Page 5: 03-60-440 Principles of Programming Languages (2016F)cs.uwindsor.ca/~jlu/440/440overview2016.pdf · 2016-09-11 · 03-60-440 Principles of Programming Languages (2016F) Jianguo Lu

4

It is about classification, history, styles (paradigms), concepts common in many languages •  Programming language classification and history

•  Syntax and semantics of programming languages – Syntax is covered in 60-214. Won’t repeat it. – Axiomatic semantics

•  Paradigms of programming –  Imperative and declarative programming – OOP: Abstract data type, inheritance, polymorphism, AOP (Aspect

Oriented Programming) – Functional programming, lambda calculus, Scheme, MapReduce – XML Programming: XSLT – Logic programming

•  Concepts in programming languages – Side-effect, type checking, strong typing, overload, override, coercion, call-

by-value, call-by-reference, polymorphism, multiple inheritance, generics, dynamic binding, imperative and declarative programming, …

Page 6: 03-60-440 Principles of Programming Languages (2016F)cs.uwindsor.ca/~jlu/440/440overview2016.pdf · 2016-09-11 · 03-60-440 Principles of Programming Languages (2016F) Jianguo Lu

5

Language description: Axiomatic Semantics

•  Language description: Syntax (covered in 03-60-214), semantics.

•  Formal methods to describe semantics: operational, denotational, axiomatic …

•  Axiomatic semantics is also used to prove the correctness of programs

–  Java has Assertion to describe the meaning of program

•  Axiomatic semantics uses a formal system (axiomatic system) to describe the meaning of programs

•  For example, {x=0} x= x+1; y= x; { x=1AND y=1}

{P} E x{P(E/x)} =

{P3} S2 S1; {P1}{P3} S2 {P2} {P2}, S1 {P1}

Axiom

Inference rule

Page 7: 03-60-440 Principles of Programming Languages (2016F)cs.uwindsor.ca/~jlu/440/440overview2016.pdf · 2016-09-11 · 03-60-440 Principles of Programming Languages (2016F) Jianguo Lu

6

Imperative programming •  Describes computation in terms of a program state and statements that change the

program state.

•  Imperative programs are a sequence of commands for the computer to perform.

•  The hardware implementation of almost all computers is imperative. –  Von Neumann machine

•  In contrast to declarative programming

Insertion sort in Java void (int[ ] A) {

int j; for (int i = 1; i < A.length; i++) { int a = A[i]; for (j = i -1; j >=0 && A[j] > a; j- -) A[j + 1] = A[j]; A[j + 1] = a; }

}

insertionSort

Page 8: 03-60-440 Principles of Programming Languages (2016F)cs.uwindsor.ca/~jlu/440/440overview2016.pdf · 2016-09-11 · 03-60-440 Principles of Programming Languages (2016F) Jianguo Lu

7

Functional Programming

•  Functional programming treats computation as the evaluation of mathematical functions.

•  It is more heavily used in academia than in industry.

•  The strength of a functional paradigm is the removal of side-effects during computation. This has uses in

– program verification, for checking the correctness of programs, – program optimisation. One particular use in program optimisation is to

transform programs for parallel programming. – The idea is used in Map-Reduce programming, widely used to process big

data.

z = f(sqrt(2), sqrt(2)); we can factor out sqrt(2) and write s = sqrt(2); z = f(s, s);

Page 9: 03-60-440 Principles of Programming Languages (2016F)cs.uwindsor.ca/~jlu/440/440overview2016.pdf · 2016-09-11 · 03-60-440 Principles of Programming Languages (2016F) Jianguo Lu

8

Functional Programming vs. Imperative Programming

•  In strict functional programming, there is no explicit memory allocation and no explicit variable assignment, so side effects of function evaluation are eliminated.

•  Looping is accomplished through the more general functional construct of recursion.

Sort in ML: fun insertsort [] = [] | insertsort (x::xs) = let fun insert (x:real, []) = [x] | insert (x:real, y::ys) = if x<=y then x::y::ys else y::insert(x, ys) in insert(x, insertsort xs) end;

Page 10: 03-60-440 Principles of Programming Languages (2016F)cs.uwindsor.ca/~jlu/440/440overview2016.pdf · 2016-09-11 · 03-60-440 Principles of Programming Languages (2016F) Jianguo Lu

9

sort function defined in Scheme language

(define (insert x l)

( if (null? l)

(list x)

(if (<= x (car l))

(cons x l)

(cons (car l) (insert x (cdr l))))))

(define (isort l)

(if (null? l)

()

(insert (car l) (isort (cdr l)))))

Page 11: 03-60-440 Principles of Programming Languages (2016F)cs.uwindsor.ca/~jlu/440/440overview2016.pdf · 2016-09-11 · 03-60-440 Principles of Programming Languages (2016F) Jianguo Lu

Map-reduce programming

• Derived from functional programming

•  Find wide applications in big data processing – Google, Yahoo, …

• Distribute operations over multiple machines

• Hadoop

10

Page 12: 03-60-440 Principles of Programming Languages (2016F)cs.uwindsor.ca/~jlu/440/440overview2016.pdf · 2016-09-11 · 03-60-440 Principles of Programming Languages (2016F) Jianguo Lu

11

Logic Programming

•  Program is a set of facts and rules. •  Based on first-order predicate logic •  Original motivation: study of mechanical theorem proving •  Used in Artificial Intelligence, databases, expert systems.

•  Insertion sort in prolog isort([ ],[ ]). isort([X|UnSorted],AllSorted) :- isort(UnSorted,Sorted), insert(X,Sorted,AllSorted). insert(X, [ ], [X]). insert(X, [Y|L], [X, Y|L]) :- X =< Y. insert(X, [Y|L], [Y|IL]) :- X > Y, insert(X, L, IL).

Page 13: 03-60-440 Principles of Programming Languages (2016F)cs.uwindsor.ca/~jlu/440/440overview2016.pdf · 2016-09-11 · 03-60-440 Principles of Programming Languages (2016F) Jianguo Lu

12

OOP

•  Abstract Data Type

• Multiple inheritance in Java

•  Polymorphism – Overloading

– Generics – Dynamic binding

Page 14: 03-60-440 Principles of Programming Languages (2016F)cs.uwindsor.ca/~jlu/440/440overview2016.pdf · 2016-09-11 · 03-60-440 Principles of Programming Languages (2016F) Jianguo Lu

13

Distributed Object and Object Persistency

from www.agiledata.org

Page 15: 03-60-440 Principles of Programming Languages (2016F)cs.uwindsor.ca/~jlu/440/440overview2016.pdf · 2016-09-11 · 03-60-440 Principles of Programming Languages (2016F) Jianguo Lu

14

AOP—Aspect Oriented Programming

•  A new programming paradigm

•  Example void transfer(Account fromAccount, Account toAccount, int amount) { if (fromAccount.getBalance() < amount) { throw new InsufficientFundsException(); } fromAccount.withdraw(amount); toAccount.deposit(amount); }

•  However, in a real-world banking application, this transfer method is not adequate. We need to:

–  Include security checks to verify that the current user has the authorization to perform this operation.

–  Enclose the operation in a database transaction in order to prevent accidental data loss.

–  Log the operation to the system log. And so on.

Page 16: 03-60-440 Principles of Programming Languages (2016F)cs.uwindsor.ca/~jlu/440/440overview2016.pdf · 2016-09-11 · 03-60-440 Principles of Programming Languages (2016F) Jianguo Lu

15

Aspect Oriented Programming void transfer(Account fromAccount, Account toAccount,

int amount) { if (!getCurrentUser().canPerform(OP_TRANSFER)) { throw new SecurityException(); } if (fromAccount.getBalance() < amount) { throw new InsufficientFundsException(); } Transaction tx = database.newTransaction(); try { fromAccount.withdraw(amount); toAcount.deposit(amount); tx.commit(); systemLog.logOperation(OP_TRANSFER,

fromAccount, toAccount, amount); } catch(Exception e) { tx.rollback(); } }

•  The code has lost its elegance and simplicity

– various new concerns tangled with the basic functionality (business logic concern).

– The transactions, security, logging, etc. all exemplify cross-cutting concerns.

•  Implementation of crosscutting concerns are scattered across numerous methods.

– Change of the implementation would require a major effort.

•  Solution: Separate different concerns.

Page 17: 03-60-440 Principles of Programming Languages (2016F)cs.uwindsor.ca/~jlu/440/440overview2016.pdf · 2016-09-11 · 03-60-440 Principles of Programming Languages (2016F) Jianguo Lu

16

XSLT

•  It is an XML-based language – Used to transform XML;

– The language itself is in XML.

•  It is a declarative language – Does not list an imperative sequence of actions to perform in a

stateful environment,

– Consists of a template rules, specifies what to add to the result.

XML

XSLT

XSLT processor

Page 18: 03-60-440 Principles of Programming Languages (2016F)cs.uwindsor.ca/~jlu/440/440overview2016.pdf · 2016-09-11 · 03-60-440 Principles of Programming Languages (2016F) Jianguo Lu

Implementation techniques of programming languages •  A continuation from 214

•  attribute grammar – Parsing result

•  type checking – Type inference rules and their implementations

• Garbage collection – How to manage memory more efficiently

•  Follow the text in the dragon book

17

Page 19: 03-60-440 Principles of Programming Languages (2016F)cs.uwindsor.ca/~jlu/440/440overview2016.pdf · 2016-09-11 · 03-60-440 Principles of Programming Languages (2016F) Jianguo Lu

18

Robert Sabastia, Von Roy &Haridi, John Mitchell, Benjemin Pierce, Glynn Winskel, Kenneth Louden, Seyed Roosta, Pratt & Zelkowitz

Page 20: 03-60-440 Principles of Programming Languages (2016F)cs.uwindsor.ca/~jlu/440/440overview2016.pdf · 2016-09-11 · 03-60-440 Principles of Programming Languages (2016F) Jianguo Lu

Bonus assignment: (8%) • Count method calls in JDK(Java Development Kit)

•  Parsing real large programs (7000+ files) – Get all the details of the programs

• Using JDT (Eclipse Java Development Tool)

19

Page 21: 03-60-440 Principles of Programming Languages (2016F)cs.uwindsor.ca/~jlu/440/440overview2016.pdf · 2016-09-11 · 03-60-440 Principles of Programming Languages (2016F) Jianguo Lu

20

Times and places •  Course web site: http://cs.uwindsor.ca/~jlu/440

•  Office: 5111 Lambton Tower

•  Email: jlu at uwindsor

•  Office hours: Monday & Wednesday 2:30-3:30

•  Midterm: October 24

•  Examinations are closed books and closed notes.

•  The only valid excuse for missing an exam is a documented medical emergency. A missed exam without medical documentation will result in a mark of zero.

Page 22: 03-60-440 Principles of Programming Languages (2016F)cs.uwindsor.ca/~jlu/440/440overview2016.pdf · 2016-09-11 · 03-60-440 Principles of Programming Languages (2016F) Jianguo Lu

21

Marking scheme

Assignments 15 % Midterm 35 % Final exam 50 %

Page 23: 03-60-440 Principles of Programming Languages (2016F)cs.uwindsor.ca/~jlu/440/440overview2016.pdf · 2016-09-11 · 03-60-440 Principles of Programming Languages (2016F) Jianguo Lu

22

•  Three assignments (15 %): –  (5%) Aspect oriented programming;

–  (5%) Functional and Logic programming;

–  (5%) XSLT programming;

•  Bonus assignment (8%) –  Generate parse tree of real Java programs.

•  TA will help you with your assignments

•  There is no text book.

•  Class attendance is important !

•  Exams will cover whatever is taught in class;

•  Exams will cover the assignments.