Survey of programming languages
Survey of programming languages
languages
1
This is the analytical and formal study of different
programming languages in terms of data types
and structures
2
Objectives of the course
•improve background for choosing appropriate languages
•ability to easily learn new languages and technologies
as they arrive (ability to focus on the big picture and not
the details)
•some things cannot be expressed as easily in certain
languages increase capacity to express ideas about
computation (thinking out of the box)
•why these languages?
•increase ability to design and implement new languages
•to better appreciate the historical context
•to become a well-rounded computer scientist
3
•in summary, this course shd make you a better
• History of programming languages
4
programming paradigms
6
OBJECT-
OBJECT-ORIENTED LANGUAGES
• In OOL, programs are segmented into objects that expose (methods)
and data using interfaces. OOL combines procedures and data
structures, c++, java are examples
7
FUNCTIONAL LANGUAGES
• functional languages are languages that promotes writing programs
purely based on mathematical functions
8
Declarative languages
• languages. Declarative languages are nonprocedural or very high
level languages that a program specifies what is to be done rather
than how to do it.
9
SCRIPTING LANGUAGES
• A script or scripting language is a
computer language with a series of commands within
a file that is capable of being executed without being
compiled. Good examples of server-
side scripting languages include Perl, PHP, and
Python, javascript
10
Influence of computer architecture on
languages
• architecture of computers has had a profound effect on language design.
• Most of the popular languages are tailored towards the von Neumann
architecture where both data and programs are stored in the same
memory and are mostly imperative.
• Operands in expressions are piped from memory to the CPU and the
result of evaluating the expression sent back to the memory cell
represented by the left side of the assignment as in a=5;
12
Recap- Architecture
• Universal Turing machine (UTM) - used only a memory tape and a finite
state box to move the tape, write a symbol or erase one after consulting
a small table of states and responses
13
14
15
• The execution of a machine code program on a von Neumann
architecture computer occurs in a process called fetch-execute cycle.
See algorithm below
16
• In a functional language programming can be done without the kind
of variables that are used in imperative languages, without
assignment statements and without iteration.
17
Programming Methodologies evolution
• New software development methodologies (e.g., object- oriented software
development) led to new programming paradigms and by extension, new
programming languages
• 950s and early 1960s – Simple applications – worry about machine efficiency
19
IMPLEMENTATION METHODS
• JIT
20
Fundamental characteristics of a
programming language
• Universality -every problem must have a solution that can be programmed
in the language, if that problem can be solved at all by a computer.
21
What makes up a programming language
• syntax is concerned with the form of programs:
• how expressions, commands, declarations, and other constructs must
be arranged to make a well-formed program eg for… next loop
22
Language processor
• Any system for processing programs – executing programs, or
preparing them for execution – is called a language processor.
23
24
Values and types
Data are raw quantities, characters, or symbols on which operations
are performed by a computer.
25
Different types of values supported by
languages
• C supports integers, real numbers, structures, arrays, unions, pointers
to variables, and pointers to functions. (Integers, real numbers, and
pointers are primitive values; structures, arrays, and unions are
composite values.)
• C++ supports all the above types of values plus objects. (Objects are
composite values.)
• JAVA supports booleans, integers, real numbers, arrays, and objects.
• (Booleans, integers, and real numbers are primitive values; arrays and
• objects are composite values.)
26
A type is a set of values that supports similar
operations in a programming language
• If v is a value of type T, we mean simply that v ∈ T.
eg to be given in class
27
• A primitive value is one that cannot be decomposed into simpler
values.
28
• The cardinality of a type T, written #T, is the number of distinct
values in T.
• For example:
• #Boolean = 2
29
Not all languages have a distinct type corresponding to Boolean. For
example, C++ has a type named bool, but its values are just small
integers;
there is a convention that zero represents false and any other integer
represents true. This convention originated in C.
• Not all languages have a distinct type corresponding to Character. For
example, C, C++, and JAVA all have a type char,
• Some languages provide not one but several integer types.
For example, JAVA provides byte {−128, . . . ,+127}, short
{−32 768, . . . ,+32 767}, int {−2 147 483 648, . . . ,+2 147 483 647}, and
long {−9 223 372 036 854 775 808, . . . ,+9 223 372 036 854 775 807}.
30
• In a Cartesian product, values of several (possibly different) types are
grouped into tuples.
• We use the notation (x, y) to stand for the pair whose first component
is x and whose second component is y. We use the notation S × T to
stand for the set of all pairs (x, y) such that x is chosen from set S and
y is chosen from set T. Formally:
• S × T = {(x, y) | x ∈ S; y ∈ T}
31
Objects
JAVA objects
Consider a JAVA program containing the following class declarations:
class Point {
private float x, y;
. . . // methods
}
class Circle extends Point {
private float r;
. . . // methods
}
class Rectangle extends Point {
private float w, h;
. . . // methods
}
32
Recursive types
• A recursive type is one defined in terms of itself eg list and trees
33
BUILD IN PRIMITIVES
• Consider the following JAVA declarations:
int countryPopulatn;
long worldPopulatn;
The variable countryPopulatn could be used to contain the current
population of any country (since no country yet has a population
exceeding 2 billion). The variable worldPopulatn could be used to
contain the world’s total population.
But note that the program would fail if worldPopulatn’s type were
int rather than long (since the world’s total population now
34
exceeds 7 billion).
C++ type definition:
35
• A discrete primitive type is a primitive type whose values
have a one-to-one relationship with a range of integers.
37
STATIC VS DYNAMIC TYPING
• Before any operation is to be performed, the types of its operands must be
checked in order to prevent a type error.
In a statically typed language, each variable and each expression has a
fixed type (which is either explicitly stated by the programmer or inferred
by the compiler). Using this information, all operands can be type-checked at
compile-time. Give examples
In a dynamically typed language, values have fixed types, but variables and
expressions have no fixed types. Give examples
• LISP, PROLOG, BASIC, PERL, and PYTHON are examples of dynamically typed
languages.
38
EXAMPLE
C++ static typing
Consider the following C++ function definition:
bool even (int n) {
return (n % 2 == 0);
}
def even (n):
return (n % 2 == 0)
• ADD QBASIC TO EXPLAIN TO STUDENTS IN CLASS
39
JUSTIFICATION FOR CHOOSING A TYPE
Static typing is more efficient. Dynamic typing requires (possibly
repeated)
run-time type checks, which slow down the program’s execution.
40
• Static typing is more secure: the compiler can certify that
the program contains no type errors. Dynamic typing provides
no such security. (This point is important because type errors
account for a significant proportion of programming errors.)
41
Expressions
An expression is a construct that will be evaluated to yield a
value.
Fundamental forms of expression:
• literals
• constructions
• function calls
• conditional expressions
• iterative expressions
• constant and variable accesses 42
Literals
The simplest kind of expression is a literal, which
denotes a fixed value of some type. EG 365 3.1416
false '%' "What?“
44
JAVA object constructions
The following JAVA object construction:
new Date(today.m, size(today.m))
• Function calls
A function call computes a result by applying a function procedure
(or method) to one or more arguments. The function call typically
has the form ‘‘F(E)’’ An operator may be thought of as denoting a
function.
USE EXAMPLES TO ILLUSTRATE
45
• A conditional expression computes a value that
depends on a condition.
46
VARIABLES
• Variables and storage ‘‘Once a programmer has
understood the use of variables, he has
understood the essence of programming
47
• In imperative (an object-oriented and concurrent)
programming languages, a variable is a container for a
value, and may be inspected and updated as often as
desired.
48
• A variable is either a simple or composite in
nature
49
• A storable value is one that can be stored in a single storage cell.
• C++’s storable values are primitive values and pointers. (Structures, arrays,
unions, and objects are not storable, since none of these can be stored in
a single storage cell. Functions also are not storable, since they cannot be
stored at all. However, pointers to all of these things are storable.)
• JAVA’s storable values are primitive values and pointers to objects. (Objects
themselves are not storable, but every object is implicitly accessed through
a pointer.)
50
Explaining simple variable in C++
• C simple variable
• Consider the simple variable declared in the following C block:
• { int n;
• n = 0;
• n = n+1;
•}
51
Explaining composite values in c++
• C++ record and array variables
• Consider the following C++ (or C) declarations:
• struct Date {
• int y, m, d;
• };
• Date today;
52
Total vs selective update of composite variable…
• Static vs dynamic vs flexible arrays…..
• A static array is an array variable whose index range is fixed at compile-
time. A dynamic array is an array variable whose index range is fixed at
the time when the array variable is created.
53
Copy semantics vs reference semantics
• When a program assigns a composite value to a variable of the same
type, what happens depends on the language. There are in fact two
distinct possibilities:
• Copy semantics. The assignment copies all components of the
composite value into the corresponding components of the composite
variable.
• Reference semantics. The assignment makes the composite variable
contain a pointer (or reference) to the composite value.
54
Lifetime
• Every variable is created (or allocated) at some definite time, and destroyed
(or deallocated) at some later time when it is no longer needed. The interval
between creation and destruction of a variable is called its lifetime.
We can classify variables according to their lifetimes:
• A global variable’s lifetime is the program’s run-time.
• A local variable’s lifetime is an activation of a block.
• A heap variable’s lifetime is arbitrary, but is bounded by the program’s
run-time.
• A persistent variable’s lifetime is arbitrary, and may transcend the run-time
of any particular program.
55
• Aglobal variable is one that is declared for use throughout the
program.
• A local variable is one that is declared within a block, for use only
within that block
56
Use example to illustrate variable lifetime
• A heap variable is one that can be created, and destroyed, at any
time during the program’s run-time.
• A heap variable remains reachable as long as it can be accessed by
following pointers from a global or local variable.
58
Dangling pointers
A dangling pointer is a pointer to a variable that has been destroyed.
Dangling
pointers arise from the following situations:
• A pointer to a heap variable still exists after the heap variable is
destroyed.
• A pointer to a local variable still exists (e.g., it is stored in a global
variable)
at exit from the block in which the local variable was declared.
59