Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/dict key access #19

Merged
merged 2 commits into from
Nov 23, 2024
Merged

Fix/dict key access #19

merged 2 commits into from
Nov 23, 2024

Conversation

ccamel
Copy link
Member

@ccamel ccamel commented Nov 23, 2024

This PR fixes the ordering of desugared terms for Dicts in clause bodies (introduced by #17) and adds tests to verify the corrected behavior.

Additionally, it includes a minor refactor of the desugaring process by separating the logic for head and body terms into distinct functions.

Copy link

coderabbitai bot commented Nov 23, 2024

Walkthrough

The pull request introduces significant changes to the engine/clause.go file by restructuring the desugaring process for clauses. The desugar function is replaced by two distinct functions, desugarHead and desugarBody, which separately handle the head and body of clauses. Additionally, new test cases are added to both engine/text_test.go and interpreter_test.go to validate the behavior of the updated compilation and querying processes, ensuring that the interpreter correctly handles new structures and predicates.

Changes

File Change Summary
engine/clause.go - Replaced desugar with desugarHead and desugarBody.
- Updated compileClause to use new desugaring functions.
- Modified desugarPred to enhance environment resolution.
engine/text_test.go - Added new test case "dict head (3)" in TestVM_Compile for a specific rule involving a compound structure.
- Added new test case "dict body (2)" for a rule involving predicates and equality.
interpreter_test.go - Introduced new test cases in TestInterpreter_Query to validate querying point structures and error handling.
- Enhanced existing tests for edge cases and error conditions.

Possibly related PRs


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

codecov bot commented Nov 23, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Impacted file tree graph

@@           Coverage Diff           @@
##             main      #19   +/-   ##
=======================================
  Coverage   97.86%   97.86%           
=======================================
  Files          24       24           
  Lines        7822     7840   +18     
=======================================
+ Hits         7655     7673   +18     
  Misses        134      134           
  Partials       33       33           
Files with missing lines Coverage Δ
engine/clause.go 99.11% <100.00%> (+0.07%) ⬆️

🚨 Try these New Features:

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (5)
engine/text_test.go (2)

364-377: Consider adding bytecode sequence documentation.

The bytecode sequence is correct but could benefit from a comment explaining the operation flow:

  1. Variable setup
  2. Predicate call
  3. Dictionary field access
  4. Unification
  5. Exit

Add this comment before the bytecode:

 bytecode: bytecode{
+    // Bytecode sequence:
+    // 1. Set up variables
+    // 2. Call p(P) to get the dictionary
+    // 3. Access P.x using the dot operator
+    // 4. Unify X with the field value
     {opcode: OpGetVar, operand: Integer(0)},
     // ... rest of the bytecode

331-382: Consider adding test cases for edge cases.

While the current test coverage for dictionary operations is good, consider adding test cases for:

  1. Nested dictionary access (e.g., P.x.y)
  2. Non-existent field access
  3. Invalid field access (e.g., accessing a field of a non-dictionary)

Example test case for nested access:

+		{title: "dict body (nested)", text: `
+x(X) :- p(P), =(X, P.x.y).
+`, result: buildOrderedMap(
+			// ... appropriate result map
+		)},
interpreter_test.go (1)

1058-1064: LGTM! Consider adding more test cases.

The test case correctly verifies the dictionary key access functionality through predicate chaining. This aligns well with the PR's objective of fixing dict key access issues.

Consider adding more test cases to cover:

  • Accessing non-existent keys through predicate chains
  • Accessing nested dictionaries through longer predicate chains
  • Error handling when the dictionary is not properly initialized in the predicate chain
engine/clause.go (2)

Line range hint 118-149: Consider adding unit tests for desugarPred function

Given the complexity of the desugarPred function, adding unit tests would help ensure its correctness across various term types and improve code reliability.


Line range hint 118-149: Enhance code readability with detailed comments in desugarPred

Adding inline comments to explain the recursive desugaring logic in desugarPred can improve maintainability and assist future developers in understanding the code flow.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 7be6699 and fe6edb60679f705ab88fd24b537a7ca70bdaded8.

📒 Files selected for processing (3)
  • engine/clause.go (3 hunks)
  • engine/text_test.go (1 hunks)
  • interpreter_test.go (1 hunks)
🔇 Additional comments (5)
engine/text_test.go (1)

331-382: LGTM! Well-structured test case for dictionary field access.

The test case effectively verifies the compilation and bytecode generation for dictionary field access in rule bodies. It complements existing dictionary-related tests by covering the dot notation access pattern.

engine/clause.go (4)

67-68: Simplify clause compilation by separating desugaring of head and body

The use of desugarHead and desugarBody functions enhances modularity and clarity in the compileClause function.


70-76: Ensure correct sequencing of desugared predicates with body

The logic correctly appends the desugared predicates from the head to the body of the clause, preserving the correct execution order.


93-98: Correct handling of head term in desugarHead function

The function safely handles both compound and non-compound head terms, ensuring that non-compound terms are returned unchanged.


100-116: Efficient desugaring of body terms

The desugarBody function correctly iterates over the body terms, desugaring each one and collecting any additional predicates.

@ccamel ccamel force-pushed the fix/dict-key-access branch from fe6edb6 to 21ef28c Compare November 23, 2024 11:24
@ccamel ccamel self-assigned this Nov 23, 2024
@ccamel ccamel force-pushed the fix/dict-key-access branch from 21ef28c to b1bd213 Compare November 23, 2024 11:28
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (2)
engine/text_test.go (1)

383-434: LGTM! Consider adding edge cases for nested dict access.

The test case effectively verifies dict access in clause body with variable binding. To make it even more robust, consider adding test cases for nested dict access patterns (e.g., P.x.y).

Example test case to consider:

 {title: "dict body (2)", text: `
 x(X) :- p(P), =(X, P.x).
+y(Y) :- p(P), =(Y, P.x.y).
 `, result: buildOrderedMap(
interpreter_test.go (1)

1079-1085: Consider adding error cases for compound queries

While the test case verifies successful compound queries with dict key access, consider adding test cases for error conditions in compound queries, such as:

  • Accessing non-existent keys in compound queries
  • Type errors in compound queries
  • Invalid key access patterns

Example test case to add:

+		{
+			program: "p(P) :- P = point{x:10}. x(X) :- p(P), X = P.z.",
+			query:   "x(X).",
+			wantResult: []result{{
+				err: fmt.Errorf("error(domain_error(dict_key,z),. /3)"),
+			}},
+		},
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between fe6edb60679f705ab88fd24b537a7ca70bdaded8 and b1bd213.

📒 Files selected for processing (2)
  • engine/text_test.go (2 hunks)
  • interpreter_test.go (2 hunks)
🔇 Additional comments (4)
engine/text_test.go (1)

285-336: LGTM! Well-structured test case for dict access in clause head.

The test case thoroughly verifies the compilation of dict access in clause head with a body, ensuring correct bytecode generation for both dict construction and field access operations.

interpreter_test.go (3)

1025-1031: LGTM: Test case verifies dict key access in rule body

The test case correctly verifies that dictionary key access works within a rule body, ensuring that the ok predicate is properly called before accessing the dict key.


1033-1038: LGTM: Test case verifies variable binding with dict key access

The test case properly verifies that variables can be bound when accessing dictionary keys, with the variable X being bound to 5 in the rule body.


1040-1045: LGTM: Test case verifies variable key access

The test case correctly verifies that dictionary keys can be accessed using variables, with the variable X being bound to the key name x.

@ccamel ccamel merged commit d4ffd77 into main Nov 23, 2024
3 checks passed
@ccamel ccamel deleted the fix/dict-key-access branch November 23, 2024 12:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant