quantrs2_tytan/problem_dsl/
ast.rs1use super::types::VarType;
4use std::collections::HashMap;
5
6#[derive(Debug, Clone)]
8pub enum AST {
9 Program {
11 declarations: Vec<Declaration>,
12 objective: Objective,
13 constraints: Vec<Constraint>,
14 },
15
16 VarDecl {
18 name: String,
19 var_type: VarType,
20 domain: Option<Domain>,
21 attributes: HashMap<String, Value>,
22 },
23
24 Expr(Expression),
26
27 Stmt(Statement),
29}
30
31#[derive(Debug, Clone)]
33pub enum Declaration {
34 Variable {
36 name: String,
37 var_type: VarType,
38 domain: Option<Domain>,
39 attributes: HashMap<String, Value>,
40 },
41
42 Parameter {
44 name: String,
45 value: Value,
46 description: Option<String>,
47 },
48
49 Set { name: String, elements: Vec<Value> },
51
52 Function {
54 name: String,
55 params: Vec<String>,
56 body: Box<Expression>,
57 },
58}
59
60#[derive(Debug, Clone)]
62pub enum Domain {
63 Range { min: f64, max: f64 },
65 Set { values: Vec<Value> },
67 IndexSet { set_name: String },
69}
70
71#[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#[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#[derive(Debug, Clone)]
99pub struct Constraint {
100 pub name: Option<String>,
101 pub expression: ConstraintExpression,
102 pub tags: Vec<String>,
103}
104
105#[derive(Debug, Clone)]
107pub enum ConstraintExpression {
108 Comparison {
110 left: Expression,
111 op: ComparisonOp,
112 right: Expression,
113 },
114
115 Logical { op: LogicalOp, operands: Vec<Self> },
117
118 Quantified {
120 quantifier: Quantifier,
121 variables: Vec<(String, String)>, constraint: Box<Self>,
123 },
124
125 Implication {
127 condition: Box<Self>,
128 consequence: Box<Self>,
129 },
130
131 Counting {
133 variables: Vec<String>,
134 op: ComparisonOp,
135 count: Expression,
136 },
137}
138
139#[derive(Debug, Clone, PartialEq, Eq)]
141pub enum ComparisonOp {
142 Equal,
143 NotEqual,
144 Less,
145 Greater,
146 LessEqual,
147 GreaterEqual,
148}
149
150#[derive(Debug, Clone)]
152pub enum LogicalOp {
153 And,
154 Or,
155 Not,
156 Xor,
157}
158
159#[derive(Debug, Clone)]
161pub enum Quantifier {
162 ForAll,
163 Exists,
164 ExactlyOne,
165 AtMostOne,
166 AtLeastOne,
167}
168
169#[derive(Debug, Clone)]
171pub enum Expression {
172 Literal(Value),
174
175 Variable(String),
177
178 IndexedVar { name: String, indices: Vec<Self> },
180
181 BinaryOp {
183 op: BinaryOperator,
184 left: Box<Self>,
185 right: Box<Self>,
186 },
187
188 UnaryOp {
190 op: UnaryOperator,
191 operand: Box<Self>,
192 },
193
194 FunctionCall { name: String, args: Vec<Self> },
196
197 Aggregation {
199 op: AggregationOp,
200 variables: Vec<(String, String)>, expression: Box<Self>,
202 },
203
204 Conditional {
206 condition: Box<ConstraintExpression>,
207 then_expr: Box<Self>,
208 else_expr: Box<Self>,
209 },
210}
211
212#[derive(Debug, Clone)]
214pub enum BinaryOperator {
215 Add,
216 Subtract,
217 Multiply,
218 Divide,
219 Power,
220 Modulo,
221}
222
223#[derive(Debug, Clone)]
225pub enum UnaryOperator {
226 Negate,
227 Abs,
228 Sqrt,
229 Exp,
230 Log,
231}
232
233#[derive(Debug, Clone)]
235pub enum AggregationOp {
236 Sum,
237 Product,
238 Min,
239 Max,
240 Count,
241}
242
243#[derive(Debug, Clone)]
245pub enum Statement {
246 Assignment { target: String, value: Expression },
248
249 If {
251 condition: ConstraintExpression,
252 then_branch: Vec<Self>,
253 else_branch: Option<Vec<Self>>,
254 },
255
256 For {
258 variable: String,
259 set: String,
260 body: Vec<Self>,
261 },
262}