David Evans CS655: Programming Languages University of Virginia Computer Science Lecture 14: Types of Types “It would.

Slides:



Advertisements
Similar presentations
David Evans CS655: Programming Languages University of Virginia Computer Science Lecture 6: Types of Types “It would.
Advertisements

Computational Models The exam. Models of computation. –The Turing machine. –The Von Neumann machine. –The calculus. –The predicate calculus. Turing.
David Evans CS655: Programming Languages University of Virginia Computer Science Lecture 20: Total Correctness; Proof-
Register Allocation CS 671 March 27, CS 671 – Spring Register Allocation - Motivation Consider adding two numbers together: Advantages: Fewer.
David Evans CS655: Programming Languages University of Virginia Computer Science Lecture 13: Operational Semantics “Then.
David Evans CS655: Programming Languages University of Virginia Computer Science Lecture 19: Minding Ps & Qs: Axiomatic.
CSE 341, Winter Type Systems Terms to learn about types: –Type –Type system –Statically typed language –Dynamically typed language –Type error –Strongly.
ISBN Chapter 3 Describing Syntax and Semantics.
CS 330 Programming Languages 09 / 19 / 2006 Instructor: Michael Eckmann.
Type Checking.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 31: Types of Types.
CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
CS 330 Programming Languages 09 / 07 / 2006 Instructor: Michael Eckmann.
CS 330 Programming Languages 09 / 13 / 2007 Instructor: Michael Eckmann.
CS 330 Programming Languages 09 / 06 / 2007 Instructor: Michael Eckmann.
Static and Dynamic Behavior Fall 2005 OOPD John Anthony.
CS 330 Programming Languages 09 / 16 / 2008 Instructor: Michael Eckmann.
Describing Syntax and Semantics
Summer 02-03Programming Language Concepts1 Programming Language Concepts (CS 360) Lecture 1: Overview, Grammars, and Little Languages Jeremy R. Johnson.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 28: Implementing Interpreters.
Cs3102: Theory of Computation Class 20: Busy Beavers Spring 2010 University of Virginia David Evans Office hours: I am not able to hold my Thursday morning.
Imperative Programming
Cs3102: Theory of Computation Class 18: Proving Undecidability Spring 2010 University of Virginia David Evans.
CS 106 Introduction to Computer Science I 01 / 25 / 2010 Instructor: Michael Eckmann.
CSC3315 (Spring 2009)1 CSC 3315 Programming Languages Hamid Harroud School of Science and Engineering, Akhawayn University
/425 Declarative Methods - J. Eisner1 Encodings and reducibility.
CS 330 Programming Languages 09 / 04 / 2008 Instructor: Michael Eckmann.
1 Project Information and Acceptance Testing Integrating Your Code Final Code Submission Acceptance Testing Other Advice and Reminders.
Week 1 - Friday.  What did we talk about last time?  Our first Java program.
“The study of algorithms is the cornerstone of computer science.” Algorithms Fall 2011.
CSE 425: Data Types I Data and Data Types Data may be more abstract than their representation –E.g., integer (unbounded) vs. 64-bit int (bounded) A language.
Chapter 6 Programming Languages (1) Introduction to CS 1 st Semester, 2015 Sanghyun Park.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 3: Rules of Evaluation.
CSE 311 Foundations of Computing I Lecture 26 Computability: Turing machines, Undecidability of the Halting Problem Spring
CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans Lecture 3: Levels of Abstraction
Semantics In Text: Chapter 3.
CS 655: Programming Languages David Evans Office 236A, University of Virginia Computer.
David Evans CS655: Programming Languages University of Virginia Computer Science Lecture 21: Proof-Carrying Code and.
12/9/20151 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
David Evans CS655: Programming Languages University of Virginia Computer Science Lecture 16: Smalltalking about Objects.
CS 106 Introduction to Computer Science I 01 / 22 / 2007 Instructor: Michael Eckmann.
How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types – type system – primitives, strings, arrays, hashes – pointers/references.
Semantic Analysis II Type Checking EECS 483 – Lecture 12 University of Michigan Wednesday, October 18, 2006.
CSE 311 Foundations of Computing I Lecture 28 Computability: Other Undecidable Problems Autumn 2011 CSE 3111.
CS412/413 Introduction to Compilers Radu Rugina Lecture 13 : Static Semantics 18 Feb 02.
Chapter 18 Object Database Management Systems. Outline Motivation for object database management Object-oriented principles Architectures for object database.
C OMPUTER P ROGRAMMING 1 Input and Variables. I/O S TATEMENTS : I NPUT & V ARIABLES Input is the term used to describe the transfer of information from.
CS 330 Programming Languages 10 / 23 / 2007 Instructor: Michael Eckmann.
CMSC 330: Organization of Programming Languages Operational Semantics.
Overview: Lecture 6: Dolores Zage. What is a program? n Operations that are to be applied to certain data in a certain sequence (definition holds for.
Lecture 3: Rules of Evaluation CS150: Computer Science
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
CSCI-383 Object-Oriented Programming & Design Lecture 17.
Chapter 1: Preliminaries Lecture # 2. Chapter 1: Preliminaries Reasons for Studying Concepts of Programming Languages Programming Domains Language Evaluation.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 27: Types of Types “It would appear.
Introduction to CSCI 1311 Dr. Mark C. Lewis
CS 326 Programming Languages, Concepts and Implementation
Lecture 4: Evaluation Rules Recursion CS200: Computer Science
Introduction to Computer Science / Procedural – 67130
Lecture 28: Types of Types
Semantics In Text: Chapter 3.
Lecture 25: Metalinguistics > (meval '((lambda (x) (* x x)) 4)
Lecture 10: Using Object-Oriented Languages
Class 34: Models of Computation CS200: Computer Science
David Evans Lecture 19: ||ism I don’t think we have found the right programming concepts for parallel computers yet.
Group 4: Song Li, Ying Lu, Hexin Wang, and Michael Walker May 1, 2000
Lecture 23: Computability CS200: Computer Science
Week 1 - Friday COMP 1600.
Presentation transcript:

David Evans CS655: Programming Languages University of Virginia Computer Science Lecture 14: Types of Types “It would appear that we have reached the limits of what it is possible to achieve with computer technology, although one should be careful with such statements, as they tend to sound pretty silly in five years.” John Von Neumann, 1949

6 March 2001CS 655: Lecture 142 Menu Latent Types, Dynamic Checking Manifest Types, Dynamic Checking Project Kickoff Manifest Types, Static Checking

6 March 2001CS 655: Lecture 143 Types integers in [0, …) Strings Beatle’s Song Titles pointers that points to integer that is prime number Colors secret information pointers that points to pointer that points to storage shared by some other pointer programs that halt Type is any set of values After Spring Break, we will see a different definition...

6 March 2001CS 655: Lecture 144 Why have types? Overloading operators + vs. FADD – compiler needs types to figure out what “+” means Detecting program errors –Better to notice error than report incorrect result Make programs easier to read, understand and maintain –Better than comments if they are checked and can be trusted Security –Can use types to constrain the behavior of programs (we’ll see Proof-Carrying Code later…)

6 March 2001CS 655: Lecture 145 Taxonomy Latent vs. Manifest –Are types visible in the program text? Checked statically vs. checked dynamically –Do you have to run the program to know if it has type errors? Checked weakly vs. strongly –How strict are the rules for using types? –Meaningless (just matter of degree) All combinations are (theoretically) possible –Language that is manifest + static + “weak”? –Language that is latent + dynamic + “strong”?

6 March 2001CS 655: Lecture 146 Labrador: BARK with Latent Types Instruction ::= STORE Loc Literal | HALT | ERROR (Same as BARK) | ADD Loc 1 Loc 2 Loc 1 gets the value of Loc 1 + Loc 2. Loc 1 and Loc 2 must be the same type, result has same type. | MUL Loc 1 Loc 2 Loc 1 gets the value of Loc 1 * Loc 2. Loc 1 and Loc 2 must be the same type. | IF Loc 1 THEN Loc 1 If value in Loc 1 is non-zero, jump to instruction corresponding to value in Loc 2. Loc 1 and Loc 2 must contain integers. Literal ::= IntLiteral | RealLiteral IntLiteral ::= [ - ] ? [ ][ ]*Has type integer. RealLiteral ::= [-] ? [ ][ ]*.[ ]*Has type real. As companions Labradors are kindly, patient,intelligent and always keen to please. They make perfect family dogs being especially good with children. Walter & Shackles Guide to Dogs

6 March 2001CS 655: Lecture 147 Labrador Program [0] STORE R [1] STORE R1 4 [2] MUL R1 R1 [3] MUL R0 R1.

6 March 2001CS 655: Lecture 148 Operational Semantics: ADD Instructions[PC] = ADD Loc 1 Loc 2  where PC’ = PC + 1 RegisterFile’[n] = RegisterFile[n] if n  Loc RegisterFile’[n] = if n  Loc 1 RegisterFile[Loc 1 ] + RegisterFile[Loc 2 ] BARK rule: What does this mean?

6 March 2001CS 655: Lecture 149 Typed Register File C = Instructions x PC x RegisterFile RegisterFile[i] = for all integers i type = integer | real value = an integer if type if integer, a real if type is real Assume functions typeof(RegisterFile[i]), valueof(RegisterFile[i])

6 March 2001CS 655: Lecture 1410 Operational Semantics: ADD integer Instructions[PC] = ADD Loc 1 Loc 2,  where PC’ = PC + 1 RegisterFile’[n] = RegisterFile[n] if n  Loc RegisterFile’[n] = if n  Loc 1 <integer, valueof(RegisterFile[Loc 1 ]) + integer valueof(RegisterFile[Loc 2 ])> typeof(RegisterFile[Loc 1 ]) = integer, typeof(RegisterFile[Loc 2 ]) = integer

6 March 2001CS 655: Lecture 1411 Operational Semantics: ADD real Instructions[PC] = ADD Loc 1 Loc 2, typeof(RegisterFile[Loc 1 ]) = real, typeof(RegisterFile[Loc 2 ]) = real  where PC’ = PC + 1 RegisterFile’[n] = RegisterFile[n] if n  Loc RegisterFile’[n] = if n  Loc 1 <real, valueof(RegisterFile[Loc 1 ]) + real valueof(RegisterFile[Loc 2 ])>

6 March 2001CS 655: Lecture 1412 Strong vs. Weak Typing What do we have? –Latent, dynamic, “strongly” typed language To get: latent, dynamic, “weakly” typed language: –Allow ADD and MUL to work on mixed types, result is real, allow IF predicate to be real –Add transition rules for Instructions[PC] = ADD Loc 1 Loc 2, typeof(RegisterFile[Loc 1 ]) = real, typeof(RegisterFile[Loc 2 ]) = integer etc.

6 March 2001CS 655: Lecture 1413 Manifest Types Often, however, explicit (manifest) types make programs easier for compilers to read, not easier for humans to read; and explicit (manifest) types are generally cumbersome for the program writer as well. Implicitly (latently) typed programming languages thus have clear advantages in terms of readability and writability. Turbak & Gifford

6 March 2001CS 655: Lecture 1414 Mastiff: BARK with Manifest Types Program ::= Declaration* Instruction* Declaration ::= TYPE Loc INTEGER Loc will hold integral values. | TYPE Loc REAL Loc will hold real values. Instruction ::= STORE Loc Literal Loc gets the value of Literal. Loc must have been declared with the same type as Literal. … (same as Labrador) Mastiff: An excellent guard dog, yet gentle and affectionate to its family. Walter & Shackles Guide to Dogs

6 March 2001CS 655: Lecture 1415 Mastiff Program [D0]TYPE R0 REAL [D1]TYPE R1 INTEGER [0] STORE R [1] STORE R1 4 [2] MUL R1 R1 [3] MUL R0 R1

6 March 2001CS 655: Lecture 1416 Input Function: I : Program  C C = Instructions x PC x RegisterFile where Instructions = same as before, PC = 0 RegisterFile[n] = if TYPE Rn INTEGER is in Declarations RegisterFile[n] = if TYPE Rn REAL is in Declarations RegisterFile[n] = for all other integers n RegisterFile[n] = if (TYPE Rn INTEGER and TYPE Rn REAL are in Declarations)

6 March 2001CS 655: Lecture 1417 STORE Loc IntLiteral Instructions[PC] = STORE Loc IntLiteral, typeof(RegisterFile[Loc]) = integer  where PC’ = PC + 1 RegisterFile’[n] = RegisterFile[n] if n  Loc RegisterFile’[n] = if n  Loc

6 March 2001CS 655: Lecture 1418 Is Dynamic Type Checking Useful?

6 March 2001CS 655: Lecture 1419 Projects Ideal Project Should: 1.Contribute to your thesis research work 2.Lead to a conference paper 3.Involve something interesting related to programming languages 4.Not take up all of your time for the rest of the semester Not all projects will be ideal. #1 is beneficial; #2 is optimistic; #3 is required; #4 is optional.

6 March 2001CS 655: Lecture 1420 Finding a Project Topic Think about how your research relates to programming languages: –Are there things that you describe in an ad hoc way today, that could be better understood if they were described more precisely? –Are the places where new abstractions (or removing old ones) would help? Send me an before March 19 describing your idea

6 March 2001CS 655: Lecture 1421 Example (Haiyong) Doing research on packet classification, performance is crucial Lots of tradeoffs between how you define your packet classifier and how well it performs, some attempts to define high- level languages for packet classifiers, but they are inflexible or inefficient Project topic: –Design language for defining packet filters that is flexible and extensible, but performs well.

6 March 2001CS 655: Lecture 1422 If you really can’t find a project related to your research Try harder You can: –Partner with someone else doing a project related to their research –Do a project on something else (possibly with other classmates). I have a few ideas.

6 March 2001CS 655: Lecture 1423 Course Project Calendar Sunday, 18 Mar (midnight): Send me about your project idea Week of 19 Mar - 23 Mar: Project meetings 26 Mar: Project proposal 10 Apr: Preliminary project report 24 Apr and 26 Apr: Presentations Tuesday, 1 May: Final report due

6 March 2001CS 655: Lecture 1424 Project Mantras Choose an ambitious topic, but be realistic about what you can accomplish in 7 weeks –Try to find a topic you will be interested in working on after the class is over to turn into conference paper Keep me informed on how your project is going and if you run into any problems. Don’t wait until due dates. Focus on design and understanding – only build things when there is a clear motivation to solve a problem or conducting an experiment.

6 March 2001CS 655: Lecture 1425 Project Questions Next: static semantics

6 March 2001CS 655: Lecture 1426 Static Semantics Static checking = at compile-time –Dynamic checking = at run (simulate)-time Know a whole program is type-correct without running it Can make claims about all possible executions Drawbacks: –May limit expressiveness of types (not everything can be checked statically) –Some type-correct programs may not pass static checking

6 March 2001CS 655: Lecture 1427 Premise;...; Premise Conclusion Conclusions are type judgments: A E : T Read: A proves E has type T. Use type okay to mean it type-checks, but has no type. Type environment: A Type bindings: [I 1 :T 1,..., I n :T n ] I n (Loc) has type T n Typing Rules

6 March 2001CS 655: Lecture 1428 Mastiff Typing Rules A IntLiteral : integer[int-literal] RealLiteral : real[real-literal] A contains [Loc:T] Loc : T[location] true

6 March 2001CS 655: Lecture 1429 Typing ADD A Loc 1 : integer, A Loc2 2 : integer A ADD Loc 1 Loc 2 : okay [add-integers] A Loc 1 : real, A Loc2 2 : real A ADD Loc 1 Loc 2 : okay [add-reals]

6 March 2001CS 655: Lecture 1430 Typing MUL A Loc 1 : integer, A Loc2 2 : integer A MUL Loc 1 Loc 2 : okay [mul-integers] A Loc 1 : real, A Loc 2 : integer A MUL Loc 1 Loc 2 : okay [mul-weak]

6 March 2001CS 655: Lecture 1431 Typing IF A Loc 1 : integer, A Loc 2 : integer A IF Loc 1 THEN Loc 2 : okay [if] A Loc 1 : real, A Loc 2 : integer A IF Loc 1 THEN Loc 2 : okay [if-weak]

6 March 2001CS 655: Lecture 1432 Type Checking Statement is well-typed if and only if it has a provable type judgment: –Construct a proof tree, where the root is the type judgment for this statement, and leaves are the axioms Declarations provide the axioms: Declarations = TYPE Loc i0 T i0 ; TYPE Loc i1 T i1,...  A = [Loc i0 : T i0, Loc i1 : T i1,... ]

6 March 2001CS 655: Lecture 1433 Type Checking Example Check instruction 3: MUL R0 R1 [D0]TYPE R0 REAL [D1]TYPE R1 INTEGER [0] STORE R [1] STORE R1 4 [2] MUL R1 R1 [3] MUL R0 R1 A R0 : real, A R1 : integer A MUL R0 R1 : okay [mul-weak] A contains [ R0 : real] R0 : real same for R1 : integer A = [ R0 : real; R1 : integer] from Declarations

6 March 2001CS 655: Lecture 1434 Summary Statically Checked Dynamically Checked Manifest TypesMastiff-SMastiff-D Latent TypesLabrador CLU, Pascal ML Scheme, Smalltalk Java (casts)

6 March 2001CS 655: Lecture 1435 Charge Think about project ideas –Spend at least 1½ hours on Thursday just thinking (not reading, not near a computer) Lots more on types to come... After Spring Break: –Data Abstraction CLU: language designed to support methodology based on data abstraction Reasoning (informally) about data abstractions –Object-Oriented Languages Later: –How to do static checking with latent types (type reconstruction, type inference) –How to use types for security