pddl-core 0.5.0

The types and the parser behind the pddl crate; depend on pddl instead
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
//! Loads a [`Problem`] for planning code that `pddl-codegen` generated from a
//! domain.
//!
//! Generated code describes its domain as a [`Signature`]: the types in
//! depth-first order, the predicates and functions with their argument types,
//! and the constants. [`load`] checks a problem against it, numbers the objects
//! and grounds the initial state and the goal. The generated task then copies
//! the facts into its typed state.
//!
//! The generated code calls this module; it is public for that, not as an API
//! of its own, and can change with `pddl-codegen`.
//!
//! ## Numbering
//! Objects are numbered type by type in the order of [`Signature::types`],
//! which lists every type before its subtypes and keeps each subtree together.
//! So the objects of a type and all its subtypes are one range,
//! `starts[t]..ends[t]`, and a type check is a range check.

use std::collections::HashMap;

use pddl_runtime::{Comparison, Expr, Formula};

use crate::{
    AtomicFormula, BinaryComparison, BinaryOp, FluentExpression, FunctionHead, GoalDescription,
    InitElement, Literal, MetricFluentExpression, MultiOp, Name, Optimization,
    PreconditionGoalDescription, PreconditionGoalDescriptions, PreferenceGoalDescription, Problem,
    Term, Type, TypedVariables,
};

/// A domain as generated code sees it. All names are lowercase.
#[derive(Debug)]
pub struct Signature {
    /// The domain's name.
    pub domain: &'static str,
    /// The types, each before its subtypes, with each subtree contiguous.
    /// The first is `object`.
    pub types: &'static [TypeSignature],
    /// The predicates, including derived ones.
    pub predicates: &'static [Symbol],
    /// The numeric functions, without `total-cost`.
    pub functions: &'static [Symbol],
    /// The constants, with the index of their type.
    pub constants: &'static [(&'static str, usize)],
}

/// A type and its place in the hierarchy.
#[derive(Debug)]
pub struct TypeSignature {
    /// The name.
    pub name: &'static str,
    /// The index of the supertype; `None` for `object`.
    pub parent: Option<usize>,
}

/// A predicate or function with the type index of each argument.
#[derive(Debug)]
pub struct Symbol {
    /// The name.
    pub name: &'static str,
    /// The type of each argument; `either` types are `object` (index 0).
    pub args: &'static [usize],
}

/// A problem, checked against a [`Signature`] and grounded.
#[derive(Debug, Clone)]
pub struct Loaded {
    /// The name of each object, by its number.
    pub names: Vec<String>,
    /// For each type, the number of its first object.
    pub starts: Vec<u32>,
    /// For each type, one past the number of its last object, subtypes included.
    pub ends: Vec<u32>,
    /// The number of each constant, in the order of [`Signature::constants`].
    pub constants: Vec<u32>,
    /// The facts of the initial state: a predicate index and its arguments.
    pub facts: Vec<(usize, Vec<u32>)>,
    /// The initial values of numeric fluents: a function index, its arguments
    /// and the value.
    pub values: Vec<(usize, Vec<u32>, f64)>,
    /// The goal.
    pub goal: Formula,
    /// Whether the problem minimizes `total-cost`, so that action costs count.
    pub minimize_total_cost: bool,
}

/// Why a problem doesn't fit a generated domain.
#[derive(Debug, Clone, PartialEq, thiserror::Error)]
pub enum LoadError {
    /// The problem names another domain.
    #[error("the problem is for domain `{found}`, not `{expected}`")]
    WrongDomain {
        /// The generated domain.
        expected: &'static str,
        /// The domain the problem names.
        found: String,
    },
    /// A type that the domain does not declare.
    #[error("unknown type `{0}`")]
    UnknownType(String),
    /// An object declared with more than one type, or with an `either` type.
    #[error("object `{0}` has more than one type")]
    MultipleTypes(String),
    /// A name that is neither an object nor a constant.
    #[error("unknown object `{0}`")]
    UnknownObject(String),
    /// A predicate or function the domain does not declare.
    #[error("unknown predicate or function `{0}`")]
    UnknownSymbol(String),
    /// A predicate or function with the wrong number of arguments.
    #[error("`{0}` has the wrong number of arguments")]
    WrongArity(String),
    /// An object of the wrong type for an argument.
    #[error("`{object}` is not of the type `{expected}` that `{symbol}` takes")]
    WrongType {
        /// The predicate or function.
        symbol: String,
        /// The object.
        object: String,
        /// The type the argument needs.
        expected: &'static str,
    },
    /// A variable used outside the quantifier that declares it.
    #[error("unknown variable `{0}`")]
    UnknownVariable(String),
    /// A construct the generated code does not support.
    #[error("unsupported in a problem: {0}")]
    Unsupported(&'static str),
    /// A function that action costs read has a value that is not a
    /// non-negative integer.
    #[error("`{function}` is {value}, but action costs must be non-negative integers")]
    InvalidCost {
        /// The function.
        function: String,
        /// Its value in the initial state.
        value: f64,
    },
}

/// Lowercases a name.
fn lc(name: &str) -> String {
    name.to_lowercase()
}

/// Checks `problem` against `signature`, numbers its objects and grounds its
/// initial state and goal.
///
/// # Errors
/// A [`LoadError`] if the problem is for another domain, uses names, types or
/// arities the domain does not declare, or uses an unsupported construct:
/// timed initial literals, object fluents, preferences, constraints, or a
/// metric other than minimizing `total-cost`.
pub fn load(signature: &Signature, problem: &Problem) -> Result<Loaded, LoadError> {
    if lc(problem.domain()) != signature.domain {
        return Err(LoadError::WrongDomain {
            expected: signature.domain,
            found: problem.domain().to_string(),
        });
    }
    if !problem.constraints().is_empty() {
        return Err(LoadError::Unsupported("constraints"));
    }
    let minimize_total_cost = match problem.metric_spec() {
        None => false,
        Some(metric) => match (metric.optimization(), metric.expression()) {
            (Optimization::Minimize, MetricFluentExpression::Function(symbol, args))
                if args.is_empty() && lc(symbol) == "total-cost" =>
            {
                true
            }
            _ => {
                return Err(LoadError::Unsupported(
                    "metrics other than minimizing total-cost",
                ))
            }
        },
    };

    let loader = Loader::new(signature, problem)?;
    let mut facts = Vec::new();
    let mut values = Vec::new();
    for element in problem.init().iter() {
        match element {
            InitElement::Literal(Literal::AtomicFormula(AtomicFormula::Predicate(atom))) => {
                let names: Vec<&Name> = atom.values().iter().collect();
                let (pred, args) = loader.symbol(true, atom.predicate(), &names)?;
                facts.push((pred, args));
            }
            // Closed world: negative initial literals hold anyway.
            InitElement::Literal(Literal::NotAtomicFormula(AtomicFormula::Predicate(_))) => {}
            InitElement::Literal(_) => {
                return Err(LoadError::Unsupported("equality in the initial state"))
            }
            InitElement::At(..) => return Err(LoadError::Unsupported("timed initial literals")),
            InitElement::IsValue(head, value) => {
                if lc(head.symbol()) == "total-cost" && head.names().is_empty() {
                    continue; // costs are not part of the state
                }
                let names: Vec<&Name> = head.names().iter().collect();
                let (func, args) = loader.symbol(false, head.symbol(), &names)?;
                values.push((func, args, **value));
            }
            InitElement::IsObject(..) => return Err(LoadError::Unsupported("object fluents")),
        }
    }
    let goal = loader.goals(problem.goals(), &HashMap::new())?;

    Ok(Loaded {
        names: loader.names,
        starts: loader.starts,
        ends: loader.ends,
        constants: loader.constants,
        facts,
        values,
        goal,
        minimize_total_cost,
    })
}

/// The variables bound by enclosing quantifiers, to object numbers.
type Scope = HashMap<String, u32>;

/// A callback for each binding of quantified variables.
type OnBinding<'f> = dyn FnMut(&Scope) -> Result<(), LoadError> + 'f;

/// The numbered objects, while the problem loads.
struct Loader<'a> {
    /// The domain.
    signature: &'a Signature,
    /// The name of each object, by number.
    names: Vec<String>,
    /// The number of each object, by name.
    numbers: HashMap<String, u32>,
    /// The first object of each type.
    starts: Vec<u32>,
    /// One past the last object of each type, subtypes included.
    ends: Vec<u32>,
    /// The number of each constant.
    constants: Vec<u32>,
}

impl<'a> Loader<'a> {
    /// Numbers the constants and objects type by type.
    fn new(signature: &'a Signature, problem: &Problem) -> Result<Self, LoadError> {
        let type_index = |ty: &Type, object: &str| -> Result<usize, LoadError> {
            let name = match ty {
                Type::Exactly(name) => lc(name),
                Type::EitherOf(names) if names.len() == 1 => lc(&names[0]),
                Type::EitherOf(_) => return Err(LoadError::MultipleTypes(object.to_string())),
            };
            signature
                .types
                .iter()
                .position(|t| t.name == name)
                .ok_or(LoadError::UnknownType(name))
        };

        // Each object's type, in declaration order: constants first.
        let mut declared: Vec<(String, usize)> = signature
            .constants
            .iter()
            .map(|&(name, ty)| (name.to_string(), ty))
            .collect();
        for typed in problem.objects().iter() {
            let name = lc(typed.value());
            let ty = type_index(typed.type_(), &name)?;
            match declared.iter().find(|(n, _)| *n == name) {
                Some(&(_, other)) if other == ty => {} // redeclared constant
                Some(_) => return Err(LoadError::MultipleTypes(name)),
                None => declared.push((name, ty)),
            }
        }

        let count = signature.types.len();
        let mut starts = vec![0; count];
        let mut ends = vec![0; count];
        let mut names = Vec::new();
        for (t, start) in starts.iter_mut().enumerate() {
            *start = u32::try_from(names.len()).expect("fewer than 2^32 objects");
            names.extend(
                declared
                    .iter()
                    .filter(|(_, ty)| *ty == t)
                    .map(|(n, _)| n.clone()),
            );
            ends[t] = u32::try_from(names.len()).expect("fewer than 2^32 objects");
        }
        // A subtree is contiguous, so its end is the largest end in it.
        for t in (0..count).rev() {
            if let Some(parent) = signature.types[t].parent {
                ends[parent] = ends[parent].max(ends[t]);
            }
        }
        let numbers: HashMap<String, u32> = names
            .iter()
            .enumerate()
            .map(|(i, n)| {
                (
                    n.clone(),
                    u32::try_from(i).expect("fewer than 2^32 objects"),
                )
            })
            .collect();
        let constants = signature
            .constants
            .iter()
            .map(|(name, _)| numbers[*name])
            .collect();

        Ok(Self {
            signature,
            names,
            numbers,
            starts,
            ends,
            constants,
        })
    }

    /// The number of an object.
    fn object(&self, name: &str) -> Result<u32, LoadError> {
        let name = lc(name);
        self.numbers
            .get(&name)
            .copied()
            .ok_or(LoadError::UnknownObject(name))
    }

    /// Resolves a predicate (`predicate`) or function and checks its arguments.
    fn symbol(
        &self,
        predicate: bool,
        name: &str,
        args: &[&Name],
    ) -> Result<(usize, Vec<u32>), LoadError> {
        let args = args
            .iter()
            .map(|a| self.object(a))
            .collect::<Result<Vec<_>, _>>()?;
        self.check(predicate, name, args)
    }

    /// Resolves a predicate or function and checks the types of its
    /// numbered arguments.
    fn check(
        &self,
        predicate: bool,
        name: &str,
        args: Vec<u32>,
    ) -> Result<(usize, Vec<u32>), LoadError> {
        let name = lc(name);
        let symbols = if predicate {
            self.signature.predicates
        } else {
            self.signature.functions
        };
        let index = symbols
            .iter()
            .position(|s| s.name == name)
            .ok_or_else(|| LoadError::UnknownSymbol(name.clone()))?;
        let symbol = &symbols[index];
        if symbol.args.len() != args.len() {
            return Err(LoadError::WrongArity(name));
        }
        for (&arg, &ty) in args.iter().zip(symbol.args) {
            if !(self.starts[ty]..self.ends[ty]).contains(&arg) {
                return Err(LoadError::WrongType {
                    symbol: name,
                    object: self.names[arg as usize].clone(),
                    expected: self.signature.types[ty].name,
                });
            }
        }
        Ok((index, args))
    }

    /// Resolves a term under the quantified variables `scope`.
    fn term(&self, term: &Term, scope: &HashMap<String, u32>) -> Result<u32, LoadError> {
        match term {
            Term::Name(name) => self.object(name),
            Term::Variable(var) => {
                let name = lc(var);
                scope
                    .get(&name)
                    .copied()
                    .ok_or(LoadError::UnknownVariable(name))
            }
            Term::Function(_) => Err(LoadError::Unsupported("object fluents")),
        }
    }

    /// Calls `f` with every binding of `vars` that extends `scope`.
    fn each_binding(
        &self,
        vars: &TypedVariables,
        scope: &HashMap<String, u32>,
        f: &mut OnBinding<'_>,
    ) -> Result<(), LoadError> {
        let mut domains = Vec::new();
        for var in vars.iter() {
            let mut objects = Vec::new();
            for ty in var.type_().clone() {
                let name = lc(&ty);
                let t = self
                    .signature
                    .types
                    .iter()
                    .position(|t| t.name == name)
                    .ok_or(LoadError::UnknownType(name))?;
                objects.extend(self.starts[t]..self.ends[t]);
            }
            objects.sort_unstable();
            objects.dedup();
            domains.push((lc(var.value()), objects));
        }
        bind(&domains, &mut scope.clone(), f)
    }

    /// Grounds a list of goals into a conjunction.
    fn goals(
        &self,
        goals: &PreconditionGoalDescriptions,
        scope: &HashMap<String, u32>,
    ) -> Result<Formula, LoadError> {
        let mut parts = Vec::new();
        for goal in goals {
            match goal {
                PreconditionGoalDescription::Preference(PreferenceGoalDescription::Goal(gd)) => {
                    parts.push(self.goal(gd, scope)?);
                }
                PreconditionGoalDescription::Preference(PreferenceGoalDescription::Preference(
                    _,
                )) => return Err(LoadError::Unsupported("preferences")),
                PreconditionGoalDescription::Forall(vars, inner) => {
                    self.each_binding(vars, scope, &mut |s| {
                        parts.push(self.goals(inner, s)?);
                        Ok(())
                    })?;
                }
            }
        }
        Ok(flatten_and(parts))
    }

    /// Grounds a goal description.
    fn goal(
        &self,
        gd: &GoalDescription,
        scope: &HashMap<String, u32>,
    ) -> Result<Formula, LoadError> {
        Ok(match gd {
            GoalDescription::AtomicFormula(formula)
            | GoalDescription::Literal(Literal::AtomicFormula(formula)) => {
                self.atom(formula, scope)?
            }
            GoalDescription::Literal(Literal::NotAtomicFormula(formula)) => {
                Formula::Not(Box::new(self.atom(formula, scope)?))
            }
            GoalDescription::Not(inner) => Formula::Not(Box::new(self.goal(inner, scope)?)),
            GoalDescription::And(parts) => flatten_and(
                parts
                    .iter()
                    .map(|p| self.goal(p, scope))
                    .collect::<Result<_, _>>()?,
            ),
            GoalDescription::Or(parts) => Formula::Or(
                parts
                    .iter()
                    .map(|p| self.goal(p, scope))
                    .collect::<Result<_, _>>()?,
            ),
            GoalDescription::Imply(a, b) => Formula::Or(vec![
                Formula::Not(Box::new(self.goal(a, scope)?)),
                self.goal(b, scope)?,
            ]),
            GoalDescription::Exists(vars, inner) => {
                let mut parts = Vec::new();
                self.each_binding(vars, scope, &mut |s| {
                    parts.push(self.goal(inner, s)?);
                    Ok(())
                })?;
                Formula::Or(parts)
            }
            GoalDescription::ForAll(vars, inner) => {
                let mut parts = Vec::new();
                self.each_binding(vars, scope, &mut |s| {
                    parts.push(self.goal(inner, s)?);
                    Ok(())
                })?;
                flatten_and(parts)
            }
            GoalDescription::FluentComparison(comparison) => Formula::Compare(
                match comparison.comparison() {
                    BinaryComparison::LessThan => Comparison::Less,
                    BinaryComparison::LessThanOrEqual => Comparison::LessOrEqual,
                    BinaryComparison::Equal => Comparison::Equal,
                    BinaryComparison::GreaterOrEqual => Comparison::GreaterOrEqual,
                    BinaryComparison::GreaterThan => Comparison::Greater,
                },
                self.expr(comparison.first(), scope)?,
                self.expr(comparison.second(), scope)?,
            ),
        })
    }

    /// Grounds an atom or an equality.
    fn atom(
        &self,
        formula: &AtomicFormula<Term>,
        scope: &HashMap<String, u32>,
    ) -> Result<Formula, LoadError> {
        Ok(match formula {
            AtomicFormula::Equality(eq) => match (eq.first(), eq.second()) {
                // The parser reads `(= (f) (g))` as an equality of terms; it is numeric.
                (Term::Function(a), Term::Function(b)) => Formula::Compare(
                    Comparison::Equal,
                    self.fluent(a.symbol(), a.terms(), scope)?,
                    self.fluent(b.symbol(), b.terms(), scope)?,
                ),
                (a, b) => {
                    if self.term(a, scope)? == self.term(b, scope)? {
                        Formula::True
                    } else {
                        Formula::False
                    }
                }
            },
            AtomicFormula::Predicate(atom) => {
                let args = atom
                    .values()
                    .iter()
                    .map(|t| self.term(t, scope))
                    .collect::<Result<Vec<_>, _>>()?;
                match self.check(true, atom.predicate(), args) {
                    Ok((pred, args)) => Formula::Atom {
                        pred,
                        args: args.into(),
                    },
                    // A fact on an object of the wrong type never holds.
                    Err(LoadError::WrongType { .. }) => Formula::False,
                    Err(e) => return Err(e),
                }
            }
        })
    }

    /// Grounds the fluent `symbol(terms)`.
    fn fluent(
        &self,
        symbol: &str,
        terms: &[Term],
        scope: &HashMap<String, u32>,
    ) -> Result<Expr, LoadError> {
        let args = terms
            .iter()
            .map(|t| self.term(t, scope))
            .collect::<Result<Vec<_>, _>>()?;
        let (func, args) = self.check(false, symbol, args)?;

        Ok(Expr::Fluent {
            func,
            args: args.into(),
        })
    }

    /// Grounds a numeric expression.
    fn expr(
        &self,
        expr: &FluentExpression,
        scope: &HashMap<String, u32>,
    ) -> Result<Expr, LoadError> {
        let boxed = |e: &FluentExpression| self.expr(e, scope).map(Box::new);
        Ok(match expr {
            FluentExpression::Number(n) => Expr::Number(**n),
            FluentExpression::Function(head) => {
                let (symbol, terms): (&Name, &[Term]) = match head {
                    FunctionHead::Simple(symbol) => (symbol, &[]),
                    FunctionHead::WithTerms(symbol, terms) => (symbol, terms),
                };
                self.fluent(symbol, terms, scope)?
            }
            FluentExpression::Negative(inner) => Expr::Neg(boxed(inner)?),
            FluentExpression::BinaryOp(op, a, b) => {
                let (a, b) = (boxed(a)?, boxed(b)?);
                match op {
                    BinaryOp::Addition => Expr::Add(a, b),
                    BinaryOp::Subtraction => Expr::Sub(a, b),
                    BinaryOp::Multiplication => Expr::Mul(a, b),
                    BinaryOp::Division => Expr::Div(a, b),
                }
            }
            FluentExpression::MultiOp(op, first, rest) => {
                let mut acc = self.expr(first, scope)?;
                for e in rest {
                    let e = boxed(e)?;
                    acc = match op {
                        MultiOp::Addition => Expr::Add(Box::new(acc), e),
                        MultiOp::Multiplication => Expr::Mul(Box::new(acc), e),
                    };
                }
                acc
            }
        })
    }
}

/// Binds the first of the quantified variables to each of its objects, then the rest.
fn bind(
    domains: &[(String, Vec<u32>)],
    scope: &mut HashMap<String, u32>,
    f: &mut OnBinding<'_>,
) -> Result<(), LoadError> {
    let Some(((name, objects), rest)) = domains.split_first() else {
        return f(scope);
    };
    let shadowed = scope.get(name).copied();
    for &object in objects {
        scope.insert(name.clone(), object);
        bind(rest, scope, f)?;
    }
    match shadowed {
        Some(object) => scope.insert(name.clone(), object),
        None => scope.remove(name),
    };
    Ok(())
}

/// A conjunction, with nested conjunctions merged into it.
fn flatten_and(parts: Vec<Formula>) -> Formula {
    let mut flat = Vec::new();
    for part in parts {
        match part {
            Formula::And(inner) => flat.extend(inner),
            other => flat.push(other),
        }
    }
    Formula::And(flat)
}

#[cfg(all(test, feature = "parser"))]
mod tests {
    use super::*;
    use crate::Parser;

    /// `object` > `vehicle` > `truck`, and `place`; a constant `depot`.
    static SIGNATURE: Signature = Signature {
        domain: "d",
        types: &[
            TypeSignature {
                name: "object",
                parent: None,
            },
            TypeSignature {
                name: "vehicle",
                parent: Some(0),
            },
            TypeSignature {
                name: "truck",
                parent: Some(1),
            },
            TypeSignature {
                name: "place",
                parent: Some(0),
            },
        ],
        predicates: &[Symbol {
            name: "at",
            args: &[1, 3],
        }],
        functions: &[Symbol {
            name: "fuel",
            args: &[1],
        }],
        constants: &[("depot", 3)],
    };

    /// Loads a problem given as text.
    fn load_str(text: &str) -> Result<Loaded, LoadError> {
        load(&SIGNATURE, &Problem::from_str(text).unwrap())
    }

    #[test]
    fn numbers_subtypes_inside_their_supertypes() {
        let loaded = load_str(
            "(define (problem p) (:domain d)
               (:objects t1 - truck v1 - vehicle shop - place t2 - truck depot - place)
               (:init (at t1 depot) (= (fuel t2) 3) (= (total-cost) 0))
               (:goal (and (at t2 shop) (forall (?v - vehicle) (>= (fuel ?v) 1))))
               (:metric minimize (total-cost)))",
        )
        .unwrap();
        assert_eq!(loaded.names, ["v1", "t1", "t2", "depot", "shop"]);
        assert_eq!(loaded.starts, [0, 0, 1, 3]);
        assert_eq!(loaded.ends, [5, 3, 3, 5]);
        assert_eq!(loaded.constants, [3]);
        assert_eq!(loaded.facts, [(0, vec![1, 3])]);
        assert_eq!(loaded.values, [(0, vec![2], 3.0)]);
        assert!(loaded.minimize_total_cost);
        let Formula::And(goal) = &loaded.goal else {
            panic!("{:?}", loaded.goal)
        };
        assert_eq!(goal.len(), 4, "{goal:?}");
    }

    #[test]
    fn rejects_problems_that_do_not_fit() {
        let problem = |objects: &str, init: &str, goal: &str| {
            load_str(&format!(
                "(define (problem p) (:domain d) (:objects {objects}) (:init {init}) (:goal {goal}))"
            ))
        };
        assert_eq!(
            problem("x - robot", "", "(and)").unwrap_err(),
            LoadError::UnknownType("robot".into())
        );
        assert_eq!(
            problem("x - (either truck place)", "", "(and)").unwrap_err(),
            LoadError::MultipleTypes("x".into())
        );
        assert_eq!(
            problem("depot - truck", "", "(and)").unwrap_err(),
            LoadError::MultipleTypes("depot".into())
        );
        assert_eq!(
            problem("t - truck", "(at t nowhere)", "(and)").unwrap_err(),
            LoadError::UnknownObject("nowhere".into())
        );
        assert_eq!(
            problem("t - truck", "(at depot t)", "(and)").unwrap_err(),
            LoadError::WrongType {
                symbol: "at".into(),
                object: "depot".into(),
                expected: "vehicle"
            }
        );
        assert_eq!(
            problem("t - truck", "(on t)", "(and)").unwrap_err(),
            LoadError::UnknownSymbol("on".into())
        );
        assert_eq!(
            problem("t - truck", "(at t)", "(and)").unwrap_err(),
            LoadError::WrongArity("at".into())
        );
        assert_eq!(
            problem("t - truck", "(at 5 (at t depot))", "(and)").unwrap_err(),
            LoadError::Unsupported("timed initial literals")
        );
        assert_eq!(
            problem("t - truck", "", "(at ?x depot)").unwrap_err(),
            LoadError::UnknownVariable("x".into())
        );
        assert!(matches!(
            load_str("(define (problem p) (:domain other) (:init) (:goal (and)))"),
            Err(LoadError::WrongDomain { .. })
        ));
        assert_eq!(
            load_str("(define (problem p) (:domain d) (:init) (:goal (and)) (:metric maximize (total-cost)))")
                .unwrap_err(),
            LoadError::Unsupported("metrics other than minimizing total-cost")
        );
    }

    #[test]
    fn reads_every_init_and_goal_form() {
        let loaded = load_str(
            "(define (problem p) (:domain d) (:objects t - truck depot - place)
               (:init (at t depot) (not (at t depot)) (= (fuel t) 2))
               (:goal (and (not (at t depot))
                           (forall (?v - vehicle) (exists (?p - place) (at ?v ?p)))
                           (<= (- (fuel t)) (* (fuel t) 2 3))
                           (= (+ (fuel t) 1 1) 4)
                           (> (/ (fuel t) 2) 0))))",
        )
        .unwrap();
        // The redeclared constant `depot` keeps one number.
        assert_eq!(loaded.names, ["t", "depot"]);
        let fact = |_: usize, _: &[u32]| true;
        let value = |_: usize, _: &[u32]| Some(2.0);
        let Formula::And(goal) = &loaded.goal else {
            panic!("{:?}", loaded.goal)
        };
        assert!(!goal[0].holds(&fact, &value));
        assert!(goal[1..].iter().all(|g| g.holds(&fact, &value)), "{goal:?}");
    }

    #[test]
    fn reads_an_equality_of_two_functions_as_numeric() {
        let loaded = load_str(
            "(define (problem p) (:domain d) (:objects t u - truck)
               (:init) (:goal (= (fuel t) (fuel u))))",
        )
        .unwrap();
        let value = |_: usize, args: &[u32]| Some(f64::from(args[0]));
        assert!(!loaded.goal.holds(&|_, _| false, &value));
        assert!(loaded.goal.holds(&|_, _| false, &|_, _| Some(1.0)));
    }

    #[test]
    fn rejects_unsupported_problem_constructs() {
        let problem = |rest: &str| {
            load_str(&format!(
                "(define (problem p) (:domain d) (:objects t - truck) {rest})"
            ))
        };
        assert_eq!(
            problem("(:init) (:goal (and)) (:constraints (always (at t depot)))").unwrap_err(),
            LoadError::Unsupported("constraints")
        );
        assert_eq!(
            problem("(:init (= (owner t) depot)) (:goal (and))").unwrap_err(),
            LoadError::Unsupported("object fluents")
        );
        assert_eq!(
            problem("(:init) (:goal (preference g (at t depot)))").unwrap_err(),
            LoadError::Unsupported("preferences")
        );
        assert_eq!(
            problem("(:init) (:goal (exists (?x - robot) (at t ?x)))").unwrap_err(),
            LoadError::UnknownType("robot".into())
        );
        assert_eq!(
            problem("(:init) (:goal (at t nowhere))").unwrap_err(),
            LoadError::UnknownObject("nowhere".into())
        );
        assert_eq!(
            problem("(:init (= (speed t) 1)) (:goal (and))").unwrap_err(),
            LoadError::UnknownSymbol("speed".into())
        );
    }

    #[test]
    fn grounds_goal_connectives() {
        let loaded = load_str(
            "(define (problem p) (:domain d) (:objects t - truck)
               (:init (at t depot))
               (:goal (and (or (at t depot) (not (= t depot)))
                           (imply (at t depot) (exists (?p - place) (at t ?p)))
                           (at depot depot)
                           (< (- (* 2 (fuel t) 1) (/ 4 (fuel t))) (+ 1 (fuel t))))))",
        )
        .unwrap();
        let fact = |pred: usize, args: &[u32]| {
            loaded
                .facts
                .iter()
                .any(|(p, a)| *p == pred && a.as_slice() == args)
        };
        let Formula::And(parts) = &loaded.goal else {
            panic!()
        };
        // The wrongly typed `(at depot depot)` never holds.
        assert_eq!(parts[2], Formula::False);
        assert!(parts[0].holds(&fact, &|_, _| None));
        assert!(parts[1].holds(&fact, &|_, _| None));
        assert!(parts[3].holds(&fact, &|_, _| Some(2.0)));
        assert!(!parts[3].holds(&fact, &|_, _| None));
    }
}