1 cosc3306: programming paradigms lecture 1: introduction haibin zhu, ph.d. computer science...

45
1 COSC3306: COSC3306: Programming Programming Paradigms Paradigms Lecture 1: Lecture 1: Introduction Introduction Haibin Zhu, Ph.D. Haibin Zhu, Ph.D. Computer Science Computer Science Nipissing University Nipissing University (C) 2003 (C) 2003

Upload: rosanna-hancock

Post on 23-Dec-2015

222 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 COSC3306: Programming Paradigms Lecture 1: Introduction Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003

11

COSC3306:COSC3306:Programming ParadigmsProgramming Paradigms

Lecture 1: IntroductionLecture 1: Introduction

Haibin Zhu, Ph.D.Haibin Zhu, Ph.D.Computer ScienceComputer ScienceNipissing University Nipissing University

(C) 2003(C) 2003

Page 2: 1 COSC3306: Programming Paradigms Lecture 1: Introduction Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003

22

Course OutlineCourse Outline

This course will give you the opportunity to learn the This course will give you the opportunity to learn the fundamental concepts underlying modern programming fundamental concepts underlying modern programming languages. languages. The primary focus is on understanding concepts, The primary focus is on understanding concepts, however, as a practical consequence, you will write however, as a practical consequence, you will write several small programs in different languages.several small programs in different languages.We will study a small set of representative programming We will study a small set of representative programming languages and use them to develop an understanding of languages and use them to develop an understanding of how different programming language concepts are how different programming language concepts are implemented by a language designer, and their impact implemented by a language designer, and their impact on the programmer.on the programmer.

Page 3: 1 COSC3306: Programming Paradigms Lecture 1: Introduction Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003

33

TextbookTextbook

Textbook:Textbook: Seyed H. Roosta, Seyed H. Roosta, Foundations of Programming Languages: Foundations of Programming Languages: Design and Implementation, Thomson, Design and Implementation, Thomson, Brook/Cole, 2003. ISBN: 0-534-39303-9Brook/Cole, 2003. ISBN: 0-534-39303-9

Reference:Reference:– Robert Sebesta, Robert Sebesta, Concepts of Programming Concepts of Programming

LanguagesLanguages, 5th Edition, Addison-Wesley, , 5th Edition, Addison-Wesley, 2001.2001.

– Other books about special languagesOther books about special languages

Page 4: 1 COSC3306: Programming Paradigms Lecture 1: Introduction Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003

44

The principal paradigmsThe principal paradigms

Imperative Programming (C)Imperative Programming (C)

Object-Oriented Programming (C++)Object-Oriented Programming (C++)

Logic/Declarative Programming (Prolog)Logic/Declarative Programming (Prolog)

Functional/Applicative Programming (Lisp)Functional/Applicative Programming (Lisp)

Page 5: 1 COSC3306: Programming Paradigms Lecture 1: Introduction Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003

55

Languages we will learnLanguages we will learn

C for efficient imperative programming C for efficient imperative programming with static types.with static types.

C++ for object-oriented programming with C++ for object-oriented programming with static types and ad hoc, subtype and static types and ad hoc, subtype and parametric polymorphism.parametric polymorphism.

Lisp for applicative programming.Lisp for applicative programming.

Prolog for Logical programming.Prolog for Logical programming.

Page 6: 1 COSC3306: Programming Paradigms Lecture 1: Introduction Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003

66

IntroductionIntroduction

Programming LanguagesProgramming Languages

Programming Language DesignProgramming Language Design

Programming Language ProcessingProgramming Language Processing

Page 7: 1 COSC3306: Programming Paradigms Lecture 1: Introduction Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003

77

Programming LanguagesProgramming Languages

A set of statements/commands used to A set of statements/commands used to direct a computer to do something useful.direct a computer to do something useful.

A notational system for describing A notational system for describing computations in both machine readable computations in both machine readable and human readable form.and human readable form.

A programming language is a language A programming language is a language intended for the description of programs.intended for the description of programs.

Page 8: 1 COSC3306: Programming Paradigms Lecture 1: Introduction Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003

88

Why So Many Programming Why So Many Programming Paradigms and Languages?Paradigms and Languages?

Building automated software tools that help Building automated software tools that help people cope effectively with people cope effectively with complexitycomplexity..Failure to manage complexity can result in Failure to manage complexity can result in catastrophic errors. A small coding mistake can catastrophic errors. A small coding mistake can kill somebody!kill somebody!Looking for better ways to build software tools to Looking for better ways to build software tools to deal with various computationally solvable deal with various computationally solvable problems.problems.There have been literally hundreds of different There have been literally hundreds of different programming languages designed, some general programming languages designed, some general purpose, others special purpose. Many purpose, others special purpose. Many are now are now obsolete. obsolete.

Page 9: 1 COSC3306: Programming Paradigms Lecture 1: Introduction Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003

99

ComplexityComplexityThe sheer number of logic components in a The sheer number of logic components in a modern CPU requires thousands of simulations to modern CPU requires thousands of simulations to check for problems, but that doesn’t mean they check for problems, but that doesn’t mean they are all found (remember the Pentium FDIV bug?).are all found (remember the Pentium FDIV bug?).A system to predict tornados has to deal with A system to predict tornados has to deal with hundreds of simultaneous differential equations.hundreds of simultaneous differential equations.Differential cryptanalysis is concerned with Differential cryptanalysis is concerned with factoring large prime numbers, requiring hundreds factoring large prime numbers, requiring hundreds or thousands of machines cooperating on the or thousands of machines cooperating on the solution (e.g., the recent breaking of DES using solution (e.g., the recent breaking of DES using hundreds of machine in the Internet).hundreds of machine in the Internet).

Page 10: 1 COSC3306: Programming Paradigms Lecture 1: Introduction Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003

1010

Important criteria of programming Important criteria of programming languages languages

universal.universal.reasonably natural for solving problems. reasonably natural for solving problems. capable of being implemented on a capable of being implemented on a computer. computer. capable of efficient implementation. capable of efficient implementation. portable.portable.verifiable.verifiable.understandable. understandable.

Page 11: 1 COSC3306: Programming Paradigms Lecture 1: Introduction Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003

1111

Programming Language DesignProgramming Language Design

A A special purpose languagespecial purpose language – designed for a specific class of applications.designed for a specific class of applications.– http://www.idiom.com/free-compilers/ECATEGhttp://www.idiom.com/free-compilers/ECATEG

ORY/specialp-1.htmlORY/specialp-1.html

A A general purpose languagegeneral purpose language – applied to a wide range of applications. applied to a wide range of applications.

Page 12: 1 COSC3306: Programming Paradigms Lecture 1: Introduction Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003

1212

Criteria in a good language designCriteria in a good language design

Writability:Writability: The quality of a language that enables a The quality of a language that enables a programmer to use it to express a computation clearly, programmer to use it to express a computation clearly, correctly, concisely, and quickly. correctly, concisely, and quickly. Readability:Readability: The quality of a language that enables a The quality of a language that enables a programmer to understand and comprehend the nature of programmer to understand and comprehend the nature of a computation easily and accurately. a computation easily and accurately. Orthogonality:Orthogonality: The quality of a language that features The quality of a language that features provided have as few restrictions as possible and be provided have as few restrictions as possible and be combinable in any meaningful way.combinable in any meaningful way.Reliability:Reliability: The quality of a language that assures a The quality of a language that assures a program will not behave in unexpected or disastrous ways program will not behave in unexpected or disastrous ways during execution. during execution. Maintainability:Maintainability: The quality of a language that eases The quality of a language that eases errors can be found and corrected and new features errors can be found and corrected and new features added. added.

Page 13: 1 COSC3306: Programming Paradigms Lecture 1: Introduction Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003

1313

Criteria (Continued)Criteria (Continued)

Generality:Generality: The quality of a language that avoids special The quality of a language that avoids special cases in the availability or use of constructs and by cases in the availability or use of constructs and by combining closely related constructs into a single more combining closely related constructs into a single more general one.general one.Uniformity:Uniformity: The quality of a language that similar The quality of a language that similar features should look similar and have similar. features should look similar and have similar. Extensibility: Extensibility: The quality of a language that provides The quality of a language that provides some general mechanism for the user to add new some general mechanism for the user to add new constructs to a language.constructs to a language.Standardability:Standardability: The quality of a language that allows The quality of a language that allows programs written to be transported from one computer to programs written to be transported from one computer to another without significant change in language structure. another without significant change in language structure. Implementability:Implementability: The quality of a language that provides The quality of a language that provides a translator or interpreter can be written. This can address a translator or interpreter can be written. This can address to complexity of the language definition. to complexity of the language definition.

Page 14: 1 COSC3306: Programming Paradigms Lecture 1: Introduction Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003

1414

Programming Language Programming Language ProcessingProcessing

Any system for processing programming Any system for processing programming languages, executing them, or preparing them for languages, executing them, or preparing them for execution is called a language processor.execution is called a language processor.Language processors include Language processors include translatorstranslators and and auxiliary tools like syntax-directed editors.auxiliary tools like syntax-directed editors.A translator that immediately executes a program is A translator that immediately executes a program is called an called an interpreterinterpreter, while a translator that , while a translator that changes a program into a form suitable for changes a program into a form suitable for execution is called a execution is called a compilercompiler. . In other words, interpretation is a one-step process, In other words, interpretation is a one-step process, in which both the program and the input are in which both the program and the input are provided to the interpreter and the output is the provided to the interpreter and the output is the result of the interpretation as shown below.result of the interpretation as shown below.

Page 15: 1 COSC3306: Programming Paradigms Lecture 1: Introduction Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003

1515

Figure 1.1 The interpretation process

© 2003 Brooks/Cole Publishing / Thomson Learning™

Page 16: 1 COSC3306: Programming Paradigms Lecture 1: Introduction Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003

1616

CompilationCompilation

CompilationCompilation is at least two-step process, is at least two-step process, in which the original program (source in which the original program (source program) is input to the compiler, and a program) is input to the compiler, and a new program (target program) is output new program (target program) is output from the compiler. The compilation steps from the compiler. The compilation steps can be visualized as the following.can be visualized as the following.

Page 17: 1 COSC3306: Programming Paradigms Lecture 1: Introduction Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003

1717

Figure 1.2 The compilation process

© 2003 Brooks/Cole Publishing / Thomson Learning™

Page 18: 1 COSC3306: Programming Paradigms Lecture 1: Introduction Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003

1818

Machine LanguagesMachine Languages

Regardless of the programming language that Regardless of the programming language that you are using, it is still a symbolic language that you are using, it is still a symbolic language that has to be translated into a form that the has to be translated into a form that the computer can execute. This encoded form is computer can execute. This encoded form is called the called the machine languagemachine language. For example, for . For example, for the execution of the assignment statementthe execution of the assignment statement

BBAA22

a typical sequence of machine code instructions a typical sequence of machine code instructions might be as the followingmight be as the following

Page 19: 1 COSC3306: Programming Paradigms Lecture 1: Introduction Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003

1919

Machine codesMachine codes OperationOperation RegisterRegister TagTag Memory AddressMemory Address

0001 01 00 0000 0000 (load)0001 01 00 0000 0000 (load) 00010001 01 01 00 00 0000 0000 0000 0000

0011 01 10 0000 0010 (add)0011 01 10 0000 0010 (add) 00110011 01 01 10 10 0000 0010 0000 0010

0010 01 00 0000 0100 (store)00100010 01 00 0000 0100 (store)0010 01 01 00 00 0000 0100 0000 0100

Page 20: 1 COSC3306: Programming Paradigms Lecture 1: Introduction Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003

2020

Low-Level LanguagesLow-Level Languages

In programs written in a In programs written in a low-level programming low-level programming languagelanguage, each symbolic instruction can generate , each symbolic instruction can generate oneone machine instruction. An assembly language is a low-level machine instruction. An assembly language is a low-level language. language. For example, for the assignment statementFor example, for the assignment statementBBAA22the following is a hypothetical assembly code into which the following is a hypothetical assembly code into which the machine instructions might be translated:the machine instructions might be translated:

MOVMOV R1, AR1, AADDADD R1, #2R1, #2MOVMOV B, R1B, R1

Page 21: 1 COSC3306: Programming Paradigms Lecture 1: Introduction Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003

2121

Figure 1.3 The assemble-link-execute cycle

© 2003 Brooks/Cole Publishing / Thomson Learning™

Page 22: 1 COSC3306: Programming Paradigms Lecture 1: Introduction Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003

2222

High-Level LanguagesHigh-Level Languages

A A high-level programming language high-level programming language has has the following characteristics:the following characteristics:– Elementary data types and structuresElementary data types and structures– Sequence controlSequence control– Data controlData control– Storage managementStorage management– Operating environmentOperating environment– Machine architecture independenceMachine architecture independence– Availability of program librariesAvailability of program libraries

Page 23: 1 COSC3306: Programming Paradigms Lecture 1: Introduction Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003

2323

Figure 1.6 The compile-link-execute cycle

© 2003 Brooks/Cole Publishing / Thomson Learning™

Page 24: 1 COSC3306: Programming Paradigms Lecture 1: Introduction Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003

2424

To be continued To be continued

Programming a Machine versus Programming a Machine versus Programming a ComputationProgramming a Computation

Programmer ProductivityProgrammer Productivity

Programming ParadigmsProgramming Paradigms

Language ImplementationLanguage Implementation

Page 25: 1 COSC3306: Programming Paradigms Lecture 1: Introduction Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003

2525

Programming a Machine versus Programming a Machine versus Programming a ComputationProgramming a Computation

In the early days of computing, computer scientists were In the early days of computing, computer scientists were really electrical engineers, physicist, and really electrical engineers, physicist, and mathematicians. Engineers were primarily interested in mathematicians. Engineers were primarily interested in making the machine do interesting things, whereas making the machine do interesting things, whereas physicists and mathematicians were primarily interested physicists and mathematicians were primarily interested in having the machine perform large-scale mathematical in having the machine perform large-scale mathematical computations and give useful answers.computations and give useful answers.These computational problems had to be formulated These computational problems had to be formulated precisely in the language of the machine, i.e., binary precisely in the language of the machine, i.e., binary machine language. machine language. In fact, the term “code” comes from the fact that a string In fact, the term “code” comes from the fact that a string of bits is an encoding of instructions to the computer.of bits is an encoding of instructions to the computer.

Page 26: 1 COSC3306: Programming Paradigms Lecture 1: Introduction Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003

2626

Programming a Machine versus Programming a Machine versus Programming a ComputationProgramming a Computation

So, the electrical engineers all scratched their So, the electrical engineers all scratched their heads, and then invented assembly language! heads, and then invented assembly language! So, then you could program the machine at a So, then you could program the machine at a higher level, but still inadequately as it is often higher level, but still inadequately as it is often very difficult to debug assembly language very difficult to debug assembly language programs, and it is even more difficult to reuse programs, and it is even more difficult to reuse code in other programs. Beside, compilers today code in other programs. Beside, compilers today are very good at generating efficient machine are very good at generating efficient machine code from high level source code (e.g., C).code from high level source code (e.g., C).

Page 27: 1 COSC3306: Programming Paradigms Lecture 1: Introduction Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003

2727

Programming a Machine versus Programming a Machine versus Programming a ComputationProgramming a Computation

Finally, a mathematically inclined engineer Finally, a mathematically inclined engineer invented Fortran (Formula Translation). Thus invented Fortran (Formula Translation). Thus was born the first popular high-level was born the first popular high-level programming language for writing programs programming language for writing programs containing mathematical expressions, for containing mathematical expressions, for example:example:– R = B**2 - 4.0*A*C Question: R = B**2 - 4.0*A*C Question: – what is the order of evaluation?what is the order of evaluation?

A compiler was implemented to translate these A compiler was implemented to translate these high-level statements and expressions into high-level statements and expressions into equivalent machine instructions that would equivalent machine instructions that would correctly perform the desired computations.correctly perform the desired computations.

Page 28: 1 COSC3306: Programming Paradigms Lecture 1: Introduction Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003

2828

Programming a Machine versus Programming a Machine versus Programming a ComputationProgramming a Computation

Many modern programming languages are descended Many modern programming languages are descended from an intended replacement to Fortran, called Algol from an intended replacement to Fortran, called Algol (for Algorithmic Language). Algol was the first language (for Algorithmic Language). Algol was the first language to introduce the idea of to introduce the idea of lexical scopinglexical scoping, which enabled , which enabled the definition of modular procedures and the expression the definition of modular procedures and the expression of recursive functions (like the factorial function).of recursive functions (like the factorial function).Of course, today we still have Fortran, but Algol is gone! Of course, today we still have Fortran, but Algol is gone! However, Simula was a successor of Algol, which However, Simula was a successor of Algol, which influenced the design of object-oriented languages, such influenced the design of object-oriented languages, such as Smalltalk, C++, and Java.as Smalltalk, C++, and Java.Lisp (List Processing) was originally a special purpose Lisp (List Processing) was originally a special purpose language developed for symbolic computation using lists. language developed for symbolic computation using lists. Scheme is a dialect of Lisp, but claims Algol as its Scheme is a dialect of Lisp, but claims Algol as its “conceptual” ancestor because of the use of lexical “conceptual” ancestor because of the use of lexical scoping.scoping.

Page 29: 1 COSC3306: Programming Paradigms Lecture 1: Introduction Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003

2929

Programmer ProductivityProgrammer Productivity

Software engineers like to talk about Software engineers like to talk about “programmer productivity,” or how many Lines of “programmer productivity,” or how many Lines of Code (LOC), and number of bugs per line of Code (LOC), and number of bugs per line of code, etc.code, etc.

A programming language can either encourage A programming language can either encourage or discourage the introduction of errors. From an or discourage the introduction of errors. From an engineering perspective, we want to engineer engineering perspective, we want to engineer error-free programs. This means the language error-free programs. This means the language needs to offer needs to offer linguistic mechanisms linguistic mechanisms that that facilitate the production of reliable software.facilitate the production of reliable software.

Page 30: 1 COSC3306: Programming Paradigms Lecture 1: Introduction Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003

3030

ExamplesExamples

Definition mechanismsDefinition mechanisms– allowing the specification of clearly defined intent through the use allowing the specification of clearly defined intent through the use

of of type declarations type declarations for both functions/procedures and data.for both functions/procedures and data.

For example: type declarations and strongly typed For example: type declarations and strongly typed function function signatures signatures mechanisms for defining independent mechanisms for defining independent lexical scopes lexical scopes to provide for to provide for modularization modularization of a of a program’s procedures and data.program’s procedures and data.The ability to define libraries of subprograms, encapsulate The ability to define libraries of subprograms, encapsulate data into Abstract Data Types (ADTs), and define data into Abstract Data Types (ADTs), and define interfaces as abstraction boundaris for the purpose of interfaces as abstraction boundaris for the purpose of information hiding and localized access to data (i.e., no information hiding and localized access to data (i.e., no global data) are all critical elements of cleanly organizing global data) are all critical elements of cleanly organizing a program.a program.

Page 31: 1 COSC3306: Programming Paradigms Lecture 1: Introduction Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003

3131

ExamplesExamples

The ability to The ability to share/reuse/extend share/reuse/extend abstract abstract data types and/or functions/procedures. data types and/or functions/procedures. (e.g., abstract base classes and virtual (e.g., abstract base classes and virtual functions in C++).functions in C++).

The ability to assert formal correctness The ability to assert formal correctness statements about critical parts of a statements about critical parts of a program and reason effectively.program and reason effectively.

Page 32: 1 COSC3306: Programming Paradigms Lecture 1: Introduction Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003

3232

ProductivityProductivity

A program is intended to carry out a specific A program is intended to carry out a specific computation, but a programmer can fail to computation, but a programmer can fail to adequately address all data value ranges, input adequately address all data value ranges, input conditions, system resource constraints, conditions, system resource constraints, memory limitations, etc. For mission critical memory limitations, etc. For mission critical applications, the programmer needs to be able applications, the programmer needs to be able to reason effectively about the behavior of a to reason effectively about the behavior of a program. This means that the features of the program. This means that the features of the language and their interaction should be clearly language and their interaction should be clearly specified and understandable. If you do not or specified and understandable. If you do not or can not clearly understand the semantics of the can not clearly understand the semantics of the language, your ability to accurately predict the language, your ability to accurately predict the behavior of your program is limited.behavior of your program is limited.

Page 33: 1 COSC3306: Programming Paradigms Lecture 1: Introduction Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003

3333

Programming ParadigmsProgramming Paradigms

““Paradigms” emerge as the result of social Paradigms” emerge as the result of social processes in which people develop ideas and processes in which people develop ideas and create principles and practices that embody those create principles and practices that embody those ideas. The term paradigm comes from the book, ideas. The term paradigm comes from the book, “The Structure of Scientific Revolutions” by Kuhn.“The Structure of Scientific Revolutions” by Kuhn.

Programming paradigms are the result of Programming paradigms are the result of people’s ideas about how programs should be people’s ideas about how programs should be constructed, and the development of constructed, and the development of formal formal linguistic mechanisms linguistic mechanisms for expressing those ideas, for expressing those ideas, and software engineering principles and practices and software engineering principles and practices for using the resulting programming language.for using the resulting programming language.

Page 34: 1 COSC3306: Programming Paradigms Lecture 1: Introduction Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003

3434

Imperative ProgrammingImperative Programming

Programming with Programming with – algorithms+data structures+assignment.algorithms+data structures+assignment.

Programs consists of actions to effect Programs consists of actions to effect changes of statechanges of state, principally through , principally through assignment operations.assignment operations.

Imperative languages: Fortran, Algol, Imperative languages: Fortran, Algol, Cobol, PL/I, Pascal, Modula-2, Ada, C, Cobol, PL/I, Pascal, Modula-2, Ada, C, have traditionally dominated in practicehave traditionally dominated in practice

Page 35: 1 COSC3306: Programming Paradigms Lecture 1: Introduction Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003

3535

Object-Oriented ProgrammingObject-Oriented Programming

Imperative programming with Imperative programming with objects+classes+inheritance.objects+classes+inheritance.OO programming is not necessarily OO programming is not necessarily imperative programming, but in practice imperative programming, but in practice most OO languages have been most OO languages have been imperative: Simula, Smalltalk, C++, imperative: Simula, Smalltalk, C++, Modula-3, Java. However, CLOS Modula-3, Java. However, CLOS (Common Lisp Object System) is an (Common Lisp Object System) is an object-oriented version of Lisp.object-oriented version of Lisp.

Page 36: 1 COSC3306: Programming Paradigms Lecture 1: Introduction Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003

3636

Functional ProgrammingFunctional Programming

Programming with Programming with functions+lists minus functions+lists minus assignmentassignmentNot all functional languages are “pure” Not all functional languages are “pure” (i.e., without the assignment statement). (i.e., without the assignment statement). Practical functional programming Practical functional programming languages adopt the functional style, but languages adopt the functional style, but for practical reasons, have to rely on non-for practical reasons, have to rely on non-pure functions for input/output. You can’t pure functions for input/output. You can’t completely escape the machine!completely escape the machine!

Page 37: 1 COSC3306: Programming Paradigms Lecture 1: Introduction Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003

3737

Logic ProgrammingLogic Programming

Programming with Programming with propositions+predicates+logical propositions+predicates+logical deductiondeduction

Prolog and special theorem proving Prolog and special theorem proving languages are designed for special languages are designed for special purposespurposes

Page 38: 1 COSC3306: Programming Paradigms Lecture 1: Introduction Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003

3838

Concurrent ProgrammingConcurrent Programming

Programming with Programming with shared shared data+synchronized executiondata+synchronized execution..

Concurrent programming cuts across Concurrent programming cuts across imperative, object-oriented, and functional imperative, object-oriented, and functional programming.programming.

Page 39: 1 COSC3306: Programming Paradigms Lecture 1: Introduction Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003

3939

Extension Language Extension Language ProgrammingProgramming

Programming with Programming with scriptsscripts. E.g, Perl, . E.g, Perl, Python, Tcl, ASP, etc.Python, Tcl, ASP, etc.

Page 40: 1 COSC3306: Programming Paradigms Lecture 1: Introduction Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003

4040

A Brief History of Programming LanguagesA Brief History of Programming Languages

Page 41: 1 COSC3306: Programming Paradigms Lecture 1: Introduction Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003

4141

Language ImplementationLanguage Implementation

There are three basic approaches to translating There are three basic approaches to translating and executing a program written in a high-level and executing a program written in a high-level language into a form that can be executed by a language into a form that can be executed by a machine.machine.– Translation to Translation to machine code machine code using a using a native-code native-code

compilercompiler, and execution on a real machine., and execution on a real machine.– Translation to Translation to bytecodes bytecodes using a using a bytecode compilerbytecode compiler, ,

followed by execution on a “virtual” machine.followed by execution on a “virtual” machine.– Translation into an internal form and execution via a Translation into an internal form and execution via a

read-eval-print interpreterread-eval-print interpreter..

Page 42: 1 COSC3306: Programming Paradigms Lecture 1: Introduction Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003

4242

Hybrid approachesHybrid approachesFor example, the original C++ implementation from AT&T For example, the original C++ implementation from AT&T Bell Labs was a translator that first translated C++ to C, Bell Labs was a translator that first translated C++ to C, which was then compiled into assembly language, which which was then compiled into assembly language, which was then assembled into machine code and executed.was then assembled into machine code and executed.New types of bytecode compilers have appeared in recent New types of bytecode compilers have appeared in recent years, which are called Just-In-Time (JIT) compilers. JIT years, which are called Just-In-Time (JIT) compilers. JIT compilers translate compiled bytecodes into native compilers translate compiled bytecodes into native machine code the first time the bytecode is executed by machine code the first time the bytecode is executed by the virtual machine. Subsequent requests to execute the the virtual machine. Subsequent requests to execute the same bytecode sequence result in the native code being same bytecode sequence result in the native code being executed, resulting in a significant speedup in execution.executed, resulting in a significant speedup in execution.Although not always the case, most compiled languages Although not always the case, most compiled languages are statically typed, meaning that type checking is done at are statically typed, meaning that type checking is done at compile-time.compile-time.Interpreted languages are often dynamically typed.Interpreted languages are often dynamically typed.

Page 43: 1 COSC3306: Programming Paradigms Lecture 1: Introduction Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003

4343

Static typingStatic typing

Generally deemed “safer” since type Generally deemed “safer” since type errors can be detected early, at compile-errors can be detected early, at compile-time, before a program is executed.time, before a program is executed.

Static typing means that the type of a Static typing means that the type of a variable is determined at compile-time and variable is determined at compile-time and that type constrains the set of values that that type constrains the set of values that the variable can hold at run-time.the variable can hold at run-time.

Page 44: 1 COSC3306: Programming Paradigms Lecture 1: Introduction Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003

4444

Dynamic typingDynamic typing

Done at run-time, so programmer type errors are Done at run-time, so programmer type errors are not detected until the program is executed. not detected until the program is executed. This implies that a type error in an infrequently This implies that a type error in an infrequently executed part of a program may not be executed part of a program may not be discovered for a long time, and only surfaces in discovered for a long time, and only surfaces in an unusual (but critical) situation, which then an unusual (but critical) situation, which then causes the program to fail. Dynamic typing causes the program to fail. Dynamic typing means that a types are associated with values at means that a types are associated with values at run-time, and the type of a variable dynamically run-time, and the type of a variable dynamically changes during execution to conform to the type changes during execution to conform to the type of the value that is currently being referenced by of the value that is currently being referenced by the variable.the variable.

Page 45: 1 COSC3306: Programming Paradigms Lecture 1: Introduction Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003

4545

SummarySummary

Programming LanguagesProgramming LanguagesProgramming Language DesignProgramming Language DesignProgramming Language ProcessingProgramming Language ProcessingProgramming a Machine versus Programming a Programming a Machine versus Programming a ComputationComputationProgramming ProductivityProgramming ProductivityProgramming ParadigmsProgramming ParadigmsA Brief History of Programming LanguagesA Brief History of Programming LanguagesLanguage ImplementationLanguage Implementation