Top Banner
MaxSMT-Based Type Inference for Python 3 Mostafa Hassan, German University in Cairo Caterina Urban Marco Eilers Peter Müller
16

MaxSMT-Based Type Inference for Python 3 · 2018-07-22 · MaxSMT-Based Type Inference for Python 3 Mostafa Hassan, German University in Cairo Caterina Urban Marco Eilers Peter Müller

Jul 25, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: MaxSMT-Based Type Inference for Python 3 · 2018-07-22 · MaxSMT-Based Type Inference for Python 3 Mostafa Hassan, German University in Cairo Caterina Urban Marco Eilers Peter Müller

MaxSMT-Based Type Inference for Python 3

Mostafa Hassan, German University in CairoCaterina UrbanMarco EilersPeter Müller

Page 2: MaxSMT-Based Type Inference for Python 3 · 2018-07-22 · MaxSMT-Based Type Inference for Python 3 Mostafa Hassan, German University in Cairo Caterina Urban Marco Eilers Peter Müller

2

Page 3: MaxSMT-Based Type Inference for Python 3 · 2018-07-22 · MaxSMT-Based Type Inference for Python 3 Mostafa Hassan, German University in Cairo Caterina Urban Marco Eilers Peter Müller

3

Motivationdef get_percentage(db, ids, name)

for id in ids:

count, entry = db.get_entry(id)

if name in entry.categories:

return TOTAL_COUNT / count

return -1

Type safety:

Static analysis:

Page 4: MaxSMT-Based Type Inference for Python 3 · 2018-07-22 · MaxSMT-Based Type Inference for Python 3 Mostafa Hassan, German University in Cairo Caterina Urban Marco Eilers Peter Müller

4

Motivationdef get_percentage(db, ids, name)

for id in ids:

count, entry = db.get_entry(id)

if name in entry.categories:

return TOTAL_COUNT / count

return -1

Type safety:

Static analysis:

def get_percentage(db: Database,

ids: List[int],

name: str) -> float:

for id in ids:

count, entry = db.get_entry(id)

if name in entry.categories:

return TOTAL_COUNT / count

return -1

Page 5: MaxSMT-Based Type Inference for Python 3 · 2018-07-22 · MaxSMT-Based Type Inference for Python 3 Mostafa Hassan, German University in Cairo Caterina Urban Marco Eilers Peter Müller

6

Challengeclass A:

def __init__(self):

self.val = 15

class B:

def __init__(self):

self.val = “Hello”

def get_val(o):

return o.val

Page 6: MaxSMT-Based Type Inference for Python 3 · 2018-07-22 · MaxSMT-Based Type Inference for Python 3 Mostafa Hassan, German University in Cairo Caterina Urban Marco Eilers Peter Müller

7

Challengeclass A:

def __init__(self):

self.val = 15

class B:

def __init__(self):

self.val = “Hello”

def get_val(o: A) -> int:

return o.val

Page 7: MaxSMT-Based Type Inference for Python 3 · 2018-07-22 · MaxSMT-Based Type Inference for Python 3 Mostafa Hassan, German University in Cairo Caterina Urban Marco Eilers Peter Müller

8

Challengeclass A:

def __init__(self):

self.val = 15

class B:

def __init__(self):

self.val = “Hello”get_val(B())

def get_val(o: A) -> int:

return o.val

Page 8: MaxSMT-Based Type Inference for Python 3 · 2018-07-22 · MaxSMT-Based Type Inference for Python 3 Mostafa Hassan, German University in Cairo Caterina Urban Marco Eilers Peter Müller

9

Challengeclass A:

def __init__(self):

self.val = 15

class B:

def __init__(self):

self.val = “Hello”get_val(B())

◾ No principal (most general) types◾ Subtyping/polymorphism

def get_val(o: B) -> str:

return o.val

Page 9: MaxSMT-Based Type Inference for Python 3 · 2018-07-22 · MaxSMT-Based Type Inference for Python 3 Mostafa Hassan, German University in Cairo Caterina Urban Marco Eilers Peter Müller

10

Typpete

def main(a):

b = a[0] == 0

...

def main(a: List[int])

->int:

...

Type error: argument in line 3

bool <: ba <: list...

b = bool...

type ptype = int | ...const a: ptypeconst b: ptypesubtype(bool, b)...

Sat. Model:a = list(int), b = bool, ...

Unsatsubtype(bool, b)...

Page 10: MaxSMT-Based Type Inference for Python 3 · 2018-07-22 · MaxSMT-Based Type Inference for Python 3 Mostafa Hassan, German University in Cairo Caterina Urban Marco Eilers Peter Müller

12

SMT-Encoding

x.f = y.g type ptype = int | str | A | ...fun subtype (ptype, ptype) boolconst x: ptypeconst y: ptypeconst A_f: ptypeconst B_f: ptypeconst C_g: ptypesubtype(y, C)subtype(x, A) subtype(C_g, A_f)∧∨subtype(x, B) subtype(C_g, B_f)∧

class A:

def __init__(self, ...):

self.f = ...

class B: ... # has field f

class C: ... # has field g

y <: Cx <: A C.g <: A.f∧

∨x <: B C.g <: B_f∧

Page 11: MaxSMT-Based Type Inference for Python 3 · 2018-07-22 · MaxSMT-Based Type Inference for Python 3 Mostafa Hassan, German University in Cairo Caterina Urban Marco Eilers Peter Müller

14

def get_true():

return True

MaxSMT: Selecting Solutions

Problem: Arbitrary choice of solution

const get_true_return: ptypesubtype(bool, get_true_return)

def get_true() -> float:

return True

Page 12: MaxSMT-Based Type Inference for Python 3 · 2018-07-22 · MaxSMT-Based Type Inference for Python 3 Mostafa Hassan, German University in Cairo Caterina Urban Marco Eilers Peter Müller

15

def get_true():

return True

MaxSMT: Selecting Solutions

Problem: Arbitrary choice of solution

const get_true_return: ptypesubtype(bool, get_true_return)

get_true_return = bool

def get_true() -> float:

return True

def get_true() -> bool:

return True

Page 13: MaxSMT-Based Type Inference for Python 3 · 2018-07-22 · MaxSMT-Based Type Inference for Python 3 Mostafa Hassan, German University in Cairo Caterina Urban Marco Eilers Peter Müller

16

MaxSMT: Error Reporting

def incr_and_double(i):

return 2 * i + 1

incr_and_double([3])

Unsat

Problem: Error reporting

const get_true_return: ptypesubtype(i, int)subtype(int, incr_and_double_return)...

...

Page 14: MaxSMT-Based Type Inference for Python 3 · 2018-07-22 · MaxSMT-Based Type Inference for Python 3 Mostafa Hassan, German University in Cairo Caterina Urban Marco Eilers Peter Müller

17

MaxSMT: Error Reporting

def incr_and_double(i):

return 2 * i + 1

incr_and_double([3])

Unsatdef incr_and_double(i: int) -> int:

return 2 * i + 1

incr_and_double([3])

# Error: argument subtype in line 4

Problem: Error reporting

const get_true_return: ptypesubtype(i, int)subtype(int, incr_and_double_return)...

const get_true_return: ptypesubtype(i, int)subtype(int, incr_and_double_return)...

Page 15: MaxSMT-Based Type Inference for Python 3 · 2018-07-22 · MaxSMT-Based Type Inference for Python 3 Mostafa Hassan, German University in Cairo Caterina Urban Marco Eilers Peter Müller

18

Conclusion

◾ Evaluated on components of four open source projects, 400-770 LOC• Consistent speedup with MaxSMT• Constraint solving takes 3-15 seconds

◾ In the paper: Subtype encoding in SMT, generics◾ Available open source• www.github.com/caterinaurban/Typpete

◾ Future work: Extend MaxSMT encoding to gradual typing

Page 16: MaxSMT-Based Type Inference for Python 3 · 2018-07-22 · MaxSMT-Based Type Inference for Python 3 Mostafa Hassan, German University in Cairo Caterina Urban Marco Eilers Peter Müller

Try it out!

www.github.com/caterinaurban/Typpete