Midterm Review John Mitchell CS 2422008. Topics uJavaScript uBlock structure and activation records uExceptions, continuations uTypes and type inference.

Slides:



Advertisements
Similar presentations
CPSC 388 – Compiler Design and Construction
Advertisements

Programming Paradigms Introduction. 6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L1:
Kathleen Fisher cs242 Reading: “A history of Haskell: Being lazy with class”,A history of Haskell: Being lazy with class Section 6.4 and Section 7 “Monads.
Variables, Environments and Closures. Overview We will Touch on the notions of variable extent and scope Introduce the notions of lexical scope and.
1 Compiler Construction Intermediate Code Generation.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 18.
Chapter 8 Runtime Support. How program structures are implemented in a computer memory? The evolution of programming language design has led to the creation.
Dr. Muhammed Al-Mulhem ICS Chapter 7 Scope, Functions, and Storage Management.
1 Names, Scopes and Bindings. 2 Names Kinds of names Kinds of names Variables, functions, classes, types, labels, blocks, operators, tasks, etc. Variables,
Cse321, Programming Languages and Compilers 1 6/19/2015 Lecture #18, March 14, 2007 Syntax directed translations, Meanings of programs, Rules for writing.
Introduction to ML - Part 2 Kenny Zhu. What is next? ML has a rich set of structured values Tuples: (17, true, “stuff”) Records: {name = “george”, age.
Run time vs. Compile time
Catriel Beeri Pls/Winter 2004/5 environment 68  Some details of implementation As part of / extension of type-checking: Each declaration d(x) associated.
Programming Languages Structure
The environment of the computation Declarations introduce names that denote entities. At execution-time, entities are bound to values or to locations:
Scope, Function Calls and Storage Management John Mitchell CS
1 Run time vs. Compile time The compiler must generate code to handle issues that arise at run time Representation of various data types Procedure linkage.
Scope, Function Calls and Storage Management John Mitchell CS
Slide 1 Vitaly Shmatikov CS 345 Scope and Activation Records.
1.3 Executing Programs. How is Computer Code Transformed into an Executable? Interpreters Compilers Hybrid systems.
Chapter TwelveModern Programming Languages1 Memory Locations For Variables.
Imperative Programming
CS3012: Formal Languages and Compilers The Runtime Environment After the analysis phases are complete, the compiler must generate executable code. The.
Final Review TA Review Session Final Exam CS 242 Monday Dec 10
Dr. Muhammed Al-MulhemICS Chapter 5 The Algol Family and Haskell.
Programming Language C++ Xulong Peng CSC415 Programming Languages.
1 Programming Language History and Evolution In Text: Chapter 2.
Types John Mitchell CS 242. Type A type is a collection of computable values that share some structural property. uExamples Integers Strings int  bool.
10/16/2015IT 3271 All about binding n Variables are bound (dynamically) to values n values must be stored somewhere in the memory. Memory Locations for.
1 COMP 3438 – Part II-Lecture 1: Overview of Compiler Design Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
CSC 580 – Theory of Programming Languages, Spring, 2009 Week 9: Functional Languages ML and Haskell, Dr. Dale E. Parson.
Basic Semantics Associating meaning with language entities.
Midterm Review John Mitchell CS 242. Midterm uThurs Nov 7, 7-9PM Terman Aud Closed book uClass schedule Thurs Oct 31, Tues Nov 5 – topics not on midterm.
Formal Semantics Chapter Twenty-ThreeModern Programming Languages, 2nd ed.1.
CS112: Structure of Programming Languages A smorgasbord of topics in programming languages including programming paradigms and syntax, semantics, implementation.
Slide 1 Introduction to JavaScript. slide 2 Common Uses of JavaScript uForm validation uPage embellishments and special effects uNavigation.
COMP3190: Principle of Programming Languages
TIVDM2Functional Programming Language Concepts 1 Concepts from Functional Programming Languages Peter Gorm Larsen.
CS536 Semantic Analysis Introduction with Emphasis on Name Analysis 1.
Introduction CPSC 388 Ellen Walker Hiram College.
CSE 425: Control Abstraction I Functions vs. Procedures It is useful to differentiate functions vs. procedures –Procedures have side effects but usually.
12/9/20151 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
Variables, Environments and Closures. Overview Touch on the notions of variable extent and scope Introduce the notions of lexical scope and dynamic.
Object-Oriented Programming Chapter Chapter
Types and Programming Languages Lecture 11 Simon Gay Department of Computing Science University of Glasgow 2006/07.
CS 330 Programming Languages 10 / 23 / 2007 Instructor: Michael Eckmann.
Slide 1 Vitaly Shmatikov CS 345 Exceptions. slide 2 Reading Assignment uMitchell, Chapter 8.2.
Types John Mitchell CS 242. Type A type is a collection of computable values that share some structural property. uExamples Integers Strings int  bool.
Operational Semantics Mooly Sagiv Reference: Semantics with Applications Chapter 2 H. Nielson and F. Nielson
Scope, Function Calls and Storage Management John Mitchell CS Reading: Chapter 7, Concepts in Programming Languages.
ISBN Chapter 12 Support for Object-Oriented Programming.
Heath Carroll Bill Hanczaryk Rich Porter.  A Theory of Type Polymorphism in Programming ◦ Robin Milner (1977)  Milner credited with introducing the.
CS314 – Section 5 Recitation 9
Programming Language History and Evolution
Operational Semantics of Scheme
Functional Programming
Object Lifetime and Pointers
Programming Languages and Compilers (CS 421)
Semantic Analysis with Emphasis on Name Analysis
Representation, Syntax, Paradigms, Types
Programming Language History and Evolution
Variables, Environments and Closures
Representation, Syntax, Paradigms, Types
Representation, Syntax, Paradigms, Types
Languages and Compilers (SProg og Oversættere)
Representation, Syntax, Paradigms, Types
Scope, Function Calls and Storage Management
Functional Programming and Haskell
Functional Programming and Haskell
Presentation transcript:

Midterm Review John Mitchell CS

Topics uJavaScript uBlock structure and activation records uExceptions, continuations uTypes and type inference uHaskell Functional programming Type classes Monads –IO Monad –General monads uOperational semantics uModularity uOO concepts encapsulation dynamic lookup subtyping inheritance uSimula and Smalltalk uC++ uJava uConcurrency

JavaScript Functions uAnonymous functions make great callbacks setTimeout(function() { alert("done"); }, 10000) uCurried function function CurriedAdd(x){ return function(y){ return x+y} }; g = CurriedAdd(2); g(3) uBlocks w/scope can be expressed using function var y=0; (function () { // begin block var x=2; // local variable x y = y+x; }) (); // end block

Lambda Calculus uGiven function f, return function f  f f. x. f (f x) uHow does this work? ( f. x. f (f x)) ( y. y+1) = x. ( y. y+1) (( y. y+1) x) = x. ( y. y+1) (x+1) = x. (x+1)+1 In pure lambda calculus, ame result if step 2 is altered.

Basic object features uUse a function to construct an object function car(make, model, year) { this.make = make; this.model = model; this.year = year; } uObjects have prototypes, can be changed var c = new car(“Ford”,”Taurus”,1988); car.prototype.print = function () { return this.year + “ “ + this.make + “ “ + this.model;} c.print();

Unusual features of JavaScript uSome built-in functions Eval, Run-time type checking functions, … uRegular expressions Useful support of pattern matching uAdd, delete methods of an object dynamically Seen examples adding methods. Do you like this? Disadvantages? myobj.a = 5; myobj.b = 12; delete myobj.a; uRedefine native functions and objects (incl undefined) uIterate over methods of an object for (variable in object) { statements } uWith statement (“considered harmful” – why??) with (object) { statements }

Garbage collection uAutomatic reclamation of unused memory Navigator 2: per page memory management –Reclaim memory when browser changes page Navigator 3: reference counting –Each memory region has associated count –Count modified when pointers are changed –Reclaim memory when count reaches zero Navigator 4: mark-and-sweep, or equivalent –Garbage collector marks reachable memory –Sweep and reclaim unreachable memory Reference Discuss garbage collection in connection with Lisp

Language features in CS242 uStack memory management Parameters, local variables in activation records uGarbage collection Automatic reclamation of inaccessible memory uClosures Function together with environment (global variables) uExceptions Jump to previously declared location, passing values uObject features Dynamic lookup, Encapsulation, Subtyping, Inheritance uConcurrency Do more than one task at a time (JavaScript is single-threaded)

Block structure and storage mgmt uBlock-structured languages and stack storage uIn-line Blocks activation records storage for local, global variables uFirst-order functions parameter passing tail recursion and iteration uHigher-order functions deviations from stack discipline language expressiveness => implementation complexity

Activation record for static scope uControl link Link to previous (calling) block uAccess link Link to record of enclosing block uReturn address Next instruction after return uReturn-result address Place to put return value uParameters Set when function called uLocal variables uIntermediate results Control link Local variables Intermediate results Environment Pointer Parameters Return address Return-result addr Access link

Example function f (x) = { x = x+1; return x; } var y = 0; print (f(y)+y); pseudo-codeactivation records pass-by-ref pass-by-value f(y) y0 Control link x Return result addr  f(y) y0 Control link x Return result addr 0 f(y)

Tail recursion elimination fun f(x,y) = if x>y then x else f(2*x, y); f(1,3); control return val x1 y3 f(4,3) Optimization pop followed by push = reuse activation record in place Conclusion Tail recursive function equiv to iterative loop control return val x2 y3 f(1,3) control return val x4 y3 f(2,3)

Complex nesting structure var x=1; function g(z) { return x+z; } function f(y) { var x = y+1; return g(y*x); } f(3); function m(…) { var x=1; … function n( … ){ function g(z) { return x+z; } … { … function f(y) { var x = y+1; return g(y*x); } … f(3); … } … n( … ) …} … m(…) Simplify to Simplified code has same block nesting, if we follow convention that each declaration begins a new block.

{ var x = 4; { function f(y){return x*y}; { function g(h) { int x=7; return h(3)+x; }; g(f); }}} Function Argument and Closures x4 access link set from closure Code for f f access Run-time stack with access links Code for g h(3) y3 access g(f) h access x7 g

Function Results and Closures c access Code for counter Code for mk_counter c(2) access inc2 mk_counter(1) count1 init1 access counter mk_c function mk_counter (init) { var count = init; function counter(inc) {count=count+inc; return count}; return counter}; var c = mk_counter(1); c(2) + c(2); JS 3

Summary of scope issues uBlock-structured lang uses stack of activ records Activation records contain parameters, local vars, … Also pointers to enclosing scope uSeveral different parameter passing mechanisms uTail calls may be optimized uFunction parameters/results require closures Closure environment pointer used on function call Stack deallocation may fail if function returned from call Closures not needed if functions not in nested blocks

Exceptions: Structured Exit uTerminate part of computation Jump out of construct Pass data as part of jump Return to most recent site set up to handle exception Unnecessary activation records may be deallocated –May need to free heap space, other resources uTwo main language constructs Declaration to establish exception handler Statement or expression to raise or throw exception Often used for unusual or exceptional condition; other uses too

JavaScript Exceptions throw e //jump to catch, passing exception object as parameter try { … //code to try } catch (e if e == …) { … //catch if first condition true } catch (e if e == …) { … //catch if second condition true } catch (e if e == …) { … //catch if third condition true } catch (e){ … // catch any exception } finally { … //code to execute after everything else } Exception_Handling_Statements

Continuation view of factorial fact(n) = if n=0 then 1 else n*fact(n-1) fact(9) fact(8) fact(7) This invocation multiplies by 9 and returns Continuation of fact(8) is x. 9*x Multiplies by 8 and returns Continuation of fact(7) is y. ( x. 9*x) (8*y) Multiplies by 7 and returns Continuation of fact(6) is z. ( y. ( x. 9*x) (8*y)) (7*z) return n9... return n8... return n7...

Types and Type Inference uType safety Can use compile-time or run-time typing Cannot have dangling pointers uType inference Compute types from the way symbols are used Know how algorithm works (for simple examples) Know what an ML polymorphic type means - fun f(g,x) = g(g(x)); > val it = fn : (t  t)*t  t Polymorphism different from overloading

fun sum(x) = x +  t v w y z :r :s u r  t = int  (int  int) r  u = int  (int  int) u = int  v s = v  w t = w  y z = r  y s = z v = int w = int … s = v  w = int  int

Haskell uHaskell is a programming language that is Similar to ML: general-purpose, strongly typed, higher-order, supports type inference Different from ML: lazy evaluation, purely functional, evolving type system. uDesigned by committee in 80’s and 90’s Haskell 1.0 in 1990, Haskell ‘98, still ongoing “A history of Haskell: Being lazy with class” HOPL 3 Paul Hudak John Hughes Simon Peyton Jones Phil Wadler

Haskell 1,000, ,000 The second life? Geeks Practitioners “Learning Haskell is a great way of training yourself to think functionally so you are ready to take full advantage of C# 3.0 when it comes out” (blog Apr 2007) “I'm already looking at coding problems and my mental perspective is now shifting back and forth between purely OO and more FP styled solutions” (blog Mar 2007)

Datatypes and Pattern Matching uRecursively defined data structure data Tree = Leaf Int | Node (Int, Tree, Tree) Node(4, Node(3, Leaf 1, Leaf 2), Node(5, Leaf 6, Leaf 7)) uRecursive function sum (Leaf n) = n sum (Node(n,t1,t2)) = n + sum(t1) + sum(t2)

Backus’ Turing Award uJohn Backus was designer of Fortran, BNF, etc. uTuring Award in 1977 uTuring Award Lecture Functional prog better than imperative programming Easier to reason about functional programs More efficient due to parallelism Algebraic laws Reason about programs Optimizing compilers

No-side-effects language test Within the scope of specific declarations of x 1,x 2, …, x n, all occurrences of an expression e containing only variables x 1,x 2, …, x n, must have the same value. uExample begin integer x=3; integer y=4; 5*(x+y)-3 … // no new declaration of x or y // 4*(x+y)+1 end ?

Polymorphism vs Overloading uParametric polymorphism Single algorithm: many types Type variable may be replaced by any type if f:t  t then f:int  int, f:bool  bool,... uOverloading Single symbol: more than one algorithm Choice of algorithm determined by type context Types may be arbitrarily different + has types int*int  int, real*real  real

Type Classes square :: Num n => n -> n square x = x*x class Num a where (+) :: a -> a -> a (*) :: a -> a -> a negate :: a -> a...etc... The class declaration says what the Num operations are Works for any type ‘n’ that supports the Num operations instance Num Int where a + b = plusInt a b a * b = mulInt a b negate a = negInt a...etc... An instance declaration for a type T says how the Num operations are implemented on T’s plusInt :: Int -> Int -> Int mulInt :: Int -> Int -> Int etc, defined as primitives

Compiling Type Classes square :: Num n => n -> n square x = x*x class Num a where (+) :: a -> a -> a (*) :: a -> a -> a negate :: a -> a...etc.. The class decl translates to: A data type decl for Num A selector function for each class operation square :: Num n -> n -> n square d x = (*) d x x data Num a = MkNum (a->a->a) (a->a->a) (a->a)...etc (*) :: Num a -> a -> a -> a (*) (MkNum _ m _...) = m When you write this......the compiler generates this A value of type (Num n) is a dictionary of the Num operations for type n

dNumInt :: Num Int dNumInt = MkNum plusInt mulInt negInt... Compiling Instance Declarations square :: Num n => n -> n square x = x*x An instance decl for type T translates to a value declaration for the Num dictionary for T square :: Num n -> n -> n square d x = (*) d x x instance Num Int where a + b = plusInt a b a * b = mulInt a b negate a = negInt a...etc.. When you write this......the compiler generates this A value of type (Num n) is a dictionary of the Num operations for type n

Before Monads uStreams Program issues a stream of requests to OS, which responds with a stream of inputs. uContinuations User supplies continuations to I/O routines to specify how to process results. uWorld-Passing The “World” is passed around and updated, like a normal data structure. Not a serious contender because designers didn’t know how to guarantee single-threaded access to the world. uStream and Continuation models inter-definable. Haskell 1.0 Report adopted Stream model.

The (>>=) Combinator uConnect two actions to make another action putChar () Char getChar (>>=) :: IO a -> (a -> IO b) -> IO b echo :: IO () echo = getChar >>= putChar

The return combinator  The action ( return v ) does no IO and immediately returns v : return :: a -> IO a return getTwoChars :: IO (Char,Char) getTwoChars = getChar>>= \c1 -> getChar>>= \c2 -> return (c1,c2)

The “do” Notation uThe “do” notation adds syntactic sugar to make monadic code easier to read. uDo syntax designed to look imperative. -- Do Notation getTwoCharsDo :: IO(Char,Char) getTwoCharsDo = do { c1 <- getChar ; c2 <- getChar ; return (c1,c2) } -- Plain Syntax getTwoChars :: IO (Char,Char) getTwoChars = getChar>>= \c1 -> getChar>>= \c2 -> return (c1,c2)

Making Modifications... uTo add error checking Purely: modify each recursive call to check for and handle errors. Impurely: throw an exception, wrap with a handler. uTo add logging Purely: modify each recursive call to thread logs. Impurely: write to a file. uTo add a count of the number of operations Purely: modify each recursive call to thread count. Impurely: increment a global variable.

The Monad Constructor Class uThe Prelude defines a type constructor class for monadic behavior: uThe Prelude defines an instance of this class for the IO type constructor.  The “do” notation works over any instance of class Monad. class Monad m where return :: a -> m a (>>=) :: m a -> (a -> m b) -> m b

Hope monad defined in lecture  We can make Hope an instance of Monad: instance Monad Hope where return = Ok (>>=) = ifOKthen eval3 :: Exp -> Hope Int -- Cases for Plus and Minus omitted but similar eval3 (Times e1 e2) = do { v1 <- eval3 e1; v2 <- eval3 e2; return (v1 * v2) } eval3 (Div e1 e2) = do { v1 <- eval3 e1; v2 <- eval3 e2; if v2 == 0 then Error "divby0" else return (v1 `div` v2)} eval3 (Const i) = return i uAnd then rewrite the evaluator to be monadic

Monad Menagerie uWe have seen many example monads IO, Hope (aka Maybe), Trace, ST, Non-determinism uThere are many more... Continuation monad STM: software transactional memory Reader: for reading values from an environment Writer: for recording values (like Trace) Parsers Random data generators (e.g, in Quickcheck) uHaskell provides many monads in its standard libraries, and users can write more.

PL Fundamentals uGrammars, parsing (skipped this year) uLambda calculus (quick overview only) uDenotational semantics (skipped this year) uFunctional vs. Imperative Programming Why don’t we use functional programming? Is implicit parallelism a good idea? Is implicit anything a good idea? uOperational semantics One lecture overview, illustrating applications to JavaScript and web security

Operational semantics uDefine meaning of a program Sequence of actions of abstract machine Aim for clarity and precision, not efficient implementation uStates generally corresponds to Term (statement or expression) being evaluated Abstract description of memory and other data structures involved in computation

JavaScript Ambiguities uExample 1 var b = 10; var f = function(){ var b = 5; function g(){var b = 8; return this.b;}; g();} var result = f(); uExample 2 var f = function(){ var a = g(); function g() { return 1;}; function g() { return 2;}; var g = function() { return 3;} return a;} var result = f();

Small part of operational semantics Precondition Computation step Rules for finding the scope object on the heap

Example application: BeamAuth

Attacks and defenses and there are more...

Summary uJavaScript uBlock structure and activation records uExceptions, continuations uTypes and type inference uHaskell Functional programming Type classes Monads –IO Monad –General monads uOperational semantics uModularity uOO concepts encapsulation dynamic lookup subtyping inheritance uSimula and Smalltalk uC++ uJava uConcurrency