SlideShare a Scribd company logo
1 of 36
Download to read offline
A Plan towards Ruby 3 Types
Yusuke Endoh
EuRuKo '19 - 22rd Jun. 2019
1
Yusuke Endoh (@mametter)
•A full-time Ruby committer at Cookpad Inc.
•with @ko1 (Ruby's speed freak)
2
PR: Cookpad Inc.
•Mission: "Make Everyday Cooking Fun"
•cookpad.com: A recipe sharing service
•Monthly Average Users: 96 million
3
PR: Cookpad Inc.
We're hiring!
HQ is in Bristol, UK
Aim to be No.1 in 100 countries
(Now in 29 Languages and 72 Countries)
4
Yusuke Endoh (@mametter)
•A full-time Ruby committer at Cookpad Inc.
• with @ko1 (Ruby's speed freak)
•My contributions:
• Keyword arguments since Ruby 2.0
• Test coverage measurement
• Optcarrot: a benckmark for Ruby 3x3
• Recent: Beginless/endless range: (1..), (..1)
5
Ruby 3 goals
•Performance (Ruby 3x3)
•Concurrency
•Static analysis  My mission
6
This Talk
•Matz's plan for Ruby 3 Types
•An introduction of Type Profiler
7
Matz's plan for Ruby 3 Types
8
Ruby 3 Types'
•Objective: Find a possible type bug
•To help development
•Requirement: Keep Ruby's experience
•Type annotation is optional
9
# s is String
def foo(s)
...
end
type
annotation
Items for Ruby 3 Static Analysis
1. Standard type signature format
2. Level-1 type checker without signatures
+ Type inference to suggest type signature for
non-annotated Ruby code
3. Level-2 type checker with signatures
10
1. Type Signature Format (.rbi)
•What types a method accepts/returns
class Array[A]
include Enumerable
def []: (Integer) -> A?
def each: { (A)->void } -> self
...
end
mix-in
generics
option type
interface
any type
Proposal:
ruby-signature
11
2. Type Checker without Signature
•Finds possible NoMethodError/TypeErrors
•Work without application's type signature
•May report false positive
def foo(s)
s.gsuub!(//, "")
s + 42
end
foo("foo")
NoMethodError?
TypeError?
Proposals:
• Type Profiler
• mruby's JIT compiler
12
2'. A kind of Type Inference
•Suggest a prototype of type signature for
non-annotated Ruby code
• Possible approach
• virtually simulating
the code in type-level
def foo(s)
s.to_s
end
foo("foo")
foo(42)
(String | Integer)
-> String ?Proposals:
• Type Profiler
• mruby's JIT compiler
13
3. Type Checker with Signatures
•Conservatively find NoMethod/TypeErrors
•Verifies that the code complies with signature
• Possible implementation
• Gradual type checking
• Tool-defined annotation def foo(s)
s.gsuub!(//, "")
s + 42
end
TypeError!
def foo:
(String) -> Integer
Proposals:
• Steep
• Sorbet
• RDL
NoMethodError!
14
Items for Ruby 3 Static Analysis
Library code
type signature
Sorbet
Steep
RDL
Type error
warnings
Application code
Type Profiler
(mruby JIT)
Type error
warnings
type signature
15
Use Cases
• I want to find a possible bug
• Write a code
→Use 2 (level-1 type checker without signature)
• I want to verify my code
• Write a code
→ Use 2' (type inference)
→ Use 3 (level-2 type checker with signature)
• I want to write a code in "type-driven" style
• Hand-write a signature and then a code
→ Use 3 (level-2 type checker with signature)
16
Current Development Status
•ruby-signature: discussed by tool developers
• Any comment is welcome:
https://github.com/ruby/ruby-signature
•Type-Profiler: Very experimental...
•Steep: Trial use in Sider, Inc.
• Good for duck typing / requires many annotations
•Sorbet: Trial use in some companies
• Gradually applicable / less support for duck typing
17
What Ruby 3 will Ship
•Matz wants to bundle "type signatures"
•A parser library for the format
•Type signature files for stdlibs
•RubyGems with type signature support
•No plan to bundle the checkers
•They will be released as external gems
18
An introduction of Type Profiler
19
Type Profiler for Checking
•Finds NoMethodError, TypeError, etc.
def foo(n)
if n < 10
n.timees {|x|
}
end
end
foo(42)
Type
Profiler
t.rb:3: [error] undefined method:
Integer#timees
Typo
20
Type Profiler for Inference
•Generates a prototype of type definition
def foo(n)
n.to_s
end
foo(42)
Type
Profiler
Object#foo ::
(Integer) -> String
21
Demo: Overloading
def my_to_s(x)
x.to_s
end
my_to_s(42)
my_to_s("STR")
my_to_s(:sym)
Type
Profiler
Object#my_to_s :: (Integer) -> String
Object#my_to_s :: (String) -> String
Object#my_to_s :: (Symbol) -> String
22
Demo: Recursive Method
def fib(n)
if n > 1
fib(n-1) + fib(n-2)
else
n
end
end
fib(10000)
Type
Profiler
Object#fib ::
(Integer) -> Integer
23
How Type Profiler Does
•Runs a Ruby code in "type-level"
Normal interpreter
def foo(n)
n.to_s
end
foo(42)
Calls w/
42
Returns
"42"
Type profiler
def foo(n)
n.to_s
end
foo(42)
Calls w/
Integer
Returns
String
Object#foo ::
(Integer) -> String
24
Type Profiler and Branch
•"Forks" the execution
def foo(n)
if n < 10
n
else
"error"
end
end
foo(42)
Fork!
Now here
We cannot tell
if n<10 or not
Returns
Integer
Returns
String
Object#foo ::
(Integer) ->
(Integer | String)
25
The Problem: State Explosion
•Analysis time (at April, at RubyKaigi)
•Analyzing the code of Type Profiler: 10 min.
•Analyzing optcarrot: 3 min.
a=b=c=d=e=nil
a = 42 if n < 10
b = 42 if n < 10
c = 42 if n < 10
d = 42 if n < 10
e = 42 if n < 10
Fork!
Fork!
Fork!
Fork!
Fork!
2
4
8
16
32
State numbers
26
The Progress since RubyKaigi
•Revamped the analysis algorithm
•"state merging" technique called in
symbolic execution
27
Basic Analysis Algorithm
•"Environment": A map from variable to type
•Example:
•TP runs each instruction iteratively
•by propagating the environments
•until no environments
are updated
28
x = 1
x = 1
x = 1
x y
{ int, str } { int }
x y
{ nil } { int }
∅ ∅
x y
{ nil } { int }
{ int } { int }
Example of Analysis
1: def foo(a)
2: if a < 10
3: b = 42
4: else
5: b = "str"
6: end
7: c = b
8: c
9: end
10:
11: ret = foo(42)
Line# a b c
1 ∅ ∅ ∅
2 ∅ ∅ ∅
3 ∅ ∅ ∅
4 - - -
5 ∅ ∅ ∅
6 -
7 ∅ ∅ ∅
8 ∅ ∅ ∅
Line# a b c
1 { Int } ∅ ∅
2 ∅ ∅ ∅
3 ∅ ∅ ∅
4 - - -
5 ∅ ∅ ∅
6 -
7 ∅ ∅ ∅
8 ∅ ∅ ∅
Line# a b c
1 { Int } ∅ ∅
2 { Int } ∅ ∅
3 ∅ ∅ ∅
4 - - -
5 ∅ ∅ ∅
6 -
7 ∅ ∅ ∅
8 ∅ ∅ ∅
Line# a b c
1 { Int } ∅ ∅
2 { Int } ∅ ∅
3 { Int } ∅ ∅
4 - - -
5 { Int } ∅ ∅
6 -
7 ∅ ∅ ∅
8 ∅ ∅ ∅
Line# a b c
1 { Int } ∅ ∅
2 { Int } ∅ ∅
3 { Int } ∅ ∅
4 - - -
5 { Int } ∅ ∅
6 -
7 { Int } { Int } ∅
8 ∅ ∅ ∅
Line# a b c
1 { Int } ∅ ∅
2 { Int } ∅ ∅
3 { Int } ∅ ∅
4 - - -
5 { Int } ∅ ∅
6 -
7 { Int } { Int } ∅
8 ∅ ∅ ∅
Line# a b c
1 { Int } ∅ ∅
2 { Int } ∅ ∅
3 { Int } ∅ ∅
4 - - -
5 { Int } ∅ ∅
6 -
7 { Int } { Int, Str } ∅
8 ∅ ∅ ∅
Line# a b c
1 { Int } ∅ ∅
2 { Int } ∅ ∅
3 { Int } ∅ ∅
4 - - -
5 { Int } ∅ ∅
6 -
7 { Int } { Int, Str } ∅
8 ∅ ∅ ∅
Line# a b c
1 { Int } ∅ ∅
2 { Int } ∅ ∅
3 { Int } ∅ ∅
4 - - -
5 { Int } ∅ ∅
6 -
7 { Int } { Int, Str } ∅
8 { Int } { Int, Str } { Int, Str }
Object#foo :: (Int) -> (Int|Str) 29
Experiment result
•Type Profiler: 10 min. ➔ 2.5 sec.
•optcarrot: 3 min. ➔ 6 sec.
30
Analysis Time of Type Profiler
0
200
400
600
800
self-profiling optcarrot
seconds
old new
31
Other Problems of Type Profiler
•It requires a test
•False positive and false suggestion
•Some Ruby features cannot be handled
• e.g., Object#send, singleton classes
# b: Integer or String
c = b
# c: Integer or String
# We lose the correspond between b and c
b + c # "May call Integer#+(String)!"
32
Development Progress: Done
•Design the basic analysis algorithm
•Based on abstract interpretation
•Support basic language features
•Variables, methods, user-defined classes, etc.
•Blocks and arrays (maybe)
•A limited set of built-in classes
33
Development Progress: To Do
•Support more features
•More built-in classes (Hash!)
•Complex arguments (optional/rest/keyword)
•Exception
•Modules
•Input/output type signature format
•A lot of improvements for practical use...
34
Acknowledgement
•Hideki Miura
•Matz, Akr, Ko1
•PPL paper co-authors
• Soutaro Matsumoto
• Katsuhiro Ueno
• Eijiro Sumii
•Stripe team & Jeff Foster
•And many people
35
Conclusion
•Explained Matz's plan for Ruby 3 static analysis
•Introduced Type Profiler
• A type analyzer for Ruby 3
applicable to a non-annotated Ruby code
• Based on abstract interpretation technique
• Little change for Ruby programming experience
•Any comments and/or contribution are welcome!
• https://github.com/mame/ruby-type-profiler
36

More Related Content

What's hot

The D Programming Language - Why I love it!
The D Programming Language - Why I love it!The D Programming Language - Why I love it!
The D Programming Language - Why I love it!
ryutenchi
 

What's hot (20)

Jaoo irony
Jaoo ironyJaoo irony
Jaoo irony
 
Introduction to Groovy
Introduction to GroovyIntroduction to Groovy
Introduction to Groovy
 
PHP7
PHP7PHP7
PHP7
 
IronRuby for the .NET Developer
IronRuby for the .NET DeveloperIronRuby for the .NET Developer
IronRuby for the .NET Developer
 
IronRuby for the Rubyist
IronRuby for the RubyistIronRuby for the Rubyist
IronRuby for the Rubyist
 
Ruxmon.2013-08.-.CodeBro!
Ruxmon.2013-08.-.CodeBro!Ruxmon.2013-08.-.CodeBro!
Ruxmon.2013-08.-.CodeBro!
 
IronRuby And The DLR
IronRuby And The DLRIronRuby And The DLR
IronRuby And The DLR
 
Flutter 2
Flutter 2Flutter 2
Flutter 2
 
Not Your Fathers C - C Application Development In 2016
Not Your Fathers C - C Application Development In 2016Not Your Fathers C - C Application Development In 2016
Not Your Fathers C - C Application Development In 2016
 
D programming language
D programming languageD programming language
D programming language
 
The D Programming Language - Why I love it!
The D Programming Language - Why I love it!The D Programming Language - Why I love it!
The D Programming Language - Why I love it!
 
Batch import of large RDF datasets into Semantic MediaWiki
Batch import of large RDF datasets into Semantic MediaWikiBatch import of large RDF datasets into Semantic MediaWiki
Batch import of large RDF datasets into Semantic MediaWiki
 
Intro to Java for C++ Developers
Intro to Java for C++ DevelopersIntro to Java for C++ Developers
Intro to Java for C++ Developers
 
Kotlin from-scratch
Kotlin from-scratchKotlin from-scratch
Kotlin from-scratch
 
Building .NET Core tools using the Roslyn API by Arthur Tabatchnic at .Net fo...
Building .NET Core tools using the Roslyn API by Arthur Tabatchnic at .Net fo...Building .NET Core tools using the Roslyn API by Arthur Tabatchnic at .Net fo...
Building .NET Core tools using the Roslyn API by Arthur Tabatchnic at .Net fo...
 
Golang 101
Golang 101Golang 101
Golang 101
 
What's new in Perl 5.12?
What's new in Perl 5.12?What's new in Perl 5.12?
What's new in Perl 5.12?
 
Intro for RoR
Intro for RoRIntro for RoR
Intro for RoR
 
Using Aspects for Language Portability (SCAM 2010)
Using Aspects for Language Portability (SCAM 2010)Using Aspects for Language Portability (SCAM 2010)
Using Aspects for Language Portability (SCAM 2010)
 
C++ to java
C++ to javaC++ to java
C++ to java
 

Similar to A Plan towards Ruby 3 Types

Optimizing Set-Similarity Join and Search with Different Prefix Schemes
Optimizing Set-Similarity Join and Search with Different Prefix SchemesOptimizing Set-Similarity Join and Search with Different Prefix Schemes
Optimizing Set-Similarity Join and Search with Different Prefix Schemes
HPCC Systems
 
Code Analysis-run time error prediction
Code Analysis-run time error predictionCode Analysis-run time error prediction
Code Analysis-run time error prediction
NIKHIL NAWATHE
 
Python Programming unit5 (1).pdf
Python Programming unit5 (1).pdfPython Programming unit5 (1).pdf
Python Programming unit5 (1).pdf
jamvantsolanki
 
Coq to Rubyによる証明駆動開発@名古屋ruby会議02
Coq to Rubyによる証明駆動開発@名古屋ruby会議02Coq to Rubyによる証明駆動開発@名古屋ruby会議02
Coq to Rubyによる証明駆動開発@名古屋ruby会議02
Hiroki Mizuno
 
System Programming Unit III
System Programming Unit IIISystem Programming Unit III
System Programming Unit III
Manoj Patil
 
Set Similarity Search using a Distributed Prefix Tree Index
Set Similarity Search using a Distributed Prefix Tree IndexSet Similarity Search using a Distributed Prefix Tree Index
Set Similarity Search using a Distributed Prefix Tree Index
HPCC Systems
 
Unit iii-111206004501-phpapp02
Unit iii-111206004501-phpapp02Unit iii-111206004501-phpapp02
Unit iii-111206004501-phpapp02
riddhi viradiya
 

Similar to A Plan towards Ruby 3 Types (20)

TypeProf for IDE: Enrich Development Experience without Annotations
TypeProf for IDE: Enrich Development Experience without AnnotationsTypeProf for IDE: Enrich Development Experience without Annotations
TypeProf for IDE: Enrich Development Experience without Annotations
 
Functions, List and String methods
Functions, List and String methodsFunctions, List and String methods
Functions, List and String methods
 
Optimizing Set-Similarity Join and Search with Different Prefix Schemes
Optimizing Set-Similarity Join and Search with Different Prefix SchemesOptimizing Set-Similarity Join and Search with Different Prefix Schemes
Optimizing Set-Similarity Join and Search with Different Prefix Schemes
 
Code Analysis-run time error prediction
Code Analysis-run time error predictionCode Analysis-run time error prediction
Code Analysis-run time error prediction
 
Scala implicits
Scala implicitsScala implicits
Scala implicits
 
Python Programming unit5 (1).pdf
Python Programming unit5 (1).pdfPython Programming unit5 (1).pdf
Python Programming unit5 (1).pdf
 
Fuzzing - Part 1
Fuzzing - Part 1Fuzzing - Part 1
Fuzzing - Part 1
 
Python by ravi rajput hcon groups
Python by ravi rajput hcon groupsPython by ravi rajput hcon groups
Python by ravi rajput hcon groups
 
Modern C++
Modern C++Modern C++
Modern C++
 
Coq to Rubyによる証明駆動開発@名古屋ruby会議02
Coq to Rubyによる証明駆動開発@名古屋ruby会議02Coq to Rubyによる証明駆動開発@名古屋ruby会議02
Coq to Rubyによる証明駆動開発@名古屋ruby会議02
 
Lua pitfalls
Lua pitfallsLua pitfalls
Lua pitfalls
 
Python course
Python coursePython course
Python course
 
Javascript
JavascriptJavascript
Javascript
 
Uni texus austin
Uni texus austinUni texus austin
Uni texus austin
 
Machine Learning on Code - SF meetup
Machine Learning on Code - SF meetupMachine Learning on Code - SF meetup
Machine Learning on Code - SF meetup
 
Asynchronous IO in Rust - Enrico Risa - Codemotion Rome 2017
Asynchronous IO in Rust - Enrico Risa - Codemotion Rome 2017Asynchronous IO in Rust - Enrico Risa - Codemotion Rome 2017
Asynchronous IO in Rust - Enrico Risa - Codemotion Rome 2017
 
System Programming Unit III
System Programming Unit IIISystem Programming Unit III
System Programming Unit III
 
App secforum2014 andrivet-cplusplus11-metaprogramming_applied_to_software_obf...
App secforum2014 andrivet-cplusplus11-metaprogramming_applied_to_software_obf...App secforum2014 andrivet-cplusplus11-metaprogramming_applied_to_software_obf...
App secforum2014 andrivet-cplusplus11-metaprogramming_applied_to_software_obf...
 
Set Similarity Search using a Distributed Prefix Tree Index
Set Similarity Search using a Distributed Prefix Tree IndexSet Similarity Search using a Distributed Prefix Tree Index
Set Similarity Search using a Distributed Prefix Tree Index
 
Unit iii-111206004501-phpapp02
Unit iii-111206004501-phpapp02Unit iii-111206004501-phpapp02
Unit iii-111206004501-phpapp02
 

More from mametter

クックパッド春の超絶技巧パンまつり 超絶技巧プログラミング編 資料
クックパッド春の超絶技巧パンまつり 超絶技巧プログラミング編 資料クックパッド春の超絶技巧パンまつり 超絶技巧プログラミング編 資料
クックパッド春の超絶技巧パンまつり 超絶技巧プログラミング編 資料
mametter
 

More from mametter (20)

error_highlight: User-friendly Error Diagnostics
error_highlight: User-friendly Error Diagnosticserror_highlight: User-friendly Error Diagnostics
error_highlight: User-friendly Error Diagnostics
 
TRICK 2022 Results
TRICK 2022 ResultsTRICK 2022 Results
TRICK 2022 Results
 
クックパッド春の超絶技巧パンまつり 超絶技巧プログラミング編 資料
クックパッド春の超絶技巧パンまつり 超絶技巧プログラミング編 資料クックパッド春の超絶技巧パンまつり 超絶技巧プログラミング編 資料
クックパッド春の超絶技巧パンまつり 超絶技巧プログラミング編 資料
 
emruby: ブラウザで動くRuby
emruby: ブラウザで動くRubyemruby: ブラウザで動くRuby
emruby: ブラウザで動くRuby
 
型プロファイラ:抽象解釈に基づくRuby 3の静的解析
型プロファイラ:抽象解釈に基づくRuby 3の静的解析型プロファイラ:抽象解釈に基づくRuby 3の静的解析
型プロファイラ:抽象解釈に基づくRuby 3の静的解析
 
Ruby 3の型推論やってます
Ruby 3の型推論やってますRuby 3の型推論やってます
Ruby 3の型推論やってます
 
マニアックなRuby 2.7新機能紹介
マニアックなRuby 2.7新機能紹介マニアックなRuby 2.7新機能紹介
マニアックなRuby 2.7新機能紹介
 
Ruby 3 の型解析に向けた計画
Ruby 3 の型解析に向けた計画Ruby 3 の型解析に向けた計画
Ruby 3 の型解析に向けた計画
 
本番環境で使える実行コード記録機能
本番環境で使える実行コード記録機能本番環境で使える実行コード記録機能
本番環境で使える実行コード記録機能
 
Transcendental Programming in Ruby
Transcendental Programming in RubyTranscendental Programming in Ruby
Transcendental Programming in Ruby
 
Ruby 3のキーワード引数について考える
Ruby 3のキーワード引数について考えるRuby 3のキーワード引数について考える
Ruby 3のキーワード引数について考える
 
TRICK 2018 results
TRICK 2018 resultsTRICK 2018 results
TRICK 2018 results
 
Esoteric, Obfuscated, Artistic Programming in Ruby
Esoteric, Obfuscated, Artistic Programming in RubyEsoteric, Obfuscated, Artistic Programming in Ruby
Esoteric, Obfuscated, Artistic Programming in Ruby
 
Cookpad Spring 1day internship 2018 超絶技巧プログラミングコース資料
Cookpad Spring 1day internship 2018 超絶技巧プログラミングコース資料Cookpad Spring 1day internship 2018 超絶技巧プログラミングコース資料
Cookpad Spring 1day internship 2018 超絶技巧プログラミングコース資料
 
Esoteric, Obfuscated, Artistic Programming in Ruby
Esoteric, Obfuscated, Artistic Programming in RubyEsoteric, Obfuscated, Artistic Programming in Ruby
Esoteric, Obfuscated, Artistic Programming in Ruby
 
An introduction and future of Ruby coverage library
An introduction and future of Ruby coverage libraryAn introduction and future of Ruby coverage library
An introduction and future of Ruby coverage library
 
Ruby でつくる型付き Ruby
Ruby でつくる型付き RubyRuby でつくる型付き Ruby
Ruby でつくる型付き Ruby
 
Ruby で高速なプログラムを書く
Ruby で高速なプログラムを書くRuby で高速なプログラムを書く
Ruby で高速なプログラムを書く
 
Optcarrot: A Pure-Ruby NES Emulator
Optcarrot: A Pure-Ruby NES EmulatorOptcarrot: A Pure-Ruby NES Emulator
Optcarrot: A Pure-Ruby NES Emulator
 
TRICK2015 results
TRICK2015 resultsTRICK2015 results
TRICK2015 results
 

Recently uploaded

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Recently uploaded (20)

Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 

A Plan towards Ruby 3 Types

  • 1. A Plan towards Ruby 3 Types Yusuke Endoh EuRuKo '19 - 22rd Jun. 2019 1
  • 2. Yusuke Endoh (@mametter) •A full-time Ruby committer at Cookpad Inc. •with @ko1 (Ruby's speed freak) 2
  • 3. PR: Cookpad Inc. •Mission: "Make Everyday Cooking Fun" •cookpad.com: A recipe sharing service •Monthly Average Users: 96 million 3
  • 4. PR: Cookpad Inc. We're hiring! HQ is in Bristol, UK Aim to be No.1 in 100 countries (Now in 29 Languages and 72 Countries) 4
  • 5. Yusuke Endoh (@mametter) •A full-time Ruby committer at Cookpad Inc. • with @ko1 (Ruby's speed freak) •My contributions: • Keyword arguments since Ruby 2.0 • Test coverage measurement • Optcarrot: a benckmark for Ruby 3x3 • Recent: Beginless/endless range: (1..), (..1) 5
  • 6. Ruby 3 goals •Performance (Ruby 3x3) •Concurrency •Static analysis  My mission 6
  • 7. This Talk •Matz's plan for Ruby 3 Types •An introduction of Type Profiler 7
  • 8. Matz's plan for Ruby 3 Types 8
  • 9. Ruby 3 Types' •Objective: Find a possible type bug •To help development •Requirement: Keep Ruby's experience •Type annotation is optional 9 # s is String def foo(s) ... end type annotation
  • 10. Items for Ruby 3 Static Analysis 1. Standard type signature format 2. Level-1 type checker without signatures + Type inference to suggest type signature for non-annotated Ruby code 3. Level-2 type checker with signatures 10
  • 11. 1. Type Signature Format (.rbi) •What types a method accepts/returns class Array[A] include Enumerable def []: (Integer) -> A? def each: { (A)->void } -> self ... end mix-in generics option type interface any type Proposal: ruby-signature 11
  • 12. 2. Type Checker without Signature •Finds possible NoMethodError/TypeErrors •Work without application's type signature •May report false positive def foo(s) s.gsuub!(//, "") s + 42 end foo("foo") NoMethodError? TypeError? Proposals: • Type Profiler • mruby's JIT compiler 12
  • 13. 2'. A kind of Type Inference •Suggest a prototype of type signature for non-annotated Ruby code • Possible approach • virtually simulating the code in type-level def foo(s) s.to_s end foo("foo") foo(42) (String | Integer) -> String ?Proposals: • Type Profiler • mruby's JIT compiler 13
  • 14. 3. Type Checker with Signatures •Conservatively find NoMethod/TypeErrors •Verifies that the code complies with signature • Possible implementation • Gradual type checking • Tool-defined annotation def foo(s) s.gsuub!(//, "") s + 42 end TypeError! def foo: (String) -> Integer Proposals: • Steep • Sorbet • RDL NoMethodError! 14
  • 15. Items for Ruby 3 Static Analysis Library code type signature Sorbet Steep RDL Type error warnings Application code Type Profiler (mruby JIT) Type error warnings type signature 15
  • 16. Use Cases • I want to find a possible bug • Write a code →Use 2 (level-1 type checker without signature) • I want to verify my code • Write a code → Use 2' (type inference) → Use 3 (level-2 type checker with signature) • I want to write a code in "type-driven" style • Hand-write a signature and then a code → Use 3 (level-2 type checker with signature) 16
  • 17. Current Development Status •ruby-signature: discussed by tool developers • Any comment is welcome: https://github.com/ruby/ruby-signature •Type-Profiler: Very experimental... •Steep: Trial use in Sider, Inc. • Good for duck typing / requires many annotations •Sorbet: Trial use in some companies • Gradually applicable / less support for duck typing 17
  • 18. What Ruby 3 will Ship •Matz wants to bundle "type signatures" •A parser library for the format •Type signature files for stdlibs •RubyGems with type signature support •No plan to bundle the checkers •They will be released as external gems 18
  • 19. An introduction of Type Profiler 19
  • 20. Type Profiler for Checking •Finds NoMethodError, TypeError, etc. def foo(n) if n < 10 n.timees {|x| } end end foo(42) Type Profiler t.rb:3: [error] undefined method: Integer#timees Typo 20
  • 21. Type Profiler for Inference •Generates a prototype of type definition def foo(n) n.to_s end foo(42) Type Profiler Object#foo :: (Integer) -> String 21
  • 22. Demo: Overloading def my_to_s(x) x.to_s end my_to_s(42) my_to_s("STR") my_to_s(:sym) Type Profiler Object#my_to_s :: (Integer) -> String Object#my_to_s :: (String) -> String Object#my_to_s :: (Symbol) -> String 22
  • 23. Demo: Recursive Method def fib(n) if n > 1 fib(n-1) + fib(n-2) else n end end fib(10000) Type Profiler Object#fib :: (Integer) -> Integer 23
  • 24. How Type Profiler Does •Runs a Ruby code in "type-level" Normal interpreter def foo(n) n.to_s end foo(42) Calls w/ 42 Returns "42" Type profiler def foo(n) n.to_s end foo(42) Calls w/ Integer Returns String Object#foo :: (Integer) -> String 24
  • 25. Type Profiler and Branch •"Forks" the execution def foo(n) if n < 10 n else "error" end end foo(42) Fork! Now here We cannot tell if n<10 or not Returns Integer Returns String Object#foo :: (Integer) -> (Integer | String) 25
  • 26. The Problem: State Explosion •Analysis time (at April, at RubyKaigi) •Analyzing the code of Type Profiler: 10 min. •Analyzing optcarrot: 3 min. a=b=c=d=e=nil a = 42 if n < 10 b = 42 if n < 10 c = 42 if n < 10 d = 42 if n < 10 e = 42 if n < 10 Fork! Fork! Fork! Fork! Fork! 2 4 8 16 32 State numbers 26
  • 27. The Progress since RubyKaigi •Revamped the analysis algorithm •"state merging" technique called in symbolic execution 27
  • 28. Basic Analysis Algorithm •"Environment": A map from variable to type •Example: •TP runs each instruction iteratively •by propagating the environments •until no environments are updated 28 x = 1 x = 1 x = 1 x y { int, str } { int } x y { nil } { int } ∅ ∅ x y { nil } { int } { int } { int }
  • 29. Example of Analysis 1: def foo(a) 2: if a < 10 3: b = 42 4: else 5: b = "str" 6: end 7: c = b 8: c 9: end 10: 11: ret = foo(42) Line# a b c 1 ∅ ∅ ∅ 2 ∅ ∅ ∅ 3 ∅ ∅ ∅ 4 - - - 5 ∅ ∅ ∅ 6 - 7 ∅ ∅ ∅ 8 ∅ ∅ ∅ Line# a b c 1 { Int } ∅ ∅ 2 ∅ ∅ ∅ 3 ∅ ∅ ∅ 4 - - - 5 ∅ ∅ ∅ 6 - 7 ∅ ∅ ∅ 8 ∅ ∅ ∅ Line# a b c 1 { Int } ∅ ∅ 2 { Int } ∅ ∅ 3 ∅ ∅ ∅ 4 - - - 5 ∅ ∅ ∅ 6 - 7 ∅ ∅ ∅ 8 ∅ ∅ ∅ Line# a b c 1 { Int } ∅ ∅ 2 { Int } ∅ ∅ 3 { Int } ∅ ∅ 4 - - - 5 { Int } ∅ ∅ 6 - 7 ∅ ∅ ∅ 8 ∅ ∅ ∅ Line# a b c 1 { Int } ∅ ∅ 2 { Int } ∅ ∅ 3 { Int } ∅ ∅ 4 - - - 5 { Int } ∅ ∅ 6 - 7 { Int } { Int } ∅ 8 ∅ ∅ ∅ Line# a b c 1 { Int } ∅ ∅ 2 { Int } ∅ ∅ 3 { Int } ∅ ∅ 4 - - - 5 { Int } ∅ ∅ 6 - 7 { Int } { Int } ∅ 8 ∅ ∅ ∅ Line# a b c 1 { Int } ∅ ∅ 2 { Int } ∅ ∅ 3 { Int } ∅ ∅ 4 - - - 5 { Int } ∅ ∅ 6 - 7 { Int } { Int, Str } ∅ 8 ∅ ∅ ∅ Line# a b c 1 { Int } ∅ ∅ 2 { Int } ∅ ∅ 3 { Int } ∅ ∅ 4 - - - 5 { Int } ∅ ∅ 6 - 7 { Int } { Int, Str } ∅ 8 ∅ ∅ ∅ Line# a b c 1 { Int } ∅ ∅ 2 { Int } ∅ ∅ 3 { Int } ∅ ∅ 4 - - - 5 { Int } ∅ ∅ 6 - 7 { Int } { Int, Str } ∅ 8 { Int } { Int, Str } { Int, Str } Object#foo :: (Int) -> (Int|Str) 29
  • 30. Experiment result •Type Profiler: 10 min. ➔ 2.5 sec. •optcarrot: 3 min. ➔ 6 sec. 30
  • 31. Analysis Time of Type Profiler 0 200 400 600 800 self-profiling optcarrot seconds old new 31
  • 32. Other Problems of Type Profiler •It requires a test •False positive and false suggestion •Some Ruby features cannot be handled • e.g., Object#send, singleton classes # b: Integer or String c = b # c: Integer or String # We lose the correspond between b and c b + c # "May call Integer#+(String)!" 32
  • 33. Development Progress: Done •Design the basic analysis algorithm •Based on abstract interpretation •Support basic language features •Variables, methods, user-defined classes, etc. •Blocks and arrays (maybe) •A limited set of built-in classes 33
  • 34. Development Progress: To Do •Support more features •More built-in classes (Hash!) •Complex arguments (optional/rest/keyword) •Exception •Modules •Input/output type signature format •A lot of improvements for practical use... 34
  • 35. Acknowledgement •Hideki Miura •Matz, Akr, Ko1 •PPL paper co-authors • Soutaro Matsumoto • Katsuhiro Ueno • Eijiro Sumii •Stripe team & Jeff Foster •And many people 35
  • 36. Conclusion •Explained Matz's plan for Ruby 3 static analysis •Introduced Type Profiler • A type analyzer for Ruby 3 applicable to a non-annotated Ruby code • Based on abstract interpretation technique • Little change for Ruby programming experience •Any comments and/or contribution are welcome! • https://github.com/mame/ruby-type-profiler 36