Skip to content

additional safety condition for terminal rules#72

Merged
shanest merged 1 commit into
CLMBRs:mainfrom
hjyeob:safer-terminal-rule
Feb 4, 2026
Merged

additional safety condition for terminal rules#72
shanest merged 1 commit into
CLMBRs:mainfrom
hjyeob:safer-terminal-rule

Conversation

@hjyeob

@hjyeob hjyeob commented Feb 4, 2026

Copy link
Copy Markdown
Contributor

Currently ultk defines terminal rule as following:
if rhs and len(rhs) == 1 and issubclass(rhs[0], Referent):
This only allows rules with one Referent-like argument as a terminal rule.

  • However, issubclass crashes when RHS annotation is a typing object (such as typing.Callable).
  • Fixed this issue by checking inspect.isclass(rhs[0]) before calling issubclass.
  • The main logic of terminal rule, one referent-like argument, remains unchanged.

Example (CRASP grammar NOT and COUNT):

def NOT(p: bool_op) -> bool_op:
    """Create a boolean operation that negates the result of another boolean operation.

    Args:
        p (bool_op): The boolean operation to negate.

    Returns:
        bool_op: A boolean operation function that performs negation and returns its truth value.
    """
    return lambda seq, idx: not p(seq, idx)
def COUNT(p: bool_op) -> count_op:
    """Create a count operation that counts how many positions up to index satisfy a predicate.

    Args:
        p (bool_op): The boolean operation (predicate) to count.

    Returns:
        count_op: A count operation function that applies given boolean operation at each index 
                  and returns the number of index where predicate holds true.
    """
    return lambda seq, idx: sum(p(seq, j) for j in range(min(idx + 1, len(seq))))

These functions will not be accepted as a valid grammar rule in current setting, but they should be accepted as non-terminal rule since their argument is another primitives.

@shanest shanest left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Great! I see the issue you were encountering now, i.e. that issubclass would raise an error if rhs[0] was not a class. Thanks for uncovering and fixing

@shanest shanest merged commit 907b087 into CLMBRs:main Feb 4, 2026
1 check failed
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