Dynamically-typed Languages. David Miller

Similar documents
Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

Concepts of Programming Languages

The role of semantic analysis in a compiler

Dynamic Languages. CSE 501 Spring 15. With materials adopted from John Mitchell

Topic 9: Type Checking

Topic 9: Type Checking

Informal Semantics of Data. semantic specification names (identifiers) attributes binding declarations scope rules visibility

SKILL AREA 304: Review Programming Language Concept. Computer Programming (YPG)

CSI33 Data Structures

Chapter 5. Names, Bindings, and Scopes

CS263: Runtime Systems Lecture: High-level language virtual machines

Com S 541. Programming Languages I

9/7/17. Outline. Name, Scope and Binding. Names. Introduction. Names (continued) Names (continued) In Text: Chapter 5

Chapter 5 Names, Binding, Type Checking and Scopes

Example: Haskell algebraic data types (1)

Key components of a lang. Deconstructing OCaml. In OCaml. Units of computation. In Java/Python. In OCaml. Units of computation.

CS 32. Lecture 1: oops

Fundamentals of Perl 6 From Zero to Scripting. By Jeffrey Goff, in conjunction with O'Reilly Media

Object Model Comparisons

Programming Languages 2nd edition Tucker and Noonan"

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

Object Model. Object Oriented Programming Spring 2015

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

Semantic Analysis. Lecture 9. February 7, 2018

COP4020 Programming Languages. Compilers and Interpreters Robert van Engelen & Chris Lacher

Chapter 9 Subprograms

Subprograms. Copyright 2015 Pearson. All rights reserved. 1-1

Programs as data first-order functional language type checking

Type Inference. Prof. Clarkson Fall Today s music: Cool, Calm, and Collected by The Rolling Stones

Cunning Plan. One-Slide Summary. Functional Programming. Functional Programming. Introduction to COOL #1. Classroom Object-Oriented Language

Scripted Components: Problem. Scripted Components. Problems with Components. Single-Language Assumption. Dr. James A. Bednar

Scripted Components Dr. James A. Bednar

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler so far

Names, Bindings, Scopes

Monday Feb 16, 5pm. solve these equalities. Midterm Thursday Feb 12. covers only ocaml part of the course, assignments 1 through 3.

Early computers (1940s) cost millions of dollars and were programmed in machine language. less error-prone method needed

SOFTWARE ARCHITECTURE 6. LISP

Fibonacci in Lisp. Computer Programming: Skills & Concepts (CP1) Programming Languages. Varieties of Programing Language

CS558 Programming Languages

Chapter 6. Data Types

CMSC 4023 Chapter 9. Fundamentals of Subprograms Introduction

Static Typing or Dynamic Typing: A Real Issue or a Simple Case of Tribalism

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

Type Bindings. Static Type Binding

Data Types. (with Examples In Haskell) COMP 524: Programming Languages Srinivas Krishnan March 22, 2011

Scala : an LLVM-targeted Scala compiler

CS558 Programming Languages

CS383 PROGRAMMING LANGUAGES. Kenny Q. Zhu Dept. of Computer Science Shanghai Jiao Tong University

RSqueak/VM Building a fast, malleable VM with students

Names, Scopes, and Bindings. CSE 307 Principles of Programming Languages Stony Brook University

Scoping and Type Checking

Simply-Typed Lambda Calculus

Python Implementation Strategies. Jeremy Hylton Python / Google

Types. Type checking. Why Do We Need Type Systems? Types and Operations. What is a type? Consensus

Perl Library Functions

SOFTWARE ARCHITECTURE 7. JAVA VIRTUAL MACHINE

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

Introduction to Python. Didzis Gosko

Names, Scopes, and Bindings. CSE 307 Principles of Programming Languages Stony Brook University

Imperative Programming

Chapter 2 Preview. Preview. History of Programming Languages. History of Programming Languages. History of Programming Languages

Lecture 7: Type Systems and Symbol Tables. CS 540 George Mason University

Sista: Improving Cog s JIT performance. Clément Béra

Chapter 9. Subprograms

Comp 311 Principles of Programming Languages Lecture 21 Semantics of OO Languages. Corky Cartwright Mathias Ricken October 20, 2010

CMSC 330: Organization of Programming Languages

G Programming Languages - Fall 2012

Announcements. My office hours are today in Gates 160 from 1PM-3PM. Programming Project 3 checkpoint due tomorrow night at 11:59PM.

Principles of Programming Languages, 2

Chapter 10. Implementing Subprograms

Back to OCaml. Summary of polymorphism. Example 1. Type inference. Example 2. Example 2. Subtype. Polymorphic types allow us to reuse code.

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

Why are there so many programming languages? Why do we have programming languages? What is a language for? What makes a language successful?

Programming Languages & Paradigms PROP HT Course Council. Subprograms. Meeting on friday! Subprograms, abstractions, encapsulation, ADT

Types for Flexible Objects

Martin Kruliš, v

Faster Haskell. Neil Mitchell

Principles of Programming Languages. Lecture Outline

Object Model. Object Oriented Programming Winter

CSC 533: Organization of Programming Languages. Spring 2005

Design issues for objectoriented. languages. Objects-only "pure" language vs mixed. Are subclasses subtypes of the superclass?

Functional Programming. Introduction To Cool

Big Bang. Designing a Statically Typed Scripting Language. Pottayil Harisanker Menon, Zachary Palmer, Scott F. Smith, Alexander Rozenshteyn

Data Types. Every program uses data, either explicitly or implicitly to arrive at a result.

Programming Languages Third Edition. Chapter 7 Basic Semantics

JavaScript: Introduction, Types

Outline. Introduction Concepts and terminology The case for static typing. Implementing a static type system Basic typing relations Adding context

! Broaden your language horizons! Different programming languages! Different language features and tradeoffs. ! Study how languages are implemented

CSE P 501 Compilers. Dynamic Languages Hal Perkins Winter UW CSE P 501 Winter 2016 X2-1

Dynamic Languages Toolkit. Presented by Andrey Tarantsov

Programming Languages, Summary CSC419; Odelia Schwartz

Names, Scopes, and Bindings II. Hwansoo Han

Chapter 9. Subprograms

Protocol Buffers, grpc

Subtyping (Dynamic Polymorphism)

EDAN65: Compilers, Lecture 13 Run;me systems for object- oriented languages. Görel Hedin Revised:

Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology

Computer Components. Software{ User Programs. Operating System. Hardware

PyPy - How to not write Virtual Machines for Dynamic Languages

Transcription:

Dynamically-typed Languages David Miller

Dynamically-typed Language Everything is a value No type declarations Examples of dynamically-typed languages APL, Io, JavaScript, Lisp, Lua, Objective-C, Perl, PHP, Python, Ruby, Scheme, Smalltalk

Dynamic vs. Static Static Types are associated with variables Each variable is of only one type during its lifetime Dynamic Types are associated with runtime values Variables may point to values of differing types during their lifetime Variable types declared or inferred Often no variable declaration Type checking done at compile time Type checking often done at run-time

Dynamic vs. Static (In Practice) Static Most often occurs in compiled languages Well suited for class-based OO programming Usually results in faster compiled code, but often takes longer to compile Easier to optimize Code generally more verbose Meta-programming more cumbersome to write Dynamic Most often occurs in interpreted languages Well suited for prototype-based OO programming May allow compilers and interpreters to run faster, but produces slower (compiled) code More difficult to optimize Code generally more succinct Well suited for meta-programming

Dynamic vs. Static (In Practice) Double Layered Hash Table: C# 1.0 Python Hashtable ht = new Hashtable(); ht["something"] = new Hashtable(); ((Hashtable)ht["something"])["someObj"] = new SomeObj(); ((SomeObj)((Hashtable)ht["something"])["someObj"]).SomeMethod(); ht = {} ht["something"] = {} ht["something"]["someobj"] = SomeObj() ht["something"]["someobj"].somemethod();

Type Inference Optional type declaration Allow variables to be declared so as to enable optimization of the (compiled) code Standard type inference Use standard type inferencing techniques to determine static type information Type prediction Use usage patterns to guess what type a variable (an object?) is based on what operation it is used in

Python Supports structured and prototype based(?) object oriented programming Uses Duck Typing No type checking at compile time. Instead, an operation on an object fails at runtime if it does not support that operation Strongly typed

PyPy Python Compiler Compiler analyses live programs Programs are read into the Python interpreter and initialized Flow Object Space used to construct the control flow graphs by running through each possible control path of the code and recording operations performed on abstract objects Flow graphs are in Static Single Information (SSI) form Extension of Static Single Assignment form in which each variable is used in only one basic block All variables that are not dead at the end of a block are explicitly carried over and renamed Resulting flow graphs are passed to the annotator that performs type inferencing

PyPy s Type Annotator Assigns annotations to each variable in the control flow graph Annotations describe the possible run-time objects that a variable can contain Flows annotations forward Type of a variable in Python can only be deduced by how it is produced, not by how it is used Starting from an entry function, with user-specified annotations for it s arguments, annotations are flowed through the blocks following calls recusively Used fixed point algorithm in situation of loops if previously annotated variables are too restrictive, generalize them and process the loop again

PyPy s Type Annotator Lattice of annotations

PyPy s Type Annotator Lattice of annotations

Self Prototype based Object Oriented language Implements full messages passing Objects consist of named slots, each of which contain a reference to another object Objects with source code associated with them are called methods When a message is sent to an object (called a receiver ) the slots of the object (and recursively, parents of the object) are search for a match If found, its content is evaluated and returned as the result of the message send Primitive operations have succeed and fail cases Flow of control normally rejoins after the result is computed

Self Code Example Self code: C code: sumto: upperbound = ( sum <- 0 to: upperbound Do: [ :index sum: sum + index ]. sum ) int sumto(int self, int upperbound) { int sum = 0; int index; for (index = self; index <= upperbound; index++) sum = sum + index; return sum; }

SELF Compiler Extracts static type information Compiles several copies of a procedure, each customized for a specific receiver type Splits calls after a join, placing a copy on each control path optimized for a particular receiver type Predicts types that are likely but unknown by static type inference and inserts run-time tests to verify predictions Implements other standard optimization techniques compile-time message lookup, aggressive procedure inlining, etc.

SELF Compiler Customized Compilation Provides type information for any calls to self (assuming no dynamic inheritance) Compiles copies of a method customized by characteristics of the calling site All subsequent calls to the method sharing the same characteristic, call the copy optimized for that calling site

SELF Compiler Message Splitting Provides type information for the successful results of primitive operations When control paths merge with different result types for each path, a subsequent message can be split The messages send is push up past the merge point Each copy of the message send can then be further optimized

SELF Compiler Type Prediction Provides type information for some messages not covered by customized compilations or message splitting Certain messages are more likely to be sent to some types of receivers than others From benchmarking measurements, types of receivers can be predicted eg. 90% of the time operators +, -, and < have integer arguments A run-time type check is inserted and the message is split with a copy compiled on each branch The success message is inlined while the fail message remains a full message send