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
XSLTで作るBrainfuck処理系
Search
MakKi
June 14, 2025
Programming
450
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
XSLTで作るBrainfuck処理系
関数型まつり2025
MakKi
June 14, 2025
More Decks by MakKi
See All by MakKi
テストだけじゃない!インプロセスDBで生まれるGoらしさ
makki_d
0
77
テストだけじゃない!インプロセスDBで生まれるGoらしさ
makki_d
0
80
SQLだけでマイグレーションしたい!
makki_d
0
1.4k
Recap: An Operating System in Go
makki_d
2
200
眼鏡と視力についての誤解を解く
makki_d
0
260
標準ライブラリの動向とイテレータのパフォーマンス
makki_d
3
790
range over funcのエラー処理
makki_d
1
1.9k
GoとテストとインプロセスDB
makki_d
3
720
君は古の言語M4を知っているか (LT)
makki_d
0
590
Other Decks in Programming
See All in Programming
速く作れる。その次は、速く確かめられる開発へ 〜AIネイティブ開発を支える、Shift Down〜 / Can build fast. Next, moving to development where we can verify fast.
rkaga
8
5.3k
モジュールの視点からSwiftを読み解く #iosdc
s_shimotori
0
290
技術的負債の返済は、AI時代の複利で効く投資 — 経営としての意思決定とその遂行
curekoshimizu
1
2k
巨大モノリシックアプリ モダン化大作戦
ktcryomm
1
1.2k
市販E-Readerを乗っ取れ 〜Embedded Swiftで電子ペーパーガジェットを制御する〜
trickart
0
230
Unity6 URPによる海底をテーマにしたVJシステムの解説 | Unity Shader 完全に理解した 勉強会
gam0022
0
250
GitHubハンズオン講座 — 実務レベルのチーム開発のフローを身につけよう
junhat6
0
110
Agents on Rails - Rails at Scale 2026
irinanazarova
0
310
手動確認はもう限界 〜XCUITestでCustom URL Schemeの遷移を起動種別ごとに自動テストする〜 / Testing Custom URL Schemes with XCUITest
otouto
0
370
Webの地図
yosuke_furukawa
PRO
6
5k
Augmenting AI with the Power of Jakarta EE
ivargrimstad
0
380
Ghostty + Neovimで作る 透明でカッコ良い開発環境
j341nono
0
150
Featured
See All Featured
エンジニアに許された特別な時間の終わり
watany
109
250k
SEO for Brand Visibility & Recognition
aleyda
0
4.8k
Utilizing Notion as your number one productivity tool
mfonobong
4
610
Data-driven link building: lessons from a $708K investment (BrightonSEO talk)
szymonslowik
1
1.3k
WCS-LA-2024
lcolladotor
0
840
Ten Tips & Tricks for a 🌱 transition
stuffmc
1
240
We Analyzed 250 Million AI Search Results: Here's What I Found
joshbly
1
2k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
52
6.1k
Music & Morning Musume
bryan
48
7.4k
Build The Right Thing And Hit Your Dates
maggiecrowley
39
3.5k
Docker and Python
trallard
47
4.2k
brightonSEO & MeasureFest 2025 - Winning Strategies for Black Friday CRO & PPC - Christian Goodrich
cargoodrich
3
860
Transcript
XSLTで作るBrainfuck処理系 XSLTは関数型言語たり得るか?
自己紹介 • 牧内大輔(MakKi) ◦ @makki_d ◦ makiuchi-d • KLab株式会社 ◦
スマホゲーム、主にサーバサイドや同期通信まわり • OSS ◦ gozxing: バーコードQRコード ◦ arelo: 自動リロード ◦ EMLauncher: スマホデバッグアプリ配信 ◦ WSNet2: 同期通信 • Go言語界隈によく出没
XSLT
• EXtensible Stylesheet Language Transformation • XMLをXMLや他の形式に変換するスタイルシート 言語 ◦ XSL自身もXMLで記述する
XSLTとは XML </> HTML < > TXT Aa XML </> XSLT XSL </>
<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html"/> <xsl:template match="/">
<html> <body> <h1>Library Books</h1> <ul> <xsl:apply-templates select="library/book"/> </ul> </body> </html> </xsl:template> <xsl:template match="book"> <li> <strong> <xsl:value-of select="title"/> </strong> by <xsl:value-of select="author"/> </li> </xsl:template> </xsl:stylesheet> convert.xsl <?xml version="1.0" encoding="utf-8"?> <library> <book> <title>Learning XSLT</title> <author>John Doe</author> </book> <book> <title>XML in Action</title> <author>Jane Smith</author> </book> </library> input.xml <html> <body> <h1>Library Books</h1> <ul> <li> <strong>Learning XSLT</strong> by John Doe </li> <li> <strong>XML in Action</strong> by Jane Smith </li> </ul> </body> </html> output.html
XSLTの特徴 • 宣言的なテンプレート <xsl:template> • テンプレートの再帰呼び出し による繰り返し処理 • パターンマッチング でテンプレートを自動選択
• 変数 <xsl:variable> が不変(再代入不可) すごく関数型言語ぽい!
XSLTは関数型言語か? • そもそもプログラミング言語 と言えるのか? ◦ プログラミング言語であるからにはチューリング完全が求められる • プログラミング言語ならばチューリング完全 であるべき ◦
計算可能なアルゴリズムは全てチューリングマシン上の計算に書き換えられる ▪ チューリングマシンはあらゆる計算可能な計算が可能 ◦ チューリングマシンと同等の能力を持つことを「チューリング完全」と呼ぶ ▪ チューリング完全ならあらゆる計算可能な計算が可能 • チューリング完全な 処理系を実装できればチューリング完全
Brainfuckを実装してみた
Brainfuckとは • 記号のみで記述 • 8つの命令しかない言語 • メモリ配列 とポインタを持つ • もちろんチューリング完全
実装が簡単! +++++++++[>++++++++>+++++++++ ++>+++++<<<-]>.>++.+++++++..+ ++.>-.------------.<++++++++. --------.+++.------.--------. >+. hello.bf
BrainfuckをXMLで表現 • 各命令をタグで表現 ◦ 記号と1:1対応 • XSLTプロセッサがパースしてくれる ◦ [と ]の対応が親子関係でわかる
• 標準入力も含めることに <?xml version="1.0" encoding="utf-8"?> <?xml-stylesheet type="text/xsl" href="bf.xsl"?> <bf> <code> <inc/><inc/> <loop> <dec/><right/><read/><inc/><print/><left/> <end/> </loop> </code> <input>Aa</input> </bf> atob.xml “Aa”を”Bb”にするプログラム: ++[->,+.]
ポインタのインクリメント( >) • 状態を引数でまるごと伝搬 ◦ ptr: ポインタ ◦ mem: メモリ配列
◦ input: 標準入力文字列 • <right/>のテンプレート ◦ 次の命令のテンプレート呼び出し ◦ 直後の兄弟要素 ▪ following-sibling::*[1] ◦ 引数のptrを+1 <xsl:template match="right"> <xsl:param name="ptr"/> <xsl:param name="mem"/> <xsl:param name="input"/> <xsl:apply-templates select="following-sibling::*[1]"> <xsl:with-param name="ptr" select="$ptr + 1"/> <xsl:with-param name="mem" select="$mem"/> <xsl:with-param name="input" select="$input"/> </xsl:apply-templates> </xsl:template>
メモリの実装 • メモリ配列の表現 ◦ 連番のXML要素 ▪ key = '_'+ptrの値 •
読み出し ◦ 名前が$keyな要素の値 ▪ sumで存在しないとき0に • 書き込み ◦ write-valテンプレート ◦ $key以外の要素のコピー ◦ $keyの要素を追加 ▪ 値を$valに <_0>65</_0> <_1>66</_1> <_2>67</_2> <_3>68</_3> sum(exsl:node-set($mem)/*[name()=$key]) <xsl:template name="write-val"> <xsl:param name="mem"/> <xsl:param name="key"/> <xsl:param name="val"/> <xsl:for-each select="exsl:node-set($mem)/*[name()!=$key]"> <xsl:copy-of select="."/> </xsl:for-each> <xsl:element name="{$key}"> <xsl:value-of select="$val"/> </xsl:element> </xsl:template>
値のインクリメント( +) • ポインタが指すメモリの現在の値 ◦ 取り出すための key('_' + ptr) ◦
値 val • write-val テンプレート ◦ メモリの"$key"に"$val+1" を書く ◦ まるごと作り直して次の命令に渡す <xsl:template match="inc"> <!-- param 省略 --> <xsl:variable name="key" select="concat('_', $ptr)"/> <xsl:variable name="val" select="sum(exsl:node-set($mem)/*[name()=$key])"/> <xsl:variable name="mem"> <xsl:call-template name="write-val"> <xsl:with-param name="mem" select="$mem"/> <xsl:with-param name="key" select="$key"/> <xsl:with-param name="val" select="$val + 1"/> </xsl:call-template> </xsl:variable> <!-- apply-templates 省略 -->
ループ([ ]) • <xsl:choose>で分岐 ◦ 非0のとき:子要素の先頭 ▪ "*[1]" ◦ 0のとき:直後の兄弟要素
▪ "following-sibling::*[1]" • ループ終端 <end/> ◦ 親要素でapply-template ▪ "parent::node()" <!-- いろいろ省略 --> <xsl:choose> <xsl:when test="$val != 0"> <xsl:apply-templates select="*[1]"> <xsl:with-param name="ptr" select="$ptr"/> <xsl:with-param name="mem" select="$mem"/> <xsl:with-param name="input" select="$input"/> </xsl:apply-templates> </xsl:when> <xsl:otherwise> <xsl:apply-templates select="following-sibling::*[1]"> <xsl:with-param name="ptr" select="$ptr"/> <xsl:with-param name="mem" select="$mem"/> <xsl:with-param name="input" select="$input"/> </xsl:apply-templates> </xsl:otherwise> </xsl:choose>
実装できた! • https://github.com/makiuchi-d/bfxslt • 解説記事 KLabTechBook Vol. 14 ◦ 技術書典18にて頒布中
▪ 物理本500円(在庫残り1冊) ▪ 電子版0円 ◦ ブログでも読めます 新刊もあります
XSLTは プログラミング言語 である
関数型言語たり得るか?
関数型言語であるためには • 関数が一級オブジェクト ◦ 関数を変数に拘束できたり演算できたり • XSLTのテンプレートは一級オブジェクトではない ◦ テンプレート自体を変数や引数に拘束できない ◦
動的生成もできない ▪ XSL自体もXMLなのでXSLからXSLを生成するXSLTを多段で呼べばあるいは ……
XSLTは 関数型言語ぽい プログラミング言語である
つづく? 実はXSLT2.0でtemplateとは別にfunctionが導入され XSLT3.0でfunctionが一級オブジェクトになったのは また別のお話……