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

deterministic unification variables #262

Merged
merged 4 commits into from
Feb 12, 2025

Conversation

gelisam
Copy link
Owner

@gelisam gelisam commented Feb 3, 2025

Improves error messages from

Expected (Integer → META(MetaPtr 53790)) but got Integer

to

Expected (Integer → ?1) but got Integer

which is not only more readable, it is also more deterministic, since the first error message prints a different number every time. the extra determinism makes it possible to write non-examples which produce a type error involving unification variables.

Summary by CodeRabbit

  • New Features

    • Enhanced error messages now provide clearer notifications when type mismatches occur.
    • Added new examples to illustrate expected functionality.
  • Refactor

    • Improved output formatting for more consistent and readable displays.
  • Chores

    • Introduced utility enhancements to streamline data management processes.

I want to thread a state throughout the value being pretty-printed, so I
have to change all the pretty-printing code to be monadic. to make this
easier to review, this commit only contains the refactoring, I am not
yet making use of the new state.
@gelisam gelisam requested a review from doyougnu February 3, 2025 12:10
Copy link

coderabbitai bot commented Feb 3, 2025

Walkthrough

This set of changes updates error reporting and pretty-printing functionality in multiple modules. It introduces a new error example that highlights a type mismatch, adds a demonstration file with a prelude declaration, and refactors pretty-printing functions in both error and task modules to use do notation and monadic wrapping. Additionally, the pretty-printing module now incorporates state management for renumbering, and new functions for creating and inspecting store instances are added.

Changes

File(s) Change Summary
examples/non-examples/type-errors/not-a-function.golden
examples/.../not-a-function.kl
Added a new error example: reports a type mismatch error (expected function type vs provided integer) and introduces a new file with a prelude declaration and example expression.
src/Expander/Error.hs
src/Expander/Task.hs
Refactored pretty-printing functions: updated error display and task pretty-printing to use do notation and monadic wrapping for clarity.
src/Pretty.hs Enhanced pretty-printing functions to incorporate state management (State Renumbering), updating method signatures and internal implementations.
src/Util/Store.hs Added new functions empty and size for initializing and inspecting store instances, with lookup now explicitly exported.

Sequence Diagram(s)

sequenceDiagram
    participant Caller
    participant PrettyModule
    participant StateManager
    Caller->>PrettyModule: Invoke pretty/prettyPrint functions
    PrettyModule->>StateManager: Initialize state (St.empty)
    StateManager-->>PrettyModule: Provide state context
    PrettyModule->>PrettyModule: Execute pp functions in state monad
    PrettyModule-->>Caller: Return formatted output
Loading

Suggested labels

CodeRabbit

Poem

In my burrow of code, I hop with glee,
Changes rustle like leaves on a tree.
Error messages and pretty prints refined,
Each line a treasure, neatly aligned.
With nimble feet and a joyful jump,
I celebrate the changes with a thump!
🥕🐇


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 generate docstrings to generate docstrings for this PR. (Beta)
  • @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

@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

🧹 Nitpick comments (1)
src/Expander/Error.hs (1)

112-287: LGTM! Improved readability with monadic style.

The refactoring to use do notation and bind intermediate values makes the code more readable and maintainable. The consistent pattern of binding pretty-printed values before composing them improves code clarity.

Consider extracting common patterns into helper functions to reduce repetition. For example:

+ppWithLabel :: Pretty VarInfo a => VarInfo -> Text -> a -> State Renumbering (Doc ann)
+ppWithLabel env label x = do
+  ppX <- pp env x
+  pure $ hang 2 $ group $ vsep [text label, ppX]

 pp env (NotList stx) = do
-  ppStx <- pp env stx
-  pure $ hang 2 $ group $ vsep [text "Expected parens, but got", ppStx]
+  ppWithLabel env "Expected parens, but got" stx
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 750d792 and 1253377.

📒 Files selected for processing (6)
  • examples/non-examples/type-errors/not-a-function.golden (1 hunks)
  • examples/non-examples/type-errors/not-a-function.kl (1 hunks)
  • src/Expander/Error.hs (3 hunks)
  • src/Expander/Task.hs (1 hunks)
  • src/Pretty.hs (6 hunks)
  • src/Util/Store.hs (4 hunks)
✅ Files skipped from review due to trivial changes (1)
  • examples/non-examples/type-errors/not-a-function.kl
⏰ Context from checks skipped due to timeout of 90000ms (5)
  • GitHub Check: macOS-latest / ghc 9.6
  • GitHub Check: ubuntu-latest / ghc 9.2.5
  • GitHub Check: ubuntu-latest / ghc 9.4
  • GitHub Check: ubuntu-latest / ghc 9.6
  • GitHub Check: stack / ghc 9.2.8
🔇 Additional comments (13)
src/Pretty.hs (5)

14-14: New imports for Set and for
No issues identified; these are used within the updated pretty-printing logic as intended.

Also applies to: 21-21


65-69: Great approach to monadic pretty-printing
Changing these functions to use the evalState St.empty monadic approach ensures consistent state management for renumbering, aligning with the PR's objective for deterministic error messages.

Also applies to: 72-76, 78-80, 82-87, 89-93


96-103: Documentation, state type definition, and refined pp signature
These updates provide clarity on how unification variables are tracked via Renumbering, improving maintainability and consistency in pretty-printing. No further concerns.

Also applies to: 104-104, 107-107, 110-110


239-243: Enhanced syntax error printing
Enclosing the message and locations in angle brackets, separated by a semicolon, helps visually parse the error message. Nicely implemented.


617-626: Value printing additions
The separate handling of IO actions, output ports, integers, constructors, and strings provides clear, distinct behavior for each value type. Good job maintaining consistency.

examples/non-examples/type-errors/not-a-function.golden (1)

1-2: Deterministic unification variable placeholders
"Expected (Integer → (?1 → ?2)) but got Integer" showcases clear, stable placeholders for unification variables, matching the PR's goal.

src/Util/Store.hs (3)

17-18: Exporting empty and size
Exposing these methods enriches the API for handling Store objects more flexibly.

Also applies to: 27-28


72-74: empty function
Providing a named constructor for an empty store improves code readability over relying on mempty.


102-103: size function
Offering this helper method aligns with common container patterns and makes it straightforward to retrieve store length.

src/Expander/Task.hs (1)

113-113: LGTM! Consistent with monadic pretty-printing refactor.

The change to wrap the return value with pure aligns with the broader refactoring effort to make pretty-printing functions return monadic values.

src/Expander/Error.hs (3)

19-19: LGTM! Required import for monadic traversal.

The addition of Data.Traversable (for) is necessary for the monadic traversal used in the refactored pretty-printing functions.


289-321: LGTM! Clear error message composition.

The refactoring to use do notation makes the complex error message composition more readable and easier to maintain. The structured binding of intermediate values improves code clarity.


325-330: LGTM! Consistent monadic style.

The changes to wrap return values with pure align with the broader refactoring effort to make pretty-printing functions return monadic values.

@doyougnu
Copy link
Collaborator

doyougnu commented Feb 7, 2025

Hey @gelisam, FYI I won't be able to get to this until Wednesday.

@gelisam
Copy link
Owner Author

gelisam commented Feb 7, 2025

No rush!

@doyougnu
Copy link
Collaborator

This LGTM, I really don't have any complaints or nits. It makes sense that the tests were not covering the meta ptrs. Hopefully this paves the way to more coverage.

@doyougnu doyougnu merged commit 2d5acb1 into main Feb 12, 2025
5 checks passed
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.

2 participants