Skip to main content

pddl_validate/
lib.rs

1//! # PDDL plan validation
2//!
3//! An independent plan validator for PDDL, a second opinion next to
4//! [VAL](https://github.com/KCL-Planning/VAL). It implements the published semantics on
5//! the [`pddl`] syntax tree and shares no grounding or search code with any planner, so
6//! it does not share their bugs either.
7//!
8//! ```
9//! use pddl::{Domain, Parser, Problem};
10//! use pddl_validate::{ValidationStatus, Validator, ViolationKind};
11//!
12//! let domain = Domain::from_str(
13//!     "(define (domain logistics) (:requirements :typing)
14//!        (:types truck place)
15//!        (:predicates (at ?t - truck ?p - place))
16//!        (:action drive
17//!          :parameters (?t - truck ?from ?to - place)
18//!          :precondition (at ?t ?from)
19//!          :effect (and (not (at ?t ?from)) (at ?t ?to))))",
20//! ).unwrap();
21//! let problem = Problem::from_str(
22//!     "(define (problem deliver) (:domain logistics)
23//!        (:objects t1 - truck home shop - place)
24//!        (:init (at t1 home))
25//!        (:goal (at t1 shop)))",
26//! ).unwrap();
27//! let validator = Validator::new(&domain, &problem).unwrap();
28//!
29//! let report = validator.validate_text("(drive t1 home shop)\n; cost = 1 (unit cost)");
30//! assert_eq!(report.status(), ValidationStatus::Valid);
31//!
32//! let report = validator.validate_text("(drive t1 shop home)");
33//! let violation = report.violation().unwrap();
34//! assert_eq!(violation.step.as_ref().unwrap().number, 1);
35//! assert!(matches!(violation.kind, ViolationKind::PreconditionUnsatisfied(_)));
36//! assert_eq!(violation.advice(),
37//!     "(drive t1 shop home) has an unsatisfied precondition at step 1: (Set (at t1 shop) to true)");
38//! ```
39//!
40//! ## Scope
41//!
42//! - **Classical plans** (level 1): STRIPS, typing, negative and disjunctive
43//!   preconditions, equality, `exists`, `forall`, `imply`, conditional effects and
44//!   stratified derived predicates.
45//! - **Numeric plans** (level 2): numeric fluents in conditions and effects (`assign`,
46//!   `increase`, `decrease`, `scale-up`, `scale-down`), action costs and the problem's
47//!   `:metric`, which [`Report::metric`] returns for a valid plan.
48//!
49//! - **Temporal plans** (level 3): durative actions with `at start`, `over all` and
50//!   `at end` conditions and effects, timestamped plans (`t: (action args) [d]`),
51//!   duration constraints and inequalities, conditional effects with `at start` or
52//!   `at end` conditions, linear continuous effects (`#t`), timed initial literals, the
53//!   epsilon separation of interfering events ([`Validator::with_epsilon`]) and
54//!   `total-time` as the makespan.
55//!
56//! - **PDDL+** (level 4): processes, which run while their precondition holds and change
57//!   fluents linearly, and events, which fire as soon as their precondition holds,
58//!   in cascades of up to 1000 rounds at one time. The validator finds the times at
59//!   which processes switch and events trigger exactly, at the roots of the linear
60//!   conditions.
61//!
62//! Every construct outside this scope makes the report
63//! [`ValidationStatus::Unsupported`], never valid: continuous change whose rate depends
64//! on a changing fluent (such as acceleration), conditions that are not linear in time,
65//! discrete effects of processes, dynamics that switch without end, derived predicates
66//! in temporal plans, preferences, constraints and object fluents.
67//!
68//! ## Semantics
69//!
70//! - Names are case-insensitive. Reports show them in ASCII lowercase.
71//! - Closed world: an atom not in the state is false.
72//! - A step's precondition and the conditions of its `when` effects are evaluated in the
73//!   state before the step. The next state is the old one minus deleted atoms plus added
74//!   ones, so an atom that a step both deletes and adds stays true.
75//! - Derived predicates are the least fixpoint of their rules, stratum by stratum, and are
76//!   recomputed after every step.
77//! - Numbers are `f64`. Comparisons allow a relative tolerance
78//!   ([`Validator::with_tolerance`], [`DEFAULT_TOLERANCE`]). A comparison that reads a
79//!   fluent without a value, divides by zero or leaves the finite range is unknown, and
80//!   conditions follow three-valued (Kleene) logic: `(or (p) (> (f) 0))` holds if `(p)`
81//!   holds, and `(and (q) (> (f) 0))` fails on `(q)` if `(q)` is false. A precondition
82//!   or goal that is still unknown makes the plan invalid, and so does an effect or
83//!   `when` condition that reads an undefined value. A derived predicate whose rule is
84//!   unknown is not derived. A metric with an undefined value leaves the plan valid,
85//!   without a [`Report::metric`]. A nullary `total-cost` without an initial value
86//!   starts at 0.
87//! - Updates of one fluent in one step combine if all are `increase` or `decrease`;
88//!   any other combination is a [`ViolationKind::ConflictingUpdates`].
89//! - In a metric, `total-time` is the number of steps of a sequential plan and the
90//!   makespan of a temporal one.
91//! - Temporal plans follow PDDL 2.1 and VAL. Events at one time form a happening whose
92//!   conditions are checked in the state before it and whose effects apply at once.
93//!   `over all` conditions must hold strictly between an action's start and end, and
94//!   interfering events must be at least epsilon apart. Durations satisfy their
95//!   constraints within an absolute tolerance ([`Validator::with_duration_tolerance`],
96//!   [`DEFAULT_DURATION_TOLERANCE`]), and a ground durative action must not overlap itself.
97//! - PDDL+ work is bounded: at most 100 000 segments (process switches and event
98//!   firings) between two plan happenings and 1000 event rounds at one time. A model past
99//!   either bound, even a legal but very busy one, is reported unsupported as
100//!   [`Construct::ZenoDynamics`], never valid.
101//! - Validation stops at the first violation. Steps count from 1, as in VAL.
102//!
103//! The crate is `no_std` with `alloc`. That is nominal for now: the `pddl` crate it reads
104//! needs `std`.
105
106#![no_std]
107#![forbid(unsafe_code)]
108#![deny(
109    clippy::unwrap_used,
110    clippy::expect_used,
111    clippy::indexing_slicing,
112    clippy::panic
113)]
114#![cfg_attr(docsrs, feature(doc_cfg))]
115
116extern crate alloc;
117
118mod atom;
119mod derived;
120mod effects;
121mod error;
122mod eval;
123mod model;
124mod plan;
125mod render;
126mod report;
127mod state;
128mod validator;
129
130pub use atom::Atom;
131pub use error::{ModelError, ModelErrorKind};
132pub use plan::{Plan, PlanError, PlanErrorKind, PlanStep};
133pub use report::{
134    Construct, FluentChange, Metric, Outcome, Report, StepRef, StepTrace, Unmet, Unsupported,
135    Violation, ViolationKind, MAX_ALTERNATIVES,
136};
137pub use validator::{Validator, DEFAULT_DURATION_TOLERANCE, DEFAULT_EPSILON, DEFAULT_TOLERANCE};
138
139/// The overall result of validating a plan.
140///
141/// [`ValidationStatus::Unsupported`] is distinct from both other outcomes: a plan that uses
142/// constructs the validator cannot interpret is never reported as valid.
143#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
144pub enum ValidationStatus {
145    /// The plan is executable and reaches the goal.
146    Valid,
147    /// The plan violates the domain or problem semantics.
148    Invalid,
149    /// The plan uses a construct the validator does not support.
150    Unsupported,
151}