David Evans CS150: Computer Science University of Virginia Computer Science Lecture 31: Types of Types.

Slides:



Advertisements
Similar presentations
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 26: Proving Uncomputability Visualization.
Advertisements

David Evans CS655: Programming Languages University of Virginia Computer Science Lecture 6: Types of Types “It would.
Assignments and Procs w/Params EOPL3 Chapter 4. Expressible vs. Denotable values Expressible Values –the language can express and compute these –represented.
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
CSE 341 Lecture 16 More Scheme: lists; helpers; let/let*; higher-order functions; lambdas slides created by Marty Stepp
David Evans CS655: Programming Languages University of Virginia Computer Science Lecture 19: Minding Ps & Qs: Axiomatic.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 26: In Praise of Idleness.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 28: Implementing Interpreters.
Cs1120 Fall 2009 David Evans Lecture 20: Programming with State.
Cs1120 Fall 2009 David Evans Lecture 19: Stateful Evaluation.
Imperative Programming
Cs3102: Theory of Computation Class 18: Proving Undecidability Spring 2010 University of Virginia David Evans.
Computing Science 1P Lecture 21: Friday 20 th April Simon Gay Department of Computing Science University of Glasgow 2006/07.
Class 37: Computability in Theory and Practice cs1120 Fall 2011 David Evans 21 November 2011 cs1120 Fall 2011 David Evans 21 November 2011.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 18: Think Globally, Mutate Locally.
Cs2220: Engineering Software Class 6: Defensive Programming Fall 2010 University of Virginia David Evans.
Lecture 31: Laziness University of Virginia cs1120 Fall 2009 David Evans.
David Evans CS200: Computer Science University of Virginia Computer Science Class 17: Mutation M. C. Escher, Day and Night.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 30: Laziness.
David Evans CS655: Programming Languages University of Virginia Computer Science Lecture 14: Types of Types “It would.
David Evans CS200: Computer Science University of Virginia Computer Science Class 16: Mutation M. C. Escher, Day and Night.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 19: Environments.
CMSC 330: Organization of Programming Languages Operational Semantics a.k.a. “WTF is Project 4, Part 3?”
Class 29: Charme School cs1120 Fall 2009 University of Virginia David Evans.
Class 28: Interpreters cs1120 Fall 2009 David Evans.
David Evans CS200: Computer Science University of Virginia Computer Science Class 32: The Meaning of Truth.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 4: Programming with Data.
David Evans CS200: Computer Science University of Virginia Computer Science Class 32: The Meaning of Truth.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 27: Types of Types “It would appear.
David Evans CS200: Computer Science University of Virginia Computer Science Class 26: Halting Problem It is plain at any.
CSE 341 Lecture 21 delayed evaluation; thunks; streams slides created by Marty Stepp
(Thunking about Thunks)
Operational Semantics of Scheme
Lecture 4: Metacircles Eval Apply David Evans
Lecture 17: Environments CS200: Computer Science
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Spring 2017.
6.001 SICP Variations on a Scheme
Lecture 7: List Recursion CS200: Computer Science
Env. Model Implementation
Class 19: Think Globally, Mutate Locally CS150: Computer Science
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Zach Tatlock Winter 2018.
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Spring 2013.
Chapter 4: Types.
The Metacircular Evaluator
David Evans Lecture 9: The Great Lambda Tree of Infinite Knowledge and Ultimate Power CS200: Computer Science University.
Lecture 28: Types of Types
FP Foundations, Scheme In Text: Chapter 14.
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Spring 2016.
Dynamic Scoping Lazy Evaluation
The Metacircular Evaluator
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Autumn 2017.
Lecture 25: Metalinguistics > (meval '((lambda (x) (* x x)) 4)
Lecture 24: Metalinguistics CS200: Computer Science
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Autumn 2018.
Lecture 27: In Praise of Idleness CS200: Computer Science
Lecture 26: The Metacircular Evaluator Eval Apply
6.001 SICP Further Variations on a Scheme
Streams, Delayed Evaluation and a Normal Order Interpreter
6.001 SICP Variations on a Scheme
Class 24: Computability Halting Problems Hockey Team Logo
Dan Grossman / Eric Mullen Autumn 2017
6.001 SICP Interpretation Parts of an interpreter
Assignments and Procs w/Params
Rehearsal: Lazy Evaluation Infinite Streams in our lazy evaluator
David Evans Lecture 32: Truthiness “It would appear that we have reached the limits of what it is possible to achieve.
Lecture 8: Recursing Lists CS150: Computer Science
Lecture 23: Computability CS200: Computer Science
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Spring 2019.
Lecture 25: The Metacircular Evaluator Eval Apply
Presentation transcript:

David Evans CS150: Computer Science University of Virginia Computer Science Lecture 31: Types of Types

2 Lecture 31: Truthiness Menu Using Laziness Evidence of Laziness? (Quiz Results) Laziness through Truthiness (Static Type Checking)

3 Lecture 31: Truthiness Lazy Evaluation Recap Don’t evaluate expressions until their value is really needed –We might save work this way, since sometimes we don’t need the value of an expression –We might change the meaning of some expressions, since the order of evaluation matters Change the Evaluation rule for Application Use thunks to delay evaluations

4 Lecture 31: Truthiness Lazy Application def evalApplication(expr, env): # make Thunk object for each operand expression ops = map (lambda sexpr: Thunk(sexpr, env), expr[1:]) return mapply(forceeval(expr[0], env), ops) def evalApplication(expr, env): subexprvals = map (lambda sexpr: meval(sexpr, env), expr) return mapply(subexprvals[0], subexprvals[1:])

5 Lecture 31: Truthiness Lazy Data Structures (define cons (lambda (a b) (lambda (p) (if p a b)))) (define car (lambda (p) (p #t))) (define cdr (lambda (p) (p #f))) Note: for PS7, you are defining these as primitives, which would not evaluate lazily.

6 Lecture 31: Truthiness Using Lazy Pairs (define cons (lambda (a b) (lambda (p) (if p a b)))) LazyCharme> (define pair (cons 3 error)) LazyCharme> pair LazyCharme> (car pair) 3 LazyCharme> (cdr pair) Error: Undefined name: error (define car (lambda (p) (p #t))) (define cdr (lambda (p) (p #f)))

7 Lecture 31: Truthiness Infinite Lists (define ints-from (lambda (n) (cons n (ints-from (+ n 1))))) LazyCharme> (define allnaturals (ints-from 0)) LazyCharme> (car allnaturals) 0 LazyCharme> (car (cdr allnaturals)) 1 LazyCharme> (car (cdr (cdr (cdr (cdr allnaturals))))) 4

8 Lecture 31: Truthiness Infinite Fibonacci Sequence (define fibo-gen (lambda (a b) (cons a (fibo-gen b (+ a b))))) (define fibos (fibo-gen 0 1)) (define get-nth (lambda (lst n) (if (= n 0) (car lst) (get-nth (cdr lst) (- n 1))))) (define fibo (lambda (n) (get-nth fibos n)))

9 Lecture 31: Truthiness Alternate Implementation (define merge-lists (lambda (lst1 lst2 proc) (if (null? lst1) null (if (null? lst2) null (cons (proc (car lst1) (car lst2)) (merge-lists (cdr lst1) (cdr lst2) proc)))))) (define fiboms (cons 0 (cons 1 (merge-lists fiboms (cdr fiboms) +))))

10 Lecture 31: Truthiness Quiz Results

11 Lecture 31: Truthiness Quiz Answers 1.Programming languages designed by John Backus: Fortran, FP, FL (BNF – not a programming language) 2.What did Gödel prove? That any axiomatic system powerful enough to express “This statement cannot be proven in the system” must be incomplete. 3.What does SSS0 mean? 3

12 Lecture 31: Truthiness class Environment: def __init__(self, parent): self._parent = parent self._frame = { } def addVariable(self, name, value): self._frame[name] = value def lookupVariable(self, name): if self._frame.has_key(name): return self._frame[name] elif self._parent: return self._parent.lookupVariable(name) else: evalError("Undefined name: %s" % (name)) elif self._parent._frame.has_key(name) self._parent._frame[name] Quiz 4: Environment Class

13 Lecture 31: Truthiness Quiz 5: Viruses Is it possible to define a procedure that protects computer users from all viruses? Here’s one procedure: o Unplug the computer from the power o Encase it in concrete o Throw it in the Potomac River This is a very different question from the “Is it possible to determine if a procedure specification is a virus?” question (which we proved in class is impossible by showing how a solution to it could be used to solve the Halting Problem.

14 Lecture 31: Truthiness Types

15 Lecture 31: Truthiness Types Numbers Strings Beatle’s Songs that don’t end on the Tonic Colors lists of lists of lists of anything programs that halt Type is a (possibly infinite) set of values You can do some things with some types, but not others

16 Lecture 31: Truthiness Why have types? Detecting programming errors: (usually) better to notice error than report incorrect result Make programs easier to read, understand and maintain: thinking about types can help understand code Verification: types make it easier to prove properties about programs Security: can use types to constrain the behavior of programs

17 Lecture 31: Truthiness Types of Types Does regular Scheme have types? > (car 3) car: expects argument of type ; given 3 > (+ (cons 1 2)) +: expects argument of type ; given (1. 2) Yes, without types (car 3) would produce some silly result. Because of types, it produces a type error.

18 Lecture 31: Truthiness Type Taxonomy Latent vs. Manifest –Are types visible in the program text? Static vs. dynamic checking –Do you have to run the program to know if it has type errors? Weak vs. Strong checking –How strict are the rules for using types? (e.g., does the predicate for an if need to be a Boolean?) –Continuum (just matter of degree)

19 Lecture 31: Truthiness Scheme/Python/Charme Latent or Manifest? –All have latent types (none visible in code) Static or Dynamic? –All are dynamic (checked when expression is evaluated) Weak or Strong? –Which is the strictest?

20 Lecture 31: Truthiness Strict Typing Scheme> (+ 1 #t) +: expects type as 2nd argument, given: #t; other arguments were: 1 Python>>> 1 + True 2 Charme> (+ 1 #t) 2

21 Lecture 31: Truthiness Scheme/Python/Charme  Java/StaticCharme Scheme, Python, and Charme have Latent, Dynamically checked types –Don’t see explicit types when you look at code –Checked when an expression is evaluated 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)

22 Lecture 31: Truthiness Java Example class Test { int tester (String s) { int x; x = s; return "okay"; } The result is an integer The place x holds an integer > javac types.java types.java:5: Incompatible type for =. Can't convert java.lang.String to int. x = s; ^ types.java:6: Incompatible type for return. Can't convert java.lang.String to int. return "okay"; ^ 2 errors The parameter must be a String javac compiles (and type checks) the program. It does not execute it.

23 Lecture 31: Truthiness What do we need to do change our Charme interpreter to provide manifest types?

24 Lecture 31: Truthiness Charge PS7 Due Friday Friday: Finish StaticCharme interpreter Monday: Project ideas and team requests for PS9