File size: 1,650 Bytes
dfad45c
 
 
 
 
 
 
 
 
 
 
9c88759
 
dfad45c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from langchain_core.tools import tool
from io import StringIO
import pandas as pd

@tool
def add_numbers_in_list(numbers: list[float]) -> float:
    """Add all numbers in a list.
    Always use this tool for summing numerical values, instead of doing math directly in the response.
    
    Args:
        numbers (list[float]): A list of numbers to add.
    Returns:
        str: The sum of the numbers in the list.
    """
    return sum(numbers)

@tool
def check_commutativity(table_str: str) -> str:
    """
    Given a binary operation table (in markdown format), returns the subset of elements 
    involved in counter-examples to commutativity, sorted alphabetically.
    
    Args:
        table_str (str): Markdown table defining the operation * on a finite set.

    Returns:
        str: Comma-separated list of elements in the counter-example set, alphabetically sorted.
    """
    # Read the table using pandas
    df = pd.read_csv(StringIO(table_str), sep="|", skipinitialspace=True, engine='python')

    # Drop empty columns due to leading/trailing pipes
    df = df.dropna(axis=1, how="all")
    df.columns = [c.strip() for c in df.columns]
    df = df.dropna(axis=0, how="all")

    # Extract header and values
    elements = df.columns[1:]
    df.index = df[df.columns[0]]
    df = df.drop(df.columns[0], axis=1)

    # Check commutativity: a*b == b*a
    counterexample_elements = set()
    for x in elements:
        for y in elements:
            if df.loc[x, y] != df.loc[y, x]:
                counterexample_elements.add(x)
                counterexample_elements.add(y)

    return ", ".join(sorted(counterexample_elements))