Big Ideas 4up
Big Ideas 4up
More generally:
• What is a PL?
• Why are new PLs created?
CS251 Programming Languages – What are they used for?
Spring 2017, Lyn Turbak
– Why are there so many?
Department of Computer Science • Why are certain PLs popular?
Wellesley College • What goes into the design of a PL?
1-2
•
at Xerox PARC
1-5 1-6
1-7 1-8
Programming Language EssenEals PL Parts
Syntax: form of a PL
• What a P in a given L look like as symbols?
• Concrete syntax vs abstract syntax trees (ASTs)
Means of CombinaEon – Scope rules: to which declaration does a variable reference refer?
– Type rules: which programs are well-typed (and therefore legal)?
• Dynamic Semantics: What is the behavior of P? What actions does it
Means of AbstracEon perform? What values does it produce?
– Evaluation rules: what is the result or effect of evaluating each language
fragment and how are these composed?
Think of the languages you know. What means of abstracEon do they have?
Pragmatics: implementation of a PL (and PL environment)
• How can we evaluate programs in the language on a computer?
1-9 • How can we optimize the performance of program execution? 1-10
Syntax (Form) vs. Semantics (Meaning) Concrete Syntax: Absolute Value FuncEon
in Natural Language Logo: to abs :n ifelse :n < 0 [output (0 - :n)] [output :n] end
Javascript: function abs (n) {if (n < 0) return -n; else return n;}
Furiously sleep ideas green colorless. Java: public static int abs (int n) {if (n < 0) return -n; else return n;}
Python: App Inventor:
Colorless green ideas sleep furiously. def abs(n):
if n < 0:
return -n
Little white rabbits sleep soundly. else:
return n
Scheme: (define abs (lambda (n) (if (< n 0) (- n) n)))
PostScript: /abs {dup 0 lt {0 swap sub} if} def
1-11 1-12
Abstract Syntax Tree (AST): This AST abstracts over the Dynamic SemanEcs Example 1
concrete syntax for the Logo,
Absolute Value FuncEon JavaScript, and Python
definiEons. The other definiEons What is the meaning of the following expression?
funcEonDeclaraEon would have different ASTs.
me body
onNa
funcD params
abs condiEonalStatement (1 + 11) * 10
n
test
then
b = a + 20; Java
C
print(b);
Python
a = 300 JavaScript
print(b); Pascal
count = 0; App Inventor
1-15 1-16
StaEc SemanEcs Example 1 Static Semantics Example 2: Detecting Loops
Which of the following Java examples is well-typed (i.e., passes the type checker)? Which of these following Python programs has inputs
How do you know? What assumpEons are you making? for which it loops forever?
A 2 * (3 + 4) F if (a) { G public boolean f(int i, boolean b) { A def f(x):
c = a + b; return b && (i > 0);
B 2 < (3 + 4) } else { } return x+1
c = a * b;
C 2 < True } H public int g(int i, boolean b) {
B def g(x): C def g2(x): G def k(x):
return i * (b ? 1 : -1);
}
while True: return g2(x) while x != 1:
D if (a < b) { pass
c = a + b;
if (x % 2) == 0:
I public int p(int w) { return x x = x/2
} else {
c = a * b; if (w > 0) { return 2*w; } else:
} } x = 3*x + 1
E def h(x): F def h2(x): return 1
J public int q(int x) { return x > 0; } while x > 0: if x <= 0:
E if (a < b) {
x = x+1 return x
c = a + b;
} else { K public int r(int y) { return g(y, y>0); } return x else:
c = a > b; return h(x+1)
} L public boolean s(int z) { return f(z); }
1-17 2-18
This is a consequence of Rice’s Theorem (see CS235). • Church-Turing Thesis: Computability is the common spirit embodied by
this collecEon of formalisms.
For example, will this program ever: • This thesis is a claim that is widely believed about the intuiEve noEons of
algorithm and effecEve computaEon. It is not a theorem that can be
• halt on certain inputs proved.
• encounter an array index out of bounds error? • Because of their similarity to later computer hardware, Turing machines
• throw a NullPointerException? (CS235) have become the gold standard for effecEvely computable.
• access a given object again? • We ll see in CS251 that Church’s lambda-calculus formalism is the
• send sensitive information over the network? foundaEon of modern programming languages.
• divide by 0? • A consequence: programming languages all have the same
• run out of memory, starting with a given amount available? computaEonal power in term of what they can express. All such
• try to treat an integer as an array? languages are said to be Turing-complete.
2-19 2-20
Pragmatics: Raffle App In App Inventor
Expressiveness and Power hIp://ai2.appinventor.mit.edu
1-23 1-24
Metaprogramming: TranslaEon Metaprogramming: Embedding
Program in
Program in
language A A to B translator
language B
Program in Interpreter Machine M
language A for language B
embedded in on machine M
language B
Interpreter
Machine M
for language B
on machine M 1-25 1-26
kernel
primiEve
values/datatypes
syntacEc sugar
system libraries
• Naming: Do variables/parameters name expressions, the values resulEng • Func(onal, func(on-oriented (e.g Racket, ML, Haskell): ComputaEon is
expressed by composing funcEons that manipulate immutable data.
from evaluaEng expressions, or mutable slots holding the values from
evaluaEng expressions? How are names declared and referenced? What • Object-oriented (e.g. Simula, Smalltalk, Java): ComputaEon is expressed in
determines their scope? terms of stateful objects that communicate by passing messages to one
another.
• State: What is mutable and immutable; i.e., what enEEes in the language
(variables, data structures, objects) can change over Eme. • Logic-oriented (e.g. Prolog): ComputaEon is expressed in terms of declaraEve
relaEonships.
• Control: What constructs are there for control flow in the language, e.g.
condiEonals, loops, non-local exits, excepEon handling, conEnuaEons? Note: In pracEce, most PLs involve mulEple paradigms. E.g.
• Data: What kinds of data structures are supported in the language, including • Python supports funcEonal features (map, filter, list comprehensions) and
products (arrays, tuples, records, dicEonaries), sums (opEons, oneofs, objects
variants), sum-of-products, and objects.
• Racket and ML have imperaEve features.
• Types: Are programs staEcally or dynamically typed? What types are
expressible? 1-29 1-30