quantrs2_tytan/problem_dsl/
ast.rs

1//! Abstract syntax tree definitions for the problem DSL.
2
3use super::types::VarType;
4use std::collections::HashMap;
5
6/// Abstract syntax tree
7#[derive(Debug, Clone)]
8pub enum AST {
9    /// Program root
10    Program {
11        declarations: Vec<Declaration>,
12        objective: Objective,
13        constraints: Vec<Constraint>,
14    },
15
16    /// Variable declaration
17    VarDecl {
18        name: String,
19        var_type: VarType,
20        domain: Option<Domain>,
21        attributes: HashMap<String, Value>,
22    },
23
24    /// Expression
25    Expr(Expression),
26
27    /// Statement
28    Stmt(Statement),
29}
30
31/// Declaration types
32#[derive(Debug, Clone)]
33pub enum Declaration {
34    /// Variable declaration
35    Variable {
36        name: String,
37        var_type: VarType,
38        domain: Option<Domain>,
39        attributes: HashMap<String, Value>,
40    },
41
42    /// Parameter declaration
43    Parameter {
44        name: String,
45        value: Value,
46        description: Option<String>,
47    },
48
49    /// Set declaration
50    Set { name: String, elements: Vec<Value> },
51
52    /// Function declaration
53    Function {
54        name: String,
55        params: Vec<String>,
56        body: Box<Expression>,
57    },
58}
59
60/// Variable domain
61#[derive(Debug, Clone)]
62pub enum Domain {
63    /// Range domain
64    Range { min: f64, max: f64 },
65    /// Set domain
66    Set { values: Vec<Value> },
67    /// Index set
68    IndexSet { set_name: String },
69}
70
71/// Value types
72#[derive(Debug, Clone)]
73pub enum Value {
74    Number(f64),
75    Boolean(bool),
76    String(String),
77    Array(Vec<Self>),
78    Tuple(Vec<Self>),
79}
80
81/// Objective function
82#[derive(Debug, Clone)]
83pub enum Objective {
84    Minimize(Expression),
85    Maximize(Expression),
86    MultiObjective {
87        objectives: Vec<(ObjectiveType, Expression, f64)>,
88    },
89}
90
91#[derive(Debug, Clone)]
92pub enum ObjectiveType {
93    Minimize,
94    Maximize,
95}
96
97/// Constraint
98#[derive(Debug, Clone)]
99pub struct Constraint {
100    pub name: Option<String>,
101    pub expression: ConstraintExpression,
102    pub tags: Vec<String>,
103}
104
105/// Constraint expression
106#[derive(Debug, Clone)]
107pub enum ConstraintExpression {
108    /// Simple comparison
109    Comparison {
110        left: Expression,
111        op: ComparisonOp,
112        right: Expression,
113    },
114
115    /// Logical combination
116    Logical { op: LogicalOp, operands: Vec<Self> },
117
118    /// Quantified constraint
119    Quantified {
120        quantifier: Quantifier,
121        variables: Vec<(String, String)>, // (var, set)
122        constraint: Box<Self>,
123    },
124
125    /// Implication
126    Implication {
127        condition: Box<Self>,
128        consequence: Box<Self>,
129    },
130
131    /// Counting constraint
132    Counting {
133        variables: Vec<String>,
134        op: ComparisonOp,
135        count: Expression,
136    },
137}
138
139/// Comparison operators
140#[derive(Debug, Clone, PartialEq, Eq)]
141pub enum ComparisonOp {
142    Equal,
143    NotEqual,
144    Less,
145    Greater,
146    LessEqual,
147    GreaterEqual,
148}
149
150/// Logical operators
151#[derive(Debug, Clone)]
152pub enum LogicalOp {
153    And,
154    Or,
155    Not,
156    Xor,
157}
158
159/// Quantifiers
160#[derive(Debug, Clone)]
161pub enum Quantifier {
162    ForAll,
163    Exists,
164    ExactlyOne,
165    AtMostOne,
166    AtLeastOne,
167}
168
169/// Expression
170#[derive(Debug, Clone)]
171pub enum Expression {
172    /// Literal value
173    Literal(Value),
174
175    /// Variable reference
176    Variable(String),
177
178    /// Indexed variable
179    IndexedVar { name: String, indices: Vec<Self> },
180
181    /// Binary operation
182    BinaryOp {
183        op: BinaryOperator,
184        left: Box<Self>,
185        right: Box<Self>,
186    },
187
188    /// Unary operation
189    UnaryOp {
190        op: UnaryOperator,
191        operand: Box<Self>,
192    },
193
194    /// Function call
195    FunctionCall { name: String, args: Vec<Self> },
196
197    /// Aggregation
198    Aggregation {
199        op: AggregationOp,
200        variables: Vec<(String, String)>, // (var, set)
201        expression: Box<Self>,
202    },
203
204    /// Conditional
205    Conditional {
206        condition: Box<ConstraintExpression>,
207        then_expr: Box<Self>,
208        else_expr: Box<Self>,
209    },
210}
211
212/// Binary operators
213#[derive(Debug, Clone)]
214pub enum BinaryOperator {
215    Add,
216    Subtract,
217    Multiply,
218    Divide,
219    Power,
220    Modulo,
221}
222
223/// Unary operators
224#[derive(Debug, Clone)]
225pub enum UnaryOperator {
226    Negate,
227    Abs,
228    Sqrt,
229    Exp,
230    Log,
231}
232
233/// Aggregation operators
234#[derive(Debug, Clone)]
235pub enum AggregationOp {
236    Sum,
237    Product,
238    Min,
239    Max,
240    Count,
241}
242
243/// Statement
244#[derive(Debug, Clone)]
245pub enum Statement {
246    /// Assignment
247    Assignment { target: String, value: Expression },
248
249    /// Conditional
250    If {
251        condition: ConstraintExpression,
252        then_branch: Vec<Self>,
253        else_branch: Option<Vec<Self>>,
254    },
255
256    /// Loop
257    For {
258        variable: String,
259        set: String,
260        body: Vec<Self>,
261    },
262}