Skip to content

Implement staged resolution with separated error types for witness resolution#5

Merged
Garciat merged 7 commits into
mainfrom
copilot/implement-staged-resolution
Dec 14, 2025
Merged

Implement staged resolution with separated error types for witness resolution#5
Garciat merged 7 commits into
mainfrom
copilot/implement-staged-resolution

Conversation

Copilot AI commented Dec 13, 2025

Copy link
Copy Markdown
Contributor
  • Create InstantiationPlan data structure to represent the fully resolved instantiation plan
  • Create Expr data structure to represent a reduced AST of interpretable Java operations
  • Implement resolveWitness method to convert ParsedType to InstantiationPlan
  • Implement compile method to convert InstantiationPlan to Expr
  • Implement interpret method to execute Expr with a Context
  • Refactor existing summon logic to use the new staged resolution approach
  • Run existing tests to ensure backward compatibility (all tests pass)
  • Format code (formatting checks pass)
  • Run code review (minor suggestions noted, no critical issues)
  • Run CodeQL security check (no vulnerabilities found)
  • Split error types: ResolutionError (NotFound, Ambiguous, Nested), InstantiationError (LookupMiss, InvocationException), SummonError (Resolution, Instantiation)
  • Replace manual pattern matching with Either.traverse in interpret method
  • Remove Candidate type (replaced with local Match record in resolveWitness)
  • Remove instantiate method from WitnessRule interface and subtypes (no longer needed with staged resolution)
  • Add Either.call helper method for wrapping Callable execution in Either
  • Use Either.call in interpret method to simplify exception handling
Original prompt

This section details on the original issue you should resolve

<issue_title>Implemented staged resolution</issue_title>
<issue_description>It would be interesting to further divide witness resolution into a couple more lowering stages.

This could help down the line as we try to do fancier stuff.

Here's some Haskell pseude-code of the idea:

data ParsedType = ...

parseType :: java.lang.reflect.Type -> ParsedType

data WitnessRule = ContextInstance ... | InstanceConstructor ...

findRules :: ParsedType -> [WitnessRule]

-- [New]
-- Represents the fully resolved instantiation plan
-- As opposed to our current `Candidate WitnessRule [ParsedType]`
data InstantiationPlan = PlanStep { target :: WitnessRule, dependencies :: [PlanStep] }

resolveWitness :: ParsedType -> Either ResolutionError InstantiationPlan

-- [New]
-- Represents a reduced AST of interpretable Java operations
data Expr k
  = InvokeConstructor java.lang.reflect.Method [Expr k]
  | Lookup k

-- [New]
compile :: InstantiationPlan -> Expr ParsedType

type Context k = k -> Maybe java.lang.Object

-- [New]
interpret :: Context k -> Expr k -> Either InstantiationError java.lang.Object

-- Then:
witness :: Ty k -> k
witness = {- some composition of: parseType >> resolveWitness >> compile >> interpret -}
```</issue_description>

## Comments on the Issue (you are @copilot in this section)

<comments>
</comments>

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

…staged methods

Co-authored-by: Garciat <118277+Garciat@users.noreply.github.com>
Copilot AI changed the title [WIP] Implement staged resolution for witness resolution Implement staged resolution for witness resolution Dec 13, 2025
Copilot AI requested a review from Garciat December 13, 2025 20:28
@Garciat

Garciat commented Dec 13, 2025

Copy link
Copy Markdown
Owner

@copilot good first step. Now let's also split the error types:

ResolutionError = NotFound | Ambiguous | Nested

InstantiationError = LookupMiss | InvocationException

SummonError = Resolution ResolutionError | Instantiation InstantiationError

…nError

Co-authored-by: Garciat <118277+Garciat@users.noreply.github.com>

Copilot AI commented Dec 13, 2025

Copy link
Copy Markdown
Contributor Author

@copilot good first step. Now let's also split the error types:

ResolutionError = NotFound | Ambiguous | Nested...

Done. Split the error types as requested:

  • ResolutionError with NotFound, Ambiguous, and Nested variants
  • InstantiationError with LookupMiss and InvocationException variants
  • SummonError as a union type with Resolution(ResolutionError) and Instantiation(InstantiationError) variants

The staged pipeline now has proper error separation:

  • resolveWitness returns Either<ResolutionError, InstantiationPlan>
  • interpret returns Either<InstantiationError, Object>
  • summon maps these to Either<SummonError, Object>

Commit: c264eb1

Copilot AI changed the title Implement staged resolution for witness resolution Implement staged resolution with separated error types for witness resolution Dec 13, 2025

@Garciat Garciat left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

@copilot If the Candidate type is no longer used, feel free to remove it.

InterpretContext context, Expr<ParsedType> expr) {
return switch (expr) {
case Expr.Lookup<ParsedType>(var type) -> context.lookup(type);
case Expr.InvokeConstructor<ParsedType>(var method, var args) -> {

@Garciat Garciat Dec 13, 2025

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

@copilot This case can be handled with Either.traverse instead of all of this manual pattern matching. Try that please.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Replaced the manual pattern matching with Either.traverse. The new implementation uses Either.traverse(args, arg -> interpret(context, arg)) to evaluate all arguments, which automatically handles error propagation and success case collection.

Commit: e4dff04

@Garciat

Garciat commented Dec 13, 2025

Copy link
Copy Markdown
Owner

@copilot apply the comments above.

Co-authored-by: Garciat <118277+Garciat@users.noreply.github.com>

Copilot AI commented Dec 13, 2025

Copy link
Copy Markdown
Contributor Author

@copilot apply the comments above.

Applied both comments:

  1. Removed Candidate type - replaced with local Match record
  2. Used Either.traverse instead of manual pattern matching in interpret

Commit: e4dff04

@copilot If the Candidate type is no longer used, feel free to remove it.

Removed the Candidate type. Replaced it with a local Match record inside resolveWitness and updated ResolutionError.Ambiguous to store List<WitnessRule> instead of List<Candidate>. Also removed the findCandidates method since the logic is now inlined.

Commit: e4dff04

Copilot AI requested a review from Garciat December 13, 2025 21:27

@Garciat Garciat left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

@copilot:

The instantiate method on WitnessRule is no longer needed, along with their implementation in the subtypes.

We can introduce a helper:

Either<Exception, A> Either.call(Callable);

That can be used in the interpret method. Along with their implementation necessary mapLeft call and other compositions.

Also for these newly introduced types, lets places them under the methods that first use them, respectively.

Copilot AI commented Dec 13, 2025

Copy link
Copy Markdown
Contributor Author

@copilot:

The instantiate method on WitnessRule is no longer needed, along with their implementation in the subtypes. ...

The comment appears to be cut off. Could you clarify what helper you'd like me to introduce? The text ends with " Either".

I can see that the instantiate method on WitnessRule is indeed no longer used since we now use the staged resolution approach (compile → interpret), but I need to understand what helper you'd like before making changes.

Co-authored-by: Garciat <118277+Garciat@users.noreply.github.com>
Copilot AI requested a review from Garciat December 13, 2025 21:50
@Garciat

Garciat commented Dec 13, 2025

Copy link
Copy Markdown
Owner

@copilot I meant:

static <A> Either<Exception, A> call(Callable<A>);

That's the signature for the helper in the Either class.

Copilot AI commented Dec 13, 2025

Copy link
Copy Markdown
Contributor Author

@copilot I meant:

"static Either<Exception, A> Either.call(Callable);"...

The comment still appears incomplete - it says "static Either but cuts off there. Could you provide the complete signature and description of the helper method you'd like me to add?

For context, I've already removed the instantiate method from WitnessRule and its subtypes in commit fdc90e4, as the staged resolution approach (compile → interpret) now handles instantiation directly.

@Garciat

Garciat commented Dec 13, 2025

Copy link
Copy Markdown
Owner

@copilot I meant:

static <A> Either<Exception, A> call(Callable<A>);

That's the signature for the helper in the Either class.

Co-authored-by: Garciat <118277+Garciat@users.noreply.github.com>

Copilot AI commented Dec 13, 2025

Copy link
Copy Markdown
Contributor Author

@copilot I meant:

static <A> Either<Exception, A> call(Callable<A>);...

Added the Either.call helper method with signature static <A> Either<Exception, A> call(Callable<A>). This method wraps the execution of a Callable, returning Either.right(result) on success or Either.left(exception) if an exception is thrown.

Commit: d469d2a

Co-authored-by: Garciat <118277+Garciat@users.noreply.github.com>
@Garciat Garciat marked this pull request as ready for review December 14, 2025 06:51
@Garciat Garciat merged commit aa428a7 into main Dec 14, 2025
2 checks passed
@Garciat Garciat deleted the copilot/implement-staged-resolution branch December 14, 2025 06:54
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.

Implemented staged resolution

2 participants