bead.dsl¶
Domain-Specific Language for constraint expressions used in template slot filling and list partitioning.
Parser¶
parser
¶
Constraint DSL parser.
This module provides the parser for constraint expressions using the Lark parsing library. The parser converts constraint strings into AST nodes.
ASTBuilder
¶
Bases: Transformer
Transformer that converts Lark parse tree to AST nodes.
string_literal(items: list[Token]) -> ast.Literal
¶
Transform string literal.
number_literal(items: list[Token]) -> ast.Literal
¶
Transform number literal.
true_literal(items: list[Token]) -> ast.Literal
¶
Transform true literal.
false_literal(items: list[Token]) -> ast.Literal
¶
Transform false literal.
variable(items: list[Token]) -> ast.Variable
¶
Transform variable reference.
binary_op(items: list[Token | ast.ASTNode]) -> ast.BinaryOp
¶
Transform binary operation.
binary_op_not_in(items: list[Token | ast.ASTNode]) -> ast.BinaryOp
¶
Transform 'not in' binary operation.
unary_op(items: list[Token | ast.ASTNode]) -> ast.UnaryOp
¶
Transform unary operation.
attribute_access(items: list[Token | ast.ASTNode]) -> ast.AttributeAccess
¶
Transform attribute access.
subscript(items: list[Token | ast.ASTNode]) -> ast.Subscript
¶
Transform subscript access.
function_call(items: list[Token | ast.ASTNode | list[ast.ASTNode] | None]) -> ast.FunctionCall
¶
Transform function call.
list_literal(items: list[Token | ast.ASTNode | list[ast.ASTNode] | None]) -> ast.ListLiteral
¶
Transform list literal.
arguments(items: list[Token | ast.ASTNode]) -> list[ast.ASTNode]
¶
Transform function arguments, returning flat list.
list_elements(items: list[Token | ast.ASTNode]) -> list[ast.ASTNode]
¶
Transform list elements, returning flat list.
atom(items: list[Token | ast.ASTNode]) -> ast.ASTNode
¶
Transform atom (handles parenthesized expressions).
parse(expression: str) -> ast.ASTNode
¶
Parse constraint expression into AST.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expression
|
str
|
Constraint expression to parse. |
required |
Returns:
| Type | Description |
|---|---|
ASTNode
|
Root node of the abstract syntax tree. |
Raises:
| Type | Description |
|---|---|
ParseError
|
If the expression cannot be parsed. |
Examples:
Evaluator¶
evaluator
¶
Constraint evaluator for DSL.
This module provides the Evaluator class that executes AST nodes against an evaluation context to produce boolean results, and the DSLEvaluator class that provides a high-level interface for evaluating constraint expressions.
Evaluator
¶
Evaluator for constraint AST nodes.
The evaluator walks the AST and computes values based on the evaluation context. It supports: - All AST node types - Operator evaluation - Function calls - Attribute access - Caching for performance
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
use_cache
|
bool
|
Whether to cache evaluation results. |
True
|
Examples:
>>> from bead.dsl.context import EvaluationContext
>>> from bead.dsl.parser import parse
>>> ctx = EvaluationContext()
>>> ctx.set_variable("x", 10)
>>> evaluator = Evaluator()
>>> node = parse("x > 5")
>>> evaluator.evaluate(node, ctx)
True
evaluate(node: ast.ASTNode, context: EvaluationContext) -> Any
¶
Evaluate an AST node in the given context.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node
|
ASTNode
|
AST node to evaluate. |
required |
context
|
EvaluationContext
|
Evaluation context with variables and functions. |
required |
Returns:
| Type | Description |
|---|---|
Any
|
Result of evaluation. |
Raises:
| Type | Description |
|---|---|
EvaluationError
|
If evaluation fails (undefined variable, type error, etc.). |
clear_cache() -> None
¶
DSLEvaluator
¶
High-level evaluator for DSL constraint expressions.
This class provides a simplified interface for evaluating constraint expressions. It handles:
- Parsing expression strings to AST
- Building evaluation contexts from dictionaries
- Caching compiled ASTs
- Registering standard library functions
- Property extraction for list partitioning
The DSLEvaluator is the primary interface for constraint evaluation in the bead package. It wraps the lower-level Evaluator class.
Attributes:
| Name | Type | Description |
|---|---|---|
evaluator |
Evaluator
|
The underlying AST evaluator instance. |
compiled_cache |
dict[str, ASTNode]
|
Cache mapping expression strings to their compiled AST nodes. |
Examples:
>>> from bead.resources.items import LexicalItem
>>> evaluator = DSLEvaluator()
>>> item = LexicalItem(lemma="walk", pos="VERB")
>>> evaluator.evaluate(
... "self.pos == 'VERB'",
... {"self": item}
... )
True
>>> evaluator.evaluate(
... "self.lemma in motion_verbs",
... {"self": item, "motion_verbs": {"walk", "run", "jump"}}
... )
True
evaluate(expression: str, context: dict[str, ContextValue | LexicalItem | FilledTemplate | Item]) -> bool | str | int | float | list[Any]
¶
Evaluate DSL expression with given context.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expression
|
str
|
DSL expression to evaluate. |
required |
context
|
dict[str, ContextValue | LexicalItem | FilledTemplate | Item]
|
Variables available during evaluation. Can include: - ContextValue: primitive values, lists, sets - LexicalItem: lexical items for single-slot constraints - FilledTemplate: filled templates for multi-slot constraints - Item: items for list partitioning |
required |
Returns:
| Type | Description |
|---|---|
bool | str | int | float | list[Any]
|
Result of evaluation. |
Raises:
| Type | Description |
|---|---|
EvaluationError
|
If evaluation fails (parse error, undefined variable, etc.). |
Examples:
extract_property_value(obj: Any, property_expression: str, context: dict[str, ContextValue] | None = None) -> Any
¶
Extract property value using DSL expression.
This method is used by ListPartitioner to extract property values from items using DSL expressions. The property_expression is evaluated with the object available as 'item' in the context.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
Object to extract property from (typically a LexicalItem or Item). |
required |
property_expression
|
str
|
DSL expression that accesses object properties (e.g., "item.lemma", "item.features.number", "len(item.lemma)"). |
required |
context
|
dict[str, ContextValue] | None
|
Additional context variables (e.g., constants, helper data). |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
Extracted property value. |
Raises:
| Type | Description |
|---|---|
EvaluationError
|
If property extraction fails. |
Examples:
clear_cache() -> None
¶
Standard Library¶
stdlib
¶
Standard library functions for constraint DSL.
This module provides built-in functions that can be used in constraint expressions. Functions are organized by category and registered with the evaluation context.
len_(s: str | list[str | int | float | bool | None] | dict[str, str | int | float | bool | None] | tuple[str | int | float | bool | None, ...]) -> int
¶
lower(s: str) -> str
¶
upper(s: str) -> str
¶
startswith(s: str, prefix: str) -> bool
¶
endswith(s: str, suffix: str) -> bool
¶
contains(s: str, substring: str) -> bool
¶
Check if string contains substring.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s
|
str
|
String to check. |
required |
substring
|
str
|
Substring to look for. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if string contains substring. |
Examples:
replace(s: str, old: str, new: str) -> str
¶
Replace occurrences of substring.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s
|
str
|
String to modify. |
required |
old
|
str
|
Substring to replace. |
required |
new
|
str
|
Replacement substring. |
required |
Returns:
| Type | Description |
|---|---|
str
|
String with replacements. |
Examples:
split(s: str, sep: str = ' ') -> list[str]
¶
count(collection: str | list[DslScalar], item: DslScalar) -> int
¶
Count occurrences of item in collection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
collection
|
str | list[DslScalar]
|
Collection to search. |
required |
item
|
DslScalar
|
Item to count. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Number of occurrences. |
Examples:
sum_(collection: list[int | float]) -> int | float
¶
min_(collection: list[DslScalar]) -> DslScalar
¶
max_(collection: list[T]) -> T
¶
any_(collection: list[T]) -> bool
¶
all_(collection: list[T]) -> bool
¶
is_str(value: DslScalar) -> bool
¶
is_int(value: DslScalar) -> bool
¶
is_float(value: DslScalar) -> bool
¶
is_bool(value: DslScalar) -> bool
¶
is_list(value: DslScalar | list[DslScalar]) -> bool
¶
str_(value: DslScalar) -> str
¶
abs_(value: int | float) -> int | float
¶
round_(value: float, ndigits: int = 0) -> float
¶
floor(value: float) -> int
¶
ceil(value: float) -> int
¶
not_(value: DslScalar | list[DslScalar]) -> bool
¶
sigmoid(x: float) -> float
¶
Sigmoid activation function.
Converts unbounded value to probability in (0, 1).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
float
|
Input value. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Sigmoid output in (0, 1). |
Examples:
softmax(values: list[float]) -> list[float]
¶
Softmax function over list of values.
Converts list of scores to probability distribution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
values
|
list[float]
|
Input scores. |
required |
Returns:
| Type | Description |
|---|---|
list[float]
|
Probability distribution (sums to 1.0). |
Examples:
sample_categorical(probs: list[float], seed: int | None = None) -> int
¶
add_noise(value: float, noise_type: str, strength: float, seed: int | None = None) -> float
¶
Add noise to a value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
float
|
Original value. |
required |
noise_type
|
str
|
Type of noise ("gaussian", "uniform"). |
required |
strength
|
float
|
Noise strength (stddev for gaussian, range for uniform). |
required |
seed
|
int | None
|
Random seed. |
None
|
Returns:
| Type | Description |
|---|---|
float
|
Value with noise added. |
Examples:
model_output(item: Item, key: str, default: DslScalar = None) -> DslScalar | list[float]
¶
Extract model output from item.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item
|
Item
|
Item with model outputs. |
required |
key
|
str
|
Key to extract (e.g., "lm_score", "embedding"). |
required |
default
|
DslScalar
|
Default value if key not found. |
None
|
Returns:
| Type | Description |
|---|---|
DslScalar | list[float]
|
Extracted value or default. |
Examples:
distance(emb1: list[float], emb2: list[float], metric: str = 'cosine') -> float
¶
Compute distance between embeddings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
emb1
|
list[float]
|
First embedding. |
required |
emb2
|
list[float]
|
Second embedding. |
required |
metric
|
str
|
Distance metric ("cosine", "euclidean", "manhattan"). |
'cosine'
|
Returns:
| Type | Description |
|---|---|
float
|
Distance value. |
Examples:
preference_prob(score1: float, score2: float, temperature: float = 1.0) -> float
¶
Compute preference probability using sigmoid.
P(choose option 1) = sigmoid((score1 - score2) / temperature)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
score1
|
float
|
Score for option 1. |
required |
score2
|
float
|
Score for option 2. |
required |
temperature
|
float
|
Temperature for scaling. |
1.0
|
Returns:
| Type | Description |
|---|---|
float
|
Probability of choosing option 1. |
Examples:
register_stdlib(context: EvaluationContext) -> None
¶
Register all standard library functions in context.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
EvaluationContext
|
Context to register functions in. |
required |
Examples:
Abstract Syntax Tree¶
ast
¶
Abstract Syntax Tree node definitions for constraint DSL.
This module defines the AST nodes that represent parsed constraint expressions. Each node type corresponds to a construct in the constraint DSL grammar.
ASTNode
¶
Bases: BeadBaseModel
Base class for all AST nodes.
All AST nodes inherit from BeadBaseModel to get: - Automatic validation - Serialization support - Metadata tracking
Literal
¶
Bases: ASTNode
Literal value node (string, number, boolean).
Examples:
>>> node = Literal(value="hello")
>>> node.value
'hello'
>>> node = Literal(value=42)
>>> node.value
42
Variable
¶
Bases: ASTNode
Variable reference node.
References a variable in the evaluation context (e.g., item attributes).
Examples:
BinaryOp
¶
Bases: ASTNode
Binary operation node.
Represents operations like: a == b, x > y, p and q
Attributes:
| Name | Type | Description |
|---|---|---|
operator |
str
|
The operator (==, !=, <, >, <=, >=, and, or, in, etc.) |
left |
ASTNode
|
Left operand |
right |
ASTNode
|
Right operand |
Examples:
>>> left = Variable(name="pos")
>>> right = Literal(value="VERB")
>>> node = BinaryOp(operator="==", left=left, right=right)
>>> node.operator
'=='
UnaryOp
¶
Bases: ASTNode
Unary operation node.
Represents operations like: not x, -y
Examples:
>>> operand = Variable(name="is_transitive")
>>> node = UnaryOp(operator="not", operand=operand)
>>> node.operator
'not'
FunctionCall
¶
Bases: ASTNode
Function call node.
Represents function calls and method calls like: - len(x), startswith("pre") - obj.method(arg)
Examples:
>>> func = Variable(name="len")
>>> arg = Variable(name="lemma")
>>> node = FunctionCall(function=func, arguments=[arg])
>>> node.function.name
'len'
ListLiteral
¶
Bases: ASTNode
List literal node.
Represents list literals like: ["a", "b", "c"]
Examples:
>>> elements = [Literal(value="a"), Literal(value="b")]
>>> node = ListLiteral(elements=elements)
>>> len(node.elements)
2
Context¶
context
¶
Evaluation context for constraint DSL.
This module provides the EvaluationContext class that manages variable bindings and function lookups during constraint evaluation.
EvaluationContext
¶
Evaluation context for constraint expressions.
The context provides: - Variable bindings (e.g., item attributes) - Function registry (built-in and custom functions) - Parent context chain for scoping
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
parent
|
EvaluationContext | None
|
Parent context for variable/function lookup chain. |
None
|
Examples:
>>> ctx = EvaluationContext()
>>> ctx.set_variable("x", 42)
>>> ctx.get_variable("x")
42
>>> ctx.set_function("double", lambda x: x * 2)
>>> ctx.call_function("double", [5])
10
set_variable(name: str, value: Any) -> None
¶
Set a variable in the context.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Variable name. |
required |
value
|
Any
|
Variable value. |
required |
get_variable(name: str) -> Any
¶
Get a variable from the context (searches parent chain).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Variable name. |
required |
Returns:
| Type | Description |
|---|---|
Any
|
Variable value. |
Raises:
| Type | Description |
|---|---|
EvaluationError
|
If variable is not defined in context or parent chain. |
has_variable(name: str) -> bool
¶
Check if variable exists in context or parent chain.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Variable name. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if variable exists, False otherwise. |
set_function(name: str, func: Callable[..., Any]) -> None
¶
Register a function in the context.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Function name. |
required |
func
|
Callable[..., Any]
|
Function to register. |
required |
call_function(name: str, args: list[Any]) -> Any
¶
Call a function with arguments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Function name. |
required |
args
|
list[Any]
|
Function arguments. |
required |
Returns:
| Type | Description |
|---|---|
Any
|
Function return value. |
Raises:
| Type | Description |
|---|---|
EvaluationError
|
If function is not defined or call fails. |
has_function(name: str) -> bool
¶
Check if function exists in context or parent chain.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Function name. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if function exists, False otherwise. |
create_child() -> EvaluationContext
¶
Create a child context with this context as parent.
Returns:
| Type | Description |
|---|---|
EvaluationContext
|
New child context. |
Examples:
Errors¶
errors
¶
DSL-specific exceptions.
DSLError
¶
Bases: Exception
Base exception for DSL errors.
ParseError
¶
Bases: DSLError
Exception raised when parsing fails.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
str
|
Error message describing what went wrong during parsing. |
required |
line
|
int | None
|
Line number where the error occurred (1-indexed). None if unknown. |
None
|
column
|
int | None
|
Column number where the error occurred (1-indexed). None if unknown. |
None
|
text
|
str | None
|
The text that caused the error. None if unavailable. |
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
line |
int | None
|
Line number where error occurred. |
column |
int | None
|
Column number where error occurred. |
text |
str | None
|
Text that caused the error. |
Examples:
>>> try:
... raise ParseError("Unexpected token", line=5, column=12, text="@invalid")
... except ParseError as e:
... print(e.line, e.column)
5 12
__str__() -> str
¶
Return formatted error message.