Visualizing the Evaluation of Functional Programs for Debugging

John Whitington,Tom Ridge
2024-11-01
Abstract:In this position paper, we present a prototype of a visualizer for functional programs. Such programs, whose evaluation model is the reduction of an expression to a value through repeated application of rewriting rules, and which tend to make little or no use of mutable state, are amenable to visualization in the same fashion as simple mathematical expressions, with which every schoolchild is familiar. We show how such visualizations may be produced for the strict functional language OCaml, by direct interpretation of the abstract syntax tree and appropriate pretty-printing. We describe (and begin to address) the challenges of presenting such program traces in limited space and of identifying their essential elements, so that our methods will one day be practical for more than toy programs. We consider the problems posed by the parts of modern functional programming which are not purely functional such as mutable state, input/output and exceptions. We describe initial work on the use of such visualizations to address the problem of program debugging, which is our ultimate aim.
Programming Languages
What problem does this paper attempt to address?
The problem that this paper attempts to solve is: how to use visualization techniques to assist programmers in debugging functional programs. Specifically, the author aims to develop a visual debugging tool that can effectively present the execution process of functional programs, thereby helping programmers understand and fix errors in the programs more easily. ### Problem Background The execution model of functional programming languages (such as OCaml) is to gradually simplify expressions into values by repeatedly applying rewrite rules. This model is similar to the simplification process of mathematical expressions, so its execution process can be presented by visual means. However, the existing debugging tools do not support functional programming well enough. Especially when dealing with complex programs, traditional debugging methods (such as inserting print statements) are inefficient and not intuitive enough. ### Main Contributions of the Paper 1. **Visual Prototype Development**: The author has developed a prototype of a visual tool for the strictly functional language OCaml. This tool generates an easily understandable program execution trace by directly interpreting the abstract syntax tree and performing appropriate formatting output. 2. **Challenges and Solutions**: - **Information Compression**: In order to present the complex program execution process within a limited space, the author proposes multiple information compression techniques, such as omitting unnecessary intermediate steps and simplifying arithmetic operations. - **Support for Non - Pure Functional Features**: Modern functional programming languages include some non - pure functional features (such as mutable state, input/output, and exception handling), which increase the difficulty of visual debugging. The author discusses how to handle these features and ensure that the visual tool can be applied to a wider range of scenarios. 3. **Debugging Goals**: The ultimate goal is to help programmers locate and fix errors in programs more quickly by visual means. The author believes that an ideal debugging tool should be able to quickly narrow down the problem scope after observing abnormal behavior in the program until the specific cause of the error is found and understood. ### Conclusion The author proposes an ambitious but practical plan to implement an interpretive debugger for popular functional languages (such as OCaml). Although the current prototype is still in its early stage, it shows the great potential of visual debugging and provides a basis for further research and improvement.