[Paper Review] WatChat: Explaining perplexing programs by debugging mental models
WatChat is a system that explains unexpected JavaScript program behavior by diagnosing and correcting users' misconceptions about language semantics. Using reverse program synthesis to infer flawed mental models, it generates contrastive, causal, and socially appropriate explanations—demonstrating effectiveness in explaining surprising behaviors like `[] + []` returning "".
Often, a good explanation for a program's unexpected behavior is a bug in the programmer's code. But sometimes, an even better explanation is a bug in the programmer's mental model of the language or API they are using. Instead of merely debugging our current code ("giving the programmer a fish"), what if our tools could directly debug our mental models ("teaching the programmer to fish")? In this paper, we apply recent ideas from computational cognitive science to offer a principled framework for doing exactly that. Given a "why?" question about a program, we automatically infer potential misconceptions about the language/API that might cause the user to be surprised by the program's behavior -- and then analyze those misconceptions to provide explanations of the program's behavior. Our key idea is to formally represent misconceptions as counterfactual (erroneous) semantics for the language/API, which can be inferred and debugged using program synthesis techniques. We demonstrate our framework, WatChat, by building systems for explanation in two domains: JavaScript type coercion, and the Git version control system. We evaluate WatChatJS and WatChatGit by comparing their outputs to experimentally-collected human-written explanations in these two domains: we show that WatChat's explanations exhibit key features of human-written explanation, unlike those of a state-of-the-art language model.
Motivation & Objective
- To address the gap in programming tools that explain unexpected behavior not through code bugs, but through flawed mental models of language semantics.
- To design a system that generates human-like, helpful explanations by modeling users' misconceptions as counterfactual semantics.
- To create a framework that supports both explanation generation and pedagogical example synthesis for diagnosing misconceptions.
- To integrate cognitive science principles—contrastiveness, selectivity, causality, and social context—into automated explanation systems for programming.
- To enable programming environments to act as collaborative, cognitive partners rather than passive debuggers.
Proposed method
- Model users' mental models as counterfactual semantics for JavaScript, representing erroneous interpretations of language behavior.
- Apply reverse program synthesis to infer plausible misconceptions (i.e., 'misinterpreters') that would cause a program to behave unexpectedly.
- Generate explanations by identifying and patching bugs in these counterfactual semantics, aligning with cognitive science principles of contrastive and selective explanation.
- Use a formal specification of JavaScript semantics and a symbolic execution engine (Rosette) to efficiently search through a space of 2^32 potential misconceptions in milliseconds.
- Invert the explanation process to synthesize diagnostic example programs that can test and correct specific misconceptions in learners.
- Integrate context-sensitive explanation generation by asking users about their expectations (e.g., 'Did you expect an empty array?'), enabling adaptive, conversational interaction.

Experimental results
Research questions
- RQ1How can programming tools explain unexpected behavior not by fixing code, but by diagnosing and correcting users' misconceptions about language semantics?
- RQ2What cognitive principles—such as contrastiveness, selectivity, and causality—can guide the generation of human-like explanations in programming environments?
- RQ3Can program synthesis techniques be inverted to infer and debug flawed mental models of programming language behavior?
- RQ4To what extent can such a system generate pedagogically useful diagnostic examples that target specific misconceptions?
- RQ5How can explanation systems be designed to be context-sensitive and socially adaptive, tailoring explanations to user expectations and background?
Key findings
- WatChat can infer and explain surprising JavaScript behaviors—such as `[] + []` returning ""—by identifying and correcting misconceptions about type coercion and array-to-string conversion.
- The system efficiently searches through a space of 2^32 potential mental models in under a millisecond, enabling real-time explanation generation.
- Explanations generated by WatChat are contrastive and selective, focusing on key differences between expected and actual behavior, such as the absence of brackets in array stringification.
- The system successfully models user expectations by asking about intended outputs, enabling adaptive, conversational debugging of mental models.
- The framework can be inverted to generate high-precision pedagogical examples that diagnose specific misconceptions, such as misunderstanding array concatenation or string conversion.
- WatChat outperforms baseline LLMs (e.g., GPT-4) in explanation quality by producing contrastive, causal, and socially appropriate explanations, avoiding verbose or incorrect step-by-step traces.

Read this paper
Open the full text in the Nubint reader and ask about any passage as you read.
Read this paper on NubintThis review was generated by AI from the original paper. Please check the paper before citing it.