Skip to content

Implement Set<T> collection type#131

Merged
chrishayen merged 3 commits into
masterfrom
feature/set-collection
Jan 2, 2026
Merged

Implement Set<T> collection type#131
chrishayen merged 3 commits into
masterfrom
feature/set-collection

Conversation

@clank-hayen

@clank-hayen clank-hayen Bot commented Jan 2, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Implement Set collection backed by std::unordered_set for O(1) lookup
  • Add set literal syntax: {1, 2, 3} for concise set creation
  • Include full set algebra: union, intersection, difference, symmetric_difference

Changes

  • Lexer/Parser: Add SET token and parse Set() constructor and {expr, ...} literals
  • Type Checker: Add type inference for sets and validate set method calls
  • Code Generator: Generate std::unordered_set code with lambda-based set operations
  • Tests: Comprehensive test suite covering all features

Set Features

Category Methods
Query length(), is_empty(), contains(elem)
Modification add(elem), remove(elem), clear()
Operations union(other), intersection(other), difference(other), symmetric_difference(other)
Predicates is_subset(other), is_superset(other)
Iteration for item in set { ... }

Test plan

  • Build compiler with new Set implementation
  • Run bishop test tests/test_sets.b
  • Verify set literals work: {1, 2, 3}
  • Verify set operations return new sets
  • Verify iteration works correctly

Closes #37

🤖 Generated with Claude Code

clank-hayen Bot and others added 3 commits January 2, 2026 02:53
Add a new Set<T> collection with O(1) lookup backed by std::unordered_set:

- Set<T>() constructor for empty sets
- Set literal syntax: {1, 2, 3}
- Query methods: length(), is_empty(), contains()
- Modification methods: add(), remove(), clear()
- Set operations: union(), intersection(), difference(), symmetric_difference()
- Set predicates: is_subset(), is_superset()
- Iteration support: for item in set

Closes #37

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
The parser was missing a handler for Set<T> variable declarations
like `Set<int> nums = {1, 2, 3};`. This caused a parse error
"unexpected token '>'" when using explicit type syntax with Set.

Added the same pattern used for List<T>, Pair<T>, Tuple<T>, and
Channel<T> variable declarations.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
The typechecker was not recognizing Set<T> as a valid type for
explicit type declarations. This caused "unknown type 'Set<int>'"
errors when using typed variable declarations like `Set<int> nums = {1, 2, 3};`.

Added the same pattern used for List<T>, Pair<T>, Tuple<T>, and
Channel<T> type validation.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@clank-hayen
clank-hayen Bot marked this pull request as ready for review January 2, 2026 03:12
@clank-hayen
clank-hayen Bot requested a review from chrishayen January 2, 2026 03:12

@clank-hayen clank-hayen Bot left a comment

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.

Code Review: Set Collection Type

REVIEW:

  • summary: Clean, well-structured implementation of Set with comprehensive test coverage.

✅ What looks good:

  1. Architecture: Properly separated files follow project conventions (sets.cpp/hpp, check_set.cpp, emit_set.cpp)
  2. Type Safety: Proper type inference, element type extraction, and Set parameter validation
  3. Set Operations: Union, intersection, difference, symmetric_difference all correctly implemented using lambda-based C++ generation
  4. Documentation: Excellent docstrings in C++ code and comprehensive README section with examples
  5. Test Coverage: 36 test cases covering creation, literals, methods, operations, predicates, and iteration
  6. Error Handling: Empty set literal properly rejected with helpful error message

Security Review (OWASP Top 10):

  • ✅ No injection risks - type-checked method calls only
  • ✅ No data exposure concerns
  • ✅ No authentication/authorization applicable
  • ✅ No deserialization issues - set literals are compile-time checked

Suggestions (non-blocking):

  1. Minor doc update: The Keywords section in README.md doesn't include Set. Consider adding it:

    `fn`, `return`, `struct`, `if`, `else`, `while`, `for`, `in`, `true`, `false`, `none`, `is`, `import`, `using`, `select`, `case`, `Channel`, `List`, `Pair`, `Tuple`, `Set`, `extern`, `go`, `sleep`, `err`, `fail`, `or`, `match`, `default`, `with`, `as`, `const`, `continue`, `break`
    

    Location: README.md, Keywords section near the end

  2. Consider for future: The emit_set_literal function stores first_elem but doesn't use it - could be removed if not needed for type deduction.

Decision: APPROVE

The implementation is correct, well-tested, follows project conventions, and introduces no security concerns. The missing keyword in the documentation is minor and non-blocking.

🤖 Reviewed by Claude Code

@clank-hayen

clank-hayen Bot commented Jan 2, 2026

Copy link
Copy Markdown
Contributor Author

Code Review: Implement Set Collection Type

Summary

Well-implemented Set collection type with comprehensive functionality. The code follows existing patterns in the codebase, has thorough test coverage, and documentation is complete.


✅ What's Good

Parser Changes:

  • Clean AST nodes (SetCreate, SetLiteral) following existing patterns
  • Set literal syntax {1, 2, 3} is intuitive and matches common languages
  • Proper line number tracking on all AST nodes
  • Supports nested generics like Set<List<int>>

Type Checker:

  • Type checking for Set methods properly validates argument types
  • Correct substitution of T placeholder with actual element type
  • Mixed-type detection in set literals provides clear error messages
  • Integration with is_valid_type() for Set types
  • For-each loop support for Set iteration is handled correctly

Code Generation:

  • Uses std::unordered_set as backing - appropriate for O(1) lookup
  • Set operations (union, intersection, difference, symmetric_difference) correctly implemented using IILEs
  • Uses std::remove_cvref_t<decltype({})> for proper type deduction in set operations

Test Coverage (389 lines):

  • Empty set creation (both inferred and typed)
  • Set literals with various types (int, str, bool)
  • Duplicate handling in literals
  • All query methods: length(), is_empty(), contains()
  • All modification methods: add(), remove(), clear()
  • All set operations: union(), intersection(), difference(), symmetric_difference()
  • All predicates: is_subset(), is_superset()
  • Set iteration via for-each
  • Typed variable declarations

Documentation:

  • README.md Set section (lines 680-761) is thorough and includes:
    • Creation syntax
    • Set literals
    • All method examples
    • Set operations with clear examples
    • Iteration with caveat about unordered iteration

Minor Observations (Non-blocking)

  1. Empty set literal error message - The error "empty set literal not allowed, use Set() instead" is clear and helpful.

  2. Set iteration order - Documentation correctly notes that iteration order is not guaranteed - good user expectation setting.

  3. Duplicate handling - Test test_set_literal_duplicates verifies that {1, 2, 2, 3, 3, 3} correctly produces a set of length 3.


Security Review (OWASP Top 10)

  • ✅ No injection vulnerabilities
  • ✅ No hardcoded secrets or credentials
  • ✅ No security misconfigurations
  • ✅ Type safety enforced through typechecker
  • ✅ No authentication/authorization concerns (language feature, not auth)

Decision: APPROVE ✅

The implementation is correct, secure, well-tested, and follows existing codebase patterns. Ready to merge.


@chrishayen - Please review and merge when ready.

🤖 Reviewed by Claude Code

@clank-hayen
clank-hayen Bot requested a review from chrishayen January 2, 2026 04:33
@chrishayen
chrishayen merged commit 51712e9 into master Jan 2, 2026
1 check passed
clank-hayen Bot pushed a commit that referenced this pull request Jan 2, 2026
Resolve merge conflicts with Set<T> collection type from PR #131.
Both Map<K, V> and Set<T> are now included.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
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.

Implement Set<T> collection

1 participant