garywelz/programming_framework
0
1#!/usr/bin/env node2/**3 * Build Peano Arithmetic discourse JSON and Mermaid.4 * Based on Landau, Foundations of Analysis (1930) and standard Peano development.5 * Structure: 5 axioms → definitions (addition, multiplication) → theorems.6 */7 8const fs = require('fs');9const path = require('path');10 11const NODES = [12 { id: "A1", type: "axiom", label: "0 is a natural number", short: "0 ∈ N", colorClass: "axiom" },13 { id: "A2", type: "axiom", label: "No predecessor of 0: S(x) ≠ 0", short: "0 not a successor", colorClass: "axiom" },14 { id: "A3", type: "axiom", label: "Successor injective: S(x)=S(y) ⇒ x=y", short: "S injective", colorClass: "axiom" },15 { id: "A4", type: "axiom", label: "Closure: S(x) ∈ N for all x ∈ N", short: "N closed under S", colorClass: "axiom" },16 { id: "A5", type: "axiom", label: "Induction: if 0∈K and (x∈K⇒S(x)∈K) then K=N", short: "Induction", colorClass: "axiom" },17 { id: "T1", type: "theorem", label: "x≠y ⇒ S(x)≠S(y)", short: "Contrapositive of A3", colorClass: "theorem" },18 { id: "T2", type: "theorem", label: "S(x)≠x for all x", short: "Successor ≠ identity", colorClass: "theorem" },19 { id: "T3", type: "theorem", label: "If x≠0 then x=S(u) for some u", short: "Every nonzero is successor", colorClass: "theorem" },20 { id: "DefAdd", type: "definition", label: "Addition: x+0=x, x+S(y)=S(x+y)", short: "Definition of +", colorClass: "definition" },21 { id: "T4", type: "theorem", label: "Addition is well-defined for all x,y", short: "Add well-defined", colorClass: "theorem" },22 { id: "T5", type: "theorem", label: "(x+y)+z = x+(y+z)", short: "Associativity of +", colorClass: "theorem" },23 { id: "T6", type: "theorem", label: "0+x = x", short: "Left identity", colorClass: "theorem" },24 { id: "T7", type: "theorem", label: "S(x)+y = S(x+y)", short: "Successor and add", colorClass: "theorem" },25 { id: "T8", type: "theorem", label: "x+y = y+x", short: "Commutativity of +", colorClass: "theorem" },26 { id: "T9", type: "theorem", label: "x+y=x+z ⇒ y=z", short: "Cancellation for +", colorClass: "theorem" },27 { id: "DefMul", type: "definition", label: "Multiplication: x·0=0, x·S(y)=x·y+x", short: "Definition of ·", colorClass: "definition" },28 { id: "T10", type: "theorem", label: "Multiplication is well-defined for all x,y", short: "Mul well-defined", colorClass: "theorem" },29 { id: "T11", type: "theorem", label: "x·0 = 0", short: "Zero times", colorClass: "theorem" },30 { id: "T12", type: "theorem", label: "0·x = 0", short: "Zero from left", colorClass: "theorem" },31 { id: "T13", type: "theorem", label: "S(x)·y = x·y + y", short: "Successor and mul", colorClass: "theorem" },32 { id: "T14", type: "theorem", label: "x·y = y·x", short: "Commutativity of ·", colorClass: "theorem" },33 { id: "T15", type: "theorem", label: "(x·y)·z = x·(y·z)", short: "Associativity of ·", colorClass: "theorem" },34 { id: "T16", type: "theorem", label: "x·(y+z) = x·y + x·z", short: "Distributivity", colorClass: "theorem" },35 { id: "T17", type: "theorem", label: "(x+y)·z = x·z + y·z", short: "Distributivity (right)", colorClass: "theorem" },36 { id: "T18", type: "theorem", label: "x≤y iff ∃z x+z=y", short: "Order definition", colorClass: "theorem" },37 { id: "T19", type: "theorem", label: "Trichotomy: exactly one of x<y, x=y, y<x", short: "Trichotomy", colorClass: "theorem" },38 { id: "T20", type: "theorem", label: "x≤y ⇒ x+z≤y+z", short: "Order + add", colorClass: "theorem" },39 { id: "T21", type: "theorem", label: "x≤y and z>0 ⇒ x·z≤y·z", short: "Order + mul", colorClass: "theorem" },40 { id: "T22", type: "theorem", label: "1·x = x (where 1=S(0))", short: "Multiplicative identity", colorClass: "theorem" },41 { id: "T23", type: "theorem", label: "x·1 = x", short: "Right identity", colorClass: "theorem" },42 { id: "T24", type: "theorem", label: "Well-ordering: every nonempty subset has least element", short: "Well-ordering", colorClass: "theorem" },43 { id: "T25", type: "theorem", label: "Strong induction principle", short: "Strong induction", colorClass: "theorem" }44];45 46// Dependencies (from → to). Based on Landau's development.47const DEPS = {48 T1: ["A3"],49 T2: ["A1", "A2", "A3", "T1", "A5"],50 T3: ["A5"],51 DefAdd: ["A5"],52 T4: ["DefAdd", "A5"],53 T5: ["DefAdd", "A5"],54 T6: ["DefAdd", "A5"],55 T7: ["DefAdd", "T6", "A5"],56 T8: ["DefAdd", "T5", "T6", "T7", "A5"],57 T9: ["DefAdd", "T8", "A5"],58 DefMul: ["DefAdd", "A5"],59 T10: ["DefMul", "A5"],60 T11: ["DefMul"],61 T12: ["DefMul", "T6", "A5"],62 T13: ["DefMul", "T8", "A5"],63 T14: ["DefMul", "T12", "T13", "A5"],64 T15: ["DefMul", "T5", "T8", "A5"],65 T16: ["DefMul", "T5", "T8", "T15", "A5"],66 T17: ["T16", "T8"],67 T18: ["DefAdd", "T8", "T9"],68 T19: ["T18", "T9"],69 T20: ["T18", "T8"],70 T21: ["T18", "T16", "T14"],71 T22: ["DefMul", "T6", "A5"],72 T23: ["T14", "T22"],73 T24: ["T18", "T19", "A5"],74 T25: ["T18", "T24", "A5"]75};76 77const discourse = {78 schemaVersion: "1.0",79 discourse: {80 id: "peano-arithmetic",81 name: "Peano Arithmetic",82 subject: "arithmetic",83 variant: "classical",84 description: "Axiomatic development of natural number arithmetic. Five axioms, definitions of addition and multiplication, and key theorems (associativity, commutativity, distributivity, order). Based on Landau, Foundations of Analysis.",85 structure: { axioms: 5, definitions: 2, theorems: 25 }86 },87 metadata: {88 created: "2026-03-15",89 lastUpdated: "2026-03-15",90 version: "1.0.0",91 license: "CC BY 4.0",92 authors: ["Welz, G."],93 methodology: "Programming Framework",94 citation: "Welz, G. (2026). Peano Arithmetic Dependency Graph. Programming Framework.",95 keywords: ["Peano", "arithmetic", "natural numbers", "induction", "foundations"]96 },97 sources: [98 { id: "landau", type: "primary", authors: "Landau, E.", title: "Foundations of Analysis", year: "1930", publisher: "Chelsea", edition: "1951", notes: "Canonical development" },99 { id: "wikipedia", type: "digital", title: "Peano axioms", url: "https://en.wikipedia.org/wiki/Peano_axioms", notes: "Overview and definitions" }100 ],101 nodes: [],102 edges: [],103 colorScheme: {104 axiom: { fill: "#e74c3c", stroke: "#c0392b" },105 definition: { fill: "#3498db", stroke: "#2980b9" },106 theorem: { fill: "#1abc9c", stroke: "#16a085" }107 }108};109 110// Add nodes111for (const n of NODES) {112 discourse.nodes.push({113 id: n.id,114 type: n.type,115 label: n.label,116 shortLabel: n.id,117 short: n.short,118 colorClass: n.colorClass119 });120 for (const dep of DEPS[n.id] || []) {121 discourse.edges.push({ from: dep, to: n.id });122 }123}124 125// Write JSON126const dataDir = path.join(__dirname, "..", "data");127const outPath = path.join(dataDir, "peano-arithmetic.json");128fs.mkdirSync(dataDir, { recursive: true });129fs.writeFileSync(outPath, JSON.stringify(discourse, null, 2), "utf8");130console.log("Wrote", outPath);131 132// Generate Mermaid133function toMermaid(filter) {134 const nodes = filter ? discourse.nodes.filter(filter) : discourse.nodes;135 const nodeIds = new Set(nodes.map(n => n.id));136 const edges = discourse.edges.filter(e => nodeIds.has(e.from) && nodeIds.has(e.to));137 const lines = ["graph TD"];138 for (const n of nodes) {139 const desc = n.short || n.label;140 const lbl = (n.shortLabel || n.id) + "\\n" + (desc.length > 30 ? desc.slice(0, 27) + "..." : desc);141 lines.push(` ${n.id}["${String(lbl).replace(/"/g, '\\"')}"]`);142 }143 for (const e of edges) {144 lines.push(` ${e.from} --> ${e.to}`);145 }146 lines.push(" classDef axiom fill:#e74c3c,color:#fff,stroke:#c0392b");147 lines.push(" classDef definition fill:#3498db,color:#fff,stroke:#2980b9");148 lines.push(" classDef theorem fill:#1abc9c,color:#fff,stroke:#16a085");149 const axiomIds = nodes.filter(n => n.type === "axiom").map(n => n.id).join(",");150 const defIds = nodes.filter(n => n.type === "definition").map(n => n.id).join(",");151 const thmIds = nodes.filter(n => n.type === "theorem").map(n => n.id).join(",");152 lines.push(` class ${axiomIds} axiom`);153 lines.push(` class ${defIds} definition`);154 lines.push(` class ${thmIds} theorem`);155 return lines.join("\n");156}157 158function closure(ids) {159 const needed = new Set(ids);160 let changed = true;161 while (changed) {162 changed = false;163 for (const e of discourse.edges) {164 if (needed.has(e.to) && !needed.has(e.from)) { needed.add(e.from); changed = true; }165 }166 }167 return n => needed.has(n.id);168}169 170function toMermaidWithCounts(filter) {171 const nodes = filter ? discourse.nodes.filter(filter) : discourse.nodes;172 const nodeIds = new Set(nodes.map(n => n.id));173 const edges = discourse.edges.filter(e => nodeIds.has(e.from) && nodeIds.has(e.to));174 return { mermaid: toMermaid(filter), nodes: nodes.length, edges: edges.length };175}176 177// 3 sections: ~10 each178const sections = [179 { name: "axioms-foundations", ids: ["A1","A2","A3","A4","A5","T1","T2","T3","DefAdd","T4","T5","T6"], title: "Axioms & Addition Foundations", desc: "Five Peano axioms, basic successor theorems, definition of addition, associativity and left identity" },180 { name: "addition-multiplication", ids: ["T7","T8","T9","DefMul","T10","T11","T12","T13","T14","T15","T16","T17"], title: "Commutativity, Multiplication, Distributivity", desc: "Commutativity and cancellation of addition, definition of multiplication, commutativity and associativity of multiplication, distributivity" },181 { name: "order-induction", ids: ["T18","T19","T20","T21","T22","T23","T24","T25"], title: "Order & Induction", desc: "Order relation, trichotomy, compatibility with operations, well-ordering, strong induction" }182];183 184const subgraphData = [];185for (const s of sections) {186 const filter = closure(s.ids);187 const { mermaid: sub, nodes: n, edges: e } = toMermaidWithCounts(filter);188 subgraphData.push({ ...s, mermaid: sub, nodes: n, edges: e });189 fs.writeFileSync(path.join(dataDir, `peano-arithmetic-${s.name}.mmd`), sub, "utf8");190 console.log("Wrote", path.join(dataDir, `peano-arithmetic-${s.name}.mmd`));191}192 193// Full graph194fs.writeFileSync(path.join(dataDir, "peano-arithmetic.mmd"), toMermaid(), "utf8");195 196// Generate HTML197const MATH_DB = process.env.MATH_DB || "/home/gdubs/copernicus-web-public/huggingface-space/mathematics-processes-database";198const NUM_DIR = path.join(MATH_DB, "processes", "number_theory");199 200function htmlTemplate(title, subtitle, mermaid, nodes, edges) {201 const mermaidEscaped = mermaid.replace(/</g, "<").replace(/>/g, ">");202 return `<!DOCTYPE html>203<html lang="en">204<head>205 <meta charset="UTF-8">206 <meta name="viewport" content="width=device-width, initial-scale=1.0">207 <title>${title} - Mathematics Process</title>208 <script src="https://cdn.jsdelivr.net/npm/mermaid@10.6.1/dist/mermaid.min.js"></script>209 <style>210 * { margin: 0; padding: 0; box-sizing: border-box; }211 body { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); min-height: 100vh; padding: 20px; }212 .container { max-width: 1600px; margin: 0 auto; background: white; border-radius: 15px; box-shadow: 0 20px 40px rgba(0,0,0,0.1); overflow: hidden; }213 .header { background: linear-gradient(135deg, #27ae60 0%, #27ae60dd 100%); color: white; padding: 30px; }214 .header h1 { margin: 0 0 10px 0; font-size: 2em; font-weight: 300; }215 .header-meta { display: flex; flex-wrap: wrap; gap: 15px; margin-top: 15px; font-size: 0.9em; opacity: 0.9; }216 .meta-item { background: rgba(255,255,255,0.2); padding: 5px 12px; border-radius: 20px; }217 .nav-links { padding: 15px 30px; background: #f8f9fa; border-bottom: 1px solid #ecf0f1; }218 .nav-links a { color: #27ae60; text-decoration: none; margin-right: 20px; font-weight: 500; }219 .nav-links a:hover { text-decoration: underline; }220 .content { padding: 30px; }221 .description { margin-bottom: 30px; }222 .flowchart-container { margin: 30px 0; }223 .flowchart-container h2 { color: #2c3e50; margin-bottom: 15px; }224 .mermaid { background: white; padding: 20px; border-radius: 10px; border: 1px solid #ecf0f1; overflow-x: hidden; overflow-y: auto; min-height: 500px; max-width: 100%; }225 .color-legend { background: #f8f9fa; padding: 20px; border-radius: 10px; margin: 30px 0; }226 .color-legend h3 { color: #2c3e50; margin-bottom: 15px; }227 .color-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 15px; }228 .color-item { display: flex; align-items: center; gap: 10px; padding: 10px; background: white; border-radius: 5px; }229 .color-box { width: 30px; height: 30px; border-radius: 4px; border: 1px solid #ddd; }230 .info-section { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 20px; margin-top: 30px; }231 .info-card { background: #f8f9fa; padding: 20px; border-radius: 10px; }232 .info-card h3 { color: #2c3e50; margin-bottom: 15px; }233 .info-card ul { list-style: none; padding: 0; }234 .info-card li { padding: 8px 0; border-bottom: 1px solid #ecf0f1; }235 .info-card li:last-child { border-bottom: none; }236 </style>237</head>238<body>239 <div class="container">240 <div class="header">241 <h1>${title}</h1>242 <div class="header-meta">243 <span class="meta-item">Mathematics</span>244 <span class="meta-item">Foundations / Arithmetic</span>245 <span class="meta-item">Source: Landau, Peano</span>246 </div>247 </div>248 <div class="nav-links">249 <a id="back-link" href="#">← Back to Mathematics Database</a>250 <a id="index-link" href="#">Peano Arithmetic Index</a>251 <a href="https://en.wikipedia.org/wiki/Peano_axioms" target="_blank">Peano Axioms (Wikipedia)</a>252 <a href="https://huggingface.co/spaces/garywelz/programming_framework" target="_blank">Programming Framework</a>253 </div>254 <script>255 (function() {256 const hostname = window.location.hostname;257 const base = hostname.includes('storage.googleapis.com')258 ? 'https://storage.googleapis.com/regal-scholar-453620-r7-podcast-storage/mathematics-processes-database'259 : '../..';260 document.getElementById('back-link').href = base + '/mathematics-database-table.html';261 document.getElementById('index-link').href = base + '/processes/number_theory/number_theory-peano-arithmetic.html';262 })();263 </script>264 <div class="content">265 <div class="description">266 <h2>Description</h2>267 <p>${subtitle}</p>268 <p style="margin-top:10px;"><em>Source: Landau, E. <a href="https://en.wikipedia.org/wiki/Peano_axioms" target="_blank">Foundations of Analysis</a> (1930); Peano, G. Arithmetices principia (1889)</em></p>269 </div>270 <div class="flowchart-container">271 <h2>Dependency Flowchart</h2>272 <p class="flowchart-note" style="font-size:0.9rem;color:#7f8c8d;margin-bottom:12px;"><strong>Note:</strong> Arrows mean "depends on" (tail → head).</p>273 <div class="mermaid">${mermaidEscaped}</div>274 </div>275 <div class="color-legend">276 <h3>Color Scheme</h3>277 <div class="color-grid">278 <div class="color-item"><div class="color-box" style="background:#e74c3c"></div><div><strong>Red</strong><br><small>Axioms</small></div></div>279 <div class="color-item"><div class="color-box" style="background:#3498db"></div><div><strong>Blue</strong><br><small>Definitions</small></div></div>280 <div class="color-item"><div class="color-box" style="background:#1abc9c"></div><div><strong>Teal</strong><br><small>Theorems</small></div></div>281 </div>282 </div>283 <div class="info-section">284 <div class="info-card">285 <h3>Statistics</h3>286 <ul>287 <li><strong>Nodes:</strong> ${nodes}</li>288 <li><strong>Edges:</strong> ${edges}</li>289 </ul>290 </div>291 <div class="info-card">292 <h3>Keywords</h3>293 <ul>294 <li>Peano</li><li>arithmetic</li><li>natural numbers</li><li>induction</li><li>successor</li><li>foundations</li>295 </ul>296 </div>297 </div>298 </div>299 </div>300 <script>301 mermaid.initialize({ startOnLoad: true, theme: 'default', flowchart: { useMaxWidth: true, htmlLabels: true, curve: 'step', nodeSpacing: 25, rankSpacing: 90, padding: 20 }, themeVariables: { fontSize: '14px', fontFamily: 'Segoe UI, Arial, sans-serif' } });302 </script>303</body>304</html>`;305}306 307if (fs.existsSync(path.join(MATH_DB, "processes"))) {308 for (const d of subgraphData) {309 const html = htmlTemplate(310 `Peano Arithmetic — ${d.title}`,311 d.desc + ". Shows how theorems depend on axioms, definitions, and prior theorems.",312 d.mermaid,313 d.nodes,314 d.edges315 );316 const fileName = "number_theory-peano-arithmetic-" + d.name;317 fs.writeFileSync(path.join(NUM_DIR, fileName + ".html"), html, "utf8");318 console.log("Wrote", path.join(NUM_DIR, fileName + ".html"));319 }320 // Index page321 const indexHtml = `<!DOCTYPE html>322<html lang="en">323<head>324 <meta charset="UTF-8">325 <meta name="viewport" content="width=device-width, initial-scale=1.0">326 <title>Peano Arithmetic - Mathematics Process</title>327 <style>328 * { margin: 0; padding: 0; box-sizing: border-box; }329 body { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); min-height: 100vh; padding: 20px; }330 .container { max-width: 900px; margin: 0 auto; background: white; border-radius: 15px; box-shadow: 0 20px 40px rgba(0,0,0,0.1); overflow: hidden; padding: 30px; }331 h1 { color: #2c3e50; margin-bottom: 15px; }332 p { color: #555; margin-bottom: 25px; line-height: 1.6; }333 .nav-links { margin-bottom: 20px; }334 .nav-links a { color: #27ae60; text-decoration: none; margin-right: 20px; font-weight: 500; }335 .nav-links a:hover { text-decoration: underline; }336 .sections { display: grid; gap: 15px; }337 .sections a { display: block; padding: 20px; background: #f8f9fa; border-radius: 10px; color: #2c3e50; text-decoration: none; font-weight: 500; border-left: 4px solid #27ae60; }338 .sections a:hover { background: #ecf0f1; }339 </style>340</head>341<body>342 <div class="container">343 <div class="nav-links">344 <a id="back-link" href="#">← Back to Mathematics Database</a>345 <a href="https://en.wikipedia.org/wiki/Peano_axioms" target="_blank">Peano Axioms (Wikipedia)</a>346 </div>347 <script>348 (function() {349 const backLink = document.getElementById('back-link');350 backLink.href = window.location.hostname.includes('storage.googleapis.com')351 ? 'https://storage.googleapis.com/regal-scholar-453620-r7-podcast-storage/mathematics-processes-database/mathematics-database-table.html'352 : '../../mathematics-database-table.html';353 })();354 </script>355 <h1>Peano Arithmetic</h1>356 <p>Axiomatic development of natural number arithmetic. Five axioms, definitions of addition and multiplication, and key theorems. Based on Landau, Foundations of Analysis. Split into three views.</p>357 <div class="sections">358 <a href="number_theory-peano-arithmetic-axioms-foundations.html">Chart 1 — Axioms & Addition Foundations</a>359 <a href="number_theory-peano-arithmetic-addition-multiplication.html">Chart 2 — Commutativity, Multiplication, Distributivity</a>360 <a href="number_theory-peano-arithmetic-order-induction.html">Chart 3 — Order & Induction</a>361 </div>362 </div>363</body>364</html>`;365 fs.writeFileSync(path.join(NUM_DIR, "number_theory-peano-arithmetic.html"), indexHtml, "utf8");366 console.log("Wrote", path.join(NUM_DIR, "number_theory-peano-arithmetic.html"));367} else {368 console.log("MATH_DB not found - skipping HTML generation.");369}370 371console.log("Done. Nodes:", discourse.nodes.length, "Edges:", discourse.edges.length);372 