programminglanguages programming languages language abstraction and computational paradigms

26
Programming Programming Languages Languages

Upload: thomasine-dixon

Post on 16-Dec-2015

239 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: ProgrammingLanguages Programming Languages Language Abstraction and Computational Paradigms

ProgrammingProgramming LanguagesLanguages

Page 2: ProgrammingLanguages Programming Languages Language Abstraction and Computational Paradigms

Language Abstraction and Language Abstraction and Computational ParadigmsComputational Paradigms

Page 3: ProgrammingLanguages Programming Languages Language Abstraction and Computational Paradigms

ObjectivesObjectives

• How we communicate influences how we How we communicate influences how we

think, and vice versa. think, and vice versa.

• Similarly, how we program computers influences Similarly, how we program computers influences how we think about them, and vice versa.how we think about them, and vice versa.

• It is the goal of this lesson to introduce the It is the goal of this lesson to introduce the major principles and concepts underlying major principles and concepts underlying all programming languages.all programming languages.

Page 4: ProgrammingLanguages Programming Languages Language Abstraction and Computational Paradigms

What is a Programming language?(1)What is a Programming language?(1)

• A simple definition could be ” a notation for A simple definition could be ” a notation for

communicating to a computer what we want communicating to a computer what we want it to do." But this definition is inadequate.it to do." But this definition is inadequate.

• A major advance in computer design A major advance in computer design occurred in the 1940s, when John von occurred in the 1940s, when John von Neumann had the idea that a computer Neumann had the idea that a computer should not be "hard-wired" to do particular should not be "hard-wired" to do particular things, but that a series of codes stored as things, but that a series of codes stored as data would determine the actions taken by data would determine the actions taken by a central processing unit.a central processing unit.

• von Neumann Machine: von Neumann Machine:

Page 5: ProgrammingLanguages Programming Languages Language Abstraction and Computational Paradigms

• Soon programmers realized that it would Soon programmers realized that it would

be a tremendous help to attach symbols to be a tremendous help to attach symbols to the instruction codes, as well as to the instruction codes, as well as to memory locations, and assembly language memory locations, and assembly language was born, with instructions such as was born, with instructions such as

LDA #2 LDA #2 STA XSTA X

• But assembly language, because of its But assembly language, because of its machine dependence, low level of machine dependence, low level of abstraction, and difficulty in being written abstraction, and difficulty in being written and understood, is also not what we usually and understood, is also not what we usually think of as a programming language.think of as a programming language.

Assembly LanguageAssembly Language

Page 6: ProgrammingLanguages Programming Languages Language Abstraction and Computational Paradigms

• programmers soon realized that a higher programmers soon realized that a higher

level of abstraction would improve their level of abstraction would improve their ability to write concise, understandable ability to write concise, understandable instructions that could be used with little instructions that could be used with little change from machine to machine. change from machine to machine.

• Certain standard constructions, such as Certain standard constructions, such as assignment, loops, and selections or assignment, loops, and selections or choices, were constantly being used and had choices, were constantly being used and had nothing to do with the particular machine; nothing to do with the particular machine; these constructions should be expressible in these constructions should be expressible in simple standard phrases that could be simple standard phrases that could be translated into machine-usable form. translated into machine-usable form.

Higher Level of AbstractionHigher Level of Abstraction

Page 7: ProgrammingLanguages Programming Languages Language Abstraction and Computational Paradigms

• An area of memory where both programs An area of memory where both programs and data are stored and a separate central and data are stored and a separate central processing unit that sequentially executes processing unit that sequentially executes instructions fetched from memory. instructions fetched from memory.

High Level: von Neumann High Level: von Neumann ModelModel

• Programs thus became relatively machine Programs thus became relatively machine independent, but the language still independent, but the language still reflected the underlying architecture of reflected the underlying architecture of the von Neumann model of a machine: the von Neumann model of a machine:

• Such as the C for the previous assembly Such as the C for the previous assembly language instructions (indicating language instructions (indicating assignment of the value 2 to the location assignment of the value 2 to the location with name X) with name X) X = 2;X = 2;

Page 8: ProgrammingLanguages Programming Languages Language Abstraction and Computational Paradigms

• but need only describe computation or but need only describe computation or processing in general.processing in general.

Non-von Neumann model Non-von Neumann model

• With increasing abstraction, and with the With increasing abstraction, and with the development of new architectures, particularly development of new architectures, particularly parallel processors, came the realization that parallel processors, came the realization that programming languages need not be based on programming languages need not be based on any particular model of computation or any particular model of computation or machine,machine,

• Most modem programming languages still Most modem programming languages still retain the flavor of this processor model of retain the flavor of this processor model of computation: sequential computation.computation: sequential computation.

Page 9: ProgrammingLanguages Programming Languages Language Abstraction and Computational Paradigms

• Computation.Computation.

A Formal Definition: A Formal Definition:

• Three key concepts in this definition. Three key concepts in this definition.

• A programming language is a notational A programming language is a notational system for describing computation in machine-system for describing computation in machine-readable and human-readable form. readable and human-readable form.

• Machine readabilityMachine readability

• Human readabilityHuman readability

Page 10: ProgrammingLanguages Programming Languages Language Abstraction and Computational Paradigms

ComputationComputation

• The generally accepted Church’s thesis states The generally accepted Church’s thesis states that it is not possible to build a machine that is that it is not possible to build a machine that is inherently more powerful than a Turing machine. inherently more powerful than a Turing machine.

• Computation is usually defined traditionally using Computation is usually defined traditionally using the mathematical concept of a Turing machine.the mathematical concept of a Turing machine.

Page 11: ProgrammingLanguages Programming Languages Language Abstraction and Computational Paradigms

Turing machineTuring machine• Turing machine is a kind of computer whose Turing machine is a kind of computer whose

operation is simple enough to be described operation is simple enough to be described with great precision. Such a machine needs with great precision. Such a machine needs also to be powerful enough to perform any also to be powerful enough to perform any computation that a computer can.computation that a computer can.

Page 12: ProgrammingLanguages Programming Languages Language Abstraction and Computational Paradigms

General View of ComputationGeneral View of Computation

• Computation instead includes all kinds of Computation instead includes all kinds of computer operations, including data computer operations, including data manipulation, text processing, and manipulation, text processing, and information storage and retrieval. information storage and retrieval.

• We will think of computation as any We will think of computation as any process that can be carried out by a process that can be carried out by a computer.computer.

• Sometimes a programming language will be Sometimes a programming language will be designed with a particular kind of processing designed with a particular kind of processing in mind, such as report generation, graphics, in mind, such as report generation, graphics, or database maintenance. or database maintenance.

Page 13: ProgrammingLanguages Programming Languages Language Abstraction and Computational Paradigms

Machine ReadabilityMachine Readability

• First, there must be an algorithm to translate a First, there must be an algorithm to translate a language, that is, a step-by-step process that is language, that is, a step-by-step process that is unambiguous and finite. unambiguous and finite.

• For a language to be machine-readable, it For a language to be machine-readable, it must have a simple enough structure to must have a simple enough structure to allow for efficient translation.allow for efficient translation.

• Second, the algorithm cannot have too Second, the algorithm cannot have too great a complexity.great a complexity.

• Usually, machine readability is ensured by Usually, machine readability is ensured by restricting the structure of a programming restricting the structure of a programming language to that of the so-called context-free language to that of the so-called context-free languages.languages.

Page 14: ProgrammingLanguages Programming Languages Language Abstraction and Computational Paradigms

Human ReadabilityHuman Readability

• Human readability requires that a programming Human readability requires that a programming language provide abstractions of the actions of language provide abstractions of the actions of computers that are easy to understand, even computers that are easy to understand, even by persons not completely familiar with the by persons not completely familiar with the underlying details of the machine. underlying details of the machine.

• The development of such abstraction The development of such abstraction mechanisms has been one of the important mechanisms has been one of the important advances in programming language.advances in programming language.

Page 15: ProgrammingLanguages Programming Languages Language Abstraction and Computational Paradigms

Human ReadabilityHuman Readability• Human readability requires that a programming Human readability requires that a programming

language provide abstractions of the actions of language provide abstractions of the actions of computers that are easy to understand, even computers that are easy to understand, even by persons not completely familiar with the by persons not completely familiar with the underlying details of the machine. underlying details of the machine.

• The development of such abstraction The development of such abstraction mechanisms has been one of the important mechanisms has been one of the important advances in programming language.advances in programming language.

• Nowaday, a programming language is no longer Nowaday, a programming language is no longer a way of describing computation, but it becomes a way of describing computation, but it becomes part of a software development environment part of a software development environment that promotes a software design methodology.that promotes a software design methodology.

Page 16: ProgrammingLanguages Programming Languages Language Abstraction and Computational Paradigms

Language AbstractionsLanguage Abstractions

• Programming language abstractions fall Programming language abstractions fall into two general categories: Data into two general categories: Data abstraction, and Control abstraction. abstraction, and Control abstraction.

• Data abstractions abstract properties of the data, Data abstractions abstract properties of the data, such as character strings, numbers, or search such as character strings, numbers, or search trees, which is the subject of computation. trees, which is the subject of computation.

• Control abstractions abstract properties of Control abstractions abstract properties of the transfer of control, that is, the the transfer of control, that is, the modification of the execution path of a modification of the execution path of a program based on the situation at hand. program based on the situation at hand. Examples of control abstractions are loops, Examples of control abstractions are loops, conditional statements, and procedure calls. conditional statements, and procedure calls.

Page 17: ProgrammingLanguages Programming Languages Language Abstraction and Computational Paradigms

Levels of AbstractionsLevels of Abstractions

• Programming language abstractions also Programming language abstractions also fall into levels:fall into levels:

• Basic abstractions collect together the Basic abstractions collect together the most localized machine information. most localized machine information.

• Structured abstractions collect more global Structured abstractions collect more global information about the structure of the program. information about the structure of the program.

• Unit abstractions collect information about Unit abstractions collect information about entire pieces of a program. entire pieces of a program.

Page 18: ProgrammingLanguages Programming Languages Language Abstraction and Computational Paradigms

Data Abstractions: Basic Data Abstractions: Basic abstractions abstractions • Basic abstractions. Basic data abstractions in programming Basic abstractions. Basic data abstractions in programming

languages abstract the internal representation of common data languages abstract the internal representation of common data values in a computer. values in a computer.

• Data types of basic data values are usually given the Data types of basic data values are usually given the names of their corresponding mathematical values, such names of their corresponding mathematical values, such as integer and real. as integer and real.

• Variables are given names and data types Variables are given names and data types using a declaration, such as the Pascal using a declaration, such as the Pascal

var x: integer;var x: integer; or the equivalent C declaration or the equivalent C declaration int x; int x;

Page 19: ProgrammingLanguages Programming Languages Language Abstraction and Computational Paradigms

Data Abstractions: Structured Data Abstractions: Structured abstractions abstractions

• The data structure is the principal method The data structure is the principal method for abstracting collections of data values for abstracting collections of data values that are related. that are related.

• A typical data structure provided by A typical data structure provided by programming languages is the array:programming languages is the array:

• Variables can be given a data structure in Variables can be given a data structure in a declaration, as in the C: a declaration, as in the C: int a[10]; int a[10];

• Data structures can also be viewed as new Data structures can also be viewed as new data types that are not internal, but are data types that are not internal, but are constructed by the programmer as needed, constructed by the programmer as needed, such as the C: such as the C: typedef int Intarray[10]; typedef int Intarray[10];

Page 20: ProgrammingLanguages Programming Languages Language Abstraction and Computational Paradigms

Data Abstractions: Unit Data Abstractions: Unit abstractions abstractions

• In a large program, it is useful and even In a large program, it is useful and even necessary to collect all the information necessary to collect all the information needed for the creation and use of a data needed for the creation and use of a data type into one location and to restrict the type into one location and to restrict the access to the details of the data type. access to the details of the data type.

• This ensures that changes in the structure of This ensures that changes in the structure of the data type do not affect large areas of the the data type do not affect large areas of the program and that programmers need not keep program and that programmers need not keep all the details of a data type in mind at all timesall the details of a data type in mind at all times

• This mechanism is called a data This mechanism is called a data encapsulation or, more commonly, an encapsulation or, more commonly, an abstract data type mechanism. abstract data type mechanism.

Page 21: ProgrammingLanguages Programming Languages Language Abstraction and Computational Paradigms

Control Abstractions: Basic Control Abstractions: Basic abstractionsabstractions

• Typical basic control abstractions are those Typical basic control abstractions are those statements in a language that combine a statements in a language that combine a few machine instructions into a more few machine instructions into a more understandable abstract statement.understandable abstract statement.

• The assignment statement is a typical The assignment statement is a typical instruction that abstracts the computation instruction that abstracts the computation and storage of a value into the location given and storage of a value into the location given by a variable, as for example, x=x+3;by a variable, as for example, x=x+3;

• Another typical basic control statement is the Another typical basic control statement is the goto statement in Fortran: GOTO 10 ……..goto statement in Fortran: GOTO 10 ……..

10 CONTINUE 10 CONTINUE

Page 22: ProgrammingLanguages Programming Languages Language Abstraction and Computational Paradigms

Control Abstractions: Structured Control Abstractions: Structured abstractionsabstractions

• Structured control abstractions divide a program Structured control abstractions divide a program into groups of instructions that are nested within into groups of instructions that are nested within tests that govern their execution. tests that govern their execution.

• Typical examples are selection statements, Typical examples are selection statements, such as the if-statement of many such as the if-statement of many languages, the case-statement of Pascal, languages, the case-statement of Pascal, and the switch-statement of C. and the switch-statement of C.

• Structured looping mechanisms come in Structured looping mechanisms come in many forms, including the while, and for many forms, including the while, and for loops of C.loops of C.

Page 23: ProgrammingLanguages Programming Languages Language Abstraction and Computational Paradigms

Control Abstractions: Structured Control Abstractions: Structured abstractionsabstractions

• A further, powerful mechanism for structuring A further, powerful mechanism for structuring control is the procedure, sometimes also control is the procedure, sometimes also called a subprogram or subroutine.called a subprogram or subroutine.

• Procedure call is a more complex mechanism Procedure call is a more complex mechanism than selection or looping, since it requires the than selection or looping, since it requires the storing of information about the condition of storing of information about the condition of the program at the point of the call and the the program at the point of the call and the way the called procedure operates.way the called procedure operates.

• Such information is stored in a runtime Such information is stored in a runtime environment. environment.

Page 24: ProgrammingLanguages Programming Languages Language Abstraction and Computational Paradigms

Control Abstractions: Unit Control Abstractions: Unit abstractionsabstractions

• Control can also be abstracted to include a collection Control can also be abstracted to include a collection of procedures that provide logically related services to of procedures that provide logically related services to other parts of a program and that form a unit, or other parts of a program and that form a unit, or standalone, part of the program.standalone, part of the program.

• Examples of unit abstractions include the Examples of unit abstractions include the package of Java and Ada.package of Java and Ada.

Page 25: ProgrammingLanguages Programming Languages Language Abstraction and Computational Paradigms

Parallel AbstractionsParallel Abstractions

• One kind of control abstraction that does One kind of control abstraction that does not fit into any one abstraction level is that not fit into any one abstraction level is that of parallel programming mechanisms. of parallel programming mechanisms.

• Ada provides the task mechanism for Ada provides the task mechanism for parallel execution.parallel execution.

• Ada’s tasks are essentially a unit abstraction.Ada’s tasks are essentially a unit abstraction.

• Other languages provide different levels of Other languages provide different levels of parallel abstractions, such as Java’s threads. parallel abstractions, such as Java’s threads.

Page 26: ProgrammingLanguages Programming Languages Language Abstraction and Computational Paradigms

Turing CompletenessTuring Completeness• It is worth noting that almost all abstraction It is worth noting that almost all abstraction

mechanisms are provided for human readability.mechanisms are provided for human readability.

• If a programming language needs to If a programming language needs to describe only computation, then it needs describe only computation, then it needs only enough mechanisms to be able to only enough mechanisms to be able to describe all the computations that a Turing describe all the computations that a Turing machine can perform, Such a language is machine can perform, Such a language is called Turing complete.called Turing complete.• A programming language is Turing complete A programming language is Turing complete provided it has integer variables and provided it has integer variables and arithmetic and sequentially executes arithmetic and sequentially executes statements, which include assignment, statements, which include assignment, selection (if) and loop (while)statements. selection (if) and loop (while)statements.