|
| 1 | +/- # \#html |
| 2 | +
|
| 3 | +`#html` コマンドは、その名の通り html をインフォビューに表示させるコマンドです。[ProofWidgets4](https://github.com/leanprover-community/ProofWidgets4) というライブラリで定義されています。 |
| 4 | +
|
| 5 | +[JSX](https://ja.react.dev/learn/writing-markup-with-jsx) によく似た構文を使うことができます。 |
| 6 | +-/ |
| 7 | +import ProofWidgets.Component.HtmlDisplay |
| 8 | +import ProofWidgets.Component.Recharts |
| 9 | +import ProofWidgets.Component.GraphDisplay |
| 10 | + |
| 11 | +-- JSX ライクな構文が使えるようにする |
| 12 | +open scoped ProofWidgets.Jsx |
| 13 | + |
| 14 | +#html <p>"ここに HTML を書きます"</p> |
| 15 | + |
| 16 | +-- いろんなHTMLタグを書くことができる |
| 17 | +#html |
| 18 | + <div> |
| 19 | + <h3>"見出し"</h3> |
| 20 | + <p> |
| 21 | + <b>"強調されたテキスト"</b> |
| 22 | + <i>"斜体のテキスト"</i> |
| 23 | + </p> |
| 24 | + <a href="https://lean-lang.org/">"リンク"</a> |
| 25 | + </div> |
| 26 | + |
| 27 | +-- 画像も表示できる |
| 28 | +#html |
| 29 | + <img src={"https://upload.wikimedia.org/wikipedia/commons/6/6a/Julia-set_N_z3-1.png"} |
| 30 | + alt="julia set"/> |
| 31 | + |
| 32 | +/- HTMLタグを使用できるだけでなく、様々なコンポーネントが定義されています。-/ |
| 33 | + |
| 34 | +/- ## MarkdownDisplay |
| 35 | +
|
| 36 | +`<MarkdownDisplay />` コンポーネントを使用すると、Markdown や TeX を表示させることができます。 |
| 37 | +
|
| 38 | +```admonish warning title="注意" |
| 39 | +`MarkdownDisplay` の例は Lean 4 Playground では動作しないようです。VSCode 上で試してみてください。 |
| 40 | +``` |
| 41 | +-/ |
| 42 | +section --# |
| 43 | + |
| 44 | +open ProofWidgets |
| 45 | + |
| 46 | +-- Markdown と TeX を表示する |
| 47 | +#html <MarkdownDisplay contents={" |
| 48 | + ## Riemann zeta function |
| 49 | +
|
| 50 | + The Riemann zeta function is defined as |
| 51 | + $$ |
| 52 | + \\zeta(s) = \\sum_{n=1}^∞ \\frac{1}{n^s} |
| 53 | + $$ |
| 54 | +
|
| 55 | + for $\\mathrm{Re} (s) > 0$. |
| 56 | +"}/> |
| 57 | + |
| 58 | +end --# |
| 59 | +/- ## GraphDisplay |
| 60 | +
|
| 61 | +`<GraphDisplay />` コンポーネントを使用すると、有向グラフを表示させることができます。 |
| 62 | +-/ |
| 63 | +section --# |
| 64 | + |
| 65 | +open ProofWidgets Jsx GraphDisplay |
| 66 | + |
| 67 | +/-- `Edge` を作る -/ |
| 68 | +def mkEdge (st : String × String) : Edge := {source := st.1, target := st.2} |
| 69 | + |
| 70 | +/-- 文字列として与えられたラベルから `Vertex` を作る -/ |
| 71 | +def mkVertex (id : String) : Vertex := {id := id} |
| 72 | + |
| 73 | +-- 有向グラフを表示する |
| 74 | +#html |
| 75 | + <GraphDisplay |
| 76 | + vertices={#["a", "b", "c", "d", "e"].map mkVertex} |
| 77 | + edges={#[("a","b"), ("b","c"), ("c","d"), ("d","e"), ("e", "a")].map mkEdge} |
| 78 | + /> |
| 79 | + |
| 80 | +end --# |
| 81 | +/- ## LineChart |
| 82 | +
|
| 83 | +ProofWidgets4 には [Recharts ライブラリ](https://recharts.org/en-US) に対するサポートがあり、`<LineChart />` コンポーネントを使用すると、関数のグラフを表示させることができます。 |
| 84 | +-/ |
| 85 | +section --# |
| 86 | + |
| 87 | +open Lean ProofWidgets Recharts |
| 88 | + |
| 89 | +open scoped ProofWidgets.Jsx in |
| 90 | + |
| 91 | +/-- 与えられた関数 `fn` の `[0, 1]` 区間上での値のグラフ -/ |
| 92 | +def Plot (fn : Float → Float) (steps := 100) : Html := |
| 93 | + -- `[0, 1)` 区間を `steps` 個に分割する |
| 94 | + let grids := Array.range steps |
| 95 | + |>.map (fun x => x.toFloat / steps.toFloat) |
| 96 | + |
| 97 | + -- データを JSON に変換 |
| 98 | + let y := grids.map fn |
| 99 | + let jsonData : Array Json := grids.zip y |
| 100 | + |>.map (fun (x,y) => json% {x: $(toJson x) , y: $(toJson y)}); |
| 101 | + |
| 102 | + <LineChart width={400} height={400} data={jsonData}> |
| 103 | + <XAxis dataKey?="x" /> |
| 104 | + <YAxis dataKey?="y" /> |
| 105 | + <Line type={.monotone} dataKey="y" stroke="#8884d8" dot?={Bool.false} /> |
| 106 | + </LineChart> |
| 107 | + |
| 108 | +#html Plot (fun x => (x - 0.3) ^ 2 + 0.1) |
| 109 | +#html Plot (fun x => 0.2 + 0.5 * Float.sin (7 * x)) |
| 110 | + |
| 111 | +end --# |
0 commit comments