One-Slide Summary • In lazy evaluation , expressions are not evaluated until their values are needed. We can use lazy evaluation to program with infinite data structures , such as a list of all Types of Types Types of Types natural numbers. • A type is a (possibly infinite) set of values. • Each type supports a set of valid operations . • Types can be latent or manifest, static or dynamic, strong or weak. • We can change the Charme interpreter to support manifest (program visible) types. #2 Outline Administrivia • Start PS7 Now • Administration • Kinga's Web Fault Research Survey • Lazy Evaluation Recap – http://www.cs.virginia.edu/~kld5r/webfault/ • Quiz Results – Worth 2 points of Extra Credit on Exam 2 • Types – Plus possibly $$$ ... • Type Taxonomy • 2009 Computing and Communication • Static Charme Scholarship for Undergraduate Women – Charme with Manifest Types – http://www.cs.virginia.edu/ccscholarship – $1000 merit scholarship, due June 30 th #3 #4 Problem Set 8 The Textbook • I get the sense that some of the students are • Understand and modify a dynamic web attempting to read the book on-line. I would application encourage everyone to read it on paper. It is • Already posted pretty well established that people read faster • Due Monday April 13th and understand better on paper than on the screen. Problem Set 9 – David Evans, Course Book Author • Team requests and ideas due Friday April 10th (email me before midnight) #5 #6
Lazy Evaluation Recap Lazy Application • Don’t evaluate expressions until their value is really needed def evalApplication(expr, env): subexprvals = map ( lambda sexpr: meval(sexpr, env), expr) – We might save work this way, since sometimes return mapply(subexprvals[0], subexprvals[1:]) we don’t need the value of an expression – We might change the meaning of some expressions, since the order of evaluation matters def evalApplication(expr, env): • Change the Evaluation rule for Application # make Thunk object for each operand expression • Use thunks to delay evaluations ops = map ( lambda sexpr: Thunk(sexpr, env), expr[1:]) return mapply(forceeval(expr[0], env), ops) #7 #8 Lazy Data Structures Using Lazy Pairs (define car (define cons (define cons (lambda (p) (p #t))) (lambda (a b) (lambda (a b) (define cdr (lambda (p) (lambda (p) Note: for PS7, you (lambda (p) (p #f))) (if p a b)))) are defining these (if p a b)))) as primitives , which would not LazyCharme> (define mypair (cons 3 error)) (define car evaluate lazily. LazyCharme> mypair (lambda (p) (p #t))) <Procedure ['p'] / ['if', 'p', 'a', 'b']> LazyCharme> (car mypair) (define cdr 3 (lambda (p) (p #f))) LazyCharme> (cdr mypair) Error: Undefined name: error #9 #10 Infinite Lists Infinite Fibonacci Sequence (define fibo-gen (lambda (a b) (define ints-from (cons a (fibo-gen b (+ a b))))) (lambda (n) (cons n (ints-from (+ n 1))))) (define fibos (fibo-gen 0 1)) (define get-nth (lambda (lst n) LazyCharme> (define allnaturals (ints-from 0)) (if (= n 0) (car lst) LazyCharme> (car allnaturals) 0 (get-nth (cdr lst) (- n 1))))) LazyCharme> (car (cdr allnaturals)) 1 (define fibo LazyCharme> (car (cdr (cdr (cdr (cdr allnaturals))))) (lambda (n) (get-nth fibos n))) 4 #11 #12
Alternate Implementation Come back and (define merge-lists understand this slide (lambda (lst1 lst2 proc) to study for the exams. Quiz Results (if (null? lst1) null (if (null? lst2) null (cons (proc (car lst1) (car lst2)) (merge-lists (cdr lst1) (cdr lst2) proc)))))) (define fiboms ;;; merge-list variant (cons 0 (cons 1 (merge-lists fiboms (cdr fiboms) +)))) #13 #14 Quiz Answers Quiz 4: Environment Class • Programming languages designed by John class Environment: def __init__(self, parent): Backus: Fortran, FP, FL self._parent = parent (BNF – not a programming language) self._frame = { } def addVariable(self, name, value): 2. What did Gödel prove? self._frame[name] = value def lookupVariable(self, name): That any axiomatic system powerful if self._frame.has_key(name): enough to express “This statement return self._frame[name] cannot be proven in the system” must elif self._parent: be incomplete. return self._parent.lookupVariable(name) else: 3. What does SSS0 mean? 3 evalError("Undefined name: %s" % (name)) #15 #16 Liberal Arts Trivia: Quiz 5: Viruses Cognitive Science • This philosophy of mind dominated for the first • Is it possible to define a procedure that half of the 20 th century. It developed as a reaction protects computer users from all viruses? to the inadequacies of introspectionism. In it, all Here’s one procedure: things which organisms do – including acting, o Unplug the computer from the power thinking and feeling – should be regarded as o Encase it in concrete actions or reactions, usually to the environment. o Throw it in the Potomac River It holds that there are no philosophical This is a very different question from the “Is it possible to differences between publicly observable determine if a procedure specification is a virus?” question processes (actions) and privately observable (which we proved in class is impossible by showing how a solution to it could be used to solve the Halting Problem). processes (thinking and feeling). • Bonus: B.F. Who? #17 #18
Liberal Arts Trivia: Civil Rights • The landmark 1967 Supreme Court case Loving v. Virginia declared Types Virginia's anti-miscegenation statue, the “Racial Integrity Act of 1924”, unconstitutional. This effectively ended laws preventing what? #19 #20 Types Why have types? Strings Numbers • Detecting programming errors: (usually) better to notice error than report incorrect programs that halt result Colors • Make programs easier to read, understand Beatle’s Songs that don’t end on the Tonic and maintain: thinking about types can help lists of lists of lists of anything understand code • Verification: types make it easier to prove • A Type is a (possibly infinite) set of values properties about programs • Security: can use types to constrain the • You can do some things with some types, behavior of programs but not others – Each Type has associated valid operations #21 #22 Type Taxonomy Types of Types • Latent vs. Manifest Does regular Scheme have types? – Are types visible in the program text? • Static vs. dynamic checking – Do you have to run the program to know if > (car 3) it has type errors? car: expects argument of type <pair>; given 3 • Weak vs. Strong checking > (+ (cons 1 2)) – How strict are the rules for using types? +: expects argument of type <number>; given (1 . 2) • (e.g., does the predicate for an if need to be a Boolean?) Yes, without types (car 3) would produce some silly result. Because of types, it produces a type error. – Continuum (just matter of degree) #23 #24
Scheme/Python/Charme Strict Typing Scheme> (+ 1 #t) • Latent or Manifest? +: expects type <number> as 2nd argument, – All have latent types (none visible in code) given: #t; other arguments were: 1 • Static or Dynamic? Python>>> 1 + True – All are dynamic (checked when expression is 2 evaluated) Charme> (+ 1 #t) • Weak or Strong? – Which is the strictest? 2 – You tell me! #25 #26 Scheme/Python/Charme Java Example The parameter → Java/StaticCharme must be a String The result class Test { is an integer • Scheme, Python, and Charme have Latent, int tester (String s) Dynamically checked types { The place x int x; – Don’t see explicit types when you look at code holds an integer x = s; – Checked when an expression is evaluated return "okay"; • Java, StaticCharme have Manifest, Statically } checked types } – Type declarations must be included in code – Types are checked statically before running the program (Java: not all types checked statically) #27 #28 Java Example The parameter must be a String The result class Test { is an integer int tester (String s) What do we need to do to { > javac types.java change our Charme interpreter The place x int x; types.java:5: Incompatible holds an integer type for =. Can't convert x = s; to provide manifest types? java.lang.String to int. x = s; return "okay"; ^ } types.java:6: Incompatible type for return. Can't convert } java.lang.String to int. return "okay"; ^ javac compiles ( and type checks ) 2 errors the program. It does not execute it. #29 #30
Recommend
More recommend