Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Coqでsprintf
Search
Masaki Hara
August 12, 2012
Programming
220
2
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Coqでsprintf
upcamp 2012 ハッカソン発表会にて
Masaki Hara
August 12, 2012
More Decks by Masaki Hara
See All by Masaki Hara
文字コードの話
qnighy
47
21k
バックエンドのためのアプリ内課金入門 (サブスク編)
qnighy
9
2.6k
Dockerfileの考え方
qnighy
50
20k
Arm移行タイムアタック
qnighy
1
770
Quine, Polyglot, 良いコード
qnighy
5
930
Prolog入門
qnighy
5
3.1k
Rubyのobject_id
qnighy
8
1.9k
Getting along with YAML comments with Psych
qnighy
2
3.2k
状態設計から「なんとなく」を無くそう
qnighy
90
30k
Other Decks in Programming
See All in Programming
R8 の設定見直しでアプリサイズ削減
andpad
0
210
RAG の “R” を Swift で覗いてみる 〜「意味から探す」検索の仕組み〜
nao_randd
0
160
JAWS-UG 東京支部が始める、JAWS-UG支部コラボ / JAWS-UG lunchtime LT Collaboration
y0hgi
0
260
月間100万人のサービス刷新で、 技術選定をHonoに振り切った話
yutobara
8
6.4k
ハーネス設計入門 〜 基礎知識の整理から実務へのステップアップ 〜
kinopeee
22
22k
Simple Storage Service(S3) is not simple
iwatsukayura
0
280
速習iPhone Duo対応
yuukiw00w
2
980
Kotlinの15年とServer-Side Kotlin
n_takehata
1
140
Jetpack Compose Mechanisms
skydoves
2
730
Compiling a real-world CLI tool to native with Static Hermes
tmikov2023
0
110
AI が書く Go コードの品質を劇的に向上させる Linter: “declscope”
mpyw
0
490
Starting & Sustaining Code-Based E2E Testing for Non-Coding QA Teams( #jasstniigata )
teyamagu
PRO
1
980
Featured
See All Featured
From Legacy to Launchpad: Building Startup-Ready Communities
dugsong
0
350
The untapped power of vector embeddings
frankvandijk
2
1.9k
What does AI have to do with Human Rights?
axbom
PRO
1
2.4k
The Director’s Chair: Orchestrating AI for Truly Effective Learning
tmiket
1
330
コードの90%をAIが書く世界で何が待っているのか / What awaits us in a world where 90% of the code is written by AI
rkaga
63
46k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
52
6.1k
AI Search: Where Are We & What Can We Do About It?
aleyda
0
8.1k
How People are Using Generative and Agentic AI to Supercharge Their Products, Projects, Services and Value Streams Today
helenjbeal
2
360
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
38
3.1k
Why Mistakes Are the Best Teachers: Turning Failure into a Pathway for Growth
auna
0
320
Have SEOs Ruined the Internet? - User Awareness of SEO in 2025
akashhashmi
0
520
Impact Scores and Hybrid Strategies: The future of link building
tamaranovitovic
0
460
Transcript
Coqでsprintf upcamp 2012 にて
Coqでsprintf • printf(“%s : %d¥n”, “Coq”, strlen(“Coq”)); • 第二引数以降の型が、第一引数に依存! •
依存型のわかりやすい例
Coqでsprintf • printf(“%s : %d¥n”, “Coq”, strlen(“Coq”)); • 第二引数以降の型が、第一引数に依存! •
依存型のわかりやすい例 • sprintf “Hello” – (string)型を返して欲しい • sprintf “There are %d pens.” – (Z -> string)型を返して欲しい • 引数によって戻り値の型が異なる
Coqでsprintf • Coqにおける文字・文字列 – 文字(ascii) : 8ビットのタプル – 文字列(string) :
asciiからなるリスト構造 • Coqにおける整数 – 自然数(nat) : ペアノの公理による非負整数 – 整数(Z) : 2進数表現による整数
toStringの実装 • Zからstringへ変換 • 一般的な再帰で書く • 停止性証明が必要なので、Fixpointではなく Functionで書く • 一部を証明モードで補完したいので、
FunctionではなくProgram Fixpointを使う
関数定義の命令 証明モードを併用しない 証明モードを併用する 単なる定義 Definition Program Definition 構造帰納法による定義 Fixpoint{struct x}
Program Fixpoint{struct x} 停止性証明による定義(1) Function{wf lt x} Program Fixpoint{wf lt x} 停止性証明による定義(2) Function{measure f x} Program Fixpoint{measure f x} 余帰納法 CoFixpoint Program CoFixpoint ※1 Functionでは、定義直後に停止性証明の証明モードに入る。証明終了後に実際 に関数が定義される。 ※2 Programでは、定義後にObligation命令(Obligation 1 や Next Obligation) を使って プレースホルダーの証明をするモードに入る。証明終了後に実際に関数が定義され る。場合によっては全て自動で証明されてしまう。 ※3 Fixpoint{struct x}では、引数xの構造について単調減少な再帰呼び出しのみ許可 される。CoFixpointでは、戻り値の構造について単調減少な再帰呼び出しのみ許可さ れる。
toStringの実装 • 非負整数Nから文字列への変換関数 • 再帰呼び出しでNの値が単調減少
toStringの実装 • 整数から文字列への変換関数
toStringの実装 • 実行例
sprintfの実装の準備 • 型と値のペアを定義する
sprintfの実装の準備 • {| TV_T := nat ; TV_V := 10
|} • {| TV_T := string ; TV_V := “Hello” |}
sprintfの実装 • sprintf_impl – 書式文字列に対する再帰で書く – 構造帰納法なので停止性証明は不要 • “%04d”などの複雑な書式文字列に対応したい場合、 停止性証明を導入する必要性が生じそう
sprintfの実装 • 帰納法&マッチング
sprintfの実装 • 実行例 • sprintf “Hello, world!” – → “Hello,
world!” • sprintf “%d + %d = %d” (1) (2) (1+2) – → “1 + 2 = 3” • sprintf “Hello, %s!” “Masaki” – → “Hello, Masaki”
ソースコード • https://gist.github.com/3329025
おまけ emacsで:wしたら強制終了するマクロを作るっていうネタあるんですけど使う人 居ますか [2012/08/11 22:28:45] qnighy (原 将己): vimでC-x C-sしたら強制終了っていう逆
のネタもあります
おまけ • 「VimでEmacsの保存コマンド→死亡」 を実装してみました
おまけ • といっても以下の2つを実行するだけ • :map <C-x><C-s> :qall!<Enter> • :map! <C-x><C-s>
:qall!<Enter> • SLでは物足らないあなたに…