What is a Programming Language
A language used to express instructions to a computer.
Consider the following statement:
set x[i] to x[i] + 1
This is clearly intended to denote the increment of an array element. How would we translate this statement to a variety of different languages, and what would it mean?
x[i] = x[i] + 1;
x
and adds i to it. The addition is a hardware operation,
so it is dependent upon the hardware in question. This resulting
address is then referenced (if it's legal - which it might not be),
1 is added to the bit-string stored there (again, as a
hardware addition, which can overflow), and the result is stored back
to that location. However, no attempt has been made to determine that
x is even a vector and that x[i] is a
number.
(vector-set! x i (+ (vector-ref x i) 1))
x is
indeed an array, (b) makes sure i is within the bounds of
the array, (c) ensures the dereferenced location contains a number,
and (d) performs abstract arithmetic (so there will be no
``overflow'').
x[i] = x[i] + 1;
Taxonomy of Programming Languages
How Many Languages?
FORTRAN, COBOL, (Visual) BASIC, ALGOL-60, ALGOL-68, PL/I, C, C++, RPG, Pascal, Modula, Oberon, Lisp, Scheme, ML, Haskell, Ada, Prolog, Goedel, Snobol, ICON, ...
Programming Language Classifications
Consider a simple algorithm for testing primality.
In Java:
public static boolean isprime (int n) {
int d;
for (d = 2; d < n; d++)
if (n % d == 0)
return false;
return true;
In Standard ML (using a recursive function):
fun isprime (n:int) : bool =
let fun no_divisor (d:int) : bool =
(d >= n) orelse
((n mod d <> 0) andalso
(no_divisor (d+1)))
in no_divisor 2
end
In Intel X86 Assembler:
( here's a little tutorial on the I386 if you want it)
.globl isprime
isprime:
pushl %ebp ; set up procedure entry
movl %esp,%ebp
pushl %esi
pushl %ebx
movl 8(%ebp),%ebx ; fetch arg n from stack
movl $2,%esi ; set divisor d := 2
cmpl %ebx,%esi ; compare n,d
jge true ; jump if d >= n
loop: movl %ebx,%eax ; set n into ....
cltd ; ... dividend register
idivl %esi ; divide by d
testl %edx,%edx ; remainder 0?
jne next ; jump if remainder non-0
xorl %eax,%eax ; set ret value := false(0)
jmp done
next: incl %esi ; increment d
cmpl %ebx,%esi ; compare n,d
jl loop ; jump if d < n
true: movl $1,%eax ; set ret value := true(1)
done: leal -8(%ebp),%esp ; clean up and exit
popl %ebx
popl %esi
leave
ret
General Characteristics of HLLs:
Machine Code Characteristics:
Programming Paradigms
Example: compute m raised to the n power (n > 0)
result := 1; while n > 0 do result := result * m; n := n 1 end while;
Assessment:
Example: compute m raised to the n power (n > 0)
fun power (m,n) =
if (n = 0)
then 1
else m * power(m,n1);
Assessment:
Example: compute m raised to the n power (n > 0)
/* define predicate power(m,n,result) */ power(M,0,1). power(M,N,Result) -> minus(N,1,N_Sub1), power(M,N_Sub1,Temp_Result), times(M,Temp_Result,Result).
Assessment:
What Makes a Good Language?
Ease of Programming:
Example:
Pascal variable declarations:
var i : integer; var j : integer := 1; var a : array[1..5] of integer; var b : array[1..5] of integer := (1,2,3,4,5);
syntax: 'var' <var_name> ':' <type> [':=' <value>] ';'
and we can have
type intarray = array[1..5] of integer; var c : intarray;
C scalar variable declaration:int i; int j = 1;syntax: <type> <var> ['=' <value>] ';'
C array variable declaration:int a[10]; int b[5] = {1,2,3,4,5};syntax: <elm-typ> <var> <dim> ['=' { <array_values> } ] ';'
and we can't use typedef to name an array type.Consistent Constructs:
Orthogonality - The property of being able to combine various features in a language in all possible combinations.
Example: ``Any kind of declaration can be used for any kind of type.''
Example: ``If one statement is allowed in a place, zero or more statements should also be allowed there.''
Remarks:
High Expressive Power:
fun merge(nil,M) = M | merge(N,nil) = N | merge(N,M) = ...
Language Security Concept:
A language is secure if it guarantees that programming errors cannot ``give rise to machine or implementation dependent effects, which are inexplicable in terms of the language itself.'' [C.A.R. Hoare '73]
What this means is that the behavior of any program, even one with bugs, can be understood within the framework of the language itself.
Example: C has numerous insecure features:
Design Issues:
Fast Translation Techniques:
How to Make Programs Portable: