Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 10, 2025

The C++ term_t::match() function existed but was not exposed in Python and TypeScript bindings. This adds the missing binding layer and API surface.

Changes

Bindings (C++)

  • Added term_match() wrapper functions in apyds/ds.cc and atsds/ds.cc
  • Exposed as static method on Term class via pybind11/embind
  • Scopes hardcoded to "r" and "f" (matching rule_t::match behavior)

API Layer

  • Python: Term.__matmul__(other) in term_t.py (using @ operator)
  • TypeScript: term_t.match(other) in index.mts
  • No scope parameters - scopes are hardcoded internally

Tests & Docs

  • 3 test cases each for Python/TypeScript covering simple/complex/fail matching scenarios
  • API reference updated in docs/api/
  • Added Matching section to docs/concepts/terms.md

Usage

# Python - using @ operator
a = Term("`x")
b = Term("value")
result = a @ b
print(result)  # ((r f `x value))
// TypeScript
const a = new term_t("`x");
const b = new term_t("value");
const result = a.match(b);
console.log(result.toString());  // ((r f `x value))

Match returns dictionary as 4-tuple format: ((r f key value)) with hardcoded scopes "r" and "f".

Original prompt

This section details on the original issue you should resolve

<issue_title>缺少term的match函数,在python和ts的binding中</issue_title>
<issue_description>需要做的事情

  1. 在python和ts的binding文件中添加,并在上层接口文件中实现下接口
  2. 在测试文件夹中的对应文件中补充一下测试
  3. 在readme和docs中更新api reference的部分,不用特地补充相关的例子
  4. 其他我没注意到的地方,如果存在</issue_description>

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


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 2 commits December 10, 2025 15:49
Co-authored-by: hzhangxyz <11623447+hzhangxyz@users.noreply.github.com>
Co-authored-by: hzhangxyz <11623447+hzhangxyz@users.noreply.github.com>
@hzhangxyz hzhangxyz marked this pull request as ready for review December 10, 2025 16:03
Copilot AI review requested due to automatic review settings December 10, 2025 16:03
Copilot AI changed the title [WIP] Add missing term match function in Python and TS bindings Add term.match() to Python and TypeScript bindings Dec 10, 2025
Copilot AI requested a review from hzhangxyz December 10, 2025 16:06
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds the missing term_match function to both Python and TypeScript bindings, enabling term unification with scope support. The implementation follows the existing patterns in the codebase for similar binding functions like term_ground and term_rename, with appropriate handling of scope parameters (passing empty strings rather than nullptr, as the match operation uses scopes to label variables in the result dictionary).

Key changes:

  • Added C++ binding functions (term_match) in both Python and TypeScript binding files
  • Implemented high-level match() methods in Python Term class and TypeScript term_t class
  • Added comprehensive test coverage with 4 test cases each (simple, complex, fail, and scoped scenarios)
  • Updated API documentation for both languages

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
apyds/ds.cc Added term_match C++ binding function and registered it in the Python module
apyds/term_t.py Implemented match() method in the Term class with proper None-to-empty-string conversion
atsds/ds.cc Added term_match C++ binding function and registered it in the TypeScript module
atsds/index.mts Implemented match() method in the term_t class with default empty string parameters
tests/test_term.py Added 4 comprehensive test cases covering various match scenarios
tests/test_term.mjs Added 4 comprehensive test cases mirroring the Python tests
docs/api/python.md Added API documentation for the match() method
docs/api/typescript.md Added API documentation for the match() method

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

const b = new term_t("b");
const result = a.match(b);
if (result !== null) {
console.log(result.toString()); // "(( `a b))"
Copy link

Copilot AI Dec 10, 2025

Choose a reason for hiding this comment

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

The expected output in the comment is incorrect. When scope parameters are not provided (or are empty strings), the result should include two spaces for the empty scopes. The output should be "(( \a b))"(with two spaces after the opening parentheses) instead of"((`a b))"`.

This matches the test expectation in tests/test_term.mjs line 109 and is consistent with how the underlying C++ implementation handles empty scopes.

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed in f9114ab. Updated to show "((r f `a b))" with hardcoded scopes "r" and "f".

atsds/index.mts Outdated
* const b = new term_t("b");
* const result = a.match(b);
* if (result !== null) {
* console.log(result.toString()); // "((`a b))"
Copy link

Copilot AI Dec 10, 2025

Choose a reason for hiding this comment

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

The expected output in the JSDoc comment is incorrect. When scope parameters are not provided (defaulting to empty strings), the result should include two spaces for the empty scopes. The output should be "(( \a b))"(with two spaces after the opening parentheses) instead of"((`a b))"`.

This matches the test expectation in tests/test_term.mjs line 109.

Suggested change
* console.log(result.toString()); // "((`a b))"
* console.log(result.toString()); // "(( `a b))"

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed in f9114ab. Updated to show "((r f `a b))" with hardcoded scopes "r" and "f".

expect(result.toString()).toBe("((scope1 scope2 `a `b))");
});


Copy link

Copilot AI Dec 10, 2025

Choose a reason for hiding this comment

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

[nitpick] There are two blank lines at the end of the file, but only one blank line is needed for consistency with the rest of the file and with the Python test file. Remove one of the extra blank lines.

Suggested change

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed in f9114ab. Removed extra blank line at end of file.

apyds/term_t.py Outdated
>>> a = Term("`a")
>>> b = Term("b")
>>> result = a.match(b)
>>> str(result) if result else None # "((`a b))"
Copy link

Copilot AI Dec 10, 2025

Choose a reason for hiding this comment

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

The expected output in the docstring example is incorrect. When scope parameters are not provided (defaulting to None, which are converted to empty strings), the result should include two spaces for the two empty scopes. The output should be "(( \a b))"(with two spaces after the opening parentheses) instead of"((`a b))"`.

This matches the test expectation in tests/test_term.py line 121.

Suggested change
>>> str(result) if result else None # "((`a b))"
>>> str(result) if result else None # "(( `a b))"

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed in f9114ab. Updated to show "((r f `a b))" with hardcoded scopes "r" and "f".

Copy link
Member

@hzhangxyz hzhangxyz left a comment

Choose a reason for hiding this comment

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

我对apyds和atsds进行了修改, 你帮我把其他部分处理了吧

  1. 检查apyds和atsds中是否存在笔误,如果有修复一下
  2. 接口变了, 所以tests和docs/api需要更新, 注意现在scope写死的,所以应该只需要simple complex fail三个情况了
  3. 最后需要使用pre commit 检查一下格式
  4. docs/concepts/下面term需要模仿rule对match添加一下介绍

Co-authored-by: hzhangxyz <11623447+hzhangxyz@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Dec 10, 2025

我对apyds和atsds进行了修改, 你帮我把其他部分处理了吧

  1. 检查apyds和atsds中是否存在笔误,如果有修复一下...

已完成所有更新 (commit f9114ab):

  1. ✅ 检查了 apyds 和 atsds - 您的修改正确,无需额外修复
  2. ✅ 更新了 tests 和 docs/api - 移除了 test_match_with_scopes,更新期望值为 "((r f ...))"
  3. ✅ 运行 pre-commit - 所有检查通过
  4. ✅ 在 docs/concepts/terms.md 添加了 Matching 部分,模仿了 rules.md 的格式

所有 Python 测试通过。

@hzhangxyz hzhangxyz merged commit f7b4df4 into main Dec 10, 2025
73 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.

缺少term的match函数,在python和ts的binding中

2 participants