26
COMP 524: Programming Languages Srinivas Krishnan January 11, 2011 Based in part on slides and notes by Bjoern Brandenburg, S. Olivier and A. Block. Programming Languages -An Overview- 1 Monday, January 17, 2011

Programming Languages - Computer Science 524: Programming Languages Srinivas Krishnan January 11, 2011 Based in part on slides and notes by Bjoern Brandenburg, S. Olivier and A. Block

  • Upload
    doandan

  • View
    219

  • Download
    3

Embed Size (px)

Citation preview

Page 1: Programming Languages - Computer Science 524: Programming Languages Srinivas Krishnan January 11, 2011 Based in part on slides and notes by Bjoern Brandenburg, S. Olivier and A. Block

COMP 524: Programming LanguagesSrinivas KrishnanJanuary 11, 2011

Based in part on slides and notes by Bjoern Brandenburg, S. Olivier and A. Block.

Programming Languages-An Overview-

1Monday, January 17, 2011

Page 2: Programming Languages - Computer Science 524: Programming Languages Srinivas Krishnan January 11, 2011 Based in part on slides and notes by Bjoern Brandenburg, S. Olivier and A. Block

Srinivas Krishnan Spring 201101/11/2011

Brief History of Languages

22Monday, January 17, 2011

Page 3: Programming Languages - Computer Science 524: Programming Languages Srinivas Krishnan January 11, 2011 Based in part on slides and notes by Bjoern Brandenburg, S. Olivier and A. Block

Srinivas Krishnan Spring 201101/11/2011

Long Long Time Ago

3

Build it Program it !!

3Monday, January 17, 2011

Page 4: Programming Languages - Computer Science 524: Programming Languages Srinivas Krishnan January 11, 2011 Based in part on slides and notes by Bjoern Brandenburg, S. Olivier and A. Block

Srinivas Krishnan Spring 201101/11/2011

Design Leap• Fixed program computer:

• Designed to perform a single task

• Von Neumann architecture: Stored-Program

• Flexibility to reprogram a machine

44Monday, January 17, 2011

Page 5: Programming Languages - Computer Science 524: Programming Languages Srinivas Krishnan January 11, 2011 Based in part on slides and notes by Bjoern Brandenburg, S. Olivier and A. Block

Srinivas Krishnan Spring 201101/11/2011

Early days• Early realization of the Von Neumann architecture: ENIAC

• Programming by toggling switches and cables: Weeks

• Used in the Manhattan project

55Monday, January 17, 2011

Page 6: Programming Languages - Computer Science 524: Programming Languages Srinivas Krishnan January 11, 2011 Based in part on slides and notes by Bjoern Brandenburg, S. Olivier and A. Block

Srinivas Krishnan Spring 201101/11/2011

Evolution: Machine Code

• Early programmers were limited by

• Expressivity of Machine Code: Hex or Binary

• High Error Rate

• Slow Development and Insanity !!!

6

Punch Card: http://commons.wikimedia.org

6Monday, January 17, 2011

Page 7: Programming Languages - Computer Science 524: Programming Languages Srinivas Krishnan January 11, 2011 Based in part on slides and notes by Bjoern Brandenburg, S. Olivier and A. Block

Srinivas Krishnan Spring 201101/11/2011

Machine Code to Assembly Language• Machine code represents CPU instructions

• Hard for humans to parse

• First Evolution:• Translate instructions to mnemonics

7

• 1010101 [0x55]• 10001001000011100101 [0x89 0xe5]• 100000111110110000111100 [0x83 0xec 0x3c]

• 0x55 push %ebp• 0x89 0xe5 mov %esp,%ebp• 0x83 0xec 0x3c sub $0x3c,%esp

Opcode Operand

7Monday, January 17, 2011

Page 8: Programming Languages - Computer Science 524: Programming Languages Srinivas Krishnan January 11, 2011 Based in part on slides and notes by Bjoern Brandenburg, S. Olivier and A. Block

Srinivas Krishnan Spring 201101/11/2011

Assembly • Evolution: Use computers

to generate machine code

• No need to worry about variable length instructions

• Assembler: • Generate machine code

from assembly languages.• Still in use: NASM, C asm

8

Example:Intel x86-32 machine code of C main( )

8Monday, January 17, 2011

Page 9: Programming Languages - Computer Science 524: Programming Languages Srinivas Krishnan January 11, 2011 Based in part on slides and notes by Bjoern Brandenburg, S. Olivier and A. Block

Srinivas Krishnan Spring 201101/11/2011

Why not Assembly ?• Limitations:

• Machine dependent• Not portable• No error checking• Hard to read and tedious to write

• Even simple tasks required 100’s of lines of code

• Very high barrier to learn, not suitable for scientists

99Monday, January 17, 2011

Page 10: Programming Languages - Computer Science 524: Programming Languages Srinivas Krishnan January 11, 2011 Based in part on slides and notes by Bjoern Brandenburg, S. Olivier and A. Block

Srinivas Krishnan Spring 201101/11/2011

Why not Assembly

10

qsort( a, lo, hi ) int a[], hi, lo;{ int h, w, p, t; if (lo < hi) { w = lo; h = hi; p = a[hi]; do { while ((w < h) && (a[w] <= p)) w = w+1; while ((h > w) && (a[h] >= p)) h = h-1; if (w < h) { t = a[w]; a[w] = a[h]; a[h] = t; } } while (w < h);

t = a[w]; a[w] = a[hi]; a[hi] = t;

qsort( a, lo, w-1 ); qsort( a, w+1, hi ); }}

10Monday, January 17, 2011

Page 11: Programming Languages - Computer Science 524: Programming Languages Srinivas Krishnan January 11, 2011 Based in part on slides and notes by Bjoern Brandenburg, S. Olivier and A. Block

Srinivas Krishnan Spring 201101/11/2011

Higher-Level Languages• Assembly is still very close to the hardware, hard to master and

write extensible programs

• Requirements:

• Data and Control flow abstractions: Loops, Conditionals etc

• Extensible and Portable: Machine Independent

• Low barrier to entry: Human Friendly Syntax

• Take advantage of computation power to generate machine code using Compilers and Interpreters

1111Monday, January 17, 2011

Page 12: Programming Languages - Computer Science 524: Programming Languages Srinivas Krishnan January 11, 2011 Based in part on slides and notes by Bjoern Brandenburg, S. Olivier and A. Block

Srinivas Krishnan Spring 201101/11/2011

What is a Programming Language ?

12

Star Stories and other languages, including the major justifications that the test led to his own. This is usually prepared by the infection of the Sinai to the back and the Star Destroyers in the parliament, by the speed of these books and the revival of environmental problems of their new Arab states of the Arctic as a more and the Vertex cover problem. All the work that he flew with the University and establishes that the BBCs list of the attention that the ICCs powers should have been due to CFCs used by the program is a day of St Cyprian, his party and to India to an entire city, and in space was a second home in its day of its people, in the States and northwest of the eye to the States are in order of 5 to the Second Largest Paris in the rotors to the business was in France in an amount of a strike over a network by the consensus is a new ethnic group for his museum, which had served and in it to put any of the computer that could then take the next in the remaining rights to the population of this speed when the others in a single by the Council that the football is a goal is for children of those was the first US Festival for the championship in 1991, with the left and a sequence of the story, of which reached the stage of his ashes to a god, and the 'astern Europe, has the other that the other where he saw him that the former GDR might cover This major human gut ur which was the largest city of the two countries. The British and the French, and was not the first time, and was not the case in the history of the war, Hong Kong was returned to the U.S. Rather, such a declaration of war, and in the United States President Bill Clinton was elected to the United States. In the Yuan dynasty. One of the United States, and the United Provinces at the time of the Soviet government.

Is this a language ?

It will execute !!!

Source:Mason, Joshua; Small, Sam; Monrose, Fabian; and MacManus, Greg. English Shellcode. CCS 2009

12Monday, January 17, 2011

Page 13: Programming Languages - Computer Science 524: Programming Languages Srinivas Krishnan January 11, 2011 Based in part on slides and notes by Bjoern Brandenburg, S. Olivier and A. Block

Srinivas Krishnan Spring 201101/11/2011

•Is Turing Complete• Simulate any turning machine

•Reality: • Simulate turning machine with space constraints

What is a Programming Language ?

13

A programming language is a formal language that is both➡ universal (any computable function can be defined)➡ implementable (on existing hardware platforms).

What is a Programming Language?➡ Java?➡ HTML?➡ Javascript?➡ LaTeX?

13Monday, January 17, 2011

Page 14: Programming Languages - Computer Science 524: Programming Languages Srinivas Krishnan January 11, 2011 Based in part on slides and notes by Bjoern Brandenburg, S. Olivier and A. Block

Srinivas Krishnan Spring 201101/11/2011

Language Design Tradeoffs

14

Declarative Imperative

Provide base primitives to build the algorithm

Ease of expressing the algorithm

Close to the user Tradeoff Performance

Close to the machine:Mostly Efficient

Shallow learning curve:Problem Domain specific

Potentially steep learning curve

Design Tradeoff

“do what I mean”

“do exactly what I say”

14Monday, January 17, 2011

Page 15: Programming Languages - Computer Science 524: Programming Languages Srinivas Krishnan January 11, 2011 Based in part on slides and notes by Bjoern Brandenburg, S. Olivier and A. Block

Srinivas Krishnan Spring 201101/11/2011

High Level Language History

15

FORTRANJohn Backus (IBM)1954 First real alternative

to assemblyScientific

Computing

LISPMac Carthy1958

Symbolic Computing

ArtificialIntelligence

1960

AlgolBackus, Bauer et al

Specify Algorithms

Design from First Principles

COBOLGrace Hopper

Data Processing Simple Syntax: Almost English like

Still In Use

15Monday, January 17, 2011

Page 16: Programming Languages - Computer Science 524: Programming Languages Srinivas Krishnan January 11, 2011 Based in part on slides and notes by Bjoern Brandenburg, S. Olivier and A. Block

Srinivas Krishnan Spring 201101/11/2011

Language Spectrum

16

Declarative ImperativeClose to the machine:How to perform a task

Close to the user :Steps needed to achieve it

Procedural / Von Neumann(Ex: Fortran, Pascal, C)

Object-Oriented(Ex: Smalltalk, Eiffel, C++, Java)

Functional(Ex: LISP/Scheme, ML, Haskell)

Logic and constraint-based(Ex: Prolog)

Dataflow(Ex: Id, Val)

Scripting(Ex: Shell, TCL, Perl, Python)

16Monday, January 17, 2011

Page 17: Programming Languages - Computer Science 524: Programming Languages Srinivas Krishnan January 11, 2011 Based in part on slides and notes by Bjoern Brandenburg, S. Olivier and A. Block

Srinivas Krishnan Spring 201101/11/2011

Language Spectrum

17

ImperativeClose to the machine:How to perform a task

Procedural / Von Neumann(Ex: Fortran, Pascal, C)

Object-Oriented(Ex: Smalltalk, Eiffel, C++, Java)

Scripting(Ex: Shell, TCL, Perl, Python)

Procedural Languages

• Progression in programming languages from assembly

• Model how computers work e.g. Load Store

• Most popular language family: C, Fortran

• Modeled directly on manipulating data (variables) usually simple typed data.

• A procedural program defines a sequential computation of statements that directly manipulates data in memory.

• Abstractions of execution units is achieved via subroutines

17Monday, January 17, 2011

Page 18: Programming Languages - Computer Science 524: Programming Languages Srinivas Krishnan January 11, 2011 Based in part on slides and notes by Bjoern Brandenburg, S. Olivier and A. Block

Srinivas Krishnan Spring 201101/11/2011

Language Spectrum

18

ImperativeClose to the machine:How to perform a task

Procedural / Von Neumann(Ex: Fortran, Pascal, C)

Object-Oriented(Ex: Smalltalk, Eiffel, C++, Java)

Scripting(Ex: Shell, TCL, Perl, Python)

Object Oriented• Next-Generation of Languages: Distributed

Model of computation

• Modeled as interactions between objects that collaborate with each other

• Complex problems can be decomposed and the interactions specified as procedural code

18Monday, January 17, 2011

Page 19: Programming Languages - Computer Science 524: Programming Languages Srinivas Krishnan January 11, 2011 Based in part on slides and notes by Bjoern Brandenburg, S. Olivier and A. Block

Srinivas Krishnan Spring 201101/11/2011

Language Spectrum

19

ImperativeClose to the machine:How to perform a task

Procedural / Von Neumann(Ex: Fortran, Pascal, C)

Object-Oriented(Ex: Smalltalk, Eiffel, C++, Java)

Scripting(Ex: Shell, TCL, Perl, Python)

Scripting Languages:

Fuzzy category of high-level languages that focus heavily on developer productivity (“rapid development”).

Often used for integration of components(“glue languages”), more recently for web development.

Traditionally imperative model, but there is a trend to include object-oriented and functional design elements.

19Monday, January 17, 2011

Page 20: Programming Languages - Computer Science 524: Programming Languages Srinivas Krishnan January 11, 2011 Based in part on slides and notes by Bjoern Brandenburg, S. Olivier and A. Block

Srinivas Krishnan Spring 201101/11/2011

Language Spectrum

20

DeclarativeClose to the user :

Steps needed to achieve it

Functional(Ex: LISP/Scheme, ML, Haskell)

Logic and constraint-based(Ex: Prolog)

Dataflow(Ex: Id, Val)

Functional Languages:

Mathematics-inspired model: program defined in terms of mathematical functions (equivalences).

A Program is a series of functions with inputs and outputs

The computer’s job is to compute the result of applying the program (a function) to the input.

There is no concept of memory:functions simply map values onto other values.

There is no concept of state:functions are only defined in terms of their arguments and other

functions.

How this is done is not specified in the program.Control flow is implicit and based on recursion.

20Monday, January 17, 2011

Page 21: Programming Languages - Computer Science 524: Programming Languages Srinivas Krishnan January 11, 2011 Based in part on slides and notes by Bjoern Brandenburg, S. Olivier and A. Block

Srinivas Krishnan Spring 201101/11/2011

Language Spectrum

21

DeclarativeClose to the user :

Steps needed to achieve it

Functional(Ex: LISP/Scheme, ML, Haskell)

Logic and constraint-based(Ex: Prolog)

Dataflow(Ex: Id, Val)

Logic Languages:

Inspired by propositional logic. Program is defined in terms of

facts (the “knowledge base”),

rules (implications, “if X then also Y”), and a

goal (query, “is Y true?”, “what makes Y true?”).

A program is a series of logical rules using facts to obtain a given goal.

The computer’s job is to construct a proof based on the given axioms (facts + rules).

21Monday, January 17, 2011

Page 22: Programming Languages - Computer Science 524: Programming Languages Srinivas Krishnan January 11, 2011 Based in part on slides and notes by Bjoern Brandenburg, S. Olivier and A. Block

Srinivas Krishnan Spring 201101/11/2011

Design Considerations

Communicate ideas.➡ Programs are read more

often than written.➡ Maintenance costs.

Exactly specify algorithms.➡ Succinct and precise.➡ No ambiguity.

Create useful programs.➡ Development must be

economically viable.22 20

What are the primary use cases?

Readability

Writability

Expressivity

Reliability

22Monday, January 17, 2011

Page 23: Programming Languages - Computer Science 524: Programming Languages Srinivas Krishnan January 11, 2011 Based in part on slides and notes by Bjoern Brandenburg, S. Olivier and A. Block

Srinivas Krishnan Spring 201101/11/2011

Expressivity

23

qsort( a, lo, hi ) int a[], hi, lo;{ int h, w, p, t; if (lo < hi) { w = lo; h = hi; p = a[hi]; do { while ((w < h) && (a[w] <= p)) w = w+1; while ((h > w) && (a[h] >= p)) h = h-1; if (w < h) { t = a[w]; a[w] = a[h]; a[h] = t; } } while (w < h);

t = a[w]; a[w] = a[hi]; a[hi] = t;

qsort( a, lo, w-1 ); qsort( a, w+1, hi ); }}

(defun quicksort (lis) (if (null lis) nil (let* ((x (car lis)) (r (cdr lis)) (fn (lambda (a) (< a x))))(append (quicksort (remove-if-not fn r)) (list x) (quicksort (remove-if fn r))))))

Quicksort in LISP Quicksort in C

(we will discuss LISP in detail later in the semester)

23Monday, January 17, 2011

Page 24: Programming Languages - Computer Science 524: Programming Languages Srinivas Krishnan January 11, 2011 Based in part on slides and notes by Bjoern Brandenburg, S. Olivier and A. Block

Srinivas Krishnan Spring 201101/11/2011

Expressivity

24

qsort( a, lo, hi ) int a[], hi, lo;{ int h, w, p, t; if (lo < hi) { w = lo; h = hi; p = a[hi]; do { while ((w < h) && (a[w] <= p)) w = w+1; while ((h > w) && (a[h] >= p)) h = h-1; if (w < h) { t = a[w]; a[w] = a[h]; a[h] = t; } } while (w < h);

t = a[w]; a[w] = a[hi]; a[hi] = t;

qsort( a, lo, w-1 ); qsort( a, w+1, hi ); }}

(defun quicksort (lis) (if (null lis) nil (let* ((x (car lis)) (r (cdr lis)) (fn (lambda (a) (< a x))))(append (quicksort (remove-if-not fn r)) (list x) (quicksort (remove-if fn r))))))

Quicksort in LISP Quicksort in C

Works on Lists as the abstraction

No operators + is A FUNCTION

Evaluation is recursive

No strong typing: Can use any datatype

24Monday, January 17, 2011

Page 25: Programming Languages - Computer Science 524: Programming Languages Srinivas Krishnan January 11, 2011 Based in part on slides and notes by Bjoern Brandenburg, S. Olivier and A. Block

Srinivas Krishnan Spring 201101/11/2011

Writability Factors

25

LISP: Concentrate on the inputs and outputs to solve the problem

Java: javadoc support ensures standardized, indexable documentation.

gcc: some warnings not used in Linux due to excessive false positives.

C++/Java: Object oriented, define objects reuse data and interface

Abstraction➡ Focus on defining concepts once

Reusability➡ DRY principle: “don’t repeat yourself ”➡ Encapsulate and provide interface➡ Generic programming: Design Patterns

Quality of development tools.➡ Efficiency of compiler-generated code.➡ Rapid Prototyping➡ Leniency of compiler / language system.➡ Turnaround time of edit-compile-test cycle..

Documentation.➡ Availability and quality.

Python: Large library, Simple syntax and easy to prototype.

25Monday, January 17, 2011

Page 26: Programming Languages - Computer Science 524: Programming Languages Srinivas Krishnan January 11, 2011 Based in part on slides and notes by Bjoern Brandenburg, S. Olivier and A. Block

Srinivas Krishnan Spring 201101/11/2011

Summary• History.

• Started with a movement away from machine code and higher abstraction

• Simultaneously “natural” languages were developed

• Programming Language Spectrum.

• Performance vs Abstraction

• Different paradigms defined often with reuse of themes

• Categories.

• Declarative: what to do.

• Functional, logic-based, dataflow.

• Imperative: how to do it.

• Procedural, object-oriented, scripting.

2626Monday, January 17, 2011