1
use std::fmt;
2

            
3
use itertools::Itertools;
4

            
5
use crate::ActDecl;
6
use crate::ActFrm;
7
use crate::ActFrmBinaryOp;
8
use crate::Action;
9
use crate::Assignment;
10
use crate::Bound;
11
use crate::CommExpr;
12
use crate::ComplexSort;
13
use crate::ConstructorDecl;
14
use crate::DataExpr;
15
use crate::DataExprBinaryOp;
16
use crate::DataExprUnaryOp;
17
use crate::DataExprUpdate;
18
use crate::EqnDecl;
19
use crate::EqnSpec;
20
use crate::FixedPointOperator;
21
use crate::IdDecl;
22
use crate::ModalityOperator;
23
use crate::MultiAction;
24
use crate::MultiActionLabel;
25
use crate::PbesEquation;
26
use crate::PbesExpr;
27
use crate::PbesExprBinaryOp;
28
use crate::ProcDecl;
29
use crate::ProcExprBinaryOp;
30
use crate::ProcessExpr;
31
use crate::PropVarDecl;
32
use crate::PropVarInst;
33
use crate::Quantifier;
34
use crate::RegFrm;
35
use crate::Rename;
36
use crate::Sort;
37
use crate::SortDecl;
38
use crate::SortExpression;
39
use crate::Span;
40
use crate::StateFrm;
41
use crate::StateFrmOp;
42
use crate::StateFrmUnaryOp;
43
use crate::StateVarAssignment;
44
use crate::StateVarDecl;
45
use crate::UntypedDataSpecification;
46
use crate::UntypedPbes;
47
use crate::UntypedProcessSpecification;
48
use crate::UntypedStateFrmSpec;
49

            
50
/// Prints location information for a span in the source.
51
pub fn print_location(input: &str, span: &Span) {
52
    input.lines().enumerate().fold(span.start, |current, (number, line)| {
53
        if current < line.len() {
54
            println!("ln {number}, col {}", span.start - current);
55
        }
56
        current - line.len()
57
    });
58
}
59

            
60
// Display implementations
61
impl fmt::Display for Sort {
62
34622
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
63
34622
        write!(f, "{self:?}")
64
34622
    }
65
}
66

            
67
impl fmt::Display for ComplexSort {
68
8533
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
69
8533
        write!(f, "{self:?}")
70
8533
    }
71
}
72

            
73
impl fmt::Display for Assignment {
74
4081
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
75
4081
        write!(f, "{} = {}", self.identifier, self.expr)
76
4081
    }
77
}
78

            
79
impl fmt::Display for UntypedProcessSpecification {
80
1197
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
81
1197
        writeln!(f, "{}", self.data_specification)?;
82

            
83
1197
        if !self.action_declarations.is_empty() {
84
1099
            writeln!(f, "act")?;
85
13951
            for act_decl in &self.action_declarations {
86
13951
                writeln!(f, "   {act_decl};")?;
87
            }
88

            
89
1099
            writeln!(f)?;
90
98
        }
91

            
92
1197
        if !self.process_declarations.is_empty() {
93
1099
            writeln!(f, "proc")?;
94
6342
            for proc_decl in &self.process_declarations {
95
6342
                writeln!(f, "   {proc_decl};")?;
96
            }
97

            
98
1099
            writeln!(f)?;
99
98
        }
100

            
101
1197
        if !self.global_variables.is_empty() {
102
7
            writeln!(f, "glob")?;
103
7
            for var_decl in &self.global_variables {
104
7
                writeln!(f, "   {var_decl};")?;
105
            }
106

            
107
7
            writeln!(f)?;
108
1190
        }
109

            
110
1197
        if let Some(init) = &self.init {
111
1183
            writeln!(f, "init {init};")?;
112
14
        }
113
1197
        Ok(())
114
1197
    }
115
}
116

            
117
impl fmt::Display for UntypedDataSpecification {
118
2898
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
119
2898
        if !self.sort_declarations.is_empty() {
120
1379
            writeln!(f, "sort")?;
121
6013
            for decl in &self.sort_declarations {
122
6013
                writeln!(f, "   {decl};")?;
123
            }
124

            
125
1379
            writeln!(f)?;
126
1519
        }
127

            
128
2898
        if !self.constructor_declarations.is_empty() {
129
63
            writeln!(f, "cons")?;
130
119
            for decl in &self.constructor_declarations {
131
119
                writeln!(f, "   {decl};")?;
132
            }
133

            
134
63
            writeln!(f)?;
135
2835
        }
136

            
137
2898
        if !self.map_declarations.is_empty() {
138
1561
            writeln!(f, "map")?;
139
49287
            for decl in &self.map_declarations {
140
49287
                writeln!(f, "   {decl};")?;
141
            }
142

            
143
1561
            writeln!(f)?;
144
1337
        }
145

            
146
4424
        for decl in &self.equation_declarations {
147
4424
            writeln!(f, "{decl}")?;
148
        }
149
2898
        Ok(())
150
2898
    }
151
}
152

            
153
impl fmt::Display for UntypedPbes {
154
21
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
155
21
        writeln!(f, "{}", self.data_specification)?;
156
21
        writeln!(f)?;
157
21
        if !self.global_variables.is_empty() {
158
            writeln!(f, "glob")?;
159
            for var_decl in &self.global_variables {
160
                writeln!(f, "   {var_decl};")?;
161
            }
162

            
163
            writeln!(f)?;
164
21
        }
165
21
        writeln!(f)?;
166

            
167
21
        if !self.equations.is_empty() {
168
21
            writeln!(f, "pbes")?;
169
35
            for equation in &self.equations {
170
35
                writeln!(f, "   {equation};")?;
171
            }
172
        }
173

            
174
21
        writeln!(f, "init {};", self.init)
175
21
    }
176
}
177

            
178
impl fmt::Display for PropVarInst {
179
49
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
180
49
        if self.arguments.is_empty() {
181
28
            write!(f, "{}", self.identifier)
182
        } else {
183
21
            write!(f, "{}({})", self.identifier, self.arguments.iter().format(", "))
184
        }
185
49
    }
186
}
187

            
188
impl fmt::Display for PbesEquation {
189
35
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
190
35
        write!(f, "{} {} = {}", self.operator, self.variable, self.formula)
191
35
    }
192
}
193

            
194
impl fmt::Display for PropVarDecl {
195
35
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
196
35
        if self.parameters.is_empty() {
197
21
            write!(f, "{}", self.identifier)
198
        } else {
199
14
            write!(f, "{}({})", self.identifier, self.parameters.iter().format(", "))
200
        }
201
35
    }
202
}
203

            
204
impl fmt::Display for PbesExpr {
205
70
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
206
70
        match self {
207
7
            PbesExpr::True => write!(f, "true"),
208
            PbesExpr::False => write!(f, "false"),
209
28
            PbesExpr::PropVarInst(instance) => write!(f, "{instance}"),
210
7
            PbesExpr::Negation(expr) => write!(f, "(! {expr})"),
211
14
            PbesExpr::Binary { op, lhs, rhs } => write!(f, "({lhs} {op} {rhs})"),
212
            PbesExpr::Quantifier {
213
                quantifier,
214
                variables,
215
                body,
216
            } => write!(f, "({} {} . {})", quantifier, variables.iter().format(", "), body),
217
14
            PbesExpr::DataValExpr(data_expr) => write!(f, "val({data_expr})"),
218
        }
219
70
    }
220
}
221

            
222
impl fmt::Display for PbesExprBinaryOp {
223
14
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
224
14
        match self {
225
14
            PbesExprBinaryOp::Conjunction => write!(f, "&&"),
226
            PbesExprBinaryOp::Disjunction => write!(f, "||"),
227
            PbesExprBinaryOp::Implies => write!(f, "=>"),
228
        }
229
14
    }
230
}
231

            
232
impl fmt::Display for EqnSpec {
233
4424
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
234
4424
        writeln!(f, "var")?;
235
21637
        for decl in &self.variables {
236
21637
            writeln!(f, "   {decl};")?;
237
        }
238

            
239
4424
        writeln!(f, "eqn")?;
240
80220
        for decl in &self.equations {
241
80220
            writeln!(f, "   {decl};")?;
242
        }
243
4424
        Ok(())
244
4424
    }
245
}
246

            
247
impl fmt::Display for SortDecl {
248
6013
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
249
6013
        write!(f, "{}", self.identifier)?;
250

            
251
6013
        if let Some(expr) = &self.expr {
252
5950
            write!(f, " = {expr}")?;
253
63
        }
254

            
255
6013
        Ok(())
256
6013
    }
257
}
258

            
259
impl fmt::Display for ActDecl {
260
13951
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
261
13951
        if self.args.is_empty() {
262
1771
            write!(f, "{}", self.identifier)
263
        } else {
264
12180
            write!(f, "{}({})", self.identifier, self.args.iter().format(", "))
265
        }
266
13951
    }
267
}
268

            
269
impl fmt::Display for EqnDecl {
270
80220
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
271
80220
        match &self.condition {
272
5460
            Some(condition) => write!(f, "{} -> {} = {}", condition, self.lhs, self.rhs),
273
74760
            None => write!(f, "{} = {}", self.lhs, self.rhs),
274
        }
275
80220
    }
276
}
277

            
278
impl fmt::Display for DataExprUnaryOp {
279
4018
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
280
4018
        match self {
281
3129
            DataExprUnaryOp::Negation => write!(f, "!"),
282
413
            DataExprUnaryOp::Minus => write!(f, "-"),
283
476
            DataExprUnaryOp::Size => write!(f, "#"),
284
        }
285
4018
    }
286
}
287

            
288
impl fmt::Display for DataExpr {
289
1388982
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
290
1388982
        match self {
291
3983
            DataExpr::EmptyList => write!(f, "[]"),
292
581
            DataExpr::EmptyBag => write!(f, "{{:}}"),
293
1456
            DataExpr::EmptySet => write!(f, "{{}}"),
294
3325
            DataExpr::List(expressions) => write!(f, "[{}]", expressions.iter().format(", ")),
295
322
            DataExpr::Bag(expressions) => write!(
296
322
                f,
297
                "{{ {} }}",
298
322
                expressions
299
322
                    .iter()
300
350
                    .format_with(", ", |e, f| f(&format_args!("{}: {}", e.expr, e.multiplicity)))
301
            ),
302
2436
            DataExpr::Set(expressions) => write!(f, "{{ {} }}", expressions.iter().format(", ")),
303
959329
            DataExpr::Id(identifier) => write!(f, "{identifier}"),
304
77952
            DataExpr::Binary { op, lhs, rhs } => write!(f, "({lhs} {op} {rhs})"),
305
4018
            DataExpr::Unary { op, expr } => write!(f, "({op} {expr})"),
306
9142
            DataExpr::Bool(value) => write!(f, "{value}"),
307
455
            DataExpr::Quantifier { op, variables, body } => {
308
455
                write!(f, "({} {} . {})", op, variables.iter().format(", "), body)
309
            }
310
203
            DataExpr::Lambda { variables, body } => write!(f, "(lambda {} . {})", variables.iter().format(", "), body),
311
281785
            DataExpr::Application { function, arguments } => {
312
281785
                if arguments.is_empty() {
313
                    write!(f, "{function}")
314
                } else {
315
281785
                    write!(f, "{}({})", function, arguments.iter().format(", "))
316
                }
317
            }
318
41748
            DataExpr::Number(value) => write!(f, "{value}"),
319
2002
            DataExpr::FunctionUpdate { expr, update } => write!(f, "{expr}[{update}]"),
320
77
            DataExpr::SetBagComp { variable, predicate } => write!(f, "{{ {variable} | {predicate} }}"),
321
168
            DataExpr::Whr { expr, assignments } => write!(f, "{} whr {} end", expr, assignments.iter().format(", ")),
322
        }
323
1388982
    }
324
}
325

            
326
impl fmt::Display for IdDecl {
327
95865
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
328
95865
        write!(f, "{}: {}", self.identifier, self.sort)
329
95865
    }
330
}
331

            
332
impl fmt::Display for DataExprUpdate {
333
2002
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
334
2002
        write!(f, "{} -> {}", self.expr, self.update)
335
2002
    }
336
}
337

            
338
impl fmt::Display for SortExpression {
339
263333
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
340
263333
        match self {
341
35056
            SortExpression::Product { lhs, rhs } => write!(f, "({lhs} # {rhs})"),
342
25844
            SortExpression::Function { domain, range } => write!(f, "({domain} -> {range})"),
343
154462
            SortExpression::Reference(ident) => write!(f, "{ident}"),
344
34622
            SortExpression::Simple(sort) => write!(f, "{sort}"),
345
8533
            SortExpression::Complex(complex, inner) => write!(f, "{complex}({inner})"),
346
4816
            SortExpression::Struct { inner } => {
347
4816
                write!(f, "struct ")?;
348
4816
                write!(f, "{}", inner.iter().format(" | "))
349
            }
350
        }
351
263333
    }
352
}
353

            
354
impl fmt::Display for UntypedStateFrmSpec {
355
1015
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
356
1015
        writeln!(f, "{}", self.data_specification)?;
357

            
358
1015
        writeln!(f, "{}", self.formula)
359
1015
    }
360
}
361

            
362
impl fmt::Display for StateFrmUnaryOp {
363
105
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
364
105
        match self {
365
            StateFrmUnaryOp::Minus => write!(f, "-"),
366
105
            StateFrmUnaryOp::Negation => write!(f, "!"),
367
        }
368
105
    }
369
}
370

            
371
impl fmt::Display for FixedPointOperator {
372
1428
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
373
1428
        match self {
374
777
            FixedPointOperator::Greatest => write!(f, "nu"),
375
651
            FixedPointOperator::Least => write!(f, "mu"),
376
        }
377
1428
    }
378
}
379

            
380
impl fmt::Display for StateFrm {
381
51464
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
382
51464
        match self {
383
3941
            StateFrm::True => write!(f, "true"),
384
3647
            StateFrm::False => write!(f, "false"),
385
            StateFrm::DataValExpr(expr) => write!(f, "val({expr})"),
386
7420
            StateFrm::Id(identifier, args) => {
387
7420
                if args.is_empty() {
388
707
                    write!(f, "{identifier}")
389
                } else {
390
6713
                    write!(f, "{}({})", identifier, args.iter().format(", "))
391
                }
392
            }
393
105
            StateFrm::Unary { op, expr } => write!(f, "({op} {expr})"),
394
            StateFrm::Modality {
395
23401
                operator,
396
23401
                formula,
397
23401
                expr,
398
23401
            } => match operator {
399
9310
                ModalityOperator::Box => write!(f, "[{formula}]{expr}"),
400
14091
                ModalityOperator::Diamond => write!(f, "<{formula}>{expr}"),
401
            },
402
            StateFrm::Quantifier {
403
1855
                quantifier,
404
1855
                variables,
405
1855
                body,
406
            } => {
407
1855
                write!(f, "({} {} . {})", quantifier, variables.iter().format(", "), body)
408
            }
409
            StateFrm::Bound {
410
7
                bound: quantifier,
411
7
                variables,
412
7
                body,
413
            } => {
414
7
                write!(f, "({} {} . {})", quantifier, variables.iter().format(", "), body)
415
            }
416
9709
            StateFrm::Binary { op, lhs, rhs } => {
417
9709
                write!(f, "({lhs} {op} {rhs})")
418
            }
419
            StateFrm::FixedPoint {
420
1365
                operator,
421
1365
                variable,
422
1365
                body,
423
            } => {
424
1365
                write!(f, "({operator} {variable} . {body})")
425
            }
426
            StateFrm::Delay(expr) => write!(f, "delay@({expr})"),
427
            StateFrm::Yaled(expr) => write!(f, "yaled@({expr})"),
428
14
            StateFrm::DataValExprLeftMult(value, expr) => write!(f, "({value} * {expr})"),
429
            StateFrm::DataValExprRightMult(expr, value) => write!(f, "({expr} * {value})"),
430
        }
431
51464
    }
432
}
433

            
434
impl fmt::Display for StateVarDecl {
435
1393
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
436
1393
        if self.arguments.is_empty() {
437
707
            write!(f, "{}", self.identifier)
438
        } else {
439
686
            write!(f, "{}({})", self.identifier, self.arguments.iter().format(","))
440
        }
441
1393
    }
442
}
443

            
444
impl fmt::Display for StateVarAssignment {
445
938
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
446
938
        write!(f, "{} : {} = {}", self.identifier, self.sort, self.expr)
447
938
    }
448
}
449

            
450
impl fmt::Display for StateFrmOp {
451
9709
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
452
9709
        match self {
453
1799
            StateFrmOp::Implies => write!(f, "=>"),
454
6538
            StateFrmOp::Conjunction => write!(f, "&&"),
455
1365
            StateFrmOp::Disjunction => write!(f, "||"),
456
7
            StateFrmOp::Addition => write!(f, "+"),
457
        }
458
9709
    }
459
}
460

            
461
impl fmt::Display for RegFrm {
462
35308
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
463
35308
        match self {
464
24199
            RegFrm::Action(action) => write!(f, "{action}"),
465
10304
            RegFrm::Iteration(body) => write!(f, "({body})*"),
466
7
            RegFrm::Plus(body) => write!(f, "({body})+"),
467
35
            RegFrm::Choice { lhs, rhs } => write!(f, "({lhs} + {rhs})"),
468
763
            RegFrm::Sequence { lhs, rhs } => write!(f, "({lhs} . {rhs})"),
469
        }
470
35308
    }
471
}
472

            
473
impl fmt::Display for DataExprBinaryOp {
474
77952
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
475
77952
        match self {
476
6629
            DataExprBinaryOp::At => write!(f, "."),
477
329
            DataExprBinaryOp::Concat => write!(f, "++"),
478
5691
            DataExprBinaryOp::Cons => write!(f, "|>"),
479
14735
            DataExprBinaryOp::Equal => write!(f, "=="),
480
4144
            DataExprBinaryOp::NotEqual => write!(f, "!="),
481
3101
            DataExprBinaryOp::LessThan => write!(f, "<"),
482
3262
            DataExprBinaryOp::LessEqual => write!(f, "<="),
483
1722
            DataExprBinaryOp::GreaterThan => write!(f, ">"),
484
1113
            DataExprBinaryOp::GreaterEqual => write!(f, ">="),
485
10815
            DataExprBinaryOp::Conj => write!(f, "&&"),
486
3395
            DataExprBinaryOp::Disj => write!(f, "||"),
487
10647
            DataExprBinaryOp::Add => write!(f, "+"),
488
4816
            DataExprBinaryOp::Subtract => write!(f, "-"),
489
588
            DataExprBinaryOp::Div => write!(f, "/"),
490
427
            DataExprBinaryOp::Implies => write!(f, "=>"),
491
3423
            DataExprBinaryOp::In => write!(f, "in"),
492
147
            DataExprBinaryOp::IntDiv => write!(f, "div"),
493
665
            DataExprBinaryOp::Mod => write!(f, "mod"),
494
2205
            DataExprBinaryOp::Multiply => write!(f, "*"),
495
98
            DataExprBinaryOp::Snoc => write!(f, "<|"),
496
        }
497
77952
    }
498
}
499

            
500
impl fmt::Display for ActFrm {
501
28000
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
502
28000
        match self {
503
7
            ActFrm::False => write!(f, "false"),
504
861
            ActFrm::True => write!(f, "true"),
505
24073
            ActFrm::MultAct(action) => write!(f, "{action}"),
506
889
            ActFrm::Binary { op, lhs, rhs } => {
507
889
                write!(f, "({lhs}) {op} ({rhs})")
508
            }
509
147
            ActFrm::DataExprVal(expr) => write!(f, "val({expr})"),
510
            ActFrm::Quantifier {
511
938
                quantifier,
512
938
                variables,
513
938
                body,
514
938
            } => write!(f, "({} {} . {})", quantifier, variables.iter().format(", "), body),
515
1085
            ActFrm::Negation(expr) => write!(f, "(!{expr})"),
516
        }
517
28000
    }
518
}
519

            
520
impl fmt::Display for ActFrmBinaryOp {
521
889
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
522
889
        match self {
523
14
            ActFrmBinaryOp::Implies => write!(f, "=>"),
524
350
            ActFrmBinaryOp::Intersect => write!(f, "&&"),
525
525
            ActFrmBinaryOp::Union => write!(f, "||"),
526
        }
527
889
    }
528
}
529

            
530
impl fmt::Display for Bound {
531
7
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
532
7
        match self {
533
            Bound::Inf => write!(f, "inf"),
534
            Bound::Sum => write!(f, "sum"),
535
7
            Bound::Sup => write!(f, "sup"),
536
        }
537
7
    }
538
}
539

            
540
impl fmt::Display for MultiAction {
541
24073
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
542
24073
        if self.actions.is_empty() {
543
10794
            write!(f, "tau")
544
        } else {
545
13279
            write!(f, "{}", self.actions.iter().format("|"))
546
        }
547
24073
    }
548
}
549

            
550
impl fmt::Display for Action {
551
13461
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
552
13461
        if self.args.is_empty() {
553
9632
            write!(f, "{}", self.id)
554
        } else {
555
3829
            write!(f, "{}({})", self.id, self.args.iter().format(", "))
556
        }
557
13461
    }
558
}
559

            
560
impl fmt::Display for Quantifier {
561
3248
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
562
3248
        match self {
563
1813
            Quantifier::Exists => write!(f, "exists"),
564
1435
            Quantifier::Forall => write!(f, "forall"),
565
        }
566
3248
    }
567
}
568

            
569
impl fmt::Display for ConstructorDecl {
570
13440
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
571
13440
        if self.args.is_empty() {
572
9562
            write!(f, "{}", self.name)
573
        } else {
574
3878
            write!(f, "{}(", self.name)?;
575
8337
            for (i, (name, sort)) in self.args.iter().enumerate() {
576
8337
                if i > 0 {
577
4459
                    write!(f, ", ")?;
578
3878
                }
579
8337
                match name {
580
3206
                    Some(name) => write!(f, "{name} : {sort}")?,
581
5131
                    None => write!(f, "{sort}")?,
582
                }
583
            }
584
3878
            write!(f, ")")?;
585

            
586
3878
            if let Some(projection) = &self.projection {
587
350
                write!(f, "?{projection}")?;
588
3528
            }
589

            
590
3878
            Ok(())
591
        }
592
13440
    }
593
}
594

            
595
impl fmt::Display for ProcDecl {
596
6342
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
597
6342
        if self.params.is_empty() {
598
966
            write!(f, "{} = {};", self.identifier, self.body)
599
        } else {
600
5376
            write!(
601
5376
                f,
602
                "{}({}) = {};",
603
                self.identifier,
604
5376
                self.params.iter().format(", "),
605
                self.body
606
            )
607
        }
608
6342
    }
609
}
610

            
611
impl fmt::Display for ProcExprBinaryOp {
612
32613
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
613
32613
        match self {
614
18711
            ProcExprBinaryOp::Sequence => write!(f, "."),
615
7294
            ProcExprBinaryOp::Choice => write!(f, "+"),
616
3759
            ProcExprBinaryOp::Parallel => write!(f, "||"),
617
            ProcExprBinaryOp::LeftMerge => write!(f, "_||"),
618
2849
            ProcExprBinaryOp::CommMerge => write!(f, "|"),
619
        }
620
32613
    }
621
}
622

            
623
impl fmt::Display for ProcessExpr {
624
96026
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
625
96026
        match self {
626
4095
            ProcessExpr::Id(identifier, assignments) => {
627
4095
                if assignments.is_empty() {
628
1295
                    write!(f, "{identifier}")
629
                } else {
630
2800
                    write!(f, "{}({})", identifier, assignments.iter().format(", "))
631
                }
632
            }
633
39326
            ProcessExpr::Action(identifier, data_exprs) => {
634
39326
                if data_exprs.is_empty() {
635
4312
                    write!(f, "{identifier}")
636
                } else {
637
35014
                    write!(f, "{}({})", identifier, data_exprs.iter().format(", "))
638
                }
639
            }
640
644
            ProcessExpr::Delta => write!(f, "delta"),
641
42
            ProcessExpr::Tau => write!(f, "tau"),
642
6636
            ProcessExpr::Sum { variables, operand } => {
643
6636
                write!(f, "(sum {} . {})", variables.iter().format(", "), operand)
644
            }
645
            ProcessExpr::Dist {
646
182
                variables,
647
182
                expr,
648
182
                operand,
649
182
            } => write!(f, "(dist {} [{}] . {})", variables.iter().format(", "), expr, operand),
650
32613
            ProcessExpr::Binary { op, lhs, rhs } => write!(f, "({lhs} {op} {rhs})"),
651
322
            ProcessExpr::Hide { actions, operand } => {
652
322
                if !actions.is_empty() {
653
322
                    write!(f, "hide({{{}}}, {})", actions.iter().format(", "), operand)
654
                } else {
655
                    Ok(())
656
                }
657
            }
658
63
            ProcessExpr::Rename { renames, operand } => {
659
63
                if !renames.is_empty() {
660
63
                    write!(f, "rename({{{}}}, {})", renames.iter().format(", "), operand)
661
                } else {
662
                    Ok(())
663
                }
664
            }
665
693
            ProcessExpr::Allow { actions, operand } => {
666
693
                if !actions.is_empty() {
667
693
                    write!(f, "allow({{{}}}, {})", actions.iter().format(", "), operand)
668
                } else {
669
                    Ok(())
670
                }
671
            }
672
49
            ProcessExpr::Block { actions, operand } => {
673
49
                if !actions.is_empty() {
674
49
                    write!(f, "block({{{}}}, {})", actions.iter().format(", "), operand)
675
                } else {
676
                    Ok(())
677
                }
678
            }
679
707
            ProcessExpr::Comm { comm, operand } => {
680
707
                if !comm.is_empty() {
681
707
                    write!(f, "comm({{{}}}, {})", comm.iter().format(", "), operand)
682
                } else {
683
                    Ok(())
684
                }
685
            }
686
10556
            ProcessExpr::Condition { condition, then, else_ } => {
687
10556
                if let Some(else_) = else_ {
688
3969
                    write!(f, "({condition}) -> ({then}) <> ({else_})")
689
                } else {
690
6587
                    write!(f, "({condition}) -> ({then})")
691
                }
692
            }
693
98
            ProcessExpr::At { expr, operand } => write!(f, "({expr})@({operand})"),
694
        }
695
96026
    }
696
}
697

            
698
impl fmt::Display for CommExpr {
699
3227
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
700
3227
        write!(f, "{} -> {}", self.from, self.to)
701
3227
    }
702
}
703

            
704
impl fmt::Display for Rename {
705
196
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
706
196
        write!(f, "{} -> {}", self.from, self.to)
707
196
    }
708
}
709

            
710
impl fmt::Display for MultiActionLabel {
711
9786
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
712
9786
        if self.actions.is_empty() {
713
            write!(f, "tau")
714
        } else {
715
9786
            write!(f, "{}", self.actions.iter().format("|"))
716
        }
717
9786
    }
718
}