SlideShare a Scribd company logo
1 of 40
Download to read offline
A Static Type Analyzer of Untyped
Ruby Code for Ruby 3
Yusuke Endoh
RubyConf 2019
19th Nov. 2019
1
Yusuke Endoh (@mametter)
• A Ruby committer:
• Keyword argument design and implementation (2.0)
• Optcarrot: a benchmark for Ruby 3x3
• Ruby 2.0 release manager
• Working at Cookpad Inc. w/ @ko1
2
PR: Cookpad Inc.
• Mission: "Make Everyday Cooking Fun"
• cookpad.com: A recipe sharing service
• Monthly Average Users: 93 million
3
PR: Cookpad Inc.
4
We're hiring!
HQ is in Bristol, UK
Aim to be No.1 in 100 countries
(Now in 30 Languages and 73 Countries)
This Talk: Types in Ruby 3
• Matz's plan for Types in Ruby 3
Multiple type checkers in one blueprint
• Ruby Signature
The standard type signature format for stdlib and gems
• Type Profiler
A type analysis for non-annotated Ruby code
5
This Talk: Types in Ruby 3
•➔ Matz's plan for Ruby 3 Types 
• Ruby Signature
• Type Profiler
6
The Objective of Ruby 3 types
Point out possible bugs without execution
• To improve development experience
(In other words, a few wrong alerts are acceptable)
7
def increment(n)
n.timees { }
n + "STRING"
end
increment(42)
NoMethodError?
TypeError?
Question 🙋
Do you want to write a type annotation?
8
extend T::Sig
sig {params(n:Integer).returns(Integer)}
def increment(n)
n + 1
end
increment(42)
type annotation
(in Sorbet style)
source code
Thank you. Just as I expected
• In Ruby 3, you can write annotations if you like
• You will gain relatively strong type checking
• Moreover, in Ruby 3,
• You don't have to write annotations manually
• You can even check the code with no annotations!
(if my project succeeds ☺)
9
Thank you. That was unexpected
• In Ruby 3, you can write annotations if you like
• You will gain relatively strong type checking
• Moreover, in Ruby 3,
• You don't have to write annotations manually
• You can even check the code with no annotations!
(if my project succeeds ☺)
10
Ruby 3 will have three items
1. Ruby Signature (RBS) language
2. Type inference for non-annotated code
(Type Profiler)
3. Type checking for annotated code
(Sorbet, RDL, Steep, etc.)
I explain 1 and 2 in this talk...
11
This Talk: Ruby 3 types
• Matz's plan for Ruby 3 Types
•➔Ruby Signature
• What it is / Examples / What Ruby 3 will ship
• Type Profiler
12
Soutaro Matsumoto (@soutaro)
• Leads the design and implementation of RBS
• https://github.com/ruby/ruby-signature
• Develops own static type checker "Steep"
• [Correction] No session about Steep this year!
• Working for Square
13
1. Ruby signature language (RBS)
• The standard language to describe types of Ruby programs
• Different syntax: to keep Ruby code unannotated
• Ruby3 will ship with signatures for stdlib and a library for RBS
14
class Inc
def increment(n)
n + 1
end
end
inc.rbs
inc.rb
class Inc
def increment: (Integer) -> Integer
end
separated
files
15
class Array[T]
def []: (Integer) -> T
| (Integer, Integer) -> Array[T]
def first: () -> T?
def each: () { (T) -> void } -> Array[T]
include Enumerable[T, Array[T]]
end
Generics
Optional Types
Overloading
Block
Mixin
interface _Duck
def quack: () -> void
end
Duck typing
Using RBS
• For type-checking
• Static type checking needs signatures of libraries
• Steep uses RBS to define the signature of your Ruby
applications
• For documentation
• RBS explains the API of a gem
16
Ship your gems with RBS
• Scaffold from Ruby code / Sorbet RBI
• Generate using Type Profiler
• [WIP] A tool to test RBS definitions by dynamic type
checking
17
$ rbs scaffold rb # from unannotated Ruby code
$ rbs scaffold rbi # from Sorbet annotated code
This Talk: Ruby 3 types
• Matz's plan for Ruby 3 Types
• Ruby Signature
•➔Type Profiler
• Demo
• Approach
• Problems
18
Type Profiler
A kind of "type inference" for non-annotated code
def increment(n)
n + 1
end
increment(42)
19
github.com/mame/ruby-type-profiler
infer def increment:
(Integer) -> Integer
Type Profiler
Serves as a (weak) type checker
def increment(n)
n.timees { }
n + "STRING"
end
increment(42)
20
[error] undefined method:
Integer#timees
check
[error] failed to resolve
overload: Integer#+(String)
check
Demo
• ao.rb
• optcarrot
21
Demo: ao.rb
• A 3D rendering program
• ~300 lines of code
• Written by Hideki Miura
• Original version (js) was
created by Syoyo Fujita
https://code.google.com/archive/p/aobench/
• Analysis time < 1sec.
22
Demo: ao.rb
23
class Vec
@x : Complex | Float | any
@y : Complex | Float | any
@z : Complex | Float | any
initialize : (Complex, Complex, Complex) -> Complex
| (Complex, Complex, Float) -> Float
…
vnormalize : () -> Vec
vlength : () -> any
vdot : (Vec) -> (Complex | Float)
x : () -> (Complex | Float)
x= : (Complex) -> Complex
| (Float) -> Float
A class signature for
Vec (3D vector)
vector
operations
"any" should be
fixed manually
Three instance variables
Demo: ao.rb
24
class Scene
@spheres : []
@plane : Plane
initialize : () -> Plane
render : (Integer, Integer, Integer) -> Integer
ambient_occlusion : (Isect) -> Vec
end
class Plane
@p : Vec
@n : Vec
initialize : (Vec, Vec) -> Vec
intersect : (Ray, Isect) -> (NilClass | Vec)
Demo: ao.rb
25
class Ray
@org : Vec
@dir : Vec
initialize : (Vec, Vec) -> Vec
dir : () -> Vec
org : () -> Vec
end
class Isect
@t : Complex | Float | any
@hit : Boolean
@pl : Vec
@n : Vec
Demo: ao.rb
• TP generates a good prototype of signatures
• Can be used as a signature with some fixes
• May be also useful for program understanding
• There are some wrong / incomplete guesses
• Due to lack of knowledge of methods, analysis
limitation, etc.
• Some of them can be fixed by TP improvement
26
Demo: optcarrot
• 8-bit machine emulator
• Circuit emulation program
• 5000 LOC
• Author: me ☺
• Analysis time ~ 20 sec.
27
https://eregon.me/blog/2016/11/28/optcarrot.html
Demo: optcarrot
28
class Optcarrot::NES
@conf : Optcarrot::Config
@video : any
@audio : any
@input : any
@cpu : Optcarrot::CPU
@apu : Optcarrot::APU
@ppu : Optcarrot::PPU
initialize : () -> None
end
Three circuit modules:
CPU, APU (Audio), PPU (Graphics)
Failed to detect
other methods
Demo: optcarrot
29
class Optcarrot::APU
...
@pulse_1 : Optcarrot::APU::Pulse
@pulse_0 : Optcarrot::APU::Pulse
@triangle : Optcarrot::APU::Triangle
@noise : Optcarrot::APU::Noise
...
end
Audio processor unit has
four wave generators
Demo: optcarrot
• TP just showed shallow analysis result
• Due to lack of knowledge about many builtin
classes/methods (such as Fiber, etc.)
• Still, it looks useful to create a prototype of
signatures
• TP is never perfect, but I believe it is promising
30
A Key Idea of Type Profiler
Runs a Ruby code in "type-level"
Traditional 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
31
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
Object#foo ::
(Integer) ->
(Integer | String)
32
Returns
String
Returns
Integer
Difficulties of Type Profiler
• Requires a starting point
• Needs to be integrated with a test framework
• Cannot analyze some language features
• Is still very preliminary
34
A staring point is required
• TP cannot infer untested methods
35
def inc(n)
n
end
inc(42)
infer
def inc: (Integer) -> Integer
A test code
def inc(n)
n
end
Untested
infer
def inc: (any) -> any
Test framework integration is needed
• Some test may lead to a wrong guess
36
def foo(n)
n+1
end
assert_raise { foo("s") }
infer
def foo: (String) -> any
A test excepts an exception
def bar(n)
n
end
foo(MockObject.new) A test passes a mock object
infer
def bar: (MockObject) -> ...
Difficult features to analyze
• Typically, TP cannot trace Object#send
• Singleton methods, Object#eval, binding, etc...
• You need manually write RBS in this case
37
def inc(n)
n
end
send("inc".to_sym, 42)
infer
def inc: (any) -> any
The Symbol cannot be
determined in type-level
TP is still Preliminary
• Designing TP is harder than MRI-compatible
normal interpreter
• Many features are not supported yet
• Notable unsupported-yet features: Module, and Exception
• The analysis performance must be improved
38
It is developed in one person-year 😖
Help, advice, and contribution are welcome!!!
Related Work
• mruby-meta-circular (Hideki Miura)
• Type Profiler has been inspired by it
• Type Analysis for JavaScript (S. H. Jensen, et al.)
• RDL infer (Jeff Foster et al.)
• An alternative approach to infer types of non-annotated
Ruby code
• Based on traditional type inference with some heuristics
39
Acknowledgement
• Hideki Miura
• Ruby committers: matz, akr, ko1, soutaro
• Katsuhiro Ueno & Eijiro Sumii
• Stripe team & Shopify team & Jeff Foster
40
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
41

More Related Content

What's hot

Not Everything is an Object - Rocksolid Tour 2013
Not Everything is an Object  - Rocksolid Tour 2013Not Everything is an Object  - Rocksolid Tour 2013
Not Everything is an Object - Rocksolid Tour 2013
Gary Short
 

What's hot (19)

TypeScript: Basic Features and Compilation Guide
TypeScript: Basic Features and Compilation GuideTypeScript: Basic Features and Compilation Guide
TypeScript: Basic Features and Compilation Guide
 
Perl::Lint - Yet Another Perl Source Code Linter
Perl::Lint - Yet Another Perl Source Code LinterPerl::Lint - Yet Another Perl Source Code Linter
Perl::Lint - Yet Another Perl Source Code Linter
 
Jaoo irony
Jaoo ironyJaoo irony
Jaoo irony
 
IronRuby for the .NET Developer
IronRuby for the .NET DeveloperIronRuby for the .NET Developer
IronRuby for the .NET Developer
 
Typescript: enjoying large scale browser development
Typescript: enjoying large scale browser developmentTypescript: enjoying large scale browser development
Typescript: enjoying large scale browser development
 
IronRuby for the Rubyist
IronRuby for the RubyistIronRuby for the Rubyist
IronRuby for the Rubyist
 
Why Scala for Web 2.0?
Why Scala for Web 2.0?Why Scala for Web 2.0?
Why Scala for Web 2.0?
 
Ruxmon.2013-08.-.CodeBro!
Ruxmon.2013-08.-.CodeBro!Ruxmon.2013-08.-.CodeBro!
Ruxmon.2013-08.-.CodeBro!
 
Crystal presentation in NY
Crystal presentation in NYCrystal presentation in NY
Crystal presentation in NY
 
JavaScript: The Language
JavaScript: The LanguageJavaScript: The Language
JavaScript: The Language
 
Not Everything is an Object - Rocksolid Tour 2013
Not Everything is an Object  - Rocksolid Tour 2013Not Everything is an Object  - Rocksolid Tour 2013
Not Everything is an Object - Rocksolid Tour 2013
 
Static analysis for perl
Static analysis for perlStatic analysis for perl
Static analysis for perl
 
Introduction to Ruby
Introduction to RubyIntroduction to Ruby
Introduction to Ruby
 
C++ to java
C++ to javaC++ to java
C++ to java
 
IronRuby And The DLR
IronRuby And The DLRIronRuby And The DLR
IronRuby And The DLR
 
TypeScript introduction to scalable javascript application
TypeScript introduction to scalable javascript applicationTypeScript introduction to scalable javascript application
TypeScript introduction to scalable javascript application
 
Python for Swift
Python for SwiftPython for Swift
Python for Swift
 
Slicing, Dicing, And Linting OpenAPI
Slicing, Dicing, And Linting OpenAPISlicing, Dicing, And Linting OpenAPI
Slicing, Dicing, And Linting OpenAPI
 
Intro to Java for C++ Developers
Intro to Java for C++ DevelopersIntro to Java for C++ Developers
Intro to Java for C++ Developers
 

Similar to A Static Type Analyzer of Untyped Ruby Code for Ruby 3

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
 
Speed geeking-lotusscript
Speed geeking-lotusscriptSpeed geeking-lotusscript
Speed geeking-lotusscript
Bill Buchan
 
Coq to Rubyによる証明駆動開発@名古屋ruby会議02
Coq to Rubyによる証明駆動開発@名古屋ruby会議02Coq to Rubyによる証明駆動開発@名古屋ruby会議02
Coq to Rubyによる証明駆動開発@名古屋ruby会議02
Hiroki Mizuno
 

Similar to A Static Type Analyzer of Untyped Ruby Code for Ruby 3 (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
 
A Type-level Ruby Interpreter for Testing and Understanding
A Type-level Ruby Interpreter for Testing and UnderstandingA Type-level Ruby Interpreter for Testing and Understanding
A Type-level Ruby Interpreter for Testing and Understanding
 
Type Profiler: An Analysis to guess type signatures
Type Profiler: An Analysis to guess type signaturesType Profiler: An Analysis to guess type signatures
Type Profiler: An Analysis to guess type signatures
 
44CON London 2015 - Reverse engineering and exploiting font rasterizers: the ...
44CON London 2015 - Reverse engineering and exploiting font rasterizers: the ...44CON London 2015 - Reverse engineering and exploiting font rasterizers: the ...
44CON London 2015 - Reverse engineering and exploiting font rasterizers: the ...
 
2R-3KS03-OOP_UNIT-I (Part-A)_2023-24.pptx
2R-3KS03-OOP_UNIT-I (Part-A)_2023-24.pptx2R-3KS03-OOP_UNIT-I (Part-A)_2023-24.pptx
2R-3KS03-OOP_UNIT-I (Part-A)_2023-24.pptx
 
Code Analysis-run time error prediction
Code Analysis-run time error predictionCode Analysis-run time error prediction
Code Analysis-run time error prediction
 
Triton and symbolic execution on gdb
Triton and symbolic execution on gdbTriton and symbolic execution on gdb
Triton and symbolic execution on gdb
 
Metaprogramming Go
Metaprogramming GoMetaprogramming Go
Metaprogramming Go
 
Programing fundamentals with C++
Programing fundamentals with C++Programing fundamentals with C++
Programing fundamentals with C++
 
Number of Computer Languages = 3
Number of Computer Languages = 3Number of Computer Languages = 3
Number of Computer Languages = 3
 
Go: What's Different ?
Go: What's Different ?Go: What's Different ?
Go: What's Different ?
 
error_highlight: User-friendly Error Diagnostics
error_highlight: User-friendly Error Diagnosticserror_highlight: User-friendly Error Diagnostics
error_highlight: User-friendly Error Diagnostics
 
CNIT 127: Ch 18: Source Code Auditing
CNIT 127: Ch 18: Source Code AuditingCNIT 127: Ch 18: Source Code Auditing
CNIT 127: Ch 18: Source Code Auditing
 
Compiler Construction
Compiler ConstructionCompiler Construction
Compiler Construction
 
12 Step Guide to Lotuscript
12 Step Guide to Lotuscript12 Step Guide to Lotuscript
12 Step Guide to Lotuscript
 
Effective C++
Effective C++Effective C++
Effective C++
 
Speed geeking-lotusscript
Speed geeking-lotusscriptSpeed geeking-lotusscript
Speed geeking-lotusscript
 
Intro to Crystal Programming Language
Intro to Crystal Programming LanguageIntro to Crystal Programming Language
Intro to Crystal Programming Language
 
Coq to Rubyによる証明駆動開発@名古屋ruby会議02
Coq to Rubyによる証明駆動開発@名古屋ruby会議02Coq to Rubyによる証明駆動開発@名古屋ruby会議02
Coq to Rubyによる証明駆動開発@名古屋ruby会議02
 
DSD-INT 2014 - NGHS Workshop Scripting in SOBEK 3 & Delft3D Flexible Mesh - P...
DSD-INT 2014 - NGHS Workshop Scripting in SOBEK 3 & Delft3D Flexible Mesh - P...DSD-INT 2014 - NGHS Workshop Scripting in SOBEK 3 & Delft3D Flexible Mesh - P...
DSD-INT 2014 - NGHS Workshop Scripting in SOBEK 3 & Delft3D Flexible Mesh - P...
 

More from mametter

クックパッド春の超絶技巧パンまつり 超絶技巧プログラミング編 資料
クックパッド春の超絶技巧パンまつり 超絶技巧プログラミング編 資料クックパッド春の超絶技巧パンまつり 超絶技巧プログラミング編 資料
クックパッド春の超絶技巧パンまつり 超絶技巧プログラミング編 資料
mametter
 
Cookpad Hackarade #04: Create Your Own Interpreter
Cookpad Hackarade #04: Create Your Own InterpreterCookpad Hackarade #04: Create Your Own Interpreter
Cookpad Hackarade #04: Create Your Own Interpreter
mametter
 

More from mametter (20)

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
 
Cookpad Hackarade #04: Create Your Own Interpreter
Cookpad Hackarade #04: Create Your Own InterpreterCookpad Hackarade #04: Create Your Own Interpreter
Cookpad Hackarade #04: Create Your Own Interpreter
 
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

AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
ankushspencer015
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
ssuser89054b
 
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 

Recently uploaded (20)

FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced LoadsFEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leap
 
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
 
Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
 
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank  Design by Working Stress - IS Method.pdfIntze Overhead Water Tank  Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.ppt
 

A Static Type Analyzer of Untyped Ruby Code for Ruby 3

  • 1. A Static Type Analyzer of Untyped Ruby Code for Ruby 3 Yusuke Endoh RubyConf 2019 19th Nov. 2019 1
  • 2. Yusuke Endoh (@mametter) • A Ruby committer: • Keyword argument design and implementation (2.0) • Optcarrot: a benchmark for Ruby 3x3 • Ruby 2.0 release manager • Working at Cookpad Inc. w/ @ko1 2
  • 3. PR: Cookpad Inc. • Mission: "Make Everyday Cooking Fun" • cookpad.com: A recipe sharing service • Monthly Average Users: 93 million 3
  • 4. PR: Cookpad Inc. 4 We're hiring! HQ is in Bristol, UK Aim to be No.1 in 100 countries (Now in 30 Languages and 73 Countries)
  • 5. This Talk: Types in Ruby 3 • Matz's plan for Types in Ruby 3 Multiple type checkers in one blueprint • Ruby Signature The standard type signature format for stdlib and gems • Type Profiler A type analysis for non-annotated Ruby code 5
  • 6. This Talk: Types in Ruby 3 •➔ Matz's plan for Ruby 3 Types  • Ruby Signature • Type Profiler 6
  • 7. The Objective of Ruby 3 types Point out possible bugs without execution • To improve development experience (In other words, a few wrong alerts are acceptable) 7 def increment(n) n.timees { } n + "STRING" end increment(42) NoMethodError? TypeError?
  • 8. Question 🙋 Do you want to write a type annotation? 8 extend T::Sig sig {params(n:Integer).returns(Integer)} def increment(n) n + 1 end increment(42) type annotation (in Sorbet style) source code
  • 9. Thank you. Just as I expected • In Ruby 3, you can write annotations if you like • You will gain relatively strong type checking • Moreover, in Ruby 3, • You don't have to write annotations manually • You can even check the code with no annotations! (if my project succeeds ☺) 9
  • 10. Thank you. That was unexpected • In Ruby 3, you can write annotations if you like • You will gain relatively strong type checking • Moreover, in Ruby 3, • You don't have to write annotations manually • You can even check the code with no annotations! (if my project succeeds ☺) 10
  • 11. Ruby 3 will have three items 1. Ruby Signature (RBS) language 2. Type inference for non-annotated code (Type Profiler) 3. Type checking for annotated code (Sorbet, RDL, Steep, etc.) I explain 1 and 2 in this talk... 11
  • 12. This Talk: Ruby 3 types • Matz's plan for Ruby 3 Types •➔Ruby Signature • What it is / Examples / What Ruby 3 will ship • Type Profiler 12
  • 13. Soutaro Matsumoto (@soutaro) • Leads the design and implementation of RBS • https://github.com/ruby/ruby-signature • Develops own static type checker "Steep" • [Correction] No session about Steep this year! • Working for Square 13
  • 14. 1. Ruby signature language (RBS) • The standard language to describe types of Ruby programs • Different syntax: to keep Ruby code unannotated • Ruby3 will ship with signatures for stdlib and a library for RBS 14 class Inc def increment(n) n + 1 end end inc.rbs inc.rb class Inc def increment: (Integer) -> Integer end separated files
  • 15. 15 class Array[T] def []: (Integer) -> T | (Integer, Integer) -> Array[T] def first: () -> T? def each: () { (T) -> void } -> Array[T] include Enumerable[T, Array[T]] end Generics Optional Types Overloading Block Mixin interface _Duck def quack: () -> void end Duck typing
  • 16. Using RBS • For type-checking • Static type checking needs signatures of libraries • Steep uses RBS to define the signature of your Ruby applications • For documentation • RBS explains the API of a gem 16
  • 17. Ship your gems with RBS • Scaffold from Ruby code / Sorbet RBI • Generate using Type Profiler • [WIP] A tool to test RBS definitions by dynamic type checking 17 $ rbs scaffold rb # from unannotated Ruby code $ rbs scaffold rbi # from Sorbet annotated code
  • 18. This Talk: Ruby 3 types • Matz's plan for Ruby 3 Types • Ruby Signature •➔Type Profiler • Demo • Approach • Problems 18
  • 19. Type Profiler A kind of "type inference" for non-annotated code def increment(n) n + 1 end increment(42) 19 github.com/mame/ruby-type-profiler infer def increment: (Integer) -> Integer
  • 20. Type Profiler Serves as a (weak) type checker def increment(n) n.timees { } n + "STRING" end increment(42) 20 [error] undefined method: Integer#timees check [error] failed to resolve overload: Integer#+(String) check
  • 22. Demo: ao.rb • A 3D rendering program • ~300 lines of code • Written by Hideki Miura • Original version (js) was created by Syoyo Fujita https://code.google.com/archive/p/aobench/ • Analysis time < 1sec. 22
  • 23. Demo: ao.rb 23 class Vec @x : Complex | Float | any @y : Complex | Float | any @z : Complex | Float | any initialize : (Complex, Complex, Complex) -> Complex | (Complex, Complex, Float) -> Float … vnormalize : () -> Vec vlength : () -> any vdot : (Vec) -> (Complex | Float) x : () -> (Complex | Float) x= : (Complex) -> Complex | (Float) -> Float A class signature for Vec (3D vector) vector operations "any" should be fixed manually Three instance variables
  • 24. Demo: ao.rb 24 class Scene @spheres : [] @plane : Plane initialize : () -> Plane render : (Integer, Integer, Integer) -> Integer ambient_occlusion : (Isect) -> Vec end class Plane @p : Vec @n : Vec initialize : (Vec, Vec) -> Vec intersect : (Ray, Isect) -> (NilClass | Vec)
  • 25. Demo: ao.rb 25 class Ray @org : Vec @dir : Vec initialize : (Vec, Vec) -> Vec dir : () -> Vec org : () -> Vec end class Isect @t : Complex | Float | any @hit : Boolean @pl : Vec @n : Vec
  • 26. Demo: ao.rb • TP generates a good prototype of signatures • Can be used as a signature with some fixes • May be also useful for program understanding • There are some wrong / incomplete guesses • Due to lack of knowledge of methods, analysis limitation, etc. • Some of them can be fixed by TP improvement 26
  • 27. Demo: optcarrot • 8-bit machine emulator • Circuit emulation program • 5000 LOC • Author: me ☺ • Analysis time ~ 20 sec. 27 https://eregon.me/blog/2016/11/28/optcarrot.html
  • 28. Demo: optcarrot 28 class Optcarrot::NES @conf : Optcarrot::Config @video : any @audio : any @input : any @cpu : Optcarrot::CPU @apu : Optcarrot::APU @ppu : Optcarrot::PPU initialize : () -> None end Three circuit modules: CPU, APU (Audio), PPU (Graphics) Failed to detect other methods
  • 29. Demo: optcarrot 29 class Optcarrot::APU ... @pulse_1 : Optcarrot::APU::Pulse @pulse_0 : Optcarrot::APU::Pulse @triangle : Optcarrot::APU::Triangle @noise : Optcarrot::APU::Noise ... end Audio processor unit has four wave generators
  • 30. Demo: optcarrot • TP just showed shallow analysis result • Due to lack of knowledge about many builtin classes/methods (such as Fiber, etc.) • Still, it looks useful to create a prototype of signatures • TP is never perfect, but I believe it is promising 30
  • 31. A Key Idea of Type Profiler Runs a Ruby code in "type-level" Traditional 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 31
  • 32. 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 Object#foo :: (Integer) -> (Integer | String) 32 Returns String Returns Integer
  • 33. Difficulties of Type Profiler • Requires a starting point • Needs to be integrated with a test framework • Cannot analyze some language features • Is still very preliminary 34
  • 34. A staring point is required • TP cannot infer untested methods 35 def inc(n) n end inc(42) infer def inc: (Integer) -> Integer A test code def inc(n) n end Untested infer def inc: (any) -> any
  • 35. Test framework integration is needed • Some test may lead to a wrong guess 36 def foo(n) n+1 end assert_raise { foo("s") } infer def foo: (String) -> any A test excepts an exception def bar(n) n end foo(MockObject.new) A test passes a mock object infer def bar: (MockObject) -> ...
  • 36. Difficult features to analyze • Typically, TP cannot trace Object#send • Singleton methods, Object#eval, binding, etc... • You need manually write RBS in this case 37 def inc(n) n end send("inc".to_sym, 42) infer def inc: (any) -> any The Symbol cannot be determined in type-level
  • 37. TP is still Preliminary • Designing TP is harder than MRI-compatible normal interpreter • Many features are not supported yet • Notable unsupported-yet features: Module, and Exception • The analysis performance must be improved 38 It is developed in one person-year 😖 Help, advice, and contribution are welcome!!!
  • 38. Related Work • mruby-meta-circular (Hideki Miura) • Type Profiler has been inspired by it • Type Analysis for JavaScript (S. H. Jensen, et al.) • RDL infer (Jeff Foster et al.) • An alternative approach to infer types of non-annotated Ruby code • Based on traditional type inference with some heuristics 39
  • 39. Acknowledgement • Hideki Miura • Ruby committers: matz, akr, ko1, soutaro • Katsuhiro Ueno & Eijiro Sumii • Stripe team & Shopify team & Jeff Foster 40
  • 40. 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 41