MYPY: A PYTHON VARIANT WITH SEAMLESS DYNAMIC AND STATIC TYPING. Jukka Lehtosalo University of Cambridge Computer Laboratory

Similar documents
Lecture 1 Introduction to Android

The P3 Compiler: compiling Python to C++ to remove overhead

Python 2 and 3 compatibility testing via optional run-time type checking

Optimizing and interfacing with Cython. Konrad HINSEN Centre de Biophysique Moléculaire (Orléans) and Synchrotron Soleil (St Aubin)

Fachbereich Informatik und Elektrotechnik SunSPOT. Ubiquitous Computing. Ubiquitous Computing, Helmut Dispert

Overview. The Android operating system is like a cake consisting of various layers.

Mobile Application Development Android

Introducing Tetra: An Educational Parallel Programming System

Extending Tizen Native Framework with Node.js

To Java SE 8, and Beyond (Plan B)

Tachyon: a Meta-circular Optimizing JavaScript Virtual Machine

VOC Documentation. Release 0.1. Russell Keith-Magee

Interpreters and virtual machines. Interpreters. Interpreters. Why interpreters? Tree-based interpreters. Text-based interpreters

Virtual Machine Learning: Thinking Like a Computer Architect

Example of Standard API

Language Based Virtual Machines... or why speed matters. by Lars Bak, Google Inc

Multi-core Programming System Overview

PHP vs. Java. In this paper, I am not discussing following two issues since each is currently hotly debated in various communities:

ANDROID BASED MOBILE APPLICATION DEVELOPMENT and its SECURITY

Android Developer Fundamental 1

Getting started with Android and App Engine

Le langage OCaml et la programmation des GPU

A JIT Compiler for Android s Dalvik VM. Ben Cheng, Bill Buzbee May 2010

Table of Contents. Adding Build Targets to the SDK 8 The Android Developer Tools (ADT) Plug-in for Eclipse 9

Performance Testing. Based on slides created by Marty Stepp

Overview of CS 282 & Android

Технологии Java. Android: Введение. Кузнецов Андрей Николаевич. Санкт-Петербургский Государственный Политехнический Университет

Take full advantage of IBM s IDEs for end- to- end mobile development

Islamic University of Gaza. Faculty of Engineering. Computer Engineering Department. Mobile Computing ECOM Eng. Wafaa Audah.

Programming in Python. Basic information. Teaching. Administration Organisation Contents of the Course. Jarkko Toivonen. Overview of Python

Android Development: a System Perspective. Javier Orensanz

CSCI E 98: Managed Environments for the Execution of Programs

BEAJRockit Mission Control. Using JRockit Mission Control in the Eclipse IDE

.NET and J2EE Intro to Software Engineering

Introduction to Oracle Mobile Application Framework Raghu Srinivasan, Director Development Mobile and Cloud Development Tools Oracle

<Insert Picture Here> Java, the language for the future

System Structures. Services Interface Structure

General Introduction

Last Class: OS and Computer Architecture. Last Class: OS and Computer Architecture

A lap around Team Foundation Server 2015 en Visual Studio 2015

CSE 403. Performance Profiling Marty Stepp

Publishing to TIZEN Using the Automated Conversion/Repackaging of Existing Android Apps. Hyeokgon Ryu, Infraware Technology, Ltd.

Effective Java Programming. efficient software development

ODROID Multithreading in Android

The Decaffeinated Robot

2! Multimedia Programming with! Python and SDL

HybriDroid: Analysis Framework for Android Hybrid Applications

Module Title: Software Development A: Mobile Application Development

Understanding Hardware Transactional Memory

Lecture 17: Mobile Computing Platforms: Android. Mythili Vutukuru CS 653 Spring 2014 March 24, Monday

Monitoring, Tracing, Debugging (Under Construction)

automated acceptance testing of mobile apps

BogDan Vatra and Andy Gryc. Qt on Android: Is it right for you?

Introduction (Apps and the Android platform)

Using EDA Databases: Milkyway & OpenAccess

Mobile Devices - An Introduction to the Android Operating Environment. Design, Architecture, and Performance Implications

Python, C++ and SWIG

Online Recruitment System 1. INTRODUCTION

An Easier Way for Cross-Platform Data Acquisition Application Development

D06 PROGRAMMING with JAVA. Ch3 Implementing Classes

Mocean Android SDK Developer Guide

CS 6371: Advanced Programming Languages

PARALLEL JAVASCRIPT. Norm Rubin (NVIDIA) Jin Wang (Georgia School of Technology)

Apache Jakarta Tomcat

9/11/15. What is Programming? CSCI 209: Software Development. Discussion: What Is Good Software? Characteristics of Good Software?

Smartphone Security for Android Applications

CS378 -Mobile Computing. Android Overview and Android Development Environment

Introduction to Android. CSG250 Wireless Networks Fall, 2008

Programming Mobile Apps with Python

Android Architecture. Alexandra Harrison & Jake Saxton

Building Applications Using Micro Focus COBOL

A Modular Approach to Teaching Mobile APPS Development

Using AllJoyn with Apache Cordova, Python & Node

GlassFish v3. Building an ex tensible modular Java EE application server. Jerome Dochez and Ludovic Champenois Sun Microsystems, Inc.

Chapter 13: Program Development and Programming Languages

vmprof Documentation Release 0.1 Maciej Fijalkowski, Antonio Cuni, Sebastian Pawlus

Synthesis for Developing Apps on Mobile Platforms

Getting Started with the Internet Communications Engine

How To Port A Program To Dynamic C (C) (C-Based) (Program) (For A Non Portable Program) (Un Portable) (Permanent) (Non Portable) C-Based (Programs) (Powerpoint)

Mobile Devices - An Introduction to the Android Operating Environment. Design, Architecture, and Performance Implications

Building native mobile apps for Digital Factory

An Introduction to Android

Introducing PgOpenCL A New PostgreSQL Procedural Language Unlocking the Power of the GPU! By Tim Child

language 1 (source) compiler language 2 (target) Figure 1: Compiling a program

Scala Actors Library. Robert Hilbrich

Jonathan Worthington Scarborough Linux User Group

MoonGen. A Scriptable High-Speed Packet Generator Design and Implementation. Paul Emmerich. January 30th, 2016 FOSDEM 2016

4. The Android System

Java on z/os. Agenda. Java runtime environments on z/os. Java SDK 5 and 6. Java System Resource Integration. Java Backend Integration

EMSCRIPTEN - COMPILING LLVM BITCODE TO JAVASCRIPT (?!)

Course MS10975A Introduction to Programming. Length: 5 Days

Crash Course in Java

Introduction to Python

CSE 373: Data Structure & Algorithms Lecture 25: Programming Languages. Nicki Dell Spring 2014

New Generation of Software Development

SOFTWARE TESTING TRAINING COURSES CONTENTS

Introduction to Android Development. Jeff Avery CS349, Mar 2013

Chapter 12 Programming Concepts and Languages

Transcription:

MYPY: A PYTHON VARIANT WITH SEAMLESS DYNAMIC AND STATIC TYPING Jukka Lehtosalo University of Cambridge Computer Laboratory

SPEAKER BIO 2000-2006 Software engineer (QPR Software and Kielikone) 2007-2009 Development manager (Kielikone) 2009 PhD research (University of Cambridge) Projects spanning web apps, Windows apps, mobile apps,distributed apps, document databases, information retrieval, pattern recognition, natural language processing, data mining, garbage collectors, compilers, localization tools, automated testing tools etc.

mypy is an experimental Python variant with seamless dynamic and static typing

Mostly, but not 100% compatible with Python mypy is an experimental Python variant with seamless dynamic and static typing

Research project, in development mypy is an experimental Python variant with seamless dynamic and static typing

mypy is an experimental Python variant with seamless dynamic and static typing Freely mix static and dynamic (duck) types Combine the benefits of static and dynamic typing

LANGUAGE GAP Python Java, C++, C, C# Scripting, web development, UI, Heavy lifting prototyping

VISION mypy (dynamic typing) Python mypy (static typing) Java, C++, C, C# Scripting, web development, UI, Heavy lifting prototyping

WHAT DOES IT LOOK LIKE? mypy, dynamic typing mypy, static typing def fib(n): a, b = 0, 1 while a < n: print(a) a, b = b, a+b void fib(int n): a, b = 0, 1 while a < n: print(a) a, b = b, a+b

WHAT DOES IT LOOK LIKE? mypy, dynamic typing mypy, static typing def fib(n): a, b = 0, 1 while a < n: print(a) a, b = b, a+b void fib(int n): a, b = 0, 1 while a < n: print(a) a, b = b, a+b Dynamically typed function

WHAT DOES IT LOOK LIKE? mypy, dynamic typing mypy, static typing def fib(n): a, b = 0, 1 while a < n: print(a) a, b = b, a+b void fib(int n): a, b = 0, 1 while a < n: print(a) a, b = b, a+b Static type signature

WHAT DOES IT LOOK LIKE? mypy, dynamic typing mypy, static typing def fib(n): a, b = 0, 1 while a < n: print(a) a, b = b, a+b void fib(int n): a, b = 0, 1 while a < n: print(a) a, b = b, a+b Type inference

DYNAMIC VS STATIC TYPING mypy, dynamic typing def err(): return 1 + x # no error (not called)

DYNAMIC VS STATIC TYPING mypy, dynamic typing def err(): return 1 + x err() # runtime error (when called)

DYNAMIC VS STATIC TYPING mypy, dynamic typing def err(): return 1 + x err() # runtime error (when called) mypy, static typing int err(): return 1 + x # compile (static) error

TYPES int, str, bool, MyClass simple type any dynamic (duck) type list<int>, dict<str, int> generic type func<str, int> function type tuple<int, str> tuple type

EXAMPLE SCRIPT import sys, re if not sys.argv[1:]: raise RuntimeError('Usage: wordfreq FILE') dict<str, int> d = {} s = open(sys.argv[1]).read() for word in re.sub('\w', ' ', s).split(): d[word] = d.get(word, 0) + 1 # Use list comprehension l = [(freq, word) for word, freq in d.items()] for freq, word in sorted(l): print('%-6d %s' % (freq, word))

EXAMPLE SCRIPT import sys, re if not sys.argv[1:]: raise RuntimeError('Usage: wordfreq FILE') dict<str, int> d = {} s = open(sys.argv[1]).read() for word in re.sub('\w', ' ', s).split(): d[word] = d.get(word, 0) + 1 # Use list comprehension l = [(freq, word) for word, freq in d.items()] for freq, word in sorted(l): print('%-6d %s' % (freq, word))

EXAMPLE CLASS dynamic typing class BankAccount: def init (self, initial_balance=0): self.balance = initial_balance def deposit(self, amount): self.balance += amount def withdraw(self, amount): self.balance -= amount def overdrawn(self): return self.balance < 0 my_account = BankAccount(15) my_account.withdraw(5) print(my_account.balance) (source: Python Wiki)

EXAMPLE CLASS static typing class BankAccount: void init (self, int initial_balance=0): self.balance = initial_balance void deposit(self, int amount): self.balance += amount void withdraw(self, int amount): self.balance -= amount bool overdrawn(self): return self.balance < 0 my_account = BankAccount(15) my_account.withdraw(5) print(my_account.balance)

EXAMPLE CLASS static typing class BankAccount: void init (self, int initial_balance=0): self.balance = initial_balance void deposit(self, int amount): self.balance += amount void withdraw(self, int amount): self.balance -= amount bool overdrawn(self): return self.balance < 0 my_account = BankAccount(15) my_account.withdraw(5) print(my_account.balance)

MYPY LIBRARY SUPPORT Mypy VM Mypy program Native mypy libraries Translation layer object mapping CPython VM Python libraries

NO GIL CPython uses Global Interpreter Lock only one thread can usually run at a time

NO GIL CPython uses Global Interpreter Lock only one thread can usually run at a time Not ok in the multicore era! Mypy will have a new, custom VM without GIL. Based on the Alore VM (http://www.alorelang.org/)

WHY STATIC TYPING?

PERFORMANCE speed mypy static typing, ahead-of-time compiler PyPy JIT compiler CPython interpreter (conceptual, not in scale) t

TYPE CHECKING Static type checker pinpoints errors in your program, before running it some_function(foo[n], self.bar(x)) ^ StaticError: Argument has invalid type list<str>, list<int> expected Less debugging Easier maintenance and refactoring

TOOL SUPPORT Static type information makes many tools more powerful: IDEs with reliable and precise code completion Refactoring tools Static analysis and linting tools Better productivity and quality

SEAMLESS DYNAMIC AND STATIC TYPING Use the best tool for the job Use dynamic typing when it makes sense Use static typing when it makes sense As real programs are not uniform: use both

Static typing may be a win if efficiency is important if your project is large or complex if you need to maintain code for several years Even when the above are true, dynamic typing is useful for tests for the user interface for glue code for extensions, scripts and plugins

Dynamically typed program Statically typed libraries

hot spot Sprinkle static types for better performance

Statically typed interfaces Dynamically typed implementations

Evolve programs from dynamic to static typing v 0.5 v 1.0 v 2.0

IMPLEMENTATION PLAN All dates tentative! Phase 1(2012) Mypy type checker + mypy-to-python translator Phase 2 (2013) Compiler to native code (C/LLVM back end), new VM Phase N (?) More Python features IDE JVM back end? Android port? (etc.)

MYPY VS OTHER PROJECTS

CYTHON Cython is a Python variant that compiles to Python C modules Cython can speed up tight loops by a lot (100 x or more) by using type declarations Cython programs can also call C functions directly Similar to Pyrex cpdef int sum3d(int[:, :, :] a): cdef int total = 0 I = a.shape[0] J = a.shape[1] K = a.shape[2] for i in range(i): for j in range(j): for k in range(k): total += a[i, j, k] return total (adapted from Cython Users Guide)

MYPY VS CYTHON Mypy (planned) Cython Powerful type system Simple type system General code speedup New, efficient VM Speedups mainly to low-level code CPython VM No GIL GIL still a problem

SHED SKIN Compiler for a statically typed subset of Python Large speedups to some programs (100x or more) Code looks like Python (explicit type declarations not needed) Whole-program type inference

MYPY VS SHED SKIN Mypy (planned) Shed Skin Large set of Python features Restricted Python feature set Dynamic and static typing Static typing only Modular, (relatively) fast compiles Very slow compilation for large programs Python lib access layer Not many Python libs

MYPY VS RPYTHON RPython is a Python subset used by PyPy to implement the JIT compiler RPython uses whole-program type inference (like Shed Skin) Differences vs mypy: similar to mypy vs Shed Skin

CONCLUSION Tell me what you think! All help and feedback is very welcome! Web site: http://www.mypy-lang.org Thank you!

ANDROID: OVERVIEW Over 500 M Android devices activated Android native APIs mostly based on Java (Dalvik) Performance important for apps Cheap devices with low-end CPUs UI responsiveness key to good user experience Battery conservation

MYPY ON ANDROID? Android Wishlist 1. Python syntax + libs 2. Good performance Can t have all of these right now 3. Easy native API access Mypy is a potential solution (by compiling to Dalvik) But there s a lot of work to do...