Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Compile Time Mathematics in Rust
Search
White-Green
September 26, 2020
Programming
110
0
Share
Compile Time Mathematics in Rust
技育展 ぼくのさいきょうの○○ テーマで発表させていただいたときの発表資料です
White-Green
September 26, 2020
More Decks by White-Green
See All by White-Green
Chasing Real-Time Observability for CRuby
whitegreen
0
830
Remotion promotion
whitegreen
1
110
Ruby as a Frontend for Programming Language Implementations (RubyKaigi 2025 LT)
whitegreen
0
110
U-22 プログラミング・コンテスト 2021登壇資料
whitegreen
0
330
技育展2021登壇資料
whitegreen
0
67
Other Decks in Programming
See All in Programming
Building on Bluesky's AT Protocol with Ruby
mackuba
0
110
実践ハーネスエンジニアリング:ステアリングループを実例から読み解く / Practical Harness Engineering: Understanding Steering Loops Through Real-World Examples
nrslib
5
4.6k
Lightning-Fast Method Calls with Ruby 4.1 ZJIT / RubyKaigi 2026
k0kubun
3
2.6k
Spec-Driven Development with AI Agents (Workshop, May 2026)
antonarhipov
3
330
認証統合から始めるフロントエンドの機能単位開発 — マイクロサービス思想の適用
koukimiura
0
100
AIベース静的検査器の偽陽性率を抑える工夫3選
orgachem
PRO
4
450
Cache-moi si tu peux : patterns et pièges du cache en production - Devoxx France 2026 - Conférence
slecache
0
340
PicoRuby for IoT: Connecting to the Cloud with MQTT
yuuu
2
770
From Formal Specification to Property Based Test
ohbarye
0
2.4k
t *testing.T は どこからやってくるの?
otakakot
1
920
(Re)make Regexp in Ruby: Democratizing internals for the JIT
makenowjust
3
1k
Claude CodeでETLジョブ実行テストを自動化してみた
yoshikikasama
0
1.2k
Featured
See All Featured
Redefining SEO in the New Era of Traffic Generation
szymonslowik
1
300
Java REST API Framework Comparison - PWX 2021
mraible
34
9.3k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
21
1.5k
How to build a perfect <img>
jonoalderson
1
5.5k
First, design no harm
axbom
PRO
2
1.2k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.8k
What the history of the web can teach us about the future of AI
inesmontani
PRO
1
550
Deep Space Network (abreviated)
tonyrice
0
130
Lightning Talk: Beautiful Slides for Beginners
inesmontani
PRO
1
540
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
PRO
199
73k
技術選定の審美眼(2025年版) / Understanding the Spiral of Technologies 2025 edition
twada
PRO
118
110k
Build The Right Thing And Hit Your Dates
maggiecrowley
39
3.1k
Transcript
© 2020 @White_Green2525 #技育展 #RoomB Compile Time Mathematics in Rust
型レベルの静的計算と処理効率化
Compile Time Mathematics in Rust © 2020 @White_Green2525 #技育展 #RoomB
⾃⼰紹介 • 九州⼯業⼤学 情報⼯学部 B2 • C#とRustが好き • この前誕⽣⽇でした 2
Compile Time Mathematics in Rust © 2020 @White_Green2525 #技育展 #RoomB
Rustとは? • C++からの置き換えを⽬指している⾔語 • ⾼速 • インタプリタを挟まずネイティブに動く • C++と同等または若⼲遅い程度の速度で動作 • 安全 • ライフタイム,所有権等 • メモリリーク,並列処理時の競合が起こらない仕組み • C++なんか⽐較にならない程安全 3
Compile Time Mathematics in Rust © 2020 @White_Green2525 #技育展 #RoomB
Rustでできること • 組み込みシステム • Arduino等 • OS • ネイティブアプリ • WinRTはRust対応 その他にもたくさん • Webフロントエンド • WebAssemblyにコンパイルできる 対応FWもある 4
Compile Time Mathematics in Rust © 2020 @White_Green2525 #技育展 #RoomB
関連型 関連定数 5 • trait(interfaceが近い)の実装時 に、実装毎に型を指定できる (関連型) • 同様に定数も(関連定数)
Compile Time Mathematics in Rust © 2020 @White_Green2525 #技育展 #RoomB
⾯⽩いことできそうですね? 6
Compile Time Mathematics in Rust © 2020 @White_Green2525 #技育展 #RoomB
型レベルで整数加算 • A=0000,0000,0000,0000,0000,0000,0000,0010=2 • B=0000,0000,0000,0000,0000,0000,0000,0100=4 • C=A+B=6 7
Compile Time Mathematics in Rust © 2020 @White_Green2525 #技育展 #RoomB
型計算の表現⼒ • チューリング完全 • 証明) ラムダ計算が実装できた □ 8
Compile Time Mathematics in Rust © 2020 @White_Green2525 #技育展 #RoomB
応⽤:⾏列サイズをコンパイル時に決定 普通 • 可変⻑配列+⾏列サイズ 静的サイズ • 可変⻑配列のみ • 16バイト節約 9
Compile Time Mathematics in Rust © 2020 @White_Green2525 #技育展 #RoomB
主産物:型レベルで計算する利点 • ⾏列計算はサイズにより定義される場合とされない場合がある • 通常:定義されない場合panicする/無効な値を返す • 最善でも単体テストで発⾒/最悪の場合潜在的な不具合として残る • 静的:定義されない場合コンパイルエラー • 不具合発⾒にかかるコストが実質0 10
Compile Time Mathematics in Rust © 2020 @White_Green2525 #技育展 #RoomB
副産物:計算処理の⾼速化 • 同サイズの正⽅⾏列同⼠の和/積演算が⾼速化 11
Compile Time Mathematics in Rust © 2020 @White_Green2525 #技育展 #RoomB
なんで⾼速になるのか • コンパイル時の最適化に起因(詳しい⼈教えて) 12 動的サイズ 静的サイズ
Compile Time Mathematics in Rust © 2020 @White_Green2525 #技育展 #RoomB
参考資料/リンク • コンピュータの構成と設計 第5版 • ISBN:978‐4822298425, 978‐4822298432(上下巻) • ディジタル回路設計とコンピュータアーキテクチャ 第2版 • ISBN:978‐4798147529 • 型システム⼊⾨ • ISBN:978‐4274069116 • 低レベルプログラミング • ISBN:978‐4798155036 13 ぼくのさいきょうの⾏列ライブラリ https://github.com/White-Green/ct-math
Compile Time Mathematics in Rust © 2020 @White_Green2525 #技育展 #RoomB
余談①:数値の表現⽅法 • Q. 型で整数を表現するときに⼆進数でやってるけど、理由は? • A. ペアノの公理から定義する形式を最初に試したけど、それだ と型名がO(N)の⻑さに、⾜し算にO(N)、掛け算にO(N^2)の時 間がかかるのでコンパイルが⻑くなる。⼆進数使うと型名の⻑ さがO(logN)になってうれしい 14
Compile Time Mathematics in Rust © 2020 @White_Green2525 #技育展 #RoomB
余談②:⾜し算の実装 • Q. 型計算だけで⼆進数の⾜し算実装どうやるの? • A. 型計算処理でprefix加算器を書く謎なことをしていました 15