Skip to content

Conversation

@rcosta358
Copy link
Collaborator

@rcosta358 rcosta358 commented Oct 6, 2025

Overview

This PR aims to solve issue #58, by applying constant propagation and folding to the AST after a refinement error is found, to simplify the error message shown to the user.

This simplification is done in a way that allows simplification steps to be expanded or collapsed by keeping track of their origin nodes, which can later be used in the VS Code extension, by serializing the derivation tree to JSON.

Additions

  • VariableResolver - extracts variables with constant values from the AST and stores them in a HashMap
  • ConstantPropagation - substitutes variables with their constant values from that HashMap
  • ConstantFolding - simplifies expressions by evaluating nodes with constant values
  • ExpressionSimplifier - combines propagation and folding and simplifies the derivation tree by removing redundant conjuncts (e.g., x && true, x == x)
  • DerivationNode, ValDerivationNode, BinaryDerivationNode, UnaryDerivationNode, VarDerivationNode - nodes that form the derivation tree
  • ExpressionSimplifierTest - tests generated by Claude Sonnet 4.5

Example

public class Example {
    
    public static void requiresNegative(@Refinement("x < 0") int x) {}
    
    public static void example(int x) {
        int a = 6;
        int b = 2;
        requiresNegative((a / b + (-5)) + x);
    }
}
image

@rcosta358 rcosta358 added enhancement New feature or request error messages labels Oct 6, 2025
@rcosta358 rcosta358 linked an issue Oct 6, 2025 that may be closed by this pull request
@rcosta358 rcosta358 self-assigned this Oct 6, 2025
Copy link
Collaborator

@alcides alcides left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me in general. There are a few minor points that you can take a look at, that might improve the code quality.

@rcosta358
Copy link
Collaborator Author

Thanks for the review!

@rcosta358 rcosta358 requested a review from alcides October 7, 2025 12:13
@rcosta358 rcosta358 merged commit d3c2dbb into liquid-java:main Oct 29, 2025
1 check passed
@rcosta358 rcosta358 deleted the simplify-expressions branch October 29, 2025 16:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request error messages

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add Constant Propagation And Folding To Predicates

2 participants