1 lecture 1: introduction (sections 1.1-1.3) csci 431 programming languages fall 2002 a modification...

25
1 Lecture 1: Introduction Lecture 1: Introduction (Sections 1.1-1.3) (Sections 1.1-1.3) CSCI 431 Programming Languages CSCI 431 Programming Languages Fall 2002 Fall 2002 A modification of slides A modification of slides developed by Felix Hernandez- developed by Felix Hernandez- Campos at UNC Chapel Hill Campos at UNC Chapel Hill

Upload: sarah-smith

Post on 13-Jan-2016

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 Lecture 1: Introduction (Sections 1.1-1.3) CSCI 431 Programming Languages Fall 2002 A modification of slides developed by Felix Hernandez-Campos at UNC

11

Lecture 1: IntroductionLecture 1: Introduction(Sections 1.1-1.3)(Sections 1.1-1.3)

CSCI 431 Programming Languages CSCI 431 Programming Languages

Fall 2002Fall 2002

A modification of slides developed by Felix A modification of slides developed by Felix Hernandez-Campos at UNC Chapel HillHernandez-Campos at UNC Chapel Hill

Page 2: 1 Lecture 1: Introduction (Sections 1.1-1.3) CSCI 431 Programming Languages Fall 2002 A modification of slides developed by Felix Hernandez-Campos at UNC

22

Programming LanguagesProgramming Languages

• What is a programming language? What is a programming language?

Programming LanguageProgramming Language

Page 3: 1 Lecture 1: Introduction (Sections 1.1-1.3) CSCI 431 Programming Languages Fall 2002 A modification of slides developed by Felix Hernandez-Campos at UNC

33

Programming Languages Programming Languages

• A language used to express instructions to a A language used to express instructions to a computer. computer.

– Definition of legal Programs (Syntax) Definition of legal Programs (Syntax) – Meaning of a Program (Semantics) Meaning of a Program (Semantics) – Style of Programming (Pragmatics) Style of Programming (Pragmatics)

• Example, Consider the following statement: Example, Consider the following statement: – set x[i] to x[i] + 1set x[i] to x[i] + 1 – This is clearly intended to denote the increment of an array This is clearly intended to denote the increment of an array

element. How would we translate this statement to a element. How would we translate this statement to a variety of different languages, and what would it mean? variety of different languages, and what would it mean?

Page 4: 1 Lecture 1: Introduction (Sections 1.1-1.3) CSCI 431 Programming Languages Fall 2002 A modification of slides developed by Felix Hernandez-Campos at UNC

44

In C (In C (circacirca 1970), we would write this as 1970), we would write this as

x[i] = x[i] + 1;x[i] = x[i] + 1;

This performs a hardware lookup for the address of This performs a hardware lookup for the address of xx and adds and adds ii to it. The addition is a hardware operation, to it. The addition is a hardware operation, so it is dependent upon the hardware in question. so it is dependent upon the hardware in question. This resulting address is then referenced (if it's legal This resulting address is then referenced (if it's legal - which it might not be), - which it might not be), 11 is added to the bit-string is added to the bit-string stored there (again, as a hardware addition, which stored there (again, as a hardware addition, which can overflow), and the result is stored back to that can overflow), and the result is stored back to that location. However, no attempt has been made to location. However, no attempt has been made to determine that determine that xx is even a vector and that is even a vector and that x[i]x[i] is a is a number. number.

Page 5: 1 Lecture 1: Introduction (Sections 1.1-1.3) CSCI 431 Programming Languages Fall 2002 A modification of slides developed by Felix Hernandez-Campos at UNC

55

In Scheme (1975), this would be In Scheme (1975), this would be transcribed as transcribed as

(vector-set! x i (+ (vector-ref x i) 1))(vector-set! x i (+ (vector-ref x i) 1))

This does all the things the corresponding C This does all the things the corresponding C operation does, but in addition it also (a) checks that operation does, but in addition it also (a) checks that the object named the object named xx is indeed an array, (b) makes sure is indeed an array, (b) makes sure it is within the bounds of the array, (c) ensures the it is within the bounds of the array, (c) ensures the dereferenced location contains a number, and (d) dereferenced location contains a number, and (d) performs abstract arithmetic (so there will be no performs abstract arithmetic (so there will be no ``overflow''). ``overflow'').

Page 6: 1 Lecture 1: Introduction (Sections 1.1-1.3) CSCI 431 Programming Languages Fall 2002 A modification of slides developed by Felix Hernandez-Campos at UNC

66

Finally, in Java (Finally, in Java (circacirca 1991), one might 1991), one might write write

x[i] = x[i] + 1;x[i] = x[i] + 1;

which looks identical to the C code. However, the which looks identical to the C code. However, the actions performed are those performed by the actions performed are those performed by the Scheme code, with one major difference: the Scheme code, with one major difference: the arithmetic is not as abstract. It is defined to be done arithmetic is not as abstract. It is defined to be done as if the machine were a 32-bit machine, which as if the machine were a 32-bit machine, which means we can always determine the result of an means we can always determine the result of an operation, no matter which machine we execute the operation, no matter which machine we execute the program on, but we cannot have our numbers grow program on, but we cannot have our numbers grow arbitrarily large. arbitrarily large.

Page 7: 1 Lecture 1: Introduction (Sections 1.1-1.3) CSCI 431 Programming Languages Fall 2002 A modification of slides developed by Felix Hernandez-Campos at UNC

77

Programming LanguagesProgramming Languages

• What is a programming language?What is a programming language?– Abstraction of virtual machineAbstraction of virtual machine

int sum(int[] x) {int sum(int[] x) { int sum = 0;int sum = 0; n = 0;n = 0; while (n < x.length) {while (n < x.length) { sum += x[n];sum += x[n]; }} return sum;return sum;}}

0010101010101000101010101010101010111110101010101111101011101010101110111010101011100010101010101000101010101010

......

Page 8: 1 Lecture 1: Introduction (Sections 1.1-1.3) CSCI 431 Programming Languages Fall 2002 A modification of slides developed by Felix Hernandez-Campos at UNC

88

A simple algorithm for testing primality in A simple algorithm for testing primality in Java: Java:

public static boolean isprime (int n) { public static boolean isprime (int n) {

int d; int d;

for (d = 2; d < n; d++) for (d = 2; d < n; d++)

if (n % d == 0) if (n % d == 0)

return false; return false;

return true; return true;

}}

Page 9: 1 Lecture 1: Introduction (Sections 1.1-1.3) CSCI 431 Programming Languages Fall 2002 A modification of slides developed by Felix Hernandez-Campos at UNC

99

In Intel X86 Assembler: In Intel X86 Assembler:

..globl isprime globl isprime

isprime: isprime:

pushl %ebp pushl %ebp ; set up procedure entry ; set up procedure entry

movl %esp,%ebp movl %esp,%ebp

pushl %esi pushl %ebx pushl %esi pushl %ebx

movl 8(%ebp),%ebx movl 8(%ebp),%ebx ; fetch arg n from stack ; fetch arg n from stack

movl $2,%esi movl $2,%esi ; set divisor d := 2 ; set divisor d := 2

cmpl %ebx,%esi cmpl %ebx,%esi ; compare n,d ; compare n,d

jge true jge true ; jump if d >= n ; jump if d >= n

loop: loop:

movl %ebx,%eax movl %ebx,%eax ; set n into .... ; set n into ....

cltd cltd ; ... dividend register ; ... dividend register

idivl %esi idivl %esi ; divide by d ; divide by d

testl %edx,%edx testl %edx,%edx ; remainder 0? ; remainder 0?

……done: done:

leal -8(%ebp),%esp leal -8(%ebp),%esp ; clean up and exit ; clean up and exit

popl %ebx popl %ebx

popl %esi popl %esi

leave leave

ret ret

Page 10: 1 Lecture 1: Introduction (Sections 1.1-1.3) CSCI 431 Programming Languages Fall 2002 A modification of slides developed by Felix Hernandez-Campos at UNC

1010

Machine Code Characteristics:Machine Code Characteristics:

• Explicit registers for values and intermediate results. Explicit registers for values and intermediate results.

• Low-level machine instructions to implement Low-level machine instructions to implement operations. operations.

• Control flow based on labels and conditional Control flow based on labels and conditional branches. branches.

• Explicit memory management (e.g., stack Explicit memory management (e.g., stack management for procedures). management for procedures).

Page 11: 1 Lecture 1: Introduction (Sections 1.1-1.3) CSCI 431 Programming Languages Fall 2002 A modification of slides developed by Felix Hernandez-Campos at UNC

1111

General Characteristics of HLLs: General Characteristics of HLLs:

• Complex Expressions (Arithmetic, Logical, ...) Complex Expressions (Arithmetic, Logical, ...)

• Structured Control Operators (Loops, Conditionals, Cases) Structured Control Operators (Loops, Conditionals, Cases)

• Composite Types (Arrays, Records, etc.) Composite Types (Arrays, Records, etc.)

• Type Declarations and Type Checking Type Declarations and Type Checking

• Multiple storage classes (global/local/heap) Multiple storage classes (global/local/heap)

• Procedures/Functions, with private scope, maybe first-class Procedures/Functions, with private scope, maybe first-class

• Maybe abstract data types, modules, objects, etc. Maybe abstract data types, modules, objects, etc.

• Maybe high-level control mechanisms (Exceptions, Back-Maybe high-level control mechanisms (Exceptions, Back-tracking, etc.) tracking, etc.)

Page 12: 1 Lecture 1: Introduction (Sections 1.1-1.3) CSCI 431 Programming Languages Fall 2002 A modification of slides developed by Felix Hernandez-Campos at UNC

1212

Programming LanguagesProgramming Languages

• What is a programming language?What is a programming language?– Donald Knuth:Donald Knuth:

» Programming is the art of telling another human being what one Programming is the art of telling another human being what one wants the computer to dowants the computer to do

int sum(int[] x) {int sum(int[] x) { int sum = 0;int sum = 0; n = 0;n = 0; while (n < x.length) {while (n < x.length) { sum += x[n];sum += x[n]; }} return sum;return sum;}}

0010101010101000101010101010101010111110101010101111101011101010101110111010101011100010101010101000101010101010

......

Page 13: 1 Lecture 1: Introduction (Sections 1.1-1.3) CSCI 431 Programming Languages Fall 2002 A modification of slides developed by Felix Hernandez-Campos at UNC

1313

The Number of Programming LanguagesThe Number of Programming Languages

• How many programming languages do you know?How many programming languages do you know?– This is a sample list…This is a sample list…

» http://http://dmozdmoz.org/Computers/Programming/Languages.org/Computers/Programming/Languages

• Why is the number of programming languages so Why is the number of programming languages so large?large?

– EvolutionEvolution– Special PurposeSpecial Purpose– Personal PreferencePersonal Preference

Page 14: 1 Lecture 1: Introduction (Sections 1.1-1.3) CSCI 431 Programming Languages Fall 2002 A modification of slides developed by Felix Hernandez-Campos at UNC

1414

The Number of Programming LanguagesThe Number of Programming Languages

• How many programming languages do you know?How many programming languages do you know?– This is a sample list…This is a sample list…

» http://dmoz.org/Computers/Programming/Languages/http://dmoz.org/Computers/Programming/Languages/

• Why is the number of programming languages so Why is the number of programming languages so large?large?

– EvolutionEvolution– Special PurposeSpecial Purpose– Personal PreferencePersonal Preference

Page 15: 1 Lecture 1: Introduction (Sections 1.1-1.3) CSCI 431 Programming Languages Fall 2002 A modification of slides developed by Felix Hernandez-Campos at UNC

1515

Evolution: GenealogyEvolution: Genealogy

From Sebesta’sFrom Sebesta’s

Concepts of Concepts of

ProgrammingProgramming

LanguagesLanguages

PrologProlog

SWI-PrologSWI-Prolog

SchemeScheme

JavaJava

Page 16: 1 Lecture 1: Introduction (Sections 1.1-1.3) CSCI 431 Programming Languages Fall 2002 A modification of slides developed by Felix Hernandez-Campos at UNC

1616

The Number of Programming LanguagesThe Number of Programming Languages

• How many programming languages do you know?How many programming languages do you know?– This is a sample list…This is a sample list…

» http://dmoz.org/Computers/Programming/Languages/http://dmoz.org/Computers/Programming/Languages/

• Why is the number of programming languages so Why is the number of programming languages so large?large?

– EvolutionEvolution– Special PurposeSpecial Purpose– Personal PreferencePersonal Preference

Page 17: 1 Lecture 1: Introduction (Sections 1.1-1.3) CSCI 431 Programming Languages Fall 2002 A modification of slides developed by Felix Hernandez-Campos at UNC

1717

The Number of Programming LanguagesThe Number of Programming Languages

• How many programming languages do you know?How many programming languages do you know?– This is a sample list…This is a sample list…

» http://dmoz.org/Computers/Programming/Languages/http://dmoz.org/Computers/Programming/Languages/

• Why is the number of programming languages so Why is the number of programming languages so large?large?

– EvolutionEvolution– Special PurposeSpecial Purpose– Personal PreferencePersonal Preference

Page 18: 1 Lecture 1: Introduction (Sections 1.1-1.3) CSCI 431 Programming Languages Fall 2002 A modification of slides developed by Felix Hernandez-Campos at UNC

1818

The Number of Programming LanguagesThe Number of Programming Languages

• How many programming languages do you know?How many programming languages do you know?– This is a sample list…This is a sample list…

» http://dmoz.org/Computers/Programming/Languages/http://dmoz.org/Computers/Programming/Languages/

• Why is the number of programming languages so Why is the number of programming languages so large?large?

– EvolutionEvolution– Special PurposeSpecial Purpose– Personal PreferencePersonal Preference

• A programming language is a way of thinkingA programming language is a way of thinking– Different people think in a different wayDifferent people think in a different way

Page 19: 1 Lecture 1: Introduction (Sections 1.1-1.3) CSCI 431 Programming Languages Fall 2002 A modification of slides developed by Felix Hernandez-Campos at UNC

1919

A simple algorithm for testing primalityA simple algorithm for testing primality

In JavaIn Java: :

public static boolean isprime (int n) { public static boolean isprime (int n) {

int d; int d;

for (d = 2; d < n; d++) for (d = 2; d < n; d++)

if (n % d == 0) if (n % d == 0)

return false; return false;

return true; return true;

}}

Page 20: 1 Lecture 1: Introduction (Sections 1.1-1.3) CSCI 431 Programming Languages Fall 2002 A modification of slides developed by Felix Hernandez-Campos at UNC

2020

A simple algorithm for testing primalityA simple algorithm for testing primality

In Standard MLIn Standard ML (using a recursive function): (using a recursive function):

fun isprime (n:int) : bool = fun isprime (n:int) : bool =

let fun no_divisor (d:int) : bool = let fun no_divisor (d:int) : bool =

(d >= n) orelse ((n mod d <> 0) andalso (d >= n) orelse ((n mod d <> 0) andalso (no_divisor (d+1))) (no_divisor (d+1)))

in no_divisor 2 endin no_divisor 2 end

Page 21: 1 Lecture 1: Introduction (Sections 1.1-1.3) CSCI 431 Programming Languages Fall 2002 A modification of slides developed by Felix Hernandez-Campos at UNC

2121

Successful Programming LanguagesSuccessful Programming Languages

• Are all languages equally successful?Are all languages equally successful?– No!No!

• What makes a language successful?What makes a language successful?– Expressive powerExpressive power– Ease of use for the noviceEase of use for the novice– Ease of implementationEase of implementation– Excellent compilersExcellent compilers– Economics, patronage, and inertiaEconomics, patronage, and inertia

Page 22: 1 Lecture 1: Introduction (Sections 1.1-1.3) CSCI 431 Programming Languages Fall 2002 A modification of slides developed by Felix Hernandez-Campos at UNC

2222

Why study programming languages?Why study programming languages?

• Use the most Use the most appropriateappropriate programming language for programming language for your taskyour task

– E.g. Java is great for writing applicationsE.g. Java is great for writing applications– E.g. C is great for systems programmingE.g. C is great for systems programming

• Make it easier to learn new languagesMake it easier to learn new languages– Evolution => SimilaritiesEvolution => Similarities

• Make better use of language featuresMake better use of language features– Obscure featuresObscure features– Cost of featuresCost of features– Simulate useful featuresSimulate useful features

Page 23: 1 Lecture 1: Introduction (Sections 1.1-1.3) CSCI 431 Programming Languages Fall 2002 A modification of slides developed by Felix Hernandez-Campos at UNC

2323

Classification of Programming LanguagesClassification of Programming Languages

• Imperative languagesImperative languages– Algorithms+data structures+assignmentAlgorithms+data structures+assignment

• Von Neumann languagesVon Neumann languages» E.g. Fortran, Basic, CE.g. Fortran, Basic, C

• Object-oriented languagesObject-oriented languages» E.g. C++, JavaE.g. C++, Java

Page 24: 1 Lecture 1: Introduction (Sections 1.1-1.3) CSCI 431 Programming Languages Fall 2002 A modification of slides developed by Felix Hernandez-Campos at UNC

2424

Classification of Programming LanguagesClassification of Programming Languages

• Declarative languagesDeclarative languages– No assignment (well sorta)No assignment (well sorta)

• Functional languagesFunctional languages– Functions+listsFunctions+lists– E.g. Lisp, ML, and HaskellE.g. Lisp, ML, and Haskell

• Dataflow languagesDataflow languages– ConcurrentConcurrent– E.g. Id and ValE.g. Id and Val

• Logic or constraint-based languagesLogic or constraint-based languages– Propositions+predicates+logical deductionPropositions+predicates+logical deduction– E.g. PrologE.g. Prolog

Page 25: 1 Lecture 1: Introduction (Sections 1.1-1.3) CSCI 431 Programming Languages Fall 2002 A modification of slides developed by Felix Hernandez-Campos at UNC

2525

SummarySummary

• Programming languages:Programming languages:– Set of abstractions => virtual machine Set of abstractions => virtual machine – A way of thinkingA way of thinking