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))