Dynamic Typing COS 441 Princeton University Fall 2004.

Slides:



Advertisements
Similar presentations
Chapter 8 Technicalities: Functions, etc. Bjarne Stroustrup
Advertisements

Microsoft Research March 20, 2000 A Programming Language for Developing Interactive Web Services Claus Brabrand BRICS, University of Aarhus, Denmark.
Abstraction and Modular Reasoning for the Verification of Software Corina Pasareanu NASA Ames Research Center.
Coding and Debugging. Requirements and Specification Recall the four steps of problem solving: Orient, Plan, Execute, Test Before you start the implementation.
I Can Slack Off, My Code’s Compiling Comic and title care of XKCD.com.
Lightweight Abstraction for Mathematical Computation in Java 1 Pavel Bourdykine and Stephen M. Watt Department of Computer Science Western University London.
CSE 341, Winter Type Systems Terms to learn about types: –Type –Type system –Statically typed language –Dynamically typed language –Type error –Strongly.
Type Checking.
CSE341: Programming Languages Lecture 18 Static vs. Dynamic Typing Dan Grossman Fall 2011.
CIS 101: Computer Programming and Problem Solving Lecture 8 Usman Roshan Department of Computer Science NJIT.
Data Abstraction COS 441 Princeton University Fall 2004.
Debugging CPSC 315 – Programming Studio Fall 2008.
Reasons to study concepts of PL
1 Lecture 1  Getting ready to program  Hardware Model  Software Model  Programming Languages  The C Language  Software Engineering  Programming.
1 Introduction to Computers and Programming Quick Review What is a Function? A module of code that performs a specific job.
Python Jordan Miller and Lauren Winkleman CS 311 Fall 2011.
Portability CPSC 315 – Programming Studio Spring 2008 Material from The Practice of Programming, by Pike and Kernighan.
Semantics for MinML COS 441 Princeton University Fall 2004.
Compilers and Interpreters. Translation to machine language Every high level language needs to be translated to machine code There are different ways.
CS 104 Introduction to Computer Science and Graphics Problems Software and Programming Language (2) Programming Languages 09/26/2008 Yang Song (Prepared.
1 The Problem o Fluid software cannot be trusted to behave as advertised unknown origin (must be assumed to be malicious) known origin (can be erroneous.
Language Issues Misunderstimated? Sublimable? Hopefuller? "I know how hard it is for you to put food on your family.” "I know the human being and fish.
1.3 Executing Programs. How is Computer Code Transformed into an Executable? Interpreters Compilers Hybrid systems.
CSE341: Programming Languages Lecture 11 Type Inference Dan Grossman Winter 2013.
Language Evaluation Criteria
High level & Low level language High level programming languages are more structured, are closer to spoken language and are more intuitive than low level.
COP4020 Programming Languages
15-740/ Oct. 17, 2012 Stefan Muller.  Problem: Software is buggy!  More specific problem: Want to make sure software doesn’t have bad property.
Types for Programs and Proofs Lecture 1. What are types? int, float, char, …, arrays types of procedures, functions, references, records, objects,...
CSC3315 (Spring 2009)1 CSC 3315 Programming Languages Hamid Harroud School of Science and Engineering, Akhawayn University
CSCE 121: Introduction to Program Design and Concepts, Honors Dr. J. Michael Moore Spring 2015 Set 3: Objects, Types, and Values 1 Based on slides.
1 Names, Scopes and Bindings Aaron Bloomfield CS 415 Fall
Abstraction IS 101Y/CMSC 101 Computational Thinking and Design Tuesday, September 17, 2013 Marie desJardins University of Maryland, Baltimore County.
Testing. 2 Overview Testing and debugging are important activities in software development. Techniques and tools are introduced. Material borrowed here.
Security - Why Bother? Your projects in this class are not likely to be used for some critical infrastructure or real-world sensitive data. Why should.
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Advanced Compilers CMPSCI 710 Spring 2004 Lecture 1 Emery Berger University of Massachusetts,
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.
Hack for HHVM Converting Facebook Julien Verlaguet Software Engineer.
Chapter 3 - Language Design Principles
Design - programming Cmpe 450 Fall Dynamic Analysis Software quality Design carefully from the start Simple and clean Fewer errors Finding errors.
12/9/20151 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
Types and Programming Languages Lecture 11 Simon Gay Department of Computing Science University of Glasgow 2006/07.
How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types – type system – primitives, strings, arrays, hashes – pointers/references.
SAFE KERNEL EXTENSIONS WITHOUT RUN-TIME CHECKING George C. Necula Peter Lee Carnegie Mellon U.
COP 3275 Chapter 01 course website: Jonathan C.L. Liu, Ph.D. CISE Department University of Florida.
The single most important skill for a computer programmer is problem solving Problem solving means the ability to formulate problems, think creatively.
Scripting Languages Info derived largely from Programming Language Pragmatics, by Michael Scott.
Types and Programming Languages Lecture 3 Simon Gay Department of Computing Science University of Glasgow 2006/07.
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.
COP4020 Programming Languages Introduction Prof. Robert van Engelen (modified by Prof. Em. Chris Lacher)
Introduction to Computer Programming Concepts M. Uyguroğlu R. Uyguroğlu.
CSCE 343 – Programming Language Concepts Welcome!.
Heath Carroll Bill Hanczaryk Rich Porter.  A Theory of Type Polymorphism in Programming ◦ Robin Milner (1977)  Milner credited with introducing the.
Language-Based Security: Overview of Types Deepak Garg Foundations of Security and Privacy October 27, 2009.
Type Checking and Type Inference
Scripting Languages Info derived largely from Programming Language Pragmatics, by Michael Scott.
Types for Programs and Proofs
Learning to Program D is for Digital.
Programming Languages and Compilers (CS 421)
Advanced Compilers CMPSCI 710 Spring 2003 Lecture 1
Type Systems Terms to learn about types: Related concepts: Type
CSCE 315 – Programming Studio, Fall 2017 Tanzir Ahmed
Type Systems Terms to learn: Type Type system
Tonga Institute of Higher Education IT 141: Information Systems
Tonga Institute of Higher Education IT 141: Information Systems
Type Systems Terms to learn about types: Related concepts: Type
Reasons To Study Programming Languages
Optimizing Compilers CISC 673 Spring 2009 Course Overview
SPL – PS1 Introduction to C++.
Presentation transcript:

Dynamic Typing COS 441 Princeton University Fall 2004

Questions What are the tradeoffs between static and dynamic types? Why are many languages with interpreter based implementations dynamically typed? –e.g. Python, Perl, Tcl, Sh, … Why languages that are often compiled statically typed? –e.g. C, C++, Java, SML, …

Warning There is much controversy over definitions of what it means to be a “dynamically typed” language –untyped? –uni-typed? –stupid and broken Try to focus on the underlying semantics and don’t get caught up in the “what you call it wars” –I’ll call it stupid and broken (Harper is only slightly more diplomatic)

Typing vs. Safety statically typeduntyped or “unityped” safeSML, Java, Modula-3 Scheme, Lisp, Perl,… unsafeC, C++, PascalBCPL, assembly

What is Safety? Safety – No unchecked runtime “errors” What’s a runtime error? Up to the definition of language! Need to carefully understand what situations are considered errors to understand what it means for a language to be safe.

What is Dynamic Typing? Language feature where “type errors” are checked at runtime What’s a type? Again depends on language definition? Why do people like dynamic typing?

Dynamic Typing Delays errors until runtime Results in more flexible language Allows for heterogeneous data-structures Easier to build prototypes

Semantics Treat type errors like other checked errors –e.g. division by zero Take original dynamic semantics modify so that all “stuck” cases go to a well defined error state Resulting language is safe because there are no stuck states

Example: MinML

Which is Better? Harper attempts to show that dynamic typing is just a “special case” of static typing –Shows that dynamic typing has performance costs –Therefore should avoid dynamic typing when possible Question: Do you believe Harper’s argument?

Implementation Issues In a statically typed language we can represent everything as simple machine words (int, code pointer, bool) In-order to implement the dynamic semantics for dynamic typing must tag our values so we can test at runtime what type a value is

Implementation Issues (cont.) Tag checking rules

Implementation Issues (cont.)

MinML Interpreter MinML interpreter is doing this tagging/untagging and tag checking already! This is because this is the only way to safely implement an interpreter in SML If we were implementing MinML with C we could avoid runtime checks and rely on MinML static type-checker Even in C you’d probably add the tags for debugging!

Pay as You Go Dynamic typing requires tagging semantics Tagging adds cost to runtime of type-error free programs Violates pay-as-you-go principle –Cost of a feature should only be imposed when it is used –Hence dynamic typing is bad

Harper’s Argument (cont.) If we add a type of tagged values to our statically typed language a programmer can choose to have dynamic typing when the programmer choose too Make tagging a programmer decision not a language designer decision use static types to do this!

Example

Example (cont.)

Example: Factorial

Example: Optimized Factorial

Observations Performing tag elimination operations automatically is hard to do in a reliable way Reliable means the program knows when the compiler is smart enough to eliminate a needless check and knows what changes to the programs won’t confuse the optimizer

Summary of Harper’s Argument Dynamic typing is just dynamic tag checking Adding tags pervasively to your implementation violates pay-as-you-go Give programmer control by adding a tagged typed to your statically typed language Let programmer perform some tag checking optimizations by hand

Complaints Writting tagged_add(Int 1,Int 2) is such a pain What if I forget to include all the values I need to tag in my tagged type? When I prototype my program everything is going to start out tagged so why bother with static types in the first place, just make everything tagged by default!

Summary of My View Point Static types are great for –modularity and data abstraction (i.e. big programs) –optimizing compilers (good type info make it easier to make programs run fast) –documentation for people who have to maintain your code

Should Perl be Statically Typed? Dan’s rule of thumb for Perl/Python hacking –It should be less than one page –I want to finish the script quickly, I don’t care how long it runs –I never want to have to maintain it or force anyone else to Of course lots of real Perl/Python scripts violate all those assumptions

“Scripting” Language Myth’s Every “scripting” language I’ve every seen sooner or later turns into a full blown language with lots of libraries and whole applications written in them In reality, no program is going to be smaller than one page, performance does matter, and someone will be stuck maintaining it

Soft Typing Approach that tries to get the best of both worlds Programmer writes programs without types Soft-type checker detects obvious errors or inserts runtime time check when not sure Soft-type checker tries to minimize runtime penalty and reduced programmer burden

Soft Typing Case Study The DIALYZER: a DIscrepancy AnaLYZer for ERlang programs Apply soft-typing to large existing well tested code base for telecom switching application

Summary Start with a statically typed language and add dynamic typing by introducing tagged types Start with a dynamically typed language and add more static checking with a soft- typing One day maybe two approaches will meet in the middle