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::Comm;
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
use crate::VarDecl;
50

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

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

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

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

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

            
84
684
        if !self.action_declarations.is_empty() {
85
628
            writeln!(f, "act")?;
86
7972
            for act_decl in &self.action_declarations {
87
7972
                writeln!(f, "   {act_decl};")?;
88
            }
89

            
90
628
            writeln!(f)?;
91
56
        }
92

            
93
684
        if !self.process_declarations.is_empty() {
94
628
            writeln!(f, "proc")?;
95
3624
            for proc_decl in &self.process_declarations {
96
3624
                writeln!(f, "   {proc_decl};")?;
97
            }
98

            
99
628
            writeln!(f)?;
100
56
        }
101

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

            
108
4
            writeln!(f)?;
109
680
        }
110

            
111
684
        if let Some(init) = &self.init {
112
676
            writeln!(f, "init {init};")?;
113
8
        }
114
684
        Ok(())
115
684
    }
116
}
117

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

            
126
768
            writeln!(f)?;
127
848
        }
128

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

            
135
            writeln!(f)?;
136
1616
        }
137

            
138
1616
        if !self.map_declarations.is_empty() {
139
852
            writeln!(f, "map")?;
140
27512
            for decl in &self.map_declarations {
141
27512
                writeln!(f, "   {decl};")?;
142
            }
143

            
144
852
            writeln!(f)?;
145
764
        }
146

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

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

            
164
            writeln!(f)?;
165
12
        }
166
12
        writeln!(f)?;
167

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

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

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

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

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

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

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

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

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

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

            
252
3412
        if let Some(expr) = &self.expr {
253
3400
            write!(f, " = {expr}")?;
254
12
        }
255

            
256
3412
        Ok(())
257
3412
    }
258
}
259

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

            
270
impl fmt::Display for VarDecl {
271
26268
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
272
26268
        write!(f, "{} : {}", self.identifier, self.sort)
273
26268
    }
274
}
275

            
276
impl fmt::Display for EqnDecl {
277
43888
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
278
43888
        match &self.condition {
279
2948
            Some(condition) => write!(f, "{} -> {} = {}", condition, self.lhs, self.rhs),
280
40940
            None => write!(f, "{} = {}", self.lhs, self.rhs),
281
        }
282
43888
    }
283
}
284

            
285
impl fmt::Display for DataExprUnaryOp {
286
2088
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
287
2088
        match self {
288
1692
            DataExprUnaryOp::Negation => write!(f, "!"),
289
164
            DataExprUnaryOp::Minus => write!(f, "-"),
290
232
            DataExprUnaryOp::Size => write!(f, "#"),
291
        }
292
2088
    }
293
}
294

            
295
impl fmt::Display for DataExpr {
296
770292
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
297
770292
        match self {
298
2216
            DataExpr::EmptyList => write!(f, "[]"),
299
140
            DataExpr::EmptyBag => write!(f, "{{:}}"),
300
680
            DataExpr::EmptySet => write!(f, "{{}}"),
301
1900
            DataExpr::List(expressions) => write!(f, "[{}]", expressions.iter().format(", ")),
302
184
            DataExpr::Bag(expressions) => write!(
303
184
                f,
304
                "{{ {} }}",
305
184
                expressions
306
184
                    .iter()
307
200
                    .format_with(", ", |e, f| f(&format_args!("{}: {}", e.expr, e.multiplicity)))
308
            ),
309
1392
            DataExpr::Set(expressions) => write!(f, "{{ {} }}", expressions.iter().format(", ")),
310
533308
            DataExpr::Id(identifier) => write!(f, "{identifier}"),
311
42352
            DataExpr::Binary { op, lhs, rhs } => write!(f, "({lhs} {op} {rhs})"),
312
2088
            DataExpr::Unary { op, expr } => write!(f, "({op} {expr})"),
313
4580
            DataExpr::Bool(value) => write!(f, "{value}"),
314
252
            DataExpr::Quantifier { op, variables, body } => {
315
252
                write!(f, "({} {} . {})", op, variables.iter().format(", "), body)
316
            }
317
116
            DataExpr::Lambda { variables, body } => write!(f, "(lambda {} . {})", variables.iter().format(", "), body),
318
155944
            DataExpr::Application { function, arguments } => {
319
155944
                if arguments.is_empty() {
320
                    write!(f, "{function}")
321
                } else {
322
155944
                    write!(f, "{}({})", function, arguments.iter().format(", "))
323
                }
324
            }
325
23856
            DataExpr::Number(value) => write!(f, "{value}"),
326
1144
            DataExpr::FunctionUpdate { expr, update } => write!(f, "{expr}[{update}]"),
327
44
            DataExpr::SetBagComp { variable, predicate } => write!(f, "{{ {variable} | {predicate} }}"),
328
96
            DataExpr::Whr { expr, assignments } => write!(f, "{} whr {} end", expr, assignments.iter().format(", ")),
329
        }
330
770292
    }
331
}
332

            
333
impl fmt::Display for IdDecl {
334
27512
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
335
27512
        write!(f, "{} : {}", self.identifier, self.sort)
336
27512
    }
337
}
338

            
339
impl fmt::Display for DataExprUpdate {
340
1144
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
341
1144
        write!(f, "{} -> {}", self.expr, self.update)
342
1144
    }
343
}
344

            
345
impl fmt::Display for SortExpression {
346
146092
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
347
146092
        match self {
348
19492
            SortExpression::Product { lhs, rhs } => write!(f, "({lhs} # {rhs})"),
349
13912
            SortExpression::Function { domain, range } => write!(f, "({domain} -> {range})"),
350
87336
            SortExpression::Reference(ident) => write!(f, "{ident}"),
351
18316
            SortExpression::Simple(sort) => write!(f, "{sort}"),
352
4284
            SortExpression::Complex(complex, inner) => write!(f, "{complex}({inner})"),
353
2752
            SortExpression::Struct { inner } => {
354
2752
                write!(f, "struct ")?;
355
2752
                write!(f, "{}", inner.iter().format(" | "))
356
            }
357
        }
358
146092
    }
359
}
360

            
361
impl fmt::Display for UntypedStateFrmSpec {
362
580
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
363
580
        writeln!(f, "{}", self.data_specification)?;
364

            
365
580
        writeln!(f, "{}", self.formula)
366
580
    }
367
}
368

            
369
impl fmt::Display for StateFrmUnaryOp {
370
60
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
371
60
        match self {
372
            StateFrmUnaryOp::Minus => write!(f, "-"),
373
60
            StateFrmUnaryOp::Negation => write!(f, "!"),
374
        }
375
60
    }
376
}
377

            
378
impl fmt::Display for FixedPointOperator {
379
808
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
380
808
        match self {
381
436
            FixedPointOperator::Greatest => write!(f, "nu"),
382
372
            FixedPointOperator::Least => write!(f, "mu"),
383
        }
384
808
    }
385
}
386

            
387
impl fmt::Display for StateFrm {
388
15304
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
389
15304
        match self {
390
1100
            StateFrm::True => write!(f, "true"),
391
144
            StateFrm::False => write!(f, "false"),
392
            StateFrm::DataValExpr(expr) => write!(f, "val({expr})"),
393
4232
            StateFrm::Id(identifier, args) => {
394
4232
                if args.is_empty() {
395
396
                    write!(f, "{identifier}")
396
                } else {
397
3836
                    write!(f, "{}({})", identifier, args.iter().format(", "))
398
                }
399
            }
400
60
            StateFrm::Unary { op, expr } => write!(f, "({op} {expr})"),
401
            StateFrm::Modality {
402
3756
                operator,
403
3756
                formula,
404
3756
                expr,
405
3756
            } => match operator {
406
1828
                ModalityOperator::Box => write!(f, "[{formula}]{expr}"),
407
1928
                ModalityOperator::Diamond => write!(f, "<{formula}>{expr}"),
408
            },
409
            StateFrm::Quantifier {
410
1060
                quantifier,
411
1060
                variables,
412
1060
                body,
413
            } => {
414
1060
                write!(f, "({} {} . {})", quantifier, variables.iter().format(", "), body)
415
            }
416
            StateFrm::Bound {
417
4
                bound: quantifier,
418
4
                variables,
419
4
                body,
420
            } => {
421
4
                write!(f, "({} {} . {})", quantifier, variables.iter().format(", "), body)
422
            }
423
4168
            StateFrm::Binary { op, lhs, rhs } => {
424
4168
                write!(f, "({lhs} {op} {rhs})")
425
            }
426
            StateFrm::FixedPoint {
427
772
                operator,
428
772
                variable,
429
772
                body,
430
            } => {
431
772
                write!(f, "({operator} {variable} . {body})")
432
            }
433
            StateFrm::Delay(expr) => write!(f, "delay@({expr})"),
434
            StateFrm::Yaled(expr) => write!(f, "yaled@({expr})"),
435
8
            StateFrm::DataValExprLeftMult(value, expr) => write!(f, "({value} * {expr})"),
436
            StateFrm::DataValExprRightMult(expr, value) => write!(f, "({expr} * {value})"),
437
        }
438
15304
    }
439
}
440

            
441
impl fmt::Display for StateVarDecl {
442
788
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
443
788
        if self.arguments.is_empty() {
444
396
            write!(f, "{}", self.identifier)
445
        } else {
446
392
            write!(f, "{}({})", self.identifier, self.arguments.iter().format(","))
447
        }
448
788
    }
449
}
450

            
451
impl fmt::Display for StateVarAssignment {
452
536
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
453
536
        write!(f, "{} : {} = {}", self.identifier, self.sort, self.expr)
454
536
    }
455
}
456

            
457
impl fmt::Display for StateFrmOp {
458
4168
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
459
4168
        match self {
460
1028
            StateFrmOp::Implies => write!(f, "=>"),
461
2356
            StateFrmOp::Conjunction => write!(f, "&&"),
462
780
            StateFrmOp::Disjunction => write!(f, "||"),
463
4
            StateFrmOp::Addition => write!(f, "+"),
464
        }
465
4168
    }
466
}
467

            
468
impl fmt::Display for RegFrm {
469
5208
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
470
5208
        match self {
471
4212
            RegFrm::Action(action) => write!(f, "{action}"),
472
536
            RegFrm::Iteration(body) => write!(f, "({body})*"),
473
4
            RegFrm::Plus(body) => write!(f, "({body})+"),
474
20
            RegFrm::Choice { lhs, rhs } => write!(f, "({lhs} + {rhs})"),
475
436
            RegFrm::Sequence { lhs, rhs } => write!(f, "({lhs} . {rhs})"),
476
        }
477
5208
    }
478
}
479

            
480
impl fmt::Display for DataExprBinaryOp {
481
42352
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
482
42352
        match self {
483
3776
            DataExprBinaryOp::At => write!(f, "."),
484
172
            DataExprBinaryOp::Concat => write!(f, "++"),
485
3124
            DataExprBinaryOp::Cons => write!(f, "|>"),
486
8108
            DataExprBinaryOp::Equal => write!(f, "=="),
487
2332
            DataExprBinaryOp::NotEqual => write!(f, "!="),
488
1424
            DataExprBinaryOp::LessThan => write!(f, "<"),
489
1608
            DataExprBinaryOp::LessEqual => write!(f, "<="),
490
960
            DataExprBinaryOp::GreaterThan => write!(f, ">"),
491
636
            DataExprBinaryOp::GreaterEqual => write!(f, ">="),
492
6104
            DataExprBinaryOp::Conj => write!(f, "&&"),
493
1888
            DataExprBinaryOp::Disj => write!(f, "||"),
494
5836
            DataExprBinaryOp::Add => write!(f, "+"),
495
2600
            DataExprBinaryOp::Subtract => write!(f, "-"),
496
320
            DataExprBinaryOp::Div => write!(f, "/"),
497
220
            DataExprBinaryOp::Implies => write!(f, "=>"),
498
1956
            DataExprBinaryOp::In => write!(f, "in"),
499
52
            DataExprBinaryOp::IntDiv => write!(f, "div"),
500
352
            DataExprBinaryOp::Mod => write!(f, "mod"),
501
840
            DataExprBinaryOp::Multiply => write!(f, "*"),
502
44
            DataExprBinaryOp::Snoc => write!(f, "<|"),
503
        }
504
42352
    }
505
}
506

            
507
impl fmt::Display for ActFrm {
508
6384
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
509
6384
        match self {
510
4
            ActFrm::False => write!(f, "false"),
511
492
            ActFrm::True => write!(f, "true"),
512
4140
            ActFrm::MultAct(action) => write!(f, "{action}"),
513
508
            ActFrm::Binary { op, lhs, rhs } => {
514
508
                write!(f, "({lhs}) {op} ({rhs})")
515
            }
516
84
            ActFrm::DataExprVal(expr) => write!(f, "val({expr})"),
517
            ActFrm::Quantifier {
518
536
                quantifier,
519
536
                variables,
520
536
                body,
521
536
            } => write!(f, "({} {} . {})", quantifier, variables.iter().format(", "), body),
522
620
            ActFrm::Negation(expr) => write!(f, "(!{expr})"),
523
        }
524
6384
    }
525
}
526

            
527
impl fmt::Display for ActFrmBinaryOp {
528
508
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
529
508
        match self {
530
8
            ActFrmBinaryOp::Implies => write!(f, "=>"),
531
200
            ActFrmBinaryOp::Intersect => write!(f, "&&"),
532
300
            ActFrmBinaryOp::Union => write!(f, "||"),
533
        }
534
508
    }
535
}
536

            
537
impl fmt::Display for Bound {
538
4
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
539
4
        match self {
540
            Bound::Inf => write!(f, "inf"),
541
            Bound::Sum => write!(f, "sum"),
542
4
            Bound::Sup => write!(f, "sup"),
543
        }
544
4
    }
545
}
546

            
547
impl fmt::Display for MultiAction {
548
4140
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
549
4140
        if self.actions.is_empty() {
550
4
            write!(f, "tau")
551
        } else {
552
4136
            write!(f, "{}", self.actions.iter().format("|"))
553
        }
554
4140
    }
555
}
556

            
557
impl fmt::Display for Action {
558
4240
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
559
4240
        if self.args.is_empty() {
560
2052
            write!(f, "{}", self.id)
561
        } else {
562
2188
            write!(f, "{}({})", self.id, self.args.iter().format(", "))
563
        }
564
4240
    }
565
}
566

            
567
impl fmt::Display for Quantifier {
568
1848
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
569
1848
        match self {
570
1036
            Quantifier::Exists => write!(f, "exists"),
571
812
            Quantifier::Forall => write!(f, "forall"),
572
        }
573
1848
    }
574
}
575

            
576
impl fmt::Display for ConstructorDecl {
577
7680
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
578
7680
        if self.args.is_empty() {
579
5464
            write!(f, "{}", self.name)
580
        } else {
581
2216
            write!(f, "{}(", self.name)?;
582
4764
            for (i, (name, sort)) in self.args.iter().enumerate() {
583
4764
                if i > 0 {
584
2548
                    write!(f, ", ")?;
585
2216
                }
586
4764
                match name {
587
1832
                    Some(name) => write!(f, "{name} : {sort}")?,
588
2932
                    None => write!(f, "{sort}")?,
589
                }
590
            }
591
2216
            write!(f, ")")?;
592

            
593
2216
            if let Some(projection) = &self.projection {
594
200
                write!(f, "?{projection}")?;
595
2016
            }
596

            
597
2216
            Ok(())
598
        }
599
7680
    }
600
}
601

            
602
impl fmt::Display for ProcDecl {
603
3624
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
604
3624
        if self.params.is_empty() {
605
552
            write!(f, "{} = {};", self.identifier, self.body)
606
        } else {
607
3072
            write!(
608
3072
                f,
609
                "{}({}) = {};",
610
                self.identifier,
611
3072
                self.params.iter().format(", "),
612
                self.body
613
            )
614
        }
615
3624
    }
616
}
617

            
618
impl fmt::Display for ProcExprBinaryOp {
619
18636
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
620
18636
        match self {
621
10692
            ProcExprBinaryOp::Sequence => write!(f, "."),
622
4168
            ProcExprBinaryOp::Choice => write!(f, "+"),
623
2148
            ProcExprBinaryOp::Parallel => write!(f, "||"),
624
            ProcExprBinaryOp::LeftMerge => write!(f, "_||"),
625
1628
            ProcExprBinaryOp::CommMerge => write!(f, "|"),
626
        }
627
18636
    }
628
}
629

            
630
impl fmt::Display for ProcessExpr {
631
54872
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
632
54872
        match self {
633
2340
            ProcessExpr::Id(identifier, assignments) => {
634
2340
                if assignments.is_empty() {
635
740
                    write!(f, "{identifier}")
636
                } else {
637
1600
                    write!(f, "{}({})", identifier, assignments.iter().format(", "))
638
                }
639
            }
640
22472
            ProcessExpr::Action(identifier, data_exprs) => {
641
22472
                if data_exprs.is_empty() {
642
2464
                    write!(f, "{identifier}")
643
                } else {
644
20008
                    write!(f, "{}({})", identifier, data_exprs.iter().format(", "))
645
                }
646
            }
647
368
            ProcessExpr::Delta => write!(f, "delta"),
648
24
            ProcessExpr::Tau => write!(f, "tau"),
649
3792
            ProcessExpr::Sum { variables, operand } => {
650
3792
                write!(f, "(sum {} . {})", variables.iter().format(", "), operand)
651
            }
652
            ProcessExpr::Dist {
653
104
                variables,
654
104
                expr,
655
104
                operand,
656
104
            } => write!(f, "(dist {} [{}] . {})", variables.iter().format(", "), expr, operand),
657
18636
            ProcessExpr::Binary { op, lhs, rhs } => write!(f, "({lhs} {op} {rhs})"),
658
184
            ProcessExpr::Hide { actions, operand } => {
659
184
                if !actions.is_empty() {
660
184
                    write!(f, "hide({{{}}}, {})", actions.iter().format(", "), operand)
661
                } else {
662
                    Ok(())
663
                }
664
            }
665
36
            ProcessExpr::Rename { renames, operand } => {
666
36
                if !renames.is_empty() {
667
36
                    write!(f, "rename({{{}}}, {})", renames.iter().format(", "), operand)
668
                } else {
669
                    Ok(())
670
                }
671
            }
672
396
            ProcessExpr::Allow { actions, operand } => {
673
396
                if !actions.is_empty() {
674
396
                    write!(f, "allow({{{}}}, {})", actions.iter().format(", "), operand)
675
                } else {
676
                    Ok(())
677
                }
678
            }
679
28
            ProcessExpr::Block { actions, operand } => {
680
28
                if !actions.is_empty() {
681
28
                    write!(f, "block({{{}}}, {})", actions.iter().format(", "), operand)
682
                } else {
683
                    Ok(())
684
                }
685
            }
686
404
            ProcessExpr::Comm { comm, operand } => {
687
404
                if !comm.is_empty() {
688
404
                    write!(f, "comm({{{}}}, {})", comm.iter().format(", "), operand)
689
                } else {
690
                    Ok(())
691
                }
692
            }
693
6032
            ProcessExpr::Condition { condition, then, else_ } => {
694
6032
                if let Some(else_) = else_ {
695
2268
                    write!(f, "({condition}) -> ({then}) <> ({else_})")
696
                } else {
697
3764
                    write!(f, "({condition}) -> ({then})")
698
                }
699
            }
700
56
            ProcessExpr::At { expr, operand } => write!(f, "({expr})@({operand})"),
701
        }
702
54872
    }
703
}
704

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

            
711
impl fmt::Display for Rename {
712
112
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
713
112
        write!(f, "{} -> {}", self.from, self.to)
714
112
    }
715
}
716

            
717
impl fmt::Display for MultiActionLabel {
718
5592
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
719
5592
        if self.actions.is_empty() {
720
            write!(f, "tau")
721
        } else {
722
5592
            write!(f, "{}", self.actions.iter().format("|"))
723
        }
724
5592
    }
725
}