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
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
//! The validator: checks a domain and problem once, then runs plans against them.
//!
//! ```mermaid
//! flowchart TD
//!     new["Validator::new"] --> names["check names, arities, types"]
//!     names --> scan["find unsupported constructs"]
//!     scan --> strata["stratify derived predicates"]
//!     strata --> init["build the initial state"]
//!     init --> step{"next step?"}
//!     step -- yes --> ground["ground: action, arity, objects, types"]
//!     ground --> pre["check the precondition"]
//!     pre --> eff["collect effects, delete, add"]
//!     eff --> close["recompute derived atoms"]
//!     close --> step
//!     step -- no --> goal["check the goal"]
//! ```

mod temporal;

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

use pddl::{AtomicFormula, Domain, InitElement, Literal, Problem};

use crate::atom::{lower, Atom};
use crate::derived;
use crate::effects::Changes;
use crate::error::ModelError;
use crate::eval::{EvalError, Evaluator, Scope};
use crate::model::check::{self, Issue};
use crate::model::universe::type_text;
use crate::model::{self as model, Model};
use crate::plan::{Plan, PlanStep};
use crate::report::{
    Construct, Metric, Outcome, Report, StepRef, StepTrace, Unsupported, Violation, ViolationKind,
};
use crate::state::State;

/// Validates plans for one domain and problem.
///
/// Construction checks the domain and problem once; [`Validator::validate`] can then run
/// any number of plans. Validation never panics, whatever the plan.
///
/// # Examples
///
/// ```
/// use pddl::{Domain, Parser, Problem};
/// use pddl_validate::{ValidationStatus, Validator};
///
/// let domain = Domain::from_str(
///     "(define (domain switch) (:predicates (on))
///        (:action flip :parameters () :precondition (not (on)) :effect (on)))",
/// ).unwrap();
/// let problem = Problem::from_str(
///     "(define (problem light) (:domain switch) (:init) (:goal (on)))",
/// ).unwrap();
/// let validator = Validator::new(&domain, &problem).unwrap();
///
/// assert_eq!(validator.validate_text("(flip)").status(), ValidationStatus::Valid);
///
/// let report = validator.validate_text("(flip)\n(flip)");
/// assert_eq!(report.status(), ValidationStatus::Invalid);
/// assert_eq!(report.violation().unwrap().to_string(),
///     "step 2 (flip): unsatisfied precondition: (not (on))");
/// ```
#[derive(Debug)]
pub struct Validator<'a> {
    /// The checked task, or why the validator cannot interpret it.
    inner: Inner<'a>,
    /// The relative tolerance of numeric comparisons; see [`Validator::with_tolerance`].
    tolerance: f64,
    /// The least time between interfering events; see [`Validator::with_epsilon`].
    epsilon: f64,
    /// The absolute tolerance of duration constraints; see
    /// [`Validator::with_duration_tolerance`].
    duration_tolerance: f64,
}

/// The default relative tolerance of numeric comparisons, `1e-12`.
///
/// Two values `a` and `b` count as equal when `|a - b| <= 1e-12 * max(1, |a|, |b|)`.
/// Near zero that absorbs differences up to `1e-12`; elsewhere it absorbs the rounding
/// of summing many decimal costs such as `0.1`. Integers below `1e12` differ by at least
/// 1, more than the tolerance, so they compare exactly.
/// [`Validator::with_tolerance(0.0)`](Validator::with_tolerance) compares exactly.
pub const DEFAULT_TOLERANCE: f64 = 1e-12;

/// The default least time between interfering events of a temporal plan, VAL's default.
pub const DEFAULT_EPSILON: f64 = 0.01;

/// The default absolute tolerance of duration constraints, `0.0005`.
///
/// Planners print durations with three decimals: `(= ?duration (/ (dist) 3))` with
/// `(dist)` 10 comes out as `[3.333]`, 0.00033 below the exact value. Rounding to three
/// decimals is off by at most 0.0005, so a duration within 0.0005 of the bound satisfies
/// `=`, `<=` and `>=`; it also accepts a duration up to 0.0005 beyond a `<=` or `>=`
/// bound. Shorter durations need a smaller tolerance:
/// [`Validator::with_duration_tolerance(0.0)`](Validator::with_duration_tolerance) leaves
/// only the relative numeric tolerance.
pub const DEFAULT_DURATION_TOLERANCE: f64 = 0.0005;

/// What [`Validator::new`] found.
#[derive(Debug)]
#[expect(
    clippy::large_enum_variant,
    reason = "one per validator, built once; boxing buys nothing"
)]
enum Inner<'a> {
    /// A task the validator interprets.
    Ready {
        /// The indexed domain and problem.
        model: Model<'a>,
        /// The problem, for its goal.
        problem: &'a Problem,
        /// The initial state, derived atoms not yet computed.
        initial: State,
    },
    /// A task that uses a construct the validator does not interpret.
    Unsupported(Unsupported),
}

/// Why a step stops the plan.
enum Stop {
    /// The step is invalid.
    Invalid(ViolationKind),
    /// The step meets a construct the validator does not interpret.
    Unsupported(Construct),
}

impl From<EvalError> for Stop {
    fn from(error: EvalError) -> Self {
        match error {
            EvalError::Unsupported(construct) => Self::Unsupported(construct),
            EvalError::Undefined(fluent) => Self::Invalid(ViolationKind::Undefined(fluent)),
            EvalError::DivisionByZero(exp) => Self::Invalid(ViolationKind::DivisionByZero(exp)),
            EvalError::NonFinite(exp) => Self::Invalid(ViolationKind::NonFinite(exp)),
            EvalError::ConflictingUpdates(fluent) => {
                Self::Invalid(ViolationKind::ConflictingUpdates(fluent))
            }
        }
    }
}

impl<'a> Validator<'a> {
    /// Checks `domain` and `problem` and prepares the initial state.
    ///
    /// A task that uses a construct the validator does not interpret yet, such as
    /// preferences, still builds a validator, whose every report is
    /// [`ValidationStatus::Unsupported`](crate::ValidationStatus::Unsupported).
    ///
    /// # Errors
    ///
    /// A [`ModelError`] if the problem is for another domain, or if a formula uses an
    /// undeclared type, predicate, function, object or variable, applies a symbol to the
    /// wrong number of arguments, or sets a derived predicate or `=`; also for two actions
    /// of one name.
    pub fn new(domain: &'a Domain, problem: &'a Problem) -> Result<Self, ModelError> {
        model::check_domain_name(domain, problem)?;
        if let Some(unsupported) = model::unsupported_sections(domain, problem) {
            return Ok(Self::unsupported(unsupported));
        }

        let mut model = Model::new(domain, problem)?;
        let checked =
            check::check_function_types(domain).and_then(|()| check::check(&model, problem));
        match checked {
            Err(Issue::Model(error)) => return Err(error),
            Err(Issue::Unsupported(unsupported)) => return Ok(Self::unsupported(unsupported)),
            Ok(()) => {}
        }

        if let Err(unsupported) = derived::stratify(&mut model) {
            return Ok(Self::unsupported(unsupported));
        }

        let initial = initial_state(&model, problem);

        Ok(Self {
            inner: Inner::Ready {
                model,
                problem,
                initial,
            },
            tolerance: DEFAULT_TOLERANCE,
            epsilon: DEFAULT_EPSILON,
            duration_tolerance: DEFAULT_DURATION_TOLERANCE,
        })
    }

    /// Builds a validator that reports `unsupported` for every plan.
    const fn unsupported(unsupported: Unsupported) -> Self {
        Self {
            inner: Inner::Unsupported(unsupported),
            tolerance: DEFAULT_TOLERANCE,
            epsilon: DEFAULT_EPSILON,
            duration_tolerance: DEFAULT_DURATION_TOLERANCE,
        }
    }

    /// Sets the relative tolerance of numeric comparisons, [`DEFAULT_TOLERANCE`] unless
    /// set.
    ///
    /// Two values `a` and `b` are equal if `|a - b| <= tolerance * max(1, |a|, |b|)`;
    /// otherwise they compare as numbers. So `(= (x) 0.3)` holds after `(x)` was set to
    /// 0.1 and increased by 0.2, and exactly one of `<`, `=` and `>` holds for any pair.
    /// A tolerance of 0 compares exactly; a negative or NaN tolerance counts as 0.
    ///
    /// # Examples
    ///
    /// ```
    /// use pddl::{Domain, Parser, Problem};
    /// use pddl_validate::{ValidationStatus, Validator};
    ///
    /// let domain = Domain::from_str("(define (domain d) (:predicates (done)) (:functions (x))
    ///     (:action add :parameters () :effect (increase (x) 0.2))
    ///     (:action check :parameters () :precondition (= (x) 0.3) :effect (done)))").unwrap();
    /// let problem = Problem::from_str("(define (problem p) (:domain d)
    ///     (:init (= (x) 0.1)) (:goal (done)))").unwrap();
    /// let validator = Validator::new(&domain, &problem).unwrap();
    /// assert_eq!(validator.validate_text("(add) (check)").status(), ValidationStatus::Valid);
    ///
    /// let exact = validator.with_tolerance(0.0);
    /// assert_eq!(exact.validate_text("(add) (check)").status(), ValidationStatus::Invalid);
    /// ```
    #[must_use]
    pub fn with_tolerance(mut self, tolerance: f64) -> Self {
        self.tolerance = if tolerance > 0.0 { tolerance } else { 0.0 };
        self
    }

    /// Sets the least time between two interfering events of a temporal plan,
    /// [`DEFAULT_EPSILON`] (VAL's default) unless set.
    ///
    /// Two events interfere (PDDL 2.1 mutex) when one changes an atom or fluent the other
    /// reads or changes, except two `increase`/`decrease` updates of one fluent. Such
    /// events must be at least `epsilon` apart; at the same time or closer, the plan is
    /// invalid with [`ViolationKind::Interference`]. A negative or NaN epsilon counts
    /// as 0, which forbids only simultaneous interference.
    #[must_use]
    pub fn with_epsilon(mut self, epsilon: f64) -> Self {
        self.epsilon = if epsilon > 0.0 { epsilon } else { 0.0 };
        self
    }

    /// Sets the absolute tolerance of a temporal plan's durations against the actions'
    /// `:duration` constraints, [`DEFAULT_DURATION_TOLERANCE`] unless set.
    ///
    /// A duration `d` satisfies a constraint with bound `b` when `|d - b| <= tolerance`,
    /// and otherwise compares as a number under [`Validator::with_tolerance`]. A negative
    /// or NaN tolerance counts as 0.
    #[must_use]
    pub fn with_duration_tolerance(mut self, tolerance: f64) -> Self {
        self.duration_tolerance = if tolerance > 0.0 { tolerance } else { 0.0 };
        self
    }

    /// Parses `text` as a plan file and validates it.
    ///
    /// A plan file that cannot be read is [`ValidationStatus::Invalid`](crate::ValidationStatus::Invalid)
    /// with [`ViolationKind::PlanSyntax`].
    #[must_use]
    pub fn validate_text(&self, text: &str) -> Report {
        match text.parse::<Plan>() {
            Ok(plan) => self.validate(&plan),
            Err(error) => {
                let violation = Violation {
                    step: None,
                    kind: ViolationKind::PlanSyntax(error),
                };
                Report::new(Outcome::Invalid(violation), Vec::new())
            }
        }
    }

    /// Runs `plan` from the initial state and checks the goal in the final state.
    ///
    /// Validation stops at the first violation, which the report names together with the
    /// step, the failing condition and a repair hint.
    #[must_use]
    pub fn validate(&self, plan: &Plan) -> Report {
        let (model, problem, initial) = match &self.inner {
            Inner::Ready {
                model,
                problem,
                initial,
            } => (model, problem, initial),
            Inner::Unsupported(unsupported) => {
                return Report::new(Outcome::Unsupported(unsupported.clone()), Vec::new());
            }
        };

        let tolerance = self.tolerance;
        let timed = plan.steps().iter().any(|step| step.time().is_some());
        if timed || model.is_temporal(problem) {
            if !model.rules.is_empty() {
                let construct = Construct::DerivedPredicatesInTemporalPlans;
                let unsupported = Unsupported::new(construct, "domain");
                return Report::new(Outcome::Unsupported(unsupported), Vec::new());
            }

            let timeline = temporal::Timeline {
                model,
                problem,
                tolerance,
                epsilon: self.epsilon,
                duration_tolerance: self.duration_tolerance,
            };
            return timeline.run(initial, plan);
        }

        let mut state = initial.clone();
        let mut trace = Vec::new();
        if let Err(error) = derived::close(model, &mut state, tolerance) {
            return stopped(Stop::from(error), None, String::from("init"), trace);
        }

        for (index, step) in plan.steps().iter().enumerate() {
            let at = step_ref(index, step);
            match run_step(model, &mut state, step, tolerance) {
                Ok(mut changes) => {
                    changes.step = Some(at);
                    trace.push(changes);
                }
                Err(stop) => {
                    let location = format!("step {}", at.number);
                    return stopped(stop, Some(at), location, trace);
                }
            }
        }

        #[expect(
            clippy::cast_precision_loss,
            reason = "plans of more than 2^53 steps do not fit in memory"
        )]
        let total_time = plan.len() as f64;
        let evaluator = Evaluator {
            model,
            state: &state,
            tolerance,
            observer: None,
        };

        finish(&evaluator, problem, total_time, trace)
    }
}

/// Checks the goal in the final state and evaluates the metric, with `total-time` set
/// to `total_time`. A metric whose value is undefined leaves the plan valid, without a
/// metric value.
fn finish(
    evaluator: &Evaluator<'_, '_>,
    problem: &Problem,
    total_time: f64,
    trace: Vec<StepTrace>,
) -> Report {
    match evaluator.preconditions(problem.goals(), &mut Scope::new()) {
        Ok(None) => {}
        Ok(Some(unmet)) => {
            let kind = ViolationKind::GoalUnsatisfied(unmet);
            return stopped(Stop::Invalid(kind), None, String::from("goal"), trace);
        }
        Err(error) => return stopped(Stop::from(error), None, String::from("goal"), trace),
    }

    let Some(spec) = problem.metric_spec() else {
        return Report::new(Outcome::Valid, trace);
    };

    match evaluator.metric(spec.expression(), total_time) {
        Ok(value) => Report::new(Outcome::Valid, trace).with_metric(Metric {
            optimization: spec.optimization(),
            value,
        }),
        Err(error) if error.is_unknown() => Report::new(Outcome::Valid, trace),
        Err(error) => stopped(Stop::from(error), None, String::from("metric"), trace),
    }
}

/// Names step `index` (from 0) for diagnostics.
fn step_ref(index: usize, step: &PlanStep) -> StepRef {
    StepRef {
        number: index + 1,
        line: step.line(),
        action: alloc::string::ToString::to_string(step),
        time: step.time(),
    }
}

/// Builds the report of a plan that stopped at `step` (or at the goal, for `None`).
fn stopped(reason: Stop, step: Option<StepRef>, location: String, trace: Vec<StepTrace>) -> Report {
    match reason {
        Stop::Invalid(kind) => Report::new(Outcome::Invalid(Violation { step, kind }), trace),
        Stop::Unsupported(construct) => {
            let unsupported = Unsupported::new(construct, location);
            Report::new(Outcome::Unsupported(unsupported), Vec::new())
        }
    }
}

/// Applies one step to `state` and returns what it changed.
fn run_step(
    model: &Model<'_>,
    state: &mut State,
    step: &PlanStep,
    tolerance: f64,
) -> Result<StepTrace, Stop> {
    let (action, mut scope) = bind(model, step)?;
    if step.duration().is_some() {
        return Err(Stop::Invalid(ViolationKind::UnexpectedDuration));
    }

    let evaluator = Evaluator {
        model,
        state: &*state,
        tolerance,
        observer: None,
    };
    if let Some(unmet) = evaluator.preconditions(action.precondition(), &mut scope)? {
        return Err(Stop::Invalid(ViolationKind::PreconditionUnsatisfied(unmet)));
    }

    let mut changes = Changes::default();
    if let Some(effects) = action.effect() {
        changes.collect(&evaluator, effects, &mut scope)?;
    }

    let trace = changes.apply(state)?;
    derived::close(model, state, tolerance)?;

    Ok(trace)
}

/// Looks up the action of `step` and binds its parameters to the step's objects.
fn bind<'m>(
    model: &'m Model<'_>,
    step: &PlanStep,
) -> Result<(&'m pddl::ActionDefinition, Scope), Stop> {
    let Some(action) = model.actions.get(step.action()) else {
        let kind = ViolationKind::UnknownAction(String::from(step.action()));
        return Err(Stop::Invalid(kind));
    };

    Ok((action, bind_parameters(model, action.parameters(), step)?))
}

/// Binds `parameters` to the objects of `step`, checking arity, objects and types.
fn bind_parameters(
    model: &Model<'_>,
    parameters: &pddl::TypedVariables,
    step: &PlanStep,
) -> Result<Scope, Stop> {
    let invalid = |kind| Err(Stop::Invalid(kind));
    if parameters.len() != step.arguments().len() {
        return invalid(ViolationKind::Arity {
            expected: parameters.len(),
            found: step.arguments().len(),
        });
    }

    let mut scope = Scope::new();
    for (parameter, object) in parameters.iter().zip(step.arguments()) {
        let name = lower(parameter.value());
        if !model.universe.contains(object) {
            return invalid(ViolationKind::UnknownObject {
                parameter: name,
                object: object.clone(),
            });
        }

        if !model.universe.is_of(object, parameter.type_()) {
            return invalid(ViolationKind::TypeMismatch {
                parameter: name,
                object: object.clone(),
                expected: type_text(parameter.type_()),
            });
        }

        scope.push((name, object.clone()));
    }

    Ok(scope)
}

/// Builds the initial state from the problem's `:init`.
///
/// Negative literals are skipped: under the closed-world assumption they hold anyway.
/// A nullary `total-cost` function that `:init` gives no value starts at 0, as Fast
/// Downward assumes. The static check has rejected every element this validator cannot
/// interpret.
fn initial_state(model: &Model<'_>, problem: &Problem) -> State {
    let mut state = State::default();
    if model.functions.get(TOTAL_COST) == Some(&0) {
        let total_cost = Atom::lowered(String::from(TOTAL_COST), Vec::new());
        state.fluents.insert(total_cost, 0.0);
    }

    for element in problem.init().iter() {
        match element {
            InitElement::Literal(Literal::AtomicFormula(AtomicFormula::Predicate(atom))) => {
                let arguments = atom.values().iter().map(|name| lower(name)).collect();
                let atom = Atom::lowered(lower(atom.predicate()), arguments);
                state.facts.insert(atom);
            }
            InitElement::IsValue(term, value) => {
                let arguments = term.names().iter().map(|name| lower(name)).collect();
                let fluent = Atom::lowered(lower(term.symbol()), arguments);
                state.fluents.insert(fluent, **value);
            }
            InitElement::Literal(_) | InitElement::At(..) | InitElement::IsObject(..) => {}
        }
    }

    state
}

/// The function that action costs accumulate in.
const TOTAL_COST: &str = "total-cost";