pddl-validate 0.1.0

Independent PDDL plan validator: classical, numeric and temporal plans with precise diagnostics
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
//! The static check of every formula of the domain and problem, before any plan runs.
//!
//! It finds undeclared names, wrong arities and unbound variables, so evaluation never
//! meets them, and it finds every construct the validator does not interpret, so no plan
//! that uses one is ever accepted. Formulas inside an unsupported construct are not
//! checked further. [`durative`] checks durative actions, and [`timed`] checks that the
//! timed initial literals agree.

mod durative;
mod timed;

use alloc::format;
use alloc::string::String;
use alloc::vec::Vec;

use pddl::{
    AtomicFormula, ConditionalEffect, Domain, EffectCondition, Effects, FluentExpression,
    FunctionHead, GoalDescription, InitElement, Literal, MetricFluentExpression,
    PreconditionGoalDescription, PreconditionGoalDescriptions, PreferenceGoalDescription,
    PrimitiveEffect, Problem, Term, TypedVariables,
};

use super::Model;
use crate::atom::lower;
use crate::error::{ModelError, ModelErrorKind};
use crate::eval::numeric::{numeric_assignment, numeric_equality};
use crate::report::{Construct, Unsupported};

/// What the static check found.
#[derive(Debug)]
pub enum Issue {
    /// The domain or problem is malformed.
    Model(ModelError),
    /// The domain or problem uses a construct the validator does not interpret.
    Unsupported(Unsupported),
}

/// Checks every action, derived-predicate rule, initial fact and goal.
///
/// # Errors
///
/// The first [`Issue`] found, in the order actions, rules, initial state, timed initial
/// literals, goal, metric.
pub fn check(model: &Model<'_>, problem: &Problem) -> Result<(), Issue> {
    for (name, action) in &model.actions {
        let mut checker = Checker::new(model, format!("action `{name}`"));
        checker.bind(action.parameters())?;
        checker.preconditions(action.precondition())?;
        if let Some(effects) = action.effect() {
            checker.effects(effects)?;
        }
    }

    for (name, action) in &model.durative {
        let mut checker = Checker::new(model, format!("durative action `{name}`"));
        checker.durative(action)?;
    }

    for (name, event) in &model.events {
        let mut checker = Checker::new(model, format!("event `{name}`"));
        checker.bind(event.parameters())?;
        checker.preconditions(event.precondition())?;
        if let Some(effects) = event.effect() {
            checker.effects(effects)?;
        }
    }

    for (name, process) in &model.processes {
        let mut checker = Checker::new(model, format!("process `{name}`"));
        checker.bind(process.parameters())?;
        checker.preconditions(process.precondition())?;
        if let Some(effect) = process.effect() {
            checker.process_effect(effect)?;
        }
    }

    for rule in &model.rules {
        let mut checker = Checker::new(model, format!("derived predicate `{}`", rule.predicate));
        checker.bind(rule.parameters)?;
        checker.goal(rule.body)?;
    }

    let checker = Checker::new(model, String::from("init"));
    for element in problem.init().iter() {
        checker.init(element)?;
    }
    timed::contradictions(problem).map_err(Issue::Model)?;

    let mut checker = Checker::new(model, String::from("goal"));
    checker.preconditions(problem.goals())?;
    if let Some(metric) = problem.metric_spec() {
        Checker::new(model, String::from("metric")).metric(metric.expression())?;
    }

    Ok(())
}

/// Checks that `domain` declares only number-valued functions.
///
/// # Errors
///
/// [`Construct::ObjectFluents`] for a function of an object type.
pub fn check_function_types(domain: &Domain) -> Result<(), Issue> {
    for function in domain.functions().iter() {
        let names = super::universe::primitive_names(function.type_ref());
        if names.iter().any(|name| name != "number") {
            let location = format!("function `{}`", lower(function.symbol()));
            let unsupported = Unsupported::new(Construct::ObjectFluents, location);
            return Err(Issue::Unsupported(unsupported));
        }
    }

    Ok(())
}

/// Walks the formulas of one action, rule or problem section.
struct Checker<'m, 'a> {
    /// The indexed domain and problem.
    model: &'m Model<'a>,
    /// Where the walked formulas are, for messages.
    location: String,
    /// The variables in scope, innermost last, lowercased.
    variables: Vec<String>,
}

impl<'m, 'a> Checker<'m, 'a> {
    /// Starts a walk at `location` with no variables in scope.
    const fn new(model: &'m Model<'a>, location: String) -> Self {
        Self {
            model,
            location,
            variables: Vec::new(),
        }
    }

    /// Builds a model error at the current location.
    fn error(&self, kind: ModelErrorKind) -> Issue {
        Issue::Model(ModelError::new(self.location.clone(), kind))
    }

    /// Builds an unsupported finding at the current location.
    fn unsupported(&self, construct: Construct) -> Issue {
        Issue::Unsupported(Unsupported::new(construct, self.location.clone()))
    }

    /// Brings `variables` into scope and checks their types and that no name repeats
    /// within the list. Returns how many were added, for [`Self::unbind`].
    fn bind(&mut self, variables: &TypedVariables) -> Result<usize, Issue> {
        let outer = self.variables.len();
        for variable in variables.iter() {
            if let Some(name) = self.model.universe.unknown_type(variable.type_()) {
                return Err(self.error(ModelErrorKind::UnknownType(name)));
            }

            let name = lower(variable.value());
            if self
                .variables
                .iter()
                .skip(outer)
                .any(|bound| *bound == name)
            {
                return Err(self.error(ModelErrorKind::DuplicateVariable(name)));
            }

            self.variables.push(name);
        }

        Ok(variables.len())
    }

    /// Takes the last `count` variables out of scope.
    fn unbind(&mut self, count: usize) {
        let keep = self.variables.len().saturating_sub(count);
        self.variables.truncate(keep);
    }

    /// Checks a precondition or goal list.
    fn preconditions(&mut self, list: &PreconditionGoalDescriptions) -> Result<(), Issue> {
        for item in list {
            match item {
                PreconditionGoalDescription::Preference(PreferenceGoalDescription::Goal(gd)) => {
                    self.goal(gd)?;
                }
                PreconditionGoalDescription::Preference(PreferenceGoalDescription::Preference(
                    _,
                )) => return Err(self.unsupported(Construct::Preferences)),
                PreconditionGoalDescription::Forall(variables, inner) => {
                    let count = self.bind(variables)?;
                    self.preconditions(inner)?;
                    self.unbind(count);
                }
            }
        }

        Ok(())
    }

    /// Checks a goal description.
    fn goal(&mut self, gd: &GoalDescription) -> Result<(), Issue> {
        match gd {
            GoalDescription::AtomicFormula(formula)
            | GoalDescription::Literal(
                Literal::AtomicFormula(formula) | Literal::NotAtomicFormula(formula),
            ) => self.atomic(formula),
            GoalDescription::And(items) | GoalDescription::Or(items) => {
                items.iter().try_for_each(|item| self.goal(item))
            }
            GoalDescription::Not(inner) => self.goal(inner),
            GoalDescription::Imply(premise, conclusion) => {
                self.goal(premise)?;
                self.goal(conclusion)
            }
            GoalDescription::Exists(variables, inner)
            | GoalDescription::ForAll(variables, inner) => {
                let count = self.bind(variables)?;
                self.goal(inner)?;
                self.unbind(count);
                Ok(())
            }
            GoalDescription::FluentComparison(comparison) => {
                self.expression(comparison.first())?;
                self.expression(comparison.second())
            }
        }
    }

    /// Checks an atomic formula: a declared predicate with the right arity, or an equality.
    fn atomic(&self, formula: &AtomicFormula<Term>) -> Result<(), Issue> {
        match formula {
            AtomicFormula::Equality(equality) => {
                if let Some(comparison) = numeric_equality(equality) {
                    self.expression(comparison.first())?;
                    return self.expression(comparison.second());
                }

                self.term(equality.first())?;
                self.term(equality.second())
            }
            AtomicFormula::Predicate(atom) => {
                let found = atom.values().len();
                let unknown = ModelErrorKind::UnknownPredicate;
                self.symbol(&self.model.predicates, atom.predicate(), found, unknown)?;
                atom.values().iter().try_for_each(|term| self.term(term))
            }
        }
    }

    /// Checks that `symbol` is declared in `table` with `found` parameters; `unknown`
    /// builds the error for a symbol that is not.
    fn symbol(
        &self,
        table: &alloc::collections::BTreeMap<String, usize>,
        symbol: &str,
        found: usize,
        unknown: fn(String) -> ModelErrorKind,
    ) -> Result<(), Issue> {
        let symbol = lower(symbol);
        let Some(&expected) = table.get(&symbol) else {
            return Err(self.error(unknown(symbol)));
        };

        if expected == found {
            Ok(())
        } else {
            Err(self.error(ModelErrorKind::Arity {
                symbol,
                expected,
                found,
            }))
        }
    }

    /// Checks that a term is a declared object or a variable in scope.
    fn term(&self, term: &Term) -> Result<(), Issue> {
        match term {
            Term::Name(name) => self.object(name),
            Term::Variable(variable) => {
                let name = lower(variable);
                if self.variables.contains(&name) {
                    Ok(())
                } else {
                    Err(self.error(ModelErrorKind::UnboundVariable(name)))
                }
            }
            Term::Function(_) => Err(self.unsupported(Construct::ObjectFluents)),
        }
    }

    /// Checks that `name` is a declared object or constant.
    fn object(&self, name: &str) -> Result<(), Issue> {
        let name = lower(name);
        if self.model.universe.contains(&name) {
            Ok(())
        } else {
            Err(self.error(ModelErrorKind::UnknownObject(name)))
        }
    }

    /// Checks an effect list.
    fn effects(&mut self, effects: &Effects) -> Result<(), Issue> {
        for effect in effects {
            match effect {
                ConditionalEffect::Effect(primitive) => self.primitive(primitive)?,
                ConditionalEffect::Forall(forall) => {
                    let count = self.bind(&forall.variables)?;
                    self.effects(&forall.effects)?;
                    self.unbind(count);
                }
                ConditionalEffect::When(when) => {
                    self.goal(&when.condition)?;
                    match &when.effect {
                        EffectCondition::Single(primitive) => self.primitive(primitive)?,
                        EffectCondition::All(primitives) => {
                            primitives.iter().try_for_each(|p| self.primitive(p))?;
                        }
                    }
                }
            }
        }

        Ok(())
    }

    /// Checks a primitive effect.
    fn primitive(&self, effect: &PrimitiveEffect) -> Result<(), Issue> {
        match effect {
            PrimitiveEffect::AtomicFormula(formula)
            | PrimitiveEffect::NotAtomicFormula(formula) => self.settable(formula),
            PrimitiveEffect::AssignNumericFluent(_, head, value) => {
                self.function(head)?;
                self.expression(value)
            }
            PrimitiveEffect::AssignObjectFluent(..) => match numeric_assignment(effect) {
                Some((head, value)) => {
                    self.function(&head)?;
                    self.expression(&value)
                }
                None => Err(self.unsupported(Construct::ObjectFluents)),
            },
        }
    }

    /// Checks an atom that an effect or the initial state sets: a declared, non-derived
    /// predicate, never an equality.
    fn settable(&self, formula: &AtomicFormula<Term>) -> Result<(), Issue> {
        match formula {
            AtomicFormula::Equality(_) => Err(self.error(ModelErrorKind::EqualitySet)),
            AtomicFormula::Predicate(atom)
                if self.model.derived.contains(&lower(atom.predicate())) =>
            {
                Err(self.error(ModelErrorKind::DerivedPredicateSet(lower(atom.predicate()))))
            }
            AtomicFormula::Predicate(_) => self.atomic(formula),
        }
    }

    /// Checks a numeric expression.
    fn expression(&self, exp: &FluentExpression) -> Result<(), Issue> {
        match exp {
            FluentExpression::Number(_) => Ok(()),
            FluentExpression::Function(head) => self.function(head),
            FluentExpression::Negative(inner) => self.expression(inner),
            FluentExpression::BinaryOp(_, left, right) => {
                self.expression(left)?;
                self.expression(right)
            }
            FluentExpression::MultiOp(_, first, rest) => {
                self.expression(first)?;
                rest.iter().try_for_each(|item| self.expression(item))
            }
        }
    }

    /// Checks a function application: a declared function with the right arity.
    fn function(&self, head: &FunctionHead) -> Result<(), Issue> {
        let unknown = ModelErrorKind::UnknownFunction;
        match head {
            FunctionHead::Simple(symbol) => self.symbol(&self.model.functions, symbol, 0, unknown),
            FunctionHead::WithTerms(symbol, terms) => {
                self.symbol(&self.model.functions, symbol, terms.len(), unknown)?;
                terms.iter().try_for_each(|term| self.term(term))
            }
        }
    }

    /// Checks a metric expression.
    fn metric(&self, exp: &MetricFluentExpression) -> Result<(), Issue> {
        match exp {
            MetricFluentExpression::Number(_) | MetricFluentExpression::TotalTime => Ok(()),
            MetricFluentExpression::Function(symbol, names)
                if self.model.is_total_time(symbol, names.len()) =>
            {
                Ok(())
            }
            MetricFluentExpression::Function(symbol, names) => {
                let unknown = ModelErrorKind::UnknownFunction;
                self.symbol(&self.model.functions, symbol, names.len(), unknown)?;
                names.iter().try_for_each(|name| self.object(name))
            }
            MetricFluentExpression::Negative(inner) => self.metric(inner),
            MetricFluentExpression::BinaryOp(_, left, right) => {
                self.metric(left)?;
                self.metric(right)
            }
            MetricFluentExpression::MultiOp(_, first, rest) => {
                self.metric(first)?;
                rest.iter().try_for_each(|item| self.metric(item))
            }
            MetricFluentExpression::IsViolated(_) => Err(self.unsupported(Construct::Preferences)),
        }
    }

    /// Checks an element of the initial state.
    fn init(&self, element: &InitElement) -> Result<(), Issue> {
        match element {
            InitElement::Literal(
                Literal::AtomicFormula(formula) | Literal::NotAtomicFormula(formula),
            )
            | InitElement::At(
                _,
                Literal::AtomicFormula(formula) | Literal::NotAtomicFormula(formula),
            ) => self.settable(&name_formula(formula)),
            InitElement::IsValue(term, _) => {
                let found = term.names().len();
                let unknown = ModelErrorKind::UnknownFunction;
                self.symbol(&self.model.functions, term.symbol(), found, unknown)?;
                term.names().iter().try_for_each(|name| self.object(name))
            }
            InitElement::IsObject(..) => Err(self.unsupported(Construct::ObjectFluents)),
        }
    }
}

/// Converts an atomic formula over names, as the initial state has, to one over terms.
fn name_formula(formula: &AtomicFormula<pddl::Name>) -> AtomicFormula<Term> {
    match formula {
        AtomicFormula::Equality(equality) => AtomicFormula::new_equality(
            Term::Name(equality.first().clone()),
            Term::Name(equality.second().clone()),
        ),
        AtomicFormula::Predicate(atom) => AtomicFormula::predicate(
            atom.predicate().clone(),
            atom.values().iter().cloned().map(Term::Name),
        ),
    }
}