{"slug": "compilable-and-executable-pseudocode-spec-solves-ai-coding-hallucinations", "title": "Compilable and Executable Pseudocode (spec) Solves AI Coding Hallucinations", "summary": "A developer proposes making AI-generated specifications compilable and executable to eliminate hallucinations in AI-assisted programming. By using a deterministic compiler instead of probabilistic AI to generate code, the approach bypasses the stage where hallucinations occur, ensuring reliable output. The method is demonstrated with SQL queries where AI often produces syntactically correct but logically flawed code.", "body_md": "**The AI-generated SQL – no one dares to say they fully “understand” it**\n\nFirst, look at the following SQL query, which aims to find credit card customers whose transaction amount has increased for three consecutive months:\n\n```\nWITH ranked AS (\n  SELECT customer_id, month, amount,\n    LAG(amount,1) OVER(PARTITION BY customer_id ORDER BY month) as prev_1,\n    LAG(amount,2) OVER(PARTITION BY customer_id ORDER BY month) as prev_2\n  FROM transactions\n)\nSELECT customer_id, COUNT(*) FROM ranked\nWHERE amount > prev_1 AND prev_1 > prev_2\nAND prev_1 IS NOT NULL AND prev_2 IS NOT NULL\nGROUP BY customer_id HAVING COUNT(*) >= 3\n```\n\nThe syntax is correct, and it runs. But is the logic sound? No one can say for sure right away.\n\nThis is the daily routine of data analysts and developers today: they give a task to an AI, get dozens of lines of SQL in seconds, copy and paste the code, and it runs. But can they really trust the results? Modern AI can generate runnable SQL, but it is often not fully reliable.\n\nAI-generated SQL is a black box. The code may look correct, run without errors, and return a clean result, but what it actually does may have nothing to do with what you intended. Worse, you only realize it’s wrong after it has already executed. During the review phase, you have to mentally execute the SQL yourself, effectively redoing the work that the AI was supposed to handle.\n\nEven worse, even if the accuracy of AI-generated SQL reaches 90%, how can you know that this time they aren’t falling into the remaining 10%? Data-driven decisions should not become a game of chance. You get a SQL query that executes successfully and returns a clean result, but what it actually does is not what the user intended. This type of error cannot be caught by syntax checking. And the verification cost during the code review phase is extremely high: to confirm whether the logic is correct, you have to break down CTEs layer by layer, run them step by step, and compare the intermediate results.\n\n**What is the real problem?**\n\nIn AI-assisted programming, there is a basic workflow: the user writes a clear specification and AI generates runnable code.\n\nThis is not a question of whether AI is “capable” or not, but a hard constraint under current technical conditions. If the user only sketches out a couple of lines of specification, no AI can generate runnable engineering-grade code out of thin air – unless the task is a common, well-defined one such as bubble sort. The actual AI-assisted programming workflow is: the user writes a spec → the AI generates code → the user reviews it → spec the refined → the AI regenerates the code. The spec is both the starting point and a constraint throughout the entire process.\n\nBut even with a perfectly clear specification, AI can still make mistakes – it’s a matter of probability. A poorly defined specification increases the chance that the AI will guess wrong, while a well-defined specification reduces that probability but can never eliminate it. This is because, at its core, AI is making probabilistic predictions. From input to output, it searches the space of possible answers for the most likely one, rather than logically deriving the only correct result.\n\nThen what can we do? Is there any way to turn a “likely answer” to a “definitive one”?\n\nThe answer is: make the spec compliant enough to compile. Once the spec can be parsed and executed by a compiler, AI no longer needs to “guess” the code. The compiler is deterministic – the same input always produces the same output. No more “works this time, breaks the next”. This isn’t about “reducing the hallucination rate”; it’s about bypassing the stage where hallucinations can occur.\n\nSo, the overall logic chain is clear: AI-assisted programming requires a spec → a well-defined spec can reduce hallucination rate → but it cannot eliminate hallucinations → make the spec compliant enough to compile → use a compiler instead of AI to generate code →hallucinations disappear.\n\n**Compilable pseudocode: an advanced form of the specification**\n\nTraditionally, the requirements document that programmers create is intended for humans. It contains natural language, charts and diagrams, and pseudocode. In the era of AI, the spec can evolve into a new form with two characteristics:\n\nHuman-readable: No need to be familiar with the target language (SQL, Java, Python), you can grasp the logic at a glance.\n\nMachine-compilable: The specification has a fixed syntactic structure and can be deterministically transformed into final code, without relying on AI guessing.\n\nThis is “compilable pseudocode”, a more compliant spec. The difference between it and traditional pseudocode is this: traditional pseudocode won’t be compiled – it is intended only for humans to read; compilable pseudocode is both human-readable and machine-readable, so it can be compiled into target code.\n\nThe value of this kind of spec is that it squeezes AI’s “creative space” down to a minimum. AI doesn't need to “understand” the business logic, “design” an implementation plan, and “guess” the correct code – it only needs to translate the uncompliant spec into a compliant one. Translation leaves a far narrower margin for error than “generating code directly from natural language”.\n\nMore importantly, you can verify the correctness of the spec before it is compiled and executed. Each step of the logic can be verified independently, so errors are caught before execution rather than being debugged afterward.\n\n**SQLazy’s workflow: The compilable pseudocode**\n\nSQLazy brings this concept to the field of complex SQL development. Its workflow is essentially compilable pseudocode: developers describe data querying logic step by step in natural language, previewing the intermediate result at each step. Once the logic is confirmed correct, the compiler deterministically generates the SQL.\n\nStill on the same requirement as above: finding the credit card customers whose transaction amount has increased for three consecutive months. Let’s implement this using SQLazy’s workflow:\n\nYou don’t need to know SQL – the four steps alone make the logic clear. At every step you can preview the intermediate result:\n\nsort: Sort rows by customer and month.\n\nsegment: Mark records where the transaction amounts fall, and generate a number for each consecutively rising segment.\n\nsummarize: Count the number of months covered by each segment.\n\nfilter: Select customers whose transaction amounts increase for at least consecutive 3 months (>=3).\n\nHere is another real-world example. A table stores time intervals for multiple accounts. Each account corresponds to multiple records, and the intervals may overlap. The goal is to merge all overlapping intervals within each account. With SQLazy, this can be implemented in just four steps:\n\nsort account_id, start_date: Sort rows by account and start date.\n\ncompute end_date[:-1] max as prev_max: Calculate the maximum end date among all rows prior to the current row.\n\nsegment condition start_date > prev_max as gid: Segment rows based on the condition, and assign a group number to each segment.\n\nsummarize start_date min as start_date, end_date max as end_date: Aggregate by account and group number.\n\nOnce the above logic is confirmed correct, the SQLazy compiler automatically generates native SQL – 100% accurate, with zero hallucination risk. If you switch the target database from MySQL to Oracle, you only need to change one option and the compiler automatically adapts to the target SQL dialect.\n\nSQLazy’s workflow is the spec, and the compiler is the last gate guaranteeing the code quality.\n\nIn this process, the role of AI is also redefined. Conventionally, AI is responsible for generating the final SQL directly from the natural language – an extremely difficult task with a high hallucination rate. With SQLazy, AI is charged with only one thing: translating users’ colloquial, step-by-step descriptions into formal workflow syntax. The former is “decision-making” while the latter is “translation”. Even if the AI’s translation is slightly off, you can spot it at a glance at the workflow level, and the cost of correction is nearly zero.\n\nUse LLM to convert the natural language into SQLazy syntax\n\nAI hallucinations in programming are a problem not because AI occasionally makes mistakes, but because of something more fundamental: under the conventional model, AI’s mistakes can only be discovered after execution. By the time you find the result is wrong, the code has already been generated – even executed – making the cost of correction extremely high.\n\nSQLazy’s compilable pseudocode shifts the moment of discovering errors from after execution to before execution. Humans confirm the logic at the spec level, and the compiler generates the final code. AI’s room for error is squeezed down to the “translation” step alone, where the result can be verified instantly by humans – solving the hallucination problem in AI programming.\n\nThis does not mean programmers are going back to the era of “writing documentation”. Rather, it redivides the labor between humans and AI: humans handle design (describing “what to do” using structured spec); AI handles translation (converting colloquial spec into compliant spec); and the compiler handles execution (guaranteeing a deterministic output).\n\nThe next phase of AI programming is probably not about making AI generate longer code, but about enabling humans to write clearer spec.", "url": "https://wpnews.pro/news/compilable-and-executable-pseudocode-spec-solves-ai-coding-hallucinations", "canonical_source": "https://dev.to/esproc_spl/compilable-and-executable-pseudocode-spec-solves-ai-coding-hallucinations-5ghk", "published_at": "2026-07-09 03:08:21+00:00", "updated_at": "2026-07-09 03:41:12.500547+00:00", "lang": "en", "topics": ["artificial-intelligence", "large-language-models", "ai-safety", "developer-tools"], "entities": [], "alternates": {"html": "https://wpnews.pro/news/compilable-and-executable-pseudocode-spec-solves-ai-coding-hallucinations", "markdown": "https://wpnews.pro/news/compilable-and-executable-pseudocode-spec-solves-ai-coding-hallucinations.md", "text": "https://wpnews.pro/news/compilable-and-executable-pseudocode-spec-solves-ai-coding-hallucinations.txt", "jsonld": "https://wpnews.pro/news/compilable-and-executable-pseudocode-spec-solves-ai-coding-hallucinations.jsonld"}}