Skip to content

feat(#247): implement set operations (union, intersection, difference, symmetric difference)#254

Merged
xmnlab merged 3 commits intoarxlang:mainfrom
Jaskirat-s7:feature-issue-247-set-ops
Apr 1, 2026
Merged

feat(#247): implement set operations (union, intersection, difference, symmetric difference)#254
xmnlab merged 3 commits intoarxlang:mainfrom
Jaskirat-s7:feature-issue-247-set-ops

Conversation

@Jaskirat-s7
Copy link
Copy Markdown
Contributor

Summary

Closes #247

LiteralSet lowers to a sorted constant integer array but had no visitor
for any set operation — making sets write-only constructs. This PR adds
compile-time support for all four core set operations via the existing
BinaryOp node.

What changed

src/irx/builders/llvmliteir.py

_try_set_binary_op(lhs, rhs, op_code) → bool — new helper method:

  • Returns False immediately if op_code isn't |, &, -, ^
  • Returns False if either operand isn't a constant integer array
  • Extracts Python set[int] values from both operands
  • Applies the Python set operation
  • Emits a new sorted constant ir.ArrayType onto result_stack
  • Promotes element type to widest integer width when operands differ

visit(BinaryOp) — one-line hook added before the existing falsy guard:

if self._try_set_binary_op(llvm_lhs, llvm_rhs, node.op_code):
    return

@Jaskirat-s7
Copy link
Copy Markdown
Contributor Author

Jaskirat-s7 commented Mar 22, 2026

@yuvimittal ,i also wrote a standalone demo script that exercises all 4 set operations through the LLVMLiteIRVisitor
pipeline that constructs two LiteralSet AST nodes, feeds them through BinaryOp with each op code, and validates the resulting constant array against Python's own set algebra->
s1 = LiteralSet({LiteralInt32(1), LiteralInt32(2)})
s2 = LiteralSet({LiteralInt32(2), LiteralInt32(3)})
visitor.visit(BinaryOp(op_code="|", lhs=s1, rhs=s2))
(→ constant [3 x i32] [i32 1, i32 2, i32 3])
Demo script output — all 5 scenarios pass:
image
All 9 tests passing (4 existing + 5 new):

image the implementation does compile-time set algebra via _try_set_binary_op where both operands are constant integer arrays, so the result is computed during IR generation with zero runtime overhead.

…nsion

Add compile-time set operations (union |, intersection &, difference -,
symmetric difference ^) for LiteralSet operands.

LiteralSet already lowers to a sorted constant integer array. When the
BinaryOp visitor detects both operands are constant int arrays and the
op_code is one of |, &, -, ^, it delegates to _try_set_binary_op which:
- extracts Python set[int] values from each constant array
- applies the Python set operation
- emits the result as a new sorted constant ir.ArrayType

No runtime IR is needed since both operands are always constant at
IR-generation time. Promotes element type to widest width when operands
have mixed integer widths.

Add 5 tests to tests/test_set.py:
- test_set_union
- test_set_intersection
- test_set_difference
- test_set_symmetric_difference
- test_set_disjoint_intersection_is_empty
@xmnlab xmnlab force-pushed the feature-issue-247-set-ops branch from f63f03c to d39fcab Compare April 1, 2026 02:51
@xmnlab xmnlab merged commit 2cbe8f7 into arxlang:main Apr 1, 2026
13 checks passed
@xmnlab
Copy link
Copy Markdown
Contributor

xmnlab commented Apr 1, 2026

thanks @Jaskirat-s7

@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 2, 2026

🎉 This PR is included in version 1.9.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feat: Set operations (union, intersection, difference) not implemented

2 participants