SlideShare a Scribd company logo
1 of 27
Dynamic Type Inference

for Gradual Hindley–Milner Typing
!1
Yusuke Miyazaki

Kyoto University

Japan
Taro Sekiyama

National Institute of Informatics

Japan
Atsushi Igarashi

Kyoto University

Japan
POPL 2019 / Cascais, Portugal
Gradual Typing

[Siek & Taha ’06]
Framework to integrate static and dynamic typing in a single
language
• Introduces the dynamic type to specify dynamically typed
parts in a program
• Combines typechecking at compile-time and run-time
• Enables gradual code evolution from a dynamically typed
program to a statically typed program
!2
Gradual Typing: Example
!3
# let g_avg (x: int) (y: *) = (x + y) / 2

val g_avg: int → * → int


# g_avg 3 5

4


# g_avg "hello" 5

Compile-time type error:

parameter “x” expects

int value




# g_avg 3 "hello"

Run-time error:

operator (+) expects int value
Value of any type can be passed

to a parameter with the dynamic type
Whether y has int type or not is
checked at run-timeThe dynamic type
Whether x has int type or not is
checked at compile-time
Gradual Typing: Another Example
!4
# (λf:*. f 3) (λx:int. x)
3: int *


# (λf:*. f 3) (λx:str. x)
Run-time error:

The parameter “x” expects a string value.

But an integer value 3 is passed to the parameter.
λx:int.x is passed to f:* 3 is passed to x:int
Type annotation can affect the result of evaluation
3 is passed to x:str
Value tagged with int
Our Work
Proposes the blame calculus λB
DTI to give a new semantics of the
gradually typed language with type inference and Hindley–Milner
polymorphism
• Introduces dynamic type inference (DTI) to deal with type variables
left undecided by compile-time type inference
• Proves properties of the calculus
• Soundness and completeness of dynamic type inference
• Gradual guarantee of λB
DTI and ITGL
• Implements an interpreter ymyzk/lambda-dti
!5
Outline
• Introduction
• Problem: Undecided Type Variables in ITGL
• Background: Implicitly Typed Gradual Language (ITGL)
• Problem of Type Variables Undecided by Compile-Time Type Inference
• Dynamic Type Inference
• Properties
• Conclusion
!6
Implicitly Typed Gradual Language (ITGL)

[Garcia & Cimini POPL ’15]
• Gradually typed language with let-polymorphism
• Compile-time type inference algorithm that infers static types
for where type annotations are omitted
• Static types: types do not contain the dynamic type
• Principal type property
!7
Compile-Time Type Inference of ITGL
!8
In: (λf. f 3) (λx. x)
Out: (λf:int→int. f 3) (λx:int. x)
Result: 3


In: (λf:*. f 3) (λx. x)
Out: (λf:*. f 3) (λx:α. x)
Add *
Problem of ITGL
!9
In: (λf. f 3) (λx. x)
Out: (λf:int→int. f 3) (λx:int. x)
Result: 3


In: (λf:*. f 3) (λx. x)
Out: (λf:*. f 3) (λx:α. x)
Result1: 3: int * (if α is replaced with int before evaluation)
Result2: Run-time error (otherwise)
Left undecided by type inference,

but cannot be ignored at run-time
Add *
bool int str
int→int int→bool

int→str bool→int …
Generally, it is difficult to find a static type for α at compile-time !
Candidates for α
Our Solution
!10
Find static types for undecided type variables during evaluation
(λf:*. f 3) (λx:α. x)
Run-time system will find out α to be int
Another Idea: Replacing with the Dynamic Type
!11
Replacing undecided type variables with the dynamic type may
sounds like a good idea, but we find some problems:
• Goes against with the original idea of ITGL that a type variable
is a placeholder for a static type
• Makes it difficult to detect a type error earlier
• Because it can make evaluation successful even when there
is no static substitutions that makes evaluation successful
We decided not to use this approach "
Outline
• Introduction
• Problem: Undecided Type Variables in ITGL
• Dynamic Type Inference
• Background: Cast for Run-Time Typecheck
• Dynamic Type Inference
• Properties
• Conclusion
!12
Cast for Run-Time Typecheck

[Wadler & Fiedler ESOP ’09]
A term for performing run-time typecheck in blame calculi
e: U U’
• Cast failure is expressed as a special term blame
• The paper deals with blame labels formally
• Casts are inserted where run-time checks are required during
translation from a gradually typed language to a blame
calculus
!13
Cast of a term e from type U to U’
Examples: Run-Time Typecheck with Casts
!14
(λx:*. x + 2) 3
⇝ (λx:*. (x: * int) + 2) (3: int *)
⟼ (3: int * int) + 2
⟼ 3 + 2 ⟼ 5

(λx:*. x + 2) "hello"
⇝ (λx:*. (x: * int) + 2) ("hello": str *)
⟼ ("hello": str * int) + 2
⟼ blame + 2 ⟼ blame
Run-time typecheck:

cast from int to int succeeds
Run-time typecheck:

cast from str to int fails
Value tagged with int
Cast inserting
translation
Another Example: Run-Time Typecheck with Casts
!15
(λf:*. f 3) (λx:int. x)
⇝ (λf:*. (f: * *→*) (3: int *))

                 ((λx:int. x): int→int *)


⟼+((λx:int. x) (3: int * int)): int *


⟼ ((λx:int. x) 3): int *
⟼+ 3: int *
Run-time typecheck:
cast from int to int succeeds
Dynamic Type Inference
!16
(λf:*. f 3) (λx:α. x)
⇝ (λf:*. (f: * *→*) (3: int *))

                   ((λx:α. x): α→α *)
⟼+((λx:α. x) (3: int * α)): α *






⟼ ((λx:int. x) 3): int *
⟼+ 3: int *
α is instantiated with int
DTI: Instantiates α with the type attached to

the value (int) and proceeds the evaluation
Generated substitution:

[α↦int]
Resulted in a value thanks to DTI
Left undecided by compile-time inference
Simply typed blame calculus + type variables

(+ let-polymorphism discussed in the paper)

U ::= * (Dynamic Type)

| α (Type Variable)

| int (Base Type)

| U→U (Function Type)
Formalizing DTI: λB
DTI
!17
e ::= x (Variable)

| c (Constant)

| λx:U.e (Abstraction)

| e1 e2 (Application)
| e: U1 U2 (Cast)
| blame (Blame)
Formalizing DTI: Reduction Relation
Two reduction rules for dynamic type inference
!18
v: *→* * α  ⟶  v: *→* * α1→α2
[α↦α1→α2]
Type substitution obtained by DTI:

Instantiate α with int then proceed reduction
v: int * α  ⟶ v
[α↦int]
e ⟶ e’S
Generate fresh type
variables α1 and α2
Formalizing DTI: Evaluation Relation
The evaluation rule makes sure that a type variable is
instantiated at most once










Example:
!19
e ⟶ e’

E[e] ⟼ S(E[e’])S
Apply the type substitution S to the entire term
S
Evaluation context
Reduction of a subterm
3: int * α ⟶ 3

(λx:α. x) (3: int * α) ⟼ (λx:int. x) 3
[α↦int]
[α↦int]
e ⟼ e’S
Outline
• Introduction
• Problem: Undecided Type Variables in ITGL
• Dynamic Type Inference
• Properties
• Conclusion
!20
Properties
Prove the following properties of the calculus
• Type safety of λB
DTI
• Soundness and completeness of DTI
• Translation from ITGL to λB
DTI is type-preserving
• Gradual guarantee of λB
DTI and ITGL
!21
Soundness and Completeness of DTI
DTI always returns an “appropriate” type substitution if it exists



• Soundness of DTI:
• A type substitution yielded by DTI is appropriate
• Completeness of DTI:
• If an appropriate type substitution exists, DTI returns one
!22
By applying it before evaluation,

the program terminates at a value
Soundness of Dynamic Type Inference
1. If a program e results in a value or blame r with DTI, then
applying the substitution S obtained by DTI beforehand does
not change the result
2. If a program e evaluates to blame with DTI, applying any
substitution results in blame
!23
Theorem (Soundness): Suppose ⊢ e: U
1. If e ⟼* r then, for any S’ s.t. ftv(S’(S(e))) = ∅, S’(S(e)) ⟼* S’(r)

for some S’’
2. If e ⟼* blame then, for any S’, S’(e) ⟼* blame for some S’’
S
S
S’’
S’’
Completeness of Dynamic Type Inference
1. If there is a type substitution S that makes a program result in
a value v, then evaluation with DTI also results in a related
value v’
2. If there is a type substitution that makes a program diverge,
evaluation with DTI also diverges
!24
Theorem (Completeness): Suppose ⊢ e: U
1. If S(e) ⟼* v, then e ⟼* v’ and S’’(v’) = v for some v’, S’, S’’
2. If S(e) diverges, then e diverges
∅ S’
In the Paper and Artifact…
• Full definition of the calculus λB
DTI
• Syntax, type system, operational semantics, translation from ITGL
• Extension to let-polymorphism which is non-parametric [Ahmed et al. POPL ’11]
• let x = v in e behaves the same as e[x:=v]
• Properties of the calculus
• Type safety
• Soundness and completeness of DTI
• Gradual guarantee of λB
DTI and ITGL with non-trivial precision relation
• Implementation of the calculus
!25
Related Work
• Replacing undecided type variables with the dynamic type

[Henglein & Rehof FPCA ’95 / Xie et al. ESOP ’18]
• Their work:
• Provides more permissive semantics
• Our work:
• Enables earlier bug detection
• Consistent with the idea that a type variable in ITGL is a
placeholder for a static type
!26
Conclusion
Blame calculus with dynamic type inference and let-polymorphism
• Dynamic type inference
• Instantiates undecided type variables along with evaluation
• Let-Polymorphism
• Properties
• Soundness and completeness of DTI
• Gradual guarantee of λB
DTI and ITGL
!27

More Related Content

What's hot

An introduction to functional programming with Swift
An introduction to functional programming with SwiftAn introduction to functional programming with Swift
An introduction to functional programming with SwiftFatih Nayebi, Ph.D.
 
if, while and for in Python
if, while and for in Pythonif, while and for in Python
if, while and for in PythonPranavSB
 
TMPA-2015: Kotlin: From Null Dereference to Smart Casts
TMPA-2015: Kotlin: From Null Dereference to Smart CastsTMPA-2015: Kotlin: From Null Dereference to Smart Casts
TMPA-2015: Kotlin: From Null Dereference to Smart CastsIosif Itkin
 
Under the hood of scala implicits (kl10tch 10.03.2015)
Under the hood of scala implicits (kl10tch 10.03.2015)Under the hood of scala implicits (kl10tch 10.03.2015)
Under the hood of scala implicits (kl10tch 10.03.2015)Alexander Podkhalyuzin
 
Types, classes and concepts
Types, classes and conceptsTypes, classes and concepts
Types, classes and conceptsNicola Bonelli
 
Type casting in java
Type casting in javaType casting in java
Type casting in javaFarooq Baloch
 
Primitives in Generics
Primitives in GenericsPrimitives in Generics
Primitives in GenericsIvan Ivanov
 
It's All About Morphisms
It's All About MorphismsIt's All About Morphisms
It's All About MorphismsUberto Barbini
 
Type checking compiler construction Chapter #6
Type checking compiler construction Chapter #6Type checking compiler construction Chapter #6
Type checking compiler construction Chapter #6Daniyal Mughal
 
Language Model (D3L1 Deep Learning for Speech and Language UPC 2017)
Language Model (D3L1 Deep Learning for Speech and Language UPC 2017)Language Model (D3L1 Deep Learning for Speech and Language UPC 2017)
Language Model (D3L1 Deep Learning for Speech and Language UPC 2017)Universitat Politècnica de Catalunya
 
Web application architecture
Web application architectureWeb application architecture
Web application architectureIlio Catallo
 
Learn a language : LISP
Learn a language : LISPLearn a language : LISP
Learn a language : LISPDevnology
 
Let Us Learn Lambda Using C# 3.0
Let Us Learn Lambda Using C# 3.0Let Us Learn Lambda Using C# 3.0
Let Us Learn Lambda Using C# 3.0Sheik Uduman Ali
 
Developer’s viewpoint on swift programming language
Developer’s viewpoint on swift programming languageDeveloper’s viewpoint on swift programming language
Developer’s viewpoint on swift programming languageAzilen Technologies Pvt. Ltd.
 

What's hot (20)

An introduction to functional programming with Swift
An introduction to functional programming with SwiftAn introduction to functional programming with Swift
An introduction to functional programming with Swift
 
Slides
SlidesSlides
Slides
 
if, while and for in Python
if, while and for in Pythonif, while and for in Python
if, while and for in Python
 
TMPA-2015: Kotlin: From Null Dereference to Smart Casts
TMPA-2015: Kotlin: From Null Dereference to Smart CastsTMPA-2015: Kotlin: From Null Dereference to Smart Casts
TMPA-2015: Kotlin: From Null Dereference to Smart Casts
 
Under the hood of scala implicits (kl10tch 10.03.2015)
Under the hood of scala implicits (kl10tch 10.03.2015)Under the hood of scala implicits (kl10tch 10.03.2015)
Under the hood of scala implicits (kl10tch 10.03.2015)
 
Types, classes and concepts
Types, classes and conceptsTypes, classes and concepts
Types, classes and concepts
 
Type casting in java
Type casting in javaType casting in java
Type casting in java
 
Cat's anatomy
Cat's anatomyCat's anatomy
Cat's anatomy
 
Introduction To C#
Introduction To C#Introduction To C#
Introduction To C#
 
Type casting
Type castingType casting
Type casting
 
Type Checking JavaScript
Type Checking JavaScriptType Checking JavaScript
Type Checking JavaScript
 
Primitives in Generics
Primitives in GenericsPrimitives in Generics
Primitives in Generics
 
It's All About Morphisms
It's All About MorphismsIt's All About Morphisms
It's All About Morphisms
 
Type checking compiler construction Chapter #6
Type checking compiler construction Chapter #6Type checking compiler construction Chapter #6
Type checking compiler construction Chapter #6
 
Language Model (D3L1 Deep Learning for Speech and Language UPC 2017)
Language Model (D3L1 Deep Learning for Speech and Language UPC 2017)Language Model (D3L1 Deep Learning for Speech and Language UPC 2017)
Language Model (D3L1 Deep Learning for Speech and Language UPC 2017)
 
Web application architecture
Web application architectureWeb application architecture
Web application architecture
 
Cats in Scala
Cats in ScalaCats in Scala
Cats in Scala
 
Learn a language : LISP
Learn a language : LISPLearn a language : LISP
Learn a language : LISP
 
Let Us Learn Lambda Using C# 3.0
Let Us Learn Lambda Using C# 3.0Let Us Learn Lambda Using C# 3.0
Let Us Learn Lambda Using C# 3.0
 
Developer’s viewpoint on swift programming language
Developer’s viewpoint on swift programming languageDeveloper’s viewpoint on swift programming language
Developer’s viewpoint on swift programming language
 

Similar to Dynamic Type Inference for Gradual Hindley–Milner Typing

Pict: A programming language based on the pi-calculus
Pict: A programming language based on the pi-calculusPict: A programming language based on the pi-calculus
Pict: A programming language based on the pi-calculusIMDS2014
 
COM1407: Type Casting, Command Line Arguments and Defining Constants
COM1407: Type Casting, Command Line Arguments and Defining Constants COM1407: Type Casting, Command Line Arguments and Defining Constants
COM1407: Type Casting, Command Line Arguments and Defining Constants Hemantha Kulathilake
 
Abstracting Vector Architectures in Library Generators: Case Study Convolutio...
Abstracting Vector Architectures in Library Generators: Case Study Convolutio...Abstracting Vector Architectures in Library Generators: Case Study Convolutio...
Abstracting Vector Architectures in Library Generators: Case Study Convolutio...ETH Zurich
 
Functional Programming You Already Know
Functional Programming You Already KnowFunctional Programming You Already Know
Functional Programming You Already KnowKevlin Henney
 
12TypeSystem.pdf
12TypeSystem.pdf12TypeSystem.pdf
12TypeSystem.pdfMdAshik35
 
Introduction to Python Programming
Introduction to Python ProgrammingIntroduction to Python Programming
Introduction to Python ProgrammingVijaySharma802
 
Mit6 087 iap10_lec02
Mit6 087 iap10_lec02Mit6 087 iap10_lec02
Mit6 087 iap10_lec02John Lawrence
 
Functional programming with FSharp
Functional programming with FSharpFunctional programming with FSharp
Functional programming with FSharpDaniele Pozzobon
 
02 functions, variables, basic input and output of c++
02   functions, variables, basic input and output of c++02   functions, variables, basic input and output of c++
02 functions, variables, basic input and output of c++Manzoor ALam
 
Introduction - Imperative and Object-Oriented Languages
Introduction - Imperative and Object-Oriented LanguagesIntroduction - Imperative and Object-Oriented Languages
Introduction - Imperative and Object-Oriented LanguagesGuido Wachsmuth
 
Lex (lexical analyzer)
Lex (lexical analyzer)Lex (lexical analyzer)
Lex (lexical analyzer)Sami Said
 
Final Design Project - Memo (with GUI)
Final Design Project - Memo (with GUI)Final Design Project - Memo (with GUI)
Final Design Project - Memo (with GUI)Alex Larcheveque
 
Testing for share
Testing for share Testing for share
Testing for share Rajeev Mehta
 

Similar to Dynamic Type Inference for Gradual Hindley–Milner Typing (20)

Pict: A programming language based on the pi-calculus
Pict: A programming language based on the pi-calculusPict: A programming language based on the pi-calculus
Pict: A programming language based on the pi-calculus
 
COM1407: Type Casting, Command Line Arguments and Defining Constants
COM1407: Type Casting, Command Line Arguments and Defining Constants COM1407: Type Casting, Command Line Arguments and Defining Constants
COM1407: Type Casting, Command Line Arguments and Defining Constants
 
Abstracting Vector Architectures in Library Generators: Case Study Convolutio...
Abstracting Vector Architectures in Library Generators: Case Study Convolutio...Abstracting Vector Architectures in Library Generators: Case Study Convolutio...
Abstracting Vector Architectures in Library Generators: Case Study Convolutio...
 
Interm codegen
Interm codegenInterm codegen
Interm codegen
 
Ch6.ppt
Ch6.pptCh6.ppt
Ch6.ppt
 
Clojure intro
Clojure introClojure intro
Clojure intro
 
Functional Programming You Already Know
Functional Programming You Already KnowFunctional Programming You Already Know
Functional Programming You Already Know
 
12TypeSystem.pdf
12TypeSystem.pdf12TypeSystem.pdf
12TypeSystem.pdf
 
Introduction to Python Programming
Introduction to Python ProgrammingIntroduction to Python Programming
Introduction to Python Programming
 
ATS Programming
ATS ProgrammingATS Programming
ATS Programming
 
Mit6 087 iap10_lec02
Mit6 087 iap10_lec02Mit6 087 iap10_lec02
Mit6 087 iap10_lec02
 
Functional programming with FSharp
Functional programming with FSharpFunctional programming with FSharp
Functional programming with FSharp
 
02 functions, variables, basic input and output of c++
02   functions, variables, basic input and output of c++02   functions, variables, basic input and output of c++
02 functions, variables, basic input and output of c++
 
Introduction - Imperative and Object-Oriented Languages
Introduction - Imperative and Object-Oriented LanguagesIntroduction - Imperative and Object-Oriented Languages
Introduction - Imperative and Object-Oriented Languages
 
Lex (lexical analyzer)
Lex (lexical analyzer)Lex (lexical analyzer)
Lex (lexical analyzer)
 
Final Design Project - Memo (with GUI)
Final Design Project - Memo (with GUI)Final Design Project - Memo (with GUI)
Final Design Project - Memo (with GUI)
 
CC Week 11.ppt
CC Week 11.pptCC Week 11.ppt
CC Week 11.ppt
 
Should i Go there
Should i Go thereShould i Go there
Should i Go there
 
Testing for share
Testing for share Testing for share
Testing for share
 
Return of c++
Return of c++Return of c++
Return of c++
 

More from Yusuke Miyazaki

Python と Docker で mypy Playground を開発した話
Python と Docker で mypy Playground を開発した話Python と Docker で mypy Playground を開発した話
Python と Docker で mypy Playground を開発した話Yusuke Miyazaki
 
Introducing wsgi_lineprof / PyCon JP 2017 LT
Introducing wsgi_lineprof / PyCon JP 2017 LTIntroducing wsgi_lineprof / PyCon JP 2017 LT
Introducing wsgi_lineprof / PyCon JP 2017 LTYusuke Miyazaki
 
オープンソースソフトウェア入門
オープンソースソフトウェア入門オープンソースソフトウェア入門
オープンソースソフトウェア入門Yusuke Miyazaki
 
型ヒントについて考えよう!
型ヒントについて考えよう!型ヒントについて考えよう!
型ヒントについて考えよう!Yusuke Miyazaki
 
Django から各種チャットツールに通知するライブラリを作った話
Django から各種チャットツールに通知するライブラリを作った話Django から各種チャットツールに通知するライブラリを作った話
Django から各種チャットツールに通知するライブラリを作った話Yusuke Miyazaki
 
iOS 開発のいま (ADF2015 LT会)
iOS 開発のいま (ADF2015 LT会)iOS 開発のいま (ADF2015 LT会)
iOS 開発のいま (ADF2015 LT会)Yusuke Miyazaki
 
iOS 開発のいま (CAMPHOR- x KMC 合同LT会)
iOS 開発のいま (CAMPHOR- x KMC 合同LT会)iOS 開発のいま (CAMPHOR- x KMC 合同LT会)
iOS 開発のいま (CAMPHOR- x KMC 合同LT会)Yusuke Miyazaki
 
最新の iOS に対応したアプリの開発
最新の iOS に対応したアプリの開発最新の iOS に対応したアプリの開発
最新の iOS に対応したアプリの開発Yusuke Miyazaki
 
コンピューターネットワーク入門
コンピューターネットワーク入門コンピューターネットワーク入門
コンピューターネットワーク入門Yusuke Miyazaki
 

More from Yusuke Miyazaki (12)

Python と Docker で mypy Playground を開発した話
Python と Docker で mypy Playground を開発した話Python と Docker で mypy Playground を開発した話
Python と Docker で mypy Playground を開発した話
 
Introducing wsgi_lineprof / PyCon JP 2017 LT
Introducing wsgi_lineprof / PyCon JP 2017 LTIntroducing wsgi_lineprof / PyCon JP 2017 LT
Introducing wsgi_lineprof / PyCon JP 2017 LT
 
オープンソースソフトウェア入門
オープンソースソフトウェア入門オープンソースソフトウェア入門
オープンソースソフトウェア入門
 
型ヒントについて考えよう!
型ヒントについて考えよう!型ヒントについて考えよう!
型ヒントについて考えよう!
 
iot.ymyzk.com の紹介
iot.ymyzk.com の紹介iot.ymyzk.com の紹介
iot.ymyzk.com の紹介
 
Django から各種チャットツールに通知するライブラリを作った話
Django から各種チャットツールに通知するライブラリを作った話Django から各種チャットツールに通知するライブラリを作った話
Django から各種チャットツールに通知するライブラリを作った話
 
iOS 開発のいま (ADF2015 LT会)
iOS 開発のいま (ADF2015 LT会)iOS 開発のいま (ADF2015 LT会)
iOS 開発のいま (ADF2015 LT会)
 
iOS 開発のいま (CAMPHOR- x KMC 合同LT会)
iOS 開発のいま (CAMPHOR- x KMC 合同LT会)iOS 開発のいま (CAMPHOR- x KMC 合同LT会)
iOS 開発のいま (CAMPHOR- x KMC 合同LT会)
 
Swift の問題点
Swift の問題点Swift の問題点
Swift の問題点
 
最新の iOS に対応したアプリの開発
最新の iOS に対応したアプリの開発最新の iOS に対応したアプリの開発
最新の iOS に対応したアプリの開発
 
コンピューターネットワーク入門
コンピューターネットワーク入門コンピューターネットワーク入門
コンピューターネットワーク入門
 
HTML初心者講座
HTML初心者講座HTML初心者講座
HTML初心者講座
 

Recently uploaded

The Ten Facts About People With Autism Presentation
The Ten Facts About People With Autism PresentationThe Ten Facts About People With Autism Presentation
The Ten Facts About People With Autism PresentationNathan Young
 
SaaStr Workshop Wednesday w/ Kyle Norton, Owner.com
SaaStr Workshop Wednesday w/ Kyle Norton, Owner.comSaaStr Workshop Wednesday w/ Kyle Norton, Owner.com
SaaStr Workshop Wednesday w/ Kyle Norton, Owner.comsaastr
 
The 3rd Intl. Workshop on NL-based Software Engineering
The 3rd Intl. Workshop on NL-based Software EngineeringThe 3rd Intl. Workshop on NL-based Software Engineering
The 3rd Intl. Workshop on NL-based Software EngineeringSebastiano Panichella
 
Quality by design.. ppt for RA (1ST SEM
Quality by design.. ppt for  RA (1ST SEMQuality by design.. ppt for  RA (1ST SEM
Quality by design.. ppt for RA (1ST SEMCharmi13
 
Dutch Power - 26 maart 2024 - Henk Kras - Circular Plastics
Dutch Power - 26 maart 2024 - Henk Kras - Circular PlasticsDutch Power - 26 maart 2024 - Henk Kras - Circular Plastics
Dutch Power - 26 maart 2024 - Henk Kras - Circular PlasticsDutch Power
 
Simulation-based Testing of Unmanned Aerial Vehicles with Aerialist
Simulation-based Testing of Unmanned Aerial Vehicles with AerialistSimulation-based Testing of Unmanned Aerial Vehicles with Aerialist
Simulation-based Testing of Unmanned Aerial Vehicles with AerialistSebastiano Panichella
 
Work Remotely with Confluence ACE 2.pptx
Work Remotely with Confluence ACE 2.pptxWork Remotely with Confluence ACE 2.pptx
Work Remotely with Confluence ACE 2.pptxmavinoikein
 
Genshin Impact PPT Template by EaTemp.pptx
Genshin Impact PPT Template by EaTemp.pptxGenshin Impact PPT Template by EaTemp.pptx
Genshin Impact PPT Template by EaTemp.pptxJohnree4
 
SBFT Tool Competition 2024 -- Python Test Case Generation Track
SBFT Tool Competition 2024 -- Python Test Case Generation TrackSBFT Tool Competition 2024 -- Python Test Case Generation Track
SBFT Tool Competition 2024 -- Python Test Case Generation TrackSebastiano Panichella
 
RACHEL-ANN M. TENIBRO PRODUCT RESEARCH PRESENTATION
RACHEL-ANN M. TENIBRO PRODUCT RESEARCH PRESENTATIONRACHEL-ANN M. TENIBRO PRODUCT RESEARCH PRESENTATION
RACHEL-ANN M. TENIBRO PRODUCT RESEARCH PRESENTATIONRachelAnnTenibroAmaz
 
Mathan flower ppt.pptx slide orchids ✨🌸
Mathan flower ppt.pptx slide orchids ✨🌸Mathan flower ppt.pptx slide orchids ✨🌸
Mathan flower ppt.pptx slide orchids ✨🌸mathanramanathan2005
 
Call Girls In Aerocity 🤳 Call Us +919599264170
Call Girls In Aerocity 🤳 Call Us +919599264170Call Girls In Aerocity 🤳 Call Us +919599264170
Call Girls In Aerocity 🤳 Call Us +919599264170Escort Service
 
PHYSICS PROJECT BY MSC - NANOTECHNOLOGY
PHYSICS PROJECT BY MSC  - NANOTECHNOLOGYPHYSICS PROJECT BY MSC  - NANOTECHNOLOGY
PHYSICS PROJECT BY MSC - NANOTECHNOLOGYpruthirajnayak525
 
Chizaram's Women Tech Makers Deck. .pptx
Chizaram's Women Tech Makers Deck.  .pptxChizaram's Women Tech Makers Deck.  .pptx
Chizaram's Women Tech Makers Deck. .pptxogubuikealex
 
Early Modern Spain. All about this period
Early Modern Spain. All about this periodEarly Modern Spain. All about this period
Early Modern Spain. All about this periodSaraIsabelJimenez
 
miladyskindiseases-200705210221 2.!!pptx
miladyskindiseases-200705210221 2.!!pptxmiladyskindiseases-200705210221 2.!!pptx
miladyskindiseases-200705210221 2.!!pptxCarrieButtitta
 
THE COUNTRY WHO SOLVED THE WORLD_HOW CHINA LAUNCHED THE CIVILIZATION REVOLUTI...
THE COUNTRY WHO SOLVED THE WORLD_HOW CHINA LAUNCHED THE CIVILIZATION REVOLUTI...THE COUNTRY WHO SOLVED THE WORLD_HOW CHINA LAUNCHED THE CIVILIZATION REVOLUTI...
THE COUNTRY WHO SOLVED THE WORLD_HOW CHINA LAUNCHED THE CIVILIZATION REVOLUTI...漢銘 謝
 
DGT @ CTAC 2024 Valencia: Most crucial invest to digitalisation_Sven Zoelle_v...
DGT @ CTAC 2024 Valencia: Most crucial invest to digitalisation_Sven Zoelle_v...DGT @ CTAC 2024 Valencia: Most crucial invest to digitalisation_Sven Zoelle_v...
DGT @ CTAC 2024 Valencia: Most crucial invest to digitalisation_Sven Zoelle_v...Henrik Hanke
 
PAG-UNLAD NG EKONOMIYA na dapat isaalang alang sa pag-aaral.
PAG-UNLAD NG EKONOMIYA na dapat isaalang alang sa pag-aaral.PAG-UNLAD NG EKONOMIYA na dapat isaalang alang sa pag-aaral.
PAG-UNLAD NG EKONOMIYA na dapat isaalang alang sa pag-aaral.KathleenAnnCordero2
 
Engaging Eid Ul Fitr Presentation for Kindergartners.pptx
Engaging Eid Ul Fitr Presentation for Kindergartners.pptxEngaging Eid Ul Fitr Presentation for Kindergartners.pptx
Engaging Eid Ul Fitr Presentation for Kindergartners.pptxAsifArshad8
 

Recently uploaded (20)

The Ten Facts About People With Autism Presentation
The Ten Facts About People With Autism PresentationThe Ten Facts About People With Autism Presentation
The Ten Facts About People With Autism Presentation
 
SaaStr Workshop Wednesday w/ Kyle Norton, Owner.com
SaaStr Workshop Wednesday w/ Kyle Norton, Owner.comSaaStr Workshop Wednesday w/ Kyle Norton, Owner.com
SaaStr Workshop Wednesday w/ Kyle Norton, Owner.com
 
The 3rd Intl. Workshop on NL-based Software Engineering
The 3rd Intl. Workshop on NL-based Software EngineeringThe 3rd Intl. Workshop on NL-based Software Engineering
The 3rd Intl. Workshop on NL-based Software Engineering
 
Quality by design.. ppt for RA (1ST SEM
Quality by design.. ppt for  RA (1ST SEMQuality by design.. ppt for  RA (1ST SEM
Quality by design.. ppt for RA (1ST SEM
 
Dutch Power - 26 maart 2024 - Henk Kras - Circular Plastics
Dutch Power - 26 maart 2024 - Henk Kras - Circular PlasticsDutch Power - 26 maart 2024 - Henk Kras - Circular Plastics
Dutch Power - 26 maart 2024 - Henk Kras - Circular Plastics
 
Simulation-based Testing of Unmanned Aerial Vehicles with Aerialist
Simulation-based Testing of Unmanned Aerial Vehicles with AerialistSimulation-based Testing of Unmanned Aerial Vehicles with Aerialist
Simulation-based Testing of Unmanned Aerial Vehicles with Aerialist
 
Work Remotely with Confluence ACE 2.pptx
Work Remotely with Confluence ACE 2.pptxWork Remotely with Confluence ACE 2.pptx
Work Remotely with Confluence ACE 2.pptx
 
Genshin Impact PPT Template by EaTemp.pptx
Genshin Impact PPT Template by EaTemp.pptxGenshin Impact PPT Template by EaTemp.pptx
Genshin Impact PPT Template by EaTemp.pptx
 
SBFT Tool Competition 2024 -- Python Test Case Generation Track
SBFT Tool Competition 2024 -- Python Test Case Generation TrackSBFT Tool Competition 2024 -- Python Test Case Generation Track
SBFT Tool Competition 2024 -- Python Test Case Generation Track
 
RACHEL-ANN M. TENIBRO PRODUCT RESEARCH PRESENTATION
RACHEL-ANN M. TENIBRO PRODUCT RESEARCH PRESENTATIONRACHEL-ANN M. TENIBRO PRODUCT RESEARCH PRESENTATION
RACHEL-ANN M. TENIBRO PRODUCT RESEARCH PRESENTATION
 
Mathan flower ppt.pptx slide orchids ✨🌸
Mathan flower ppt.pptx slide orchids ✨🌸Mathan flower ppt.pptx slide orchids ✨🌸
Mathan flower ppt.pptx slide orchids ✨🌸
 
Call Girls In Aerocity 🤳 Call Us +919599264170
Call Girls In Aerocity 🤳 Call Us +919599264170Call Girls In Aerocity 🤳 Call Us +919599264170
Call Girls In Aerocity 🤳 Call Us +919599264170
 
PHYSICS PROJECT BY MSC - NANOTECHNOLOGY
PHYSICS PROJECT BY MSC  - NANOTECHNOLOGYPHYSICS PROJECT BY MSC  - NANOTECHNOLOGY
PHYSICS PROJECT BY MSC - NANOTECHNOLOGY
 
Chizaram's Women Tech Makers Deck. .pptx
Chizaram's Women Tech Makers Deck.  .pptxChizaram's Women Tech Makers Deck.  .pptx
Chizaram's Women Tech Makers Deck. .pptx
 
Early Modern Spain. All about this period
Early Modern Spain. All about this periodEarly Modern Spain. All about this period
Early Modern Spain. All about this period
 
miladyskindiseases-200705210221 2.!!pptx
miladyskindiseases-200705210221 2.!!pptxmiladyskindiseases-200705210221 2.!!pptx
miladyskindiseases-200705210221 2.!!pptx
 
THE COUNTRY WHO SOLVED THE WORLD_HOW CHINA LAUNCHED THE CIVILIZATION REVOLUTI...
THE COUNTRY WHO SOLVED THE WORLD_HOW CHINA LAUNCHED THE CIVILIZATION REVOLUTI...THE COUNTRY WHO SOLVED THE WORLD_HOW CHINA LAUNCHED THE CIVILIZATION REVOLUTI...
THE COUNTRY WHO SOLVED THE WORLD_HOW CHINA LAUNCHED THE CIVILIZATION REVOLUTI...
 
DGT @ CTAC 2024 Valencia: Most crucial invest to digitalisation_Sven Zoelle_v...
DGT @ CTAC 2024 Valencia: Most crucial invest to digitalisation_Sven Zoelle_v...DGT @ CTAC 2024 Valencia: Most crucial invest to digitalisation_Sven Zoelle_v...
DGT @ CTAC 2024 Valencia: Most crucial invest to digitalisation_Sven Zoelle_v...
 
PAG-UNLAD NG EKONOMIYA na dapat isaalang alang sa pag-aaral.
PAG-UNLAD NG EKONOMIYA na dapat isaalang alang sa pag-aaral.PAG-UNLAD NG EKONOMIYA na dapat isaalang alang sa pag-aaral.
PAG-UNLAD NG EKONOMIYA na dapat isaalang alang sa pag-aaral.
 
Engaging Eid Ul Fitr Presentation for Kindergartners.pptx
Engaging Eid Ul Fitr Presentation for Kindergartners.pptxEngaging Eid Ul Fitr Presentation for Kindergartners.pptx
Engaging Eid Ul Fitr Presentation for Kindergartners.pptx
 

Dynamic Type Inference for Gradual Hindley–Milner Typing

  • 1. Dynamic Type Inference
 for Gradual Hindley–Milner Typing !1 Yusuke Miyazaki
 Kyoto University
 Japan Taro Sekiyama
 National Institute of Informatics
 Japan Atsushi Igarashi
 Kyoto University
 Japan POPL 2019 / Cascais, Portugal
  • 2. Gradual Typing
 [Siek & Taha ’06] Framework to integrate static and dynamic typing in a single language • Introduces the dynamic type to specify dynamically typed parts in a program • Combines typechecking at compile-time and run-time • Enables gradual code evolution from a dynamically typed program to a statically typed program !2
  • 3. Gradual Typing: Example !3 # let g_avg (x: int) (y: *) = (x + y) / 2
 val g_avg: int → * → int 
 # g_avg 3 5
 4 
 # g_avg "hello" 5
 Compile-time type error:
 parameter “x” expects
 int value 
 
 # g_avg 3 "hello"
 Run-time error:
 operator (+) expects int value Value of any type can be passed
 to a parameter with the dynamic type Whether y has int type or not is checked at run-timeThe dynamic type Whether x has int type or not is checked at compile-time
  • 4. Gradual Typing: Another Example !4 # (λf:*. f 3) (λx:int. x) 3: int * 
 # (λf:*. f 3) (λx:str. x) Run-time error:
 The parameter “x” expects a string value.
 But an integer value 3 is passed to the parameter. λx:int.x is passed to f:* 3 is passed to x:int Type annotation can affect the result of evaluation 3 is passed to x:str Value tagged with int
  • 5. Our Work Proposes the blame calculus λB DTI to give a new semantics of the gradually typed language with type inference and Hindley–Milner polymorphism • Introduces dynamic type inference (DTI) to deal with type variables left undecided by compile-time type inference • Proves properties of the calculus • Soundness and completeness of dynamic type inference • Gradual guarantee of λB DTI and ITGL • Implements an interpreter ymyzk/lambda-dti !5
  • 6. Outline • Introduction • Problem: Undecided Type Variables in ITGL • Background: Implicitly Typed Gradual Language (ITGL) • Problem of Type Variables Undecided by Compile-Time Type Inference • Dynamic Type Inference • Properties • Conclusion !6
  • 7. Implicitly Typed Gradual Language (ITGL)
 [Garcia & Cimini POPL ’15] • Gradually typed language with let-polymorphism • Compile-time type inference algorithm that infers static types for where type annotations are omitted • Static types: types do not contain the dynamic type • Principal type property !7
  • 8. Compile-Time Type Inference of ITGL !8 In: (λf. f 3) (λx. x) Out: (λf:int→int. f 3) (λx:int. x) Result: 3 
 In: (λf:*. f 3) (λx. x) Out: (λf:*. f 3) (λx:α. x) Add *
  • 9. Problem of ITGL !9 In: (λf. f 3) (λx. x) Out: (λf:int→int. f 3) (λx:int. x) Result: 3 
 In: (λf:*. f 3) (λx. x) Out: (λf:*. f 3) (λx:α. x) Result1: 3: int * (if α is replaced with int before evaluation) Result2: Run-time error (otherwise) Left undecided by type inference,
 but cannot be ignored at run-time Add * bool int str int→int int→bool
 int→str bool→int … Generally, it is difficult to find a static type for α at compile-time ! Candidates for α
  • 10. Our Solution !10 Find static types for undecided type variables during evaluation (λf:*. f 3) (λx:α. x) Run-time system will find out α to be int
  • 11. Another Idea: Replacing with the Dynamic Type !11 Replacing undecided type variables with the dynamic type may sounds like a good idea, but we find some problems: • Goes against with the original idea of ITGL that a type variable is a placeholder for a static type • Makes it difficult to detect a type error earlier • Because it can make evaluation successful even when there is no static substitutions that makes evaluation successful We decided not to use this approach "
  • 12. Outline • Introduction • Problem: Undecided Type Variables in ITGL • Dynamic Type Inference • Background: Cast for Run-Time Typecheck • Dynamic Type Inference • Properties • Conclusion !12
  • 13. Cast for Run-Time Typecheck
 [Wadler & Fiedler ESOP ’09] A term for performing run-time typecheck in blame calculi e: U U’ • Cast failure is expressed as a special term blame • The paper deals with blame labels formally • Casts are inserted where run-time checks are required during translation from a gradually typed language to a blame calculus !13 Cast of a term e from type U to U’
  • 14. Examples: Run-Time Typecheck with Casts !14 (λx:*. x + 2) 3 ⇝ (λx:*. (x: * int) + 2) (3: int *) ⟼ (3: int * int) + 2 ⟼ 3 + 2 ⟼ 5
 (λx:*. x + 2) "hello" ⇝ (λx:*. (x: * int) + 2) ("hello": str *) ⟼ ("hello": str * int) + 2 ⟼ blame + 2 ⟼ blame Run-time typecheck:
 cast from int to int succeeds Run-time typecheck:
 cast from str to int fails Value tagged with int Cast inserting translation
  • 15. Another Example: Run-Time Typecheck with Casts !15 (λf:*. f 3) (λx:int. x) ⇝ (λf:*. (f: * *→*) (3: int *))
                  ((λx:int. x): int→int *) 
 ⟼+((λx:int. x) (3: int * int)): int * 
 ⟼ ((λx:int. x) 3): int * ⟼+ 3: int * Run-time typecheck: cast from int to int succeeds
  • 16. Dynamic Type Inference !16 (λf:*. f 3) (λx:α. x) ⇝ (λf:*. (f: * *→*) (3: int *))
                    ((λx:α. x): α→α *) ⟼+((λx:α. x) (3: int * α)): α * 
 
 
 ⟼ ((λx:int. x) 3): int * ⟼+ 3: int * α is instantiated with int DTI: Instantiates α with the type attached to
 the value (int) and proceeds the evaluation Generated substitution:
 [α↦int] Resulted in a value thanks to DTI Left undecided by compile-time inference
  • 17. Simply typed blame calculus + type variables
 (+ let-polymorphism discussed in the paper)
 U ::= * (Dynamic Type)
 | α (Type Variable)
 | int (Base Type)
 | U→U (Function Type) Formalizing DTI: λB DTI !17 e ::= x (Variable)
 | c (Constant)
 | λx:U.e (Abstraction)
 | e1 e2 (Application) | e: U1 U2 (Cast) | blame (Blame)
  • 18. Formalizing DTI: Reduction Relation Two reduction rules for dynamic type inference !18 v: *→* * α  ⟶  v: *→* * α1→α2 [α↦α1→α2] Type substitution obtained by DTI:
 Instantiate α with int then proceed reduction v: int * α  ⟶ v [α↦int] e ⟶ e’S Generate fresh type variables α1 and α2
  • 19. Formalizing DTI: Evaluation Relation The evaluation rule makes sure that a type variable is instantiated at most once 
 
 
 
 
 Example: !19 e ⟶ e’
 E[e] ⟼ S(E[e’])S Apply the type substitution S to the entire term S Evaluation context Reduction of a subterm 3: int * α ⟶ 3
 (λx:α. x) (3: int * α) ⟼ (λx:int. x) 3 [α↦int] [α↦int] e ⟼ e’S
  • 20. Outline • Introduction • Problem: Undecided Type Variables in ITGL • Dynamic Type Inference • Properties • Conclusion !20
  • 21. Properties Prove the following properties of the calculus • Type safety of λB DTI • Soundness and completeness of DTI • Translation from ITGL to λB DTI is type-preserving • Gradual guarantee of λB DTI and ITGL !21
  • 22. Soundness and Completeness of DTI DTI always returns an “appropriate” type substitution if it exists
 
 • Soundness of DTI: • A type substitution yielded by DTI is appropriate • Completeness of DTI: • If an appropriate type substitution exists, DTI returns one !22 By applying it before evaluation,
 the program terminates at a value
  • 23. Soundness of Dynamic Type Inference 1. If a program e results in a value or blame r with DTI, then applying the substitution S obtained by DTI beforehand does not change the result 2. If a program e evaluates to blame with DTI, applying any substitution results in blame !23 Theorem (Soundness): Suppose ⊢ e: U 1. If e ⟼* r then, for any S’ s.t. ftv(S’(S(e))) = ∅, S’(S(e)) ⟼* S’(r)
 for some S’’ 2. If e ⟼* blame then, for any S’, S’(e) ⟼* blame for some S’’ S S S’’ S’’
  • 24. Completeness of Dynamic Type Inference 1. If there is a type substitution S that makes a program result in a value v, then evaluation with DTI also results in a related value v’ 2. If there is a type substitution that makes a program diverge, evaluation with DTI also diverges !24 Theorem (Completeness): Suppose ⊢ e: U 1. If S(e) ⟼* v, then e ⟼* v’ and S’’(v’) = v for some v’, S’, S’’ 2. If S(e) diverges, then e diverges ∅ S’
  • 25. In the Paper and Artifact… • Full definition of the calculus λB DTI • Syntax, type system, operational semantics, translation from ITGL • Extension to let-polymorphism which is non-parametric [Ahmed et al. POPL ’11] • let x = v in e behaves the same as e[x:=v] • Properties of the calculus • Type safety • Soundness and completeness of DTI • Gradual guarantee of λB DTI and ITGL with non-trivial precision relation • Implementation of the calculus !25
  • 26. Related Work • Replacing undecided type variables with the dynamic type
 [Henglein & Rehof FPCA ’95 / Xie et al. ESOP ’18] • Their work: • Provides more permissive semantics • Our work: • Enables earlier bug detection • Consistent with the idea that a type variable in ITGL is a placeholder for a static type !26
  • 27. Conclusion Blame calculus with dynamic type inference and let-polymorphism • Dynamic type inference • Instantiates undecided type variables along with evaluation • Let-Polymorphism • Properties • Soundness and completeness of DTI • Gradual guarantee of λB DTI and ITGL !27