Gradual Python with Colored Local Type Inference Joe Angell April - - PowerPoint PPT Presentation

gradual python with colored local type inference
SMART_READER_LITE
LIVE PREVIEW

Gradual Python with Colored Local Type Inference Joe Angell April - - PowerPoint PPT Presentation

Gradual Python with Colored Local Type Inference Joe Angell April 29, 2009 Joe Angell Gradual Python with Colored Local Type Inference Motivation Python programmers use dynamic typing Lets type check what we can, and what the programmer


slide-1
SLIDE 1

Gradual Python with Colored Local Type Inference

Joe Angell April 29, 2009

Joe Angell Gradual Python with Colored Local Type Inference

slide-2
SLIDE 2

Motivation

Python programmers use dynamic typing Lets type check what we can, and what the programmer wants Gradual Typing lets you use type annotations to statically check code portions In Python nearly everything is first class, so to fit that paradigm we want first class parametric polymorphism Type inference saves lives

Joe Angell Gradual Python with Colored Local Type Inference

slide-3
SLIDE 3

Overview

Gradual Typing Colored Local Type Inference Integration of the two

Joe Angell Gradual Python with Colored Local Type Inference

slide-4
SLIDE 4

Gradual Typing Example

def div(a, b): if b == 0: return "err" else: return a // b def tydiv(a : int, b : int) -> dynamic: if b == 0: return "err" else: return a // b

Joe Angell Gradual Python with Colored Local Type Inference

slide-5
SLIDE 5

Parametric Polymorphism Example

from gp import * @generic("T") @generic("S") def myapply(fn : fun("T", "T", "S"), a : "T", b: "T") -> "S": return fn(a, b) def tydiv(a : int, b : int) -> dynamic: if b == 0: return "err" else: return a / b def div(a : int, b : int) -> float: ## returns int in Python 2.5 return a / b ## myapply<int,dynamic>(fdiv, 5, 2) x = inst_(myapply, int, dynamic)(tydiv, 5, 0)

Joe Angell Gradual Python with Colored Local Type Inference

slide-6
SLIDE 6

Colored Local Intro

Aimed at implicit instantiation, lambda signature inference, and local variable inference Based on F≤ a second-order lamdbda calculus with subtyping Bidirectional partial propegation of types Local Subtype Constraint Solving Extend the language without undecidability Fall back on type annotation

Joe Angell Gradual Python with Colored Local Type Inference

slide-7
SLIDE 7

Colored Local Example

from gp import * @generic("T") @generic("S") def myapply(fn : fun("T", "T", "S"), a : "T", b: "T") -> "S": return fn(a, b) def tydiv(a : int, b : int) -> dynamic: if b == 0: return "err" else: return a / b def div(a : int, b : int) -> float: ## returns int in Python 2.5 return a / b ## myapply<int,dynamic>(fdiv, 5, 2) x = myapply(tydiv, 5, 0)) # x : dynamic print(myapply(div, x, 2)) ## runtime check from dyn to int

Joe Angell Gradual Python with Colored Local Type Inference

slide-8
SLIDE 8

Colored Local Overview Implicit Instantiation

Infer type of Function Body with ? as Guess Infer type of Argument with Parameter type as Guess Subtype Constraint: Argument <: Parameter Subtype Constraint: Return Type <: Inherited Guess Solve Constraints, Substitute TV's in Return Type with Smallest Supertype

  • f Guess

Joe Angell Gradual Python with Colored Local Type Inference

slide-9
SLIDE 9

Future Work

Extend inference to classes and object types Extend to lambda Will Local variable inference be useful? Soundness and Completeness proofs

Joe Angell Gradual Python with Colored Local Type Inference

slide-10
SLIDE 10

Consistency

(CRefl) τ ∼ τ

(CFun)

σ1 ∼ τ1 σ2 ∼ τ2 σ1 → σ2 ∼ τ1 → τ2 (CUnR) τ ∼ ? (CUnL) ? ∼ τ

Joe Angell Gradual Python with Colored Local Type Inference

slide-11
SLIDE 11

Monkey Patching and Dynamic Metaprogramming

class Record: def getkey(self): return self.key r = Record() r.key = file.readline() ## setattr(object, name, value) setattr(r, file.readline(), parse(file.readline())) if isinstance(r, Record): ... elif isinstance(r, Foo): ... r.key = eval("5 if x == 0 else True")

Joe Angell Gradual Python with Colored Local Type Inference