prolog-trace-viz 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (57) hide show
  1. package/README.md +148 -0
  2. package/dist/analyzer.d.ts +64 -0
  3. package/dist/analyzer.d.ts.map +1 -0
  4. package/dist/analyzer.js +903 -0
  5. package/dist/analyzer.js.map +1 -0
  6. package/dist/build-info.d.ts +15 -0
  7. package/dist/build-info.d.ts.map +1 -0
  8. package/dist/build-info.js +26 -0
  9. package/dist/build-info.js.map +1 -0
  10. package/dist/clauses.d.ts +15 -0
  11. package/dist/clauses.d.ts.map +1 -0
  12. package/dist/clauses.js +80 -0
  13. package/dist/clauses.js.map +1 -0
  14. package/dist/cli.d.ts +34 -0
  15. package/dist/cli.d.ts.map +1 -0
  16. package/dist/cli.js +162 -0
  17. package/dist/cli.js.map +1 -0
  18. package/dist/errors.d.ts +19 -0
  19. package/dist/errors.d.ts.map +1 -0
  20. package/dist/errors.js +66 -0
  21. package/dist/errors.js.map +1 -0
  22. package/dist/executor.d.ts +25 -0
  23. package/dist/executor.d.ts.map +1 -0
  24. package/dist/executor.js +115 -0
  25. package/dist/executor.js.map +1 -0
  26. package/dist/index.d.ts +3 -0
  27. package/dist/index.d.ts.map +1 -0
  28. package/dist/index.js +128 -0
  29. package/dist/index.js.map +1 -0
  30. package/dist/mermaid.d.ts +47 -0
  31. package/dist/mermaid.d.ts.map +1 -0
  32. package/dist/mermaid.js +197 -0
  33. package/dist/mermaid.js.map +1 -0
  34. package/dist/output.d.ts +24 -0
  35. package/dist/output.d.ts.map +1 -0
  36. package/dist/output.js +40 -0
  37. package/dist/output.js.map +1 -0
  38. package/dist/parser.d.ts +84 -0
  39. package/dist/parser.d.ts.map +1 -0
  40. package/dist/parser.js +1007 -0
  41. package/dist/parser.js.map +1 -0
  42. package/dist/prolog-parser.d.ts +2 -0
  43. package/dist/prolog-parser.d.ts.map +1 -0
  44. package/dist/prolog-parser.js +2 -0
  45. package/dist/prolog-parser.js.map +1 -0
  46. package/dist/renderer.d.ts +33 -0
  47. package/dist/renderer.d.ts.map +1 -0
  48. package/dist/renderer.js +131 -0
  49. package/dist/renderer.js.map +1 -0
  50. package/dist/wrapper.d.ts +30 -0
  51. package/dist/wrapper.d.ts.map +1 -0
  52. package/dist/wrapper.js +87 -0
  53. package/dist/wrapper.js.map +1 -0
  54. package/package.json +55 -0
  55. package/scripts/generate-build-info.js +63 -0
  56. package/scripts/release.js +151 -0
  57. package/tracer.pl +202 -0
package/README.md ADDED
@@ -0,0 +1,148 @@
1
+ # prolog-trace-viz
2
+
3
+ Generate beautiful, educational Mermaid diagrams from Prolog execution traces.
4
+
5
+ ## Features
6
+
7
+ - Custom Prolog tracer using SWI-Prolog's trace interception hook
8
+ - Captures accurate unification information directly from execution
9
+ - Generates colour-coded Mermaid diagrams
10
+ - Produces complete markdown documentation with step-by-step breakdowns
11
+ - Tracks pending goals, variable bindings, and clause usage
12
+ - No external dependencies beyond SWI-Prolog
13
+
14
+ ## Prerequisites
15
+
16
+ **SWI-Prolog 7.0 or later** - Install from https://www.swi-prolog.org/Download.html
17
+
18
+ The tool uses a custom tracer built on SWI-Prolog's `prolog_trace_interception/4` hook, which requires version 7.0 or later. No additional packages are required.
19
+
20
+ ## Installation
21
+
22
+ ```
23
+ npm install -g prolog-trace-viz
24
+ ```
25
+
26
+ Or run directly with npx:
27
+
28
+ ```
29
+ npx prolog-trace-viz <prolog-file> <query>
30
+ ```
31
+
32
+ ## Usage
33
+
34
+ ```
35
+ prolog-trace-viz <prolog-file> <query> [options]
36
+ ```
37
+
38
+ ### Arguments
39
+
40
+ - `<prolog-file>` - Path to your Prolog source file
41
+ - `<query>` - Prolog query to trace (e.g., `"append([1,2], [3,4], X)"`)
42
+
43
+ ### Options
44
+
45
+ | Option | Description |
46
+ |--------|-------------|
47
+ | `-o, --output <file>` | Write output to file instead of stdout |
48
+ | `--verbose` | Display detailed processing information |
49
+ | `--quiet` | Suppress all non-error output except final result |
50
+ | `-h, --help` | Show help message |
51
+ | `-v, --version` | Show version number |
52
+
53
+ ### Examples
54
+
55
+ Basic usage:
56
+
57
+ ```
58
+ prolog-trace-viz program.pl "append([1,2], [3,4], X)"
59
+ ```
60
+
61
+ Save to file:
62
+
63
+ ```
64
+ prolog-trace-viz program.pl "member(X, [a,b,c])" -o trace.md
65
+ ```
66
+
67
+ With verbose output:
68
+
69
+ ```
70
+ prolog-trace-viz program.pl "factorial(5, X)" --verbose
71
+ ```
72
+
73
+ ## Example Output
74
+
75
+ Given a simple Prolog file `append.pl`:
76
+
77
+ ```prolog
78
+ append([], L, L).
79
+ append([H|T], L, [H|R]) :- append(T, L, R).
80
+ ```
81
+
82
+ Running:
83
+
84
+ ```
85
+ prolog-trace-viz append.pl "append([1,2], [3], X)"
86
+ ```
87
+
88
+ Produces a markdown document with:
89
+
90
+ 1. **Query section** - The original query in a code block
91
+ 2. **Execution tree** - A Mermaid diagram showing the trace
92
+ 3. **Legend** - Explanation of visual elements
93
+ 4. **Step-by-step breakdown** - Detailed execution steps
94
+ 5. **Final answer** - The result bindings
95
+ 6. **Clauses used** - Summary of which clauses matched
96
+
97
+ ### Visual Elements
98
+
99
+ | Symbol | Meaning |
100
+ |--------|---------|
101
+ | 🎯 | Initial query |
102
+ | πŸ”„ | Currently solving |
103
+ | ⏸️ | Pending (queued) |
104
+ | βœ… | Solved |
105
+ | πŸŽ‰ | Success |
106
+
107
+ ### Colour Scheme
108
+
109
+ - **Blue** - Query nodes
110
+ - **Yellow** - Solving nodes
111
+ - **Grey** - Pending nodes
112
+ - **Green** - Solved/Success nodes
113
+
114
+ ## Architecture
115
+
116
+ The tool uses a custom Prolog tracer that leverages SWI-Prolog's `prolog_trace_interception/4` hook to capture execution events. This approach provides several advantages:
117
+
118
+ - **Accurate unifications**: Direct access to variable bindings via `prolog_frame_attribute/3`
119
+ - **No code instrumentation**: Your Prolog code runs unmodified
120
+ - **Reliable clause tracking**: Clause numbers come from Prolog's internal tracking
121
+ - **Structured output**: JSON-based trace format for easy parsing
122
+
123
+ ### Pipeline
124
+
125
+ 1. Parse user's Prolog file to extract clauses
126
+ 2. Generate wrapper that loads custom tracer
127
+ 3. Execute query with trace interception active
128
+ 4. Export trace events as JSON
129
+ 5. Build execution tree from trace events
130
+ 6. Analyze tree and generate visualization
131
+ 7. Render as Mermaid diagram in markdown
132
+
133
+ ## Development
134
+
135
+ ```
136
+ # Install dependencies
137
+ npm install
138
+
139
+ # Build
140
+ npm run build
141
+
142
+ # Run tests
143
+ npm test
144
+ ```
145
+
146
+ ## Licence
147
+
148
+ MIT
@@ -0,0 +1,64 @@
1
+ import { ExecutionNode, TraceEvent } from './parser.js';
2
+ export interface VisualizationNode {
3
+ id: string;
4
+ type: 'query' | 'solving' | 'pending' | 'solved' | 'success' | 'clause-body' | 'match';
5
+ label: string;
6
+ emoji: string;
7
+ level: number;
8
+ clauseNumber?: number;
9
+ }
10
+ export interface VisualizationEdge {
11
+ id: string;
12
+ from: string;
13
+ to: string;
14
+ type: 'active' | 'queue' | 'activate';
15
+ label: string;
16
+ stepNumber: number;
17
+ }
18
+ export interface PendingGoal {
19
+ id: string;
20
+ goal: string;
21
+ queuedAt: string;
22
+ activatedAt?: string;
23
+ }
24
+ export interface ClauseUsage {
25
+ clauseNumber: number;
26
+ clauseText: string;
27
+ usageCount: number;
28
+ usedAtSteps: number[];
29
+ }
30
+ export interface ExecutionStep {
31
+ stepNumber: number;
32
+ description: string;
33
+ goal: string;
34
+ clauseMatched?: string;
35
+ bindings?: Record<string, string>;
36
+ newGoals?: string[];
37
+ }
38
+ export type DetailLevel = 'minimal' | 'standard' | 'detailed' | 'full';
39
+ export interface AnalysisOptions {
40
+ detailLevel?: DetailLevel;
41
+ }
42
+ export interface AnalysisResult {
43
+ nodes: VisualizationNode[];
44
+ edges: VisualizationEdge[];
45
+ pendingGoals: Map<string, PendingGoal>;
46
+ executionOrder: string[];
47
+ clausesUsed: ClauseUsage[];
48
+ executionSteps: ExecutionStep[];
49
+ finalAnswer?: string;
50
+ }
51
+ import { Clause } from './clauses.js';
52
+ /**
53
+ * Analyzes an execution tree and produces visualization data.
54
+ */
55
+ export declare function analyzeTree(root: ExecutionNode, clauses?: Clause[], options?: AnalysisOptions, traceEvents?: TraceEvent[]): AnalysisResult;
56
+ /**
57
+ * Assigns level-based variable names to avoid confusion in recursive calls.
58
+ */
59
+ export declare function assignLevelVariables(node: ExecutionNode, level: number): void;
60
+ /**
61
+ * Determines the execution order (left-to-right, depth-first).
62
+ */
63
+ export declare function determineExecutionOrder(root: ExecutionNode): string[];
64
+ //# sourceMappingURL=analyzer.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"analyzer.d.ts","sourceRoot":"","sources":["../src/analyzer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACxD,MAAM,WAAW,iBAAiB;IAChC,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,OAAO,GAAG,SAAS,GAAG,SAAS,GAAG,QAAQ,GAAG,SAAS,GAAG,aAAa,GAAG,OAAO,CAAC;IACvF,KAAK,EAAE,MAAM,CAAC;IACd,KAAK,EAAE,MAAM,CAAC;IACd,KAAK,EAAE,MAAM,CAAC;IACd,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB;AAED,MAAM,WAAW,iBAAiB;IAChC,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,CAAC;IACb,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,QAAQ,GAAG,OAAO,GAAG,UAAU,CAAC;IACtC,KAAK,EAAE,MAAM,CAAC;IACd,UAAU,EAAE,MAAM,CAAC;CACpB;AAED,MAAM,WAAW,WAAW;IAC1B,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,MAAM,CAAC;IACjB,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB;AAED,MAAM,WAAW,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