{
 "id": "markov-junior-rewrite-rules",
 "title": "MarkovJunior (Rewrite-Rule Generation)",
 "aliases": [
  "MarkovJunior",
  "probabilistic rewrite rules"
 ],
 "domain": "procgen",
 "subdomain": "constraint-based synthesis",
 "stage": "procedural",
 "realtime": "interactive",
 "platforms": [
  "cpu"
 ],
 "year_introduced": 2022,
 "inputs": [
  "layout"
 ],
 "outputs": [
  "voxels",
  "texture",
  "layout"
 ],
 "style": "both",
 "dynamic": "static",
 "summary": "A probabilistic programming language for grid generation: sequences of pattern-rewrite rules, optionally guided by inference toward goal states, with WFC available as one of its node types.",
 "how_it_works": "A model is a tree of nodes executed over a grid of colours. A rewrite node has an input pattern and output pattern, like 'RB -> WW'; at each step it picks a random matching location and rewrites it. Nodes are composed with sequential, Markov (repeat until nothing applies) and one-shot semantics, giving growth processes, maze carvers and flood fills. Inference nodes use constraint propagation and search to steer rewriting toward a goal state, such as a path connecting two points, rather than hoping random rewriting finds it. Also includes a WFC node and a convolution node. Models are XML descriptions running in a C# interpreter that renders 2D and 3D output.",
 "when_to_use": "Choose it when you want to prototype grid-based generators quickly (mazes, dungeons, voxel buildings, growth patterns) with explicit, readable rules and goal inference. Prefer `wave-function-collapse` when you have an example to imitate, and `bsp-dungeon-generation` or `l-systems` when a hand-written recursive procedure is clearer than a rule database.",
 "cost": {
  "class": "offline",
  "notes": "Interpreter cost scales with grid size and rule count; inference nodes add search cost. Designed for authoring, not per-frame use."
 },
 "failure_modes": [
  "Rule order and node semantics are subtle: a misplaced Markov node can loop forever or terminate immediately",
  "Inference can fail to find any solution, leaving the run in an unsolved state",
  "Output is grid-bound; exporting to meshes needs an extra meshing step",
  "Random rewriting without inference often misses global goals like connectivity"
 ],
 "sources": [
  {
   "role": "primary",
   "key": "gh:mxgmn/markovjunior",
   "type": "code",
   "title": "mxgmn/MarkovJunior",
   "description": "Probabilistic language based on pattern matching and constraint propagation, 153 examples",
   "authors": [
    "mxgmn"
   ],
   "year": 2022,
   "venue": "GitHub",
   "url": "https://github.com/mxgmn/MarkovJunior",
   "ids": {
    "repo": "mxgmn/MarkovJunior"
   },
   "license": "MIT",
   "extra": {
    "stars": 8621,
    "stars_checked": "2026-11-29",
    "archived": false,
    "fork": false,
    "pushed_at": "2026-03-20T00:05:35Z",
    "language": "C#",
    "homepage": null,
    "license_name": "MIT License"
   },
   "provenance": {
    "title": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/mxgmn/MarkovJunior",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "authors": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/mxgmn/MarkovJunior",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "year": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/mxgmn/MarkovJunior",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "venue": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/mxgmn/MarkovJunior",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "url": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/mxgmn/MarkovJunior",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "license": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/mxgmn/MarkovJunior",
     "checked_at": "2026-11-29T15:51:27.588Z"
    }
   },
   "status": "verified",
   "checked_at": "2026-11-29T15:51:27.588Z",
   "title_sim": null,
   "__bib": "mxgmn2022mxgmn"
  },
  {
   "role": "implementation",
   "key": "gh:mxgmn/markovjunior",
   "type": "code",
   "title": "mxgmn/MarkovJunior",
   "description": "Probabilistic language based on pattern matching and constraint propagation, 153 examples",
   "authors": [
    "mxgmn"
   ],
   "year": 2022,
   "venue": "GitHub",
   "url": "https://github.com/mxgmn/MarkovJunior",
   "ids": {
    "repo": "mxgmn/MarkovJunior"
   },
   "license": "MIT",
   "extra": {
    "stars": 8621,
    "stars_checked": "2026-11-29",
    "archived": false,
    "fork": false,
    "pushed_at": "2026-03-20T00:05:35Z",
    "language": "C#",
    "homepage": null,
    "license_name": "MIT License"
   },
   "provenance": {
    "title": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/mxgmn/MarkovJunior",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "authors": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/mxgmn/MarkovJunior",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "year": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/mxgmn/MarkovJunior",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "venue": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/mxgmn/MarkovJunior",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "url": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/mxgmn/MarkovJunior",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "license": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/mxgmn/MarkovJunior",
     "checked_at": "2026-11-29T15:51:27.588Z"
    }
   },
   "status": "verified",
   "checked_at": "2026-11-29T15:51:27.588Z",
   "title_sim": null,
   "__bib": "mxgmn2022mxgmn"
  }
 ],
 "presets": [
  {
   "id": "markov-junior-rewrite-rules-live",
   "title": "MarkovJunior — discrete symbol rewrite grid (left) vs sculpted dungeon tile architecture (right)",
   "runtime": "glsl-fragment",
   "params": {
    "P_GRID_RES": 16,
    "P_RULE_STAGE": 0.78,
    "P_ANIM_RATE": 0.85,
    "P_SPLIT": 0.46
   },
   "basis": "original",
   "license_note": "MIT — written for this index.",
   "code": "const float P_GRID_RES = 16.0; // P Grid Resolution [10.0, 26.0, 2.0]\nconst float P_RULE_STAGE = 0.78; // P Rewrite Progress [0.1, 1.0, 0.02]\nconst float P_ANIM_RATE = 0.85; // P Rewrite Sweep [0.0, 2.5, 0.05]\nconst float P_SPLIT = 0.46; // P Symbols / Tiles [0.0, 1.0, 0.02]\n\nfloat hash21(vec2 p) {\n  p = fract(p * vec2(127.1, 311.7));\n  p += dot(p, p + 34.5);\n  return fract(p.x * p.y);\n}\n\n// Deterministic MarkovJunior-style multi-stage grid symbol classifier:\n// Returns symbol ID: 0=void/black, 1=corridor/white, 2=room/gold, 3=river/cyan, 4=wall/crimson\nfloat evalMarkovCell(vec2 cell, float stage) {\n  // Stage 1: Growing orthogonal maze corridors from central seed\n  vec2 macro = floor(cell * 0.5);\n  float h = hash21(macro);\n  float isCorrX = step(0.5, mod(cell.y, 2.0)) * step(0.32, h);\n  float isCorrY = step(0.5, mod(cell.x, 2.0)) * step(h, 0.68);\n  float sym = max(isCorrX, isCorrY);\n\n  // Stage 2: Room-carving rewrite rule (B-W-W -> R-R-R) in seeded chambers\n  vec2 roomCell = floor(cell / 5.0);\n  vec2 roomLocal = mod(cell, 5.0);\n  if (hash21(roomCell + 7.3) > 0.45 && roomLocal.x >= 1.0 && roomLocal.x <= 3.0 && roomLocal.y >= 1.0 && roomLocal.y <= 3.0) {\n    if (stage > 0.35) sym = 2.0;\n  }\n\n  // Stage 3: Winding river rewrite rule cutting through grid\n  float riverX = floor(sin(cell.y * 0.38) * 2.2 + 0.5);\n  if (abs(cell.x - riverX) < 0.6 && stage > 0.60) {\n    sym = 3.0;\n  }\n\n  // Stage 4: Wall-border rewrite rule: any black cell adjacent to corridor/room becomes wall (4)\n  if (sym < 0.5 && stage > 0.25) {\n    float n1 = step(0.5, mod(cell.y + 1.0, 2.0));\n    float n2 = step(0.5, mod(cell.x + 1.0, 2.0));\n    if (n1 + n2 > 0.5) sym = 4.0;\n  }\n  return sym;\n}\n\nvec3 aces(vec3 x) {\n  x = max(x, 0.0);\n  return (x * (2.51 * x + 0.03)) / (x * (2.43 * x + 0.59) + 0.14);\n}\n\nvoid mainImage(out vec4 fragColor, in vec2 fragCoord) {\n  vec2 uv = (fragCoord - 0.5 * iResolution.xy) / iResolution.y;\n  float screenX = fragCoord.x / max(iResolution.x, 1.0);\n  float split = clamp(P_SPLIT, 0.0, 1.0);\n  bool isSymbols = screenX < split;\n  float gridRes = clamp(floor(P_GRID_RES + 0.5), 8.0, 28.0);\n  float stage = clamp(P_RULE_STAGE, 0.05, 1.0);\n  float sweep = iTime * max(P_ANIM_RATE, 0.0);\n\n  vec2 g = uv * gridRes;\n  vec2 cell = floor(g);\n  vec2 f = fract(g);\n\n  // Expanding Markov rewrite wave radiating outward from seed origin\n  float distSeed = length(cell) / (gridRes * 0.65);\n  float activeWave = fract(sweep * 0.16) * 1.35 + stage * 0.55;\n  float cellStage = stage * smoothstep(activeWave + 0.18, activeWave - 0.08, distSeed);\n  float frontier = exp(-abs(distSeed - activeWave) * 18.0);\n\n  float sym = evalMarkovCell(cell, cellStage);\n  vec2 dEdge = min(f, 1.0 - f);\n  float border = smoothstep(0.08, 0.02, min(dEdge.x, dEdge.y));\n\n  vec3 col;\n  if (isSymbols) {\n    // Left: Pure MarkovJunior flat color-coded symbol grid (classic B/W/R/U/Y palette) + active rewrite cursor\n    vec3 symCol = vec3(0.06, 0.07, 0.10); // 0 = black void\n    if (sym > 0.5 && sym < 1.5) symCol = vec3(0.90, 0.92, 0.96); // 1 = white path\n    else if (sym > 1.5 && sym < 2.5) symCol = vec3(0.98, 0.76, 0.20); // 2 = gold room\n    else if (sym > 2.5 && sym < 3.5) symCol = vec3(0.14, 0.80, 0.96); // 3 = cyan water\n    else if (sym > 3.5) symCol = vec3(0.84, 0.24, 0.26); // 4 = crimson wall\n\n    col = mix(symCol, vec3(0.04, 0.05, 0.08), border * 0.65);\n    col += vec3(0.32, 1.0, 0.72) * frontier * 0.55;\n  } else {\n    // Right: Rendered beveled dungeon tiles & animated water from the rewritten symbol grid!\n    float bevel = smoothstep(0.0, 0.18, min(dEdge.x, dEdge.y));\n    vec2 slope = vec2(smoothstep(0.82, 1.0, f.x) - smoothstep(0.18, 0.0, f.x),\n                      smoothstep(0.82, 1.0, f.y) - smoothstep(0.18, 0.0, f.y));\n    vec3 n = normalize(vec3(-slope.x * 1.4, -slope.y * 1.4, 1.0));\n    vec3 lDir = normalize(vec3(0.55, 0.65, 0.82));\n    float diff = max(dot(n, lDir), 0.0);\n\n    vec3 tileCol = vec3(0.05, 0.06, 0.09);\n    if (sym > 0.5 && sym < 1.5) {\n      // Carved stone corridor flagstones\n      tileCol = vec3(0.46, 0.48, 0.52) * (0.45 + 0.65 * diff) * (0.75 + 0.25 * bevel);\n    } else if (sym > 1.5 && sym < 2.5) {\n      // Warm inlaid golden mosaic room floor\n      float inlay = smoothstep(0.28, 0.20, length(f - 0.5));\n      tileCol = mix(vec3(0.68, 0.52, 0.26), vec3(0.96, 0.80, 0.34), inlay) * (0.45 + 0.65 * diff);\n    } else if (sym > 2.5 && sym < 3.5) {\n      // Flowing subterranean canal water\n      float rip = 0.5 + 0.5 * sin(g.y * 6.0 - sweep * 4.5 + sin(g.x * 4.0));\n      tileCol = mix(vec3(0.06, 0.42, 0.62), vec3(0.22, 0.86, 0.96), rip * 0.65 + 0.25);\n    } else if (sym > 3.5) {\n      // Raised masonry dungeon wall parapet\n      float brick = step(0.10, fract(f.x * 2.0 + step(0.5, f.y) * 0.5));\n      tileCol = mix(vec3(0.26, 0.16, 0.18), vec3(0.54, 0.28, 0.26), brick) * (0.38 + 0.82 * diff);\n    }\n    col = tileCol + vec3(0.28, 0.95, 0.82) * frontier * 0.25;\n  }\n\n  float sep = smoothstep(0.0035, 0.0008, abs(screenX - split));\n  col = mix(col, vec3(0.95, 0.97, 1.0), sep);\n\n  fragColor = vec4(pow(clamp(aces(col), 0.0, 1.0), vec3(1.0 / 2.2)), 1.0);\n}",
   "execution_fidelity": "faithful-pass",
   "proof": {
    "kind": "rendered",
    "label": "rendered 2026-11-30",
    "ok": true,
    "proof": {
     "ok": true,
     "technique": "markov-junior-rewrite-rules",
     "runtime": "glsl-fragment",
     "checked_at": "2026-11-30T20:35:52.000Z",
     "renderer": "Headless EGL Tesla T4/PCIe/SSE2, OpenGL 3.30 Core, 512×512, iTime=1.5",
     "stats": {
      "mean": 0.743,
      "std": 0.1499,
      "min": 0.2657,
      "max": 0.9241
     },
     "image": "/specimens/markov-junior-rewrite-rules-live.webp",
     "reason": null
    }
   }
  }
 ],
 "related": [
  "wave-function-collapse",
  "model-synthesis",
  "bsp-dungeon-generation",
  "l-systems"
 ],
 "extends": [
  "wave-function-collapse"
 ],
 "supersedes": [],
 "combines_with": [],
 "provenance": {
  "prose": "original text written for this index (research draft, editorially reviewed)",
  "sources_verified_at": "2026-11-29T15:51:27.588Z",
  "draft": "data/drafts/procgen/markov-junior-rewrite-rules.json"
 },
 "solves": [
  "prototype a dungeon or maze generator with readable rules",
  "grow voxel structures from simple pattern rewrites",
  "grid generation with goals like a path from a to b",
  "procedural content without an example to imitate",
  "generate grid patterns guided by constraints"
 ],
 "authority": "mxgmn, 2022",
 "url": "https://texelatlas.vercel.app/t/markov-junior-rewrite-rules/",
 "execution_fidelity": "faithful-pass",
 "fidelity_label": "Faithful Pass",
 "benchmark_eligible": true,
 "gpu_weight": {
  "weight_x": 3.38,
  "tier": "light",
  "p50_ms": 0.1112,
  "p95_ms": 0.1142,
  "baseline_p50_ms": 0.0329,
  "gpu": "Tesla T4/PCIe/SSE2",
  "resolution": "1920x1080",
  "method": "opengl-timer-query",
  "frames": 32,
  "preset_id": "markov-junior-rewrite-rules-live",
  "execution_fidelity": "faithful-pass",
  "fidelity_label": "Faithful Pass",
  "benchmark_eligible": true,
  "generated_at": "2026-11-30T21:07:47Z",
  "l4": {
   "weight_x": 2.77,
   "tier": "light",
   "p50_ms": 0.0441,
   "p95_ms": 0.0447,
   "speedup_x": 2.52,
   "gpu": "NVIDIA L4/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0159
  },
  "a100": {
   "weight_x": 2.12,
   "tier": "light",
   "p50_ms": 0.0611,
   "p95_ms": 0.0618,
   "speedup_x": 1.82,
   "gpu": "NVIDIA A100-SXM4-40GB/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0288
  }
 }
}