ppl1
DESCRIPTION
twertrTRANSCRIPT
-
Principles of
Programming LanguagesLecture 1
Slides by Daniel Deutch, based on
lecture notes by Prof. Mira Balaban
-
Introduction
We will study Modeling and Programming Computational ProcessesDesign PrinciplesModularity, abstraction, contractsProgramming languages featuresFunctional ProgrammingE.g. Scheme, MLFunctions are first-class objectsLogic ProgrammingE.g. PrologDeclarative ProgrammingImperative ProgrammingE.g. C,Java, PascalFocuses on change of stateNot always a crisp distinction for instance scheme can be used for imperative programming. -
*
Declarative Knowledge
What is true
*
Lets see an example.
-
*
To find an approximation of x:
Make a guess G Improve the guess by averaging G and x/G Keep improving the guess until it is good enoughImperative and Functional Knowledge
How to
[Heron of Alexandria]
An algorithm due to:
*
-
More topics
TypesType Inference and Type CheckingStatic and Dynamic TypingDifferent Semantics (e.g. Operational)Interpreters vs. CompilersLazy and applicative evaluation -
Languages that will be studied
SchemeDynamically TypedFunctions are first-class citizensSimple (though lots of parenthesis )and allows to show different programming styles
MLStatically typed languagePolymorphic types PrologDeclarative, Logic programming languageLanguages are important, but we will focus on the principles -
Administrative Issues
Web-siteExercisesMid-termExam Grade -
Use of Slides
Slides are teaching-aids, i.e. by nature incompleteCompulsory material include everything taught in class, practical sessions as well as compulsory reading if mentioned -
Today
Scheme basicsSyntax and SemanticsThe interpreterExpressions, values, types.. -
*
Scheme
LISP = LISt ProcessingInvented in 1959 by John McCarthyScheme is a dialect of LISP invented by Gerry Sussman and Guy Steele*
-
*
The Scheme Interpreter
The Read/Evaluate/Print LoopRead an expressionCompute its valuePrint the resultRepeat the aboveThe(Global) Environment
Mapping of names to valuesName Value
score23total25percentage92*
-
*
Language Elements
Syntax Semantics
Means of Abstraction(define score 23)Associates score with 23 in environment tableMeans of Combination(composites)(+ 3 17 5)Application of proc to arguments Result = 25Primitives23+*#t, #f23Primitive Proc (add)Primitive Proc (mult)Boolean*
-
*
Computing in Scheme
==> 23
23
==> (+ 3 17 5)
25
==> (+ 3 (* 5 6) 8 2)
43
==> (define score 23)
23
score
Name Value
Environment Table
Opening parenthesis
Expression whose value is a procedure
Other expressions
Closing parenthesis
*
-
*
Computing in Scheme
==> score
23
==> (define total 25)
==> (* 100 (/ score total))
92
==> (define percentage (* 100 (/ score total))
23
score
25
total
92
percentage
==>
Atomic (cant decompose) but not primitive
A name-value pair in the env. is called binding
Name Value
Environment
*
-
*
Evaluation of Expressions
To Evaluate a combination: (as opposed to special form)
Evaluate all of the sub-expressions in some order
Apply the procedure that is the value of the leftmost sub-expression to the arguments (the values of the other sub-expressions)
The value of a numeral: number
The value of a built-in operator: machine instructions to execute
The value of any name: the associated value in the environment
*
-
*
Using Evaluation Rules
==> (define score 23)
==> (* (+ 5 6 ) (- score (* 2 3 2 )))
Special Form (second sub-expression is not evaluated)
*
+
5
6
11
-
23
*
3
2
2
12
11
121
*
-
*
Abstraction Compound Procedures
How does one describe procedures?(lambda (x) (* x x))To process
something
multiply it by itself
formal parameters
body
Internal representation
Special form creates a procedure object and returns it as a valueProc (x) (* x x)
*
-
*
More on lambdas
The use of the word lambda is taken from lambda calculus.A lambda body can consist of a sequence of
expressionsThe value returned is the value of the last oneSo why have multiple expressions at all?*
-
*
Evaluation of An Expression
To Apply a compound procedure: (to a list of arguments)
Evaluate the body of the procedure with the formal parameters replaced by the corresponding actual values
==> ((lambda(x)(* x x)) 5)
Proc(x)(* x x)
5
(* 5 5)
25
*
-
*
Evaluation of An Expression
To Apply a compound procedure: (to a list of arguments)
Evaluate the body of the procedure with the formal parameters replaced by the corresponding actual values
To Evaluate a combination: (other than special form)
Evaluate all of the sub-expressions in any orderApply the procedure that is the value of the leftmost sub-expression to the arguments (the values of the other sub-expressions)The value of a numeral: number
The value of a built-in operator: machine instructions to execute
The value of any name: the associated object in the environment
*
-
*
Using Abstractions
==> (square 3)
9
==> (+ (square 3) (square 4))
==> (define square (lambda(x)(* x x)))
Environment Table
square
Proc (x)(* x x)
NameValue(* 3 3) (* 4 4)
9
16
+
25
*
-
*
Yet More Abstractions
==> (define f
(lambda(a)
(sum-of-two-squares (+ a 3) (* a 3))))
==> (sum-of-two-squares 3 4)
25
==> (define sum-of-two-squares
(lambda(x y)(+ (square x) (square y))))
Try it outcompute (f 3) on your own
*
-
*
Evaluation of An Expression (reminder)
To Apply a compound procedure: (to a list of arguments)
Evaluate the body of the procedure with the formal parameters substituted by the corresponding actual values
To Evaluate a combination: (other than special form)
Evaluate all of the sub-expressions in any orderApply the procedure that is the value of the leftmost sub-expression to the arguments (the values of the other sub-expressions)The value of a numeral: number
The value of a built-in operator: machine instructions to execute
The value of any name: the associated object in the environment
*
-
*
Lets not forget The Environment
==> (define x 8)
==> (+ x 1)
9
==> (define x 5)
==> (+ x 1)
6
The value of (+ x 1) depends on the environment!
*
-
*
Using the substitution model
(define square (lambda (x) (* x x)))
(define average (lambda (x y) (/ (+ x y) 2)))(average 5 (square 3))
(average 5 (* 3 3))
(average 5 9)first evaluate operands,
then substitute
(/ (+ 5 9) 2)
(/ 14 2)if operator is a primitive procedure,
7replace by result of operation*
-
*
Booleans
Two distinguished values denoted by the constants
#t and #f
The type of these values is boolean
==> (< 2 3)
#t
==> (< 4 3)
#f
*
-
*
Values and types
Values have types. For example:
In scheme almost every expression has a value
Examples:
The value of 23 is 23The value of + is a primitive procedure for additionThe value of (lambda (x) (* x x)) is the compound
procedure proc(x) (* x x) (also denoted The type of 23 is numeral The type of + is a primitive procedureThe type of proc (x) (* x x) is a compound procedureThe type of (> x 1) is a boolean (or logical)*
-
Atomic and Compound Types
Atomic typesNumbers, Booleans, Symbols (TBD)Composite typesTypes composed of other typesSo far: only proceduresWe will see others later -
*
No Value?
In scheme most expressions have values Not all! Those that dont usually have side effectsExample : what is the value of the expression
(define x 8)
And of
(display x)
[display is a primitive func., prints the value of its argument to the screen]
In scheme, the value of a define, display expression is undefined . This means implementation-dependentNever write code that relies on such value!*
-
Dynamic Typing
Note that we never specify explicitly types of variablesHowever primitive functions expect values of a certain type!E.g. + expects numeral valuesSo will our procedures (To be discussed soon)The Scheme interpreter checks type correctness at run-time: dynamic typing[As opposed to static typing verified by a compiler ] -
*
More examples
==> (define x 8)
==> (define x (* x 2))
==> x
16
==> (define x y)
reference to undefined identifier: y
==> (define + -)
==> (+ 2 2)
0
Bad practice, disalowed by some interpreters
Name Value
Environment Table
8
x
16
#
+
*
-
*
The IF special form
ERROR
2
(if (< 2 3) 2 3) ==> 2
(if (< 2 3) 2 (/ 1 0)) ==>
(if )
If the value of is #t,
Evaluate and return it
Otherwise
Evaluate and return it
*
-
*
IF is a special form
In a general form, we first evaluate all arguments and then apply the function(if ) is different:determines whether we evaluate or .
We evaluate only one of them !
*
(If (attack Russians) (send bomb) (do nothing))
(If (= I 1) (set I 2) (set I 3))
If + recursion
-
Conditionals
(lambda (a b)
(cond ( (> a b) a)
( (< a b) b)
(else -1 )))
-
*
Syntactic Sugar for naming procedures
(define square (lambda (x) (* x x))
(define (square x) (* x x))
Instead of writing:
We can write:
*
-
*
(define second )
(second 2 15 3) ==> 15
(second 34 -5 16) ==> -5
Some examples:
(lambda (x) (* 2 x))
(lambda (x y z) y)
Using syntactic sugar:
(define (twice x) (* 2 x))
Using syntactic sugar:
(define (second x y z) y)
(define twice )
(twice 2) ==> 4
(twice 3) ==> 6
*
-
Symbols
> (quote a)
a
> a
a
> (define a a)
> a
a
> b
a
> (define b a)
> (eq? a b)
#t
> (symbol? a)
#t
> (define c 1)
> (symbol? c)
#f
> (number? c)
#t
Symbols are atomic types, their values unbreakable:
abc is just a symbol
-
More on Types
A procedure type is a composite type, as it is composed of the types of its inputs (domain) and output (range)In fact, the procedure type can be instantiated with any type for domain and range, resulting in a different type for the procedure (=data)Such types are called polymorphicAnother polymorphic type: arrays of values of type X (e.g. STL vectors in C++) -
Type constructor
Defines a composite type out of other typesThe type constructor for functions is denoted ->Example: [Number X Number > Number] is the type of all procedures that get as input two numbers, and return a numberIf all types are allowed we use a type variable:[T > T] is the type of all procs. That return the same type as they get as inputNote: there is nothing in the syntax for defining types! This is a convention we manually enforce (for now..). -
Scheme Type Grammar
Type --> Unit | Non-Unit [Unit=Void]
Non-unit -> Atomic | Composite | Type-variable
Atomic --> Number | Boolean | Symbol
Composite --> Procedure | Union
Procedure --> Unit -> Type | [ (Non-Unit *)* Non-Unit -> Type ]
Union --> Type union Type
Type-variable -> A symbol starting with an upper case letter
-
Value constructor
Means of defining an instance of a particular type.The value constructors for procedures is lambdaEach lambda expression generates a new procedure0
and
such that
the
is
2
=
y
x
y
y
x