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
//! Serde support: PDDL domains and problems as JSON, YAML, TOML, NOVA or any
//! other self-describing format.
//!
//! Every PDDL type implements [`Serialize`], and every type with
//! a [`Parser`] also implements [`Deserialize`].
//!
//! # Schema
//!
//! The schema follows PDDL, so a PDDL reader can read it without a reference:
//!
//! - Names are strings. Variables keep their `?`, which tells them apart from
//!   constants: `"truck"`, `"?t"`. Numbers are numbers. PDDL has no negative
//!   literals, so in a formula a negative number reads back as a negation.
//! - Atoms and function terms are arrays, the symbol first: `["at", "?t", "depot"]`,
//!   `["total-cost"]`, and `["=", "?a", "?b"]` for equality.
//! - Every other form `(op a b ...)` is an object with the keyword as its only key
//!   and the arguments in PDDL order: `{"imply": [a, b]}`, `{"forall": [vars, gd]}`,
//!   `{"increase": [["total-cost"], 1]}`, `{">=": [a, b]}`. Forms with one argument
//!   take it directly: `{"not": ["at", "?t"]}`, `{"at start": gd}`, `{"over all": gd}`.
//!   `{"-": x}` is negation and `{"-": [a, b]}` is subtraction.
//! - A conjunction of one is written as that member alone, as in PDDL.
//! - A preference is `{"preference": gd}`, with an extra `"name"` key when it
//!   is named: `{"preference": gd, "name": "p"}`.
//! - Typed lists are objects from name to type, in order: `{"?t": "truck"}`.
//!   `either` types are arrays: `{"?x": ["truck", "plane"]}`.
//!
//! A domain:
//!
//! ```json
//! {
//!   "domain": "logistics",
//!   "requirements": ["strips", "typing", "action-costs"],
//!   "types": {"truck": "object", "location": "object"},
//!   "predicates": {"at": {"?t": "truck", "?l": "location"}},
//!   "functions": {
//!     "distance": {"?a": "location", "?b": "location"},
//!     "total-cost": {},
//!     "home": {"parameters": {"?t": "truck"}, "type": "location"}
//!   },
//!   "actions": {
//!     "drive": {
//!       "parameters": {"?t": "truck", "?from": "location", "?to": "location"},
//!       "precondition": {"and": [["at", "?t", "?from"], {"not": ["=", "?from", "?to"]}]},
//!       "effect": {"and": [
//!         {"not": ["at", "?t", "?from"]},
//!         ["at", "?t", "?to"],
//!         {"increase": [["total-cost"], ["distance", "?from", "?to"]]}
//!       ]}
//!     }
//!   },
//!   "durative-actions": {
//!     "load": {
//!       "parameters": {"?t": "truck"},
//!       "duration": {"=": ["?duration", 2]},
//!       "condition": {"at start": ["at", "?t", "depot"]},
//!       "effect": {"at end": ["loaded", "?t"]}
//!     }
//!   },
//!   "derived": [
//!     {"predicate": "parked", "parameters": {"?t": "truck"}, "condition": ["at", "?t", "depot"]}
//!   ]
//! }
//! ```
//!
//! Functions of type `number` map to their parameters alone. Other functions spell
//! out `parameters` and `type`. Sections that are empty in PDDL are left out.
//!
//! A problem:
//!
//! ```json
//! {
//!   "problem": "p1",
//!   "domain": "logistics",
//!   "objects": {"t1": "truck", "a": "location"},
//!   "init": [["at", "t1", "a"], {"=": [["distance", "a", "depot"], 5]}, {"at": [10, ["open", "depot"]]}],
//!   "goal": ["at", "t1", "depot"],
//!   "metric": {"minimize": ["total-cost"]}
//! }
//! ```
//!
//! # NOVA schema
//!
//! [`schema/pddl.novs`](https://github.com/sunsided/pddl-rs/blob/main/schema/pddl.novs)
//! describes this schema for [NOVA](https://novafile.dev/) documents written with
//! `novafile::serde`, down to the formulas: goals, effects, expressions, duration
//! constraints, timed and constraint goals, `init` elements and metrics follow the
//! PDDL 3.1 grammar, each keyword form a record with the keyword as its only field.
//! NOVA can't tell a variable from a name, so `?duration`, `#t` and `total-time`
//! are plain names there. `nvf validate file.nova --schema schema/pddl.novs` checks a whole
//! [`PddlFile`](crate::PddlFile), `{domains: [...], problems: [...]}`.
//!
//! # Deserialization
//!
//! Deserializing rebuilds the PDDL text and runs the [`Parser`] on it, so the input is
//! checked against the grammar and yields exactly what parsing the PDDL would. Where
//! the Rust types can represent one PDDL form in several ways, the result is the
//! parser's choice, and actions, durative actions, derived predicates, processes and
//! events come back in that order. Error messages quote the rebuilt PDDL.
//!
//! The format must be self-describing; formats like `bincode` aren't supported.
//!
//! # Key order
//!
//! Typed lists are objects, so the order of an action's parameters is the order of
//! the object's keys. Parsing from text keeps it, but some in-memory maps sort their
//! keys: enable `serde_json`'s and `toml`'s `preserve_order` features when going
//! through `serde_json::Value` or `toml::Table`, or when writing TOML.

mod actions;
mod durative;
mod expressions;
mod goals;
mod structure;
mod terms;

use crate::parsers::Parser;
use crate::PreferenceName;
use ::serde::de::{self, Deserializer, MapAccess, SeqAccess, Visitor};
use ::serde::ser::{SerializeMap, SerializeSeq, Serializer};
use ::serde::{Deserialize, Serialize};
use std::collections::HashSet;
use std::fmt;

/// The keys of an object being serialized, to fail on one that repeats.
///
/// A format's map keeps only one entry per key (`serde_json::Value` keeps the
/// last), so a repeated name, e.g. two `(:action a ...)`, would be lost silently.
#[derive(Default)]
pub(crate) struct UniqueKeys<'a>(HashSet<&'a str>);

impl<'a> UniqueKeys<'a> {
    /// Records `key`.
    ///
    /// # Errors
    /// Fails with a custom serialization error if `key` was recorded before.
    pub(crate) fn insert<E: ::serde::ser::Error>(&mut self, key: &'a str) -> Result<(), E> {
        if self.0.insert(key) {
            Ok(())
        } else {
            Err(E::custom(format_args!(
                "duplicate key `{key}`: an object can hold it only once"
            )))
        }
    }
}

/// Serializes as `{key: value}`.
pub(crate) struct Keyed<'a, V: ?Sized>(pub &'a str, pub &'a V);

impl<V: Serialize + ?Sized> Serialize for Keyed<'_, V> {
    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        let mut map = serializer.serialize_map(Some(1))?;
        map.serialize_entry(self.0, self.1)?;
        map.end()
    }
}

/// Serializes as `[head, items...]`, the shape of atoms and function terms.
pub(crate) struct Head<'a, H: ?Sized, T>(pub &'a H, pub &'a [T]);

impl<H: Serialize + ?Sized, T: Serialize> Serialize for Head<'_, H, T> {
    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        let mut seq = serializer.serialize_seq(Some(1 + self.1.len()))?;
        seq.serialize_element(self.0)?;
        for item in self.1 {
            seq.serialize_element(item)?;
        }
        seq.end()
    }
}

/// Serializes `items` as `{"and": items}`, or a single item alone.
pub(crate) fn conjunction<S: Serializer, T: Serialize>(
    serializer: S,
    items: &[T],
) -> Result<S::Ok, S::Error> {
    match items {
        [one] => one.serialize(serializer),
        _ => Keyed("and", items).serialize(serializer),
    }
}

/// Serializes `{"preference": goal}`, with a `"name"` key if the preference is named.
pub(crate) fn preference<S: Serializer, G: Serialize + ?Sized>(
    serializer: S,
    name: Option<&PreferenceName>,
    goal: &G,
) -> Result<S::Ok, S::Error> {
    let mut map = serializer.serialize_map(Some(1 + usize::from(name.is_some())))?;
    map.serialize_entry("preference", goal)?;
    if let Some(name) = name {
        map.serialize_entry("name", name)?;
    }
    map.end()
}

/// Deserializes `T` by rebuilding its PDDL text with `render` and parsing it.
pub(crate) fn from_pddl<'de, D, T>(deserializer: D, render: Render) -> Result<T, D::Error>
where
    D: Deserializer<'de>,
    T: Parser<Item = T>,
{
    parse_value(&Value::deserialize(deserializer)?, render)
}

/// Like [`from_pddl`], but an empty array or object is the default value, for
/// sections that PDDL can't write empty, such as `(:predicates)`.
pub(crate) fn from_pddl_or_default<'de, D, T>(
    deserializer: D,
    render: Render,
) -> Result<T, D::Error>
where
    D: Deserializer<'de>,
    T: Parser<Item = T> + Default,
{
    let value = Value::deserialize(deserializer)?;
    match &value {
        Value::Seq(items) if items.is_empty() => Ok(T::default()),
        Value::Map(entries) if entries.is_empty() => Ok(T::default()),
        _ => parse_value(&value, render),
    }
}

/// Rebuilds the PDDL text of `value` with `render` and parses it as `T`.
fn parse_value<T: Parser<Item = T>, E: de::Error>(value: &Value, render: Render) -> Result<T, E> {
    let mut text = String::new();
    render(&mut text, value).map_err(E::custom)?;
    T::from_str(&text).map_err(|e| E::custom(format!("{e} in `{text}`")))
}

/// Implements `Deserialize` through [`from_pddl_or_default`] with the given renderer.
macro_rules! deserialize_from_pddl_or_default {
    ($($ty:ty => $render:expr),+ $(,)?) => {$(
        impl<'de> ::serde::Deserialize<'de> for $ty {
            fn deserialize<D: ::serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
                $crate::serde::from_pddl_or_default(deserializer, $render)
            }
        }
    )+};
}
pub(crate) use deserialize_from_pddl_or_default;

/// Implements `Deserialize` through [`from_pddl`] with the given renderer.
macro_rules! deserialize_from_pddl {
    ($($ty:ty => $render:expr),+ $(,)?) => {$(
        impl<'de> ::serde::Deserialize<'de> for $ty {
            fn deserialize<D: ::serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
                $crate::serde::from_pddl(deserializer, $render)
            }
        }
    )+};
}
pub(crate) use deserialize_from_pddl;

/// Implements `Serialize` as the string the type derefs to, for names.
macro_rules! serialize_deref {
    ($($ty:ty),+ $(,)?) => {$(
        impl ::serde::Serialize for $ty {
            fn serialize<S: ::serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
                (**self).serialize(serializer)
            }
        }
    )+};
}
pub(crate) use serialize_deref;

/// Implements `Serialize` as the type's `Display` output, for keywords.
macro_rules! serialize_display {
    ($($ty:ty),+ $(,)?) => {$(
        impl ::serde::Serialize for $ty {
            fn serialize<S: ::serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
                serializer.collect_str(self)
            }
        }
    )+};
}
pub(crate) use serialize_display;

/// A deserialized value in any self-describing format, before it is turned into PDDL.
#[derive(Debug)]
pub(crate) enum Value {
    /// A string: a name, variable or keyword.
    Str(String),
    /// A number, as text, so that it reaches the parser unchanged.
    Num(String),
    /// An array.
    Seq(Vec<Value>),
    /// An object, its entries in order.
    Map(Vec<(String, Value)>),
}

impl<'de> Deserialize<'de> for Value {
    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        deserializer.deserialize_any(ValueVisitor)
    }
}

/// Builds a [`Value`] from whatever the format holds.
struct ValueVisitor;

impl<'de> Visitor<'de> for ValueVisitor {
    type Value = Value;

    fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str("a string, number, array or object")
    }

    fn visit_str<E: de::Error>(self, v: &str) -> Result<Value, E> {
        Ok(Value::Str(v.to_owned()))
    }

    fn visit_i64<E: de::Error>(self, v: i64) -> Result<Value, E> {
        Ok(Value::Num(v.to_string()))
    }

    fn visit_u64<E: de::Error>(self, v: u64) -> Result<Value, E> {
        Ok(Value::Num(v.to_string()))
    }

    fn visit_f64<E: de::Error>(self, v: f64) -> Result<Value, E> {
        if v.is_finite() {
            Ok(Value::Num(v.to_string()))
        } else {
            Err(E::custom(format!("{v} is not a finite number")))
        }
    }

    fn visit_seq<A: SeqAccess<'de>>(self, mut seq: A) -> Result<Value, A::Error> {
        let mut items = Vec::new();
        while let Some(item) = seq.next_element()? {
            items.push(item);
        }
        Ok(Value::Seq(items))
    }

    fn visit_map<A: MapAccess<'de>>(self, mut map: A) -> Result<Value, A::Error> {
        let mut entries = Vec::new();
        while let Some(entry) = map.next_entry()? {
            entries.push(entry);
        }
        Ok(Value::Map(entries))
    }
}

/// Writes the PDDL text of a [`Value`], or explains why it has none.
pub(crate) type Render = fn(&mut String, &Value) -> Result<(), String>;

/// The keys written as two words in PDDL.
const TWO_WORD_KEYS: &[&str] = &["at start", "at end", "over all"];

/// The keywords that take a single argument, even when it is an array such as an atom.
const UNARY_KEYS: &[&str] = &[
    "not",
    "at start",
    "at end",
    "over all",
    "always",
    "sometime",
    "at-most-once",
    "preference",
    "is-violated",
    "minimize",
    "maximize",
];

/// Writes a name, keyword or variable, refusing anything that the reader
/// wouldn't take as exactly this one atom: a parenthesis or space would change
/// the structure of the text, and an inner `?` or `:` or a leading operator
/// would split it, e.g. `a?b` into the constant `a` and the variable `?b`.
pub(crate) fn atom(out: &mut String, s: &str) -> Result<(), String> {
    let single_atom = crate::parsers::sexpr::read(s).is_ok_and(|forest| {
        forest.unbalanced.is_none()
            && matches!(forest.nodes.as_slice(), [node] if node.atom() == Some(s))
    });
    if !single_atom {
        return Err(format!("`{s}` is not a valid PDDL name"));
    }
    out.push_str(s);
    Ok(())
}

/// Writes a formula or term: strings and numbers as atoms, arrays as `( ... )`,
/// objects as keyword forms or, if their keys are variables, as typed lists.
pub(crate) fn formula(out: &mut String, value: &Value) -> Result<(), String> {
    match value {
        Value::Str(s) => atom(out, s),
        // PDDL has no negative literals: `-5` is the negation `(- 5)`.
        Value::Num(n) => {
            match n.strip_prefix('-') {
                Some(magnitude) => {
                    out.push_str("(- ");
                    out.push_str(magnitude);
                    out.push(')');
                }
                None => out.push_str(n),
            }
            Ok(())
        }
        Value::Seq(items) => {
            out.push('(');
            spaced(out, items, formula)?;
            out.push(')');
            Ok(())
        }
        Value::Map(entries) if entries.iter().all(|(key, _)| key.starts_with('?')) => {
            out.push('(');
            typed_list(out, entries)?;
            out.push(')');
            Ok(())
        }
        Value::Map(entries) => keyed(out, entries),
    }
}

/// Writes `items` separated by spaces.
pub(crate) fn spaced(out: &mut String, items: &[Value], render: Render) -> Result<(), String> {
    for (i, item) in items.iter().enumerate() {
        if i > 0 {
            out.push(' ');
        }
        render(out, item)?;
    }
    Ok(())
}

/// Writes a keyword form `{"op": args}` as `(op args...)`.
fn keyed(out: &mut String, entries: &[(String, Value)]) -> Result<(), String> {
    let (key, value, name) = match entries {
        [(key, value)] => (key.as_str(), value, None),
        [(a, goal), (b, name)] | [(b, name), (a, goal)] if a == "preference" && b == "name" => {
            ("preference", goal, Some(name))
        }
        _ => {
            let keys: Vec<_> = entries.iter().map(|(key, _)| key.as_str()).collect();
            return Err(format!(
                "expected an object with a single keyword, found keys {keys:?}"
            ));
        }
    };
    out.push('(');
    if TWO_WORD_KEYS.contains(&key) {
        out.push_str(key);
    } else {
        atom(out, key)?;
    }
    if let Some(name) = name {
        out.push(' ');
        string(out, name)?;
    }
    match value {
        Value::Seq(items) if !takes_one(key, items) => {
            for item in items {
                out.push(' ');
                formula(out, item)?;
            }
        }
        _ => {
            out.push(' ');
            formula(out, value)?;
        }
    }
    out.push(')');
    Ok(())
}

/// Whether the keyword's array value is its single argument rather than its
/// argument list: always for unary keywords, and for `-` when the array is a
/// function term, i.e. `{"-": ["f", "?x"]}` negates `(f ?x)`.
fn takes_one(key: &str, items: &[Value]) -> bool {
    UNARY_KEYS.contains(&key)
        || (key == "-"
            && matches!(items.first(), Some(Value::Str(s)) if !s.starts_with(['?', '#'])))
}

/// Writes the entries of a typed list, `name - type`, separated by spaces.
pub(crate) fn typed_list(out: &mut String, entries: &[(String, Value)]) -> Result<(), String> {
    for (i, (name, r#type)) in entries.iter().enumerate() {
        if i > 0 {
            out.push(' ');
        }
        atom(out, name)?;
        out.push_str(" - ");
        type_(out, r#type)?;
    }
    Ok(())
}

/// Writes a type: a name, or `(either ...)` for an array of names.
pub(crate) fn type_(out: &mut String, value: &Value) -> Result<(), String> {
    match value {
        Value::Str(s) => atom(out, s),
        Value::Seq(items) => {
            out.push_str("(either");
            for item in items {
                out.push(' ');
                string(out, item)?;
            }
            out.push(')');
            Ok(())
        }
        _ => Err(format!("expected a type, found {}", describe(value))),
    }
}

/// Writes a value that must be a string.
pub(crate) fn string(out: &mut String, value: &Value) -> Result<(), String> {
    atom(out, as_str(value)?)
}

/// The string in `value`.
pub(crate) fn as_str(value: &Value) -> Result<&str, String> {
    match value {
        Value::Str(s) => Ok(s),
        _ => Err(format!("expected a string, found {}", describe(value))),
    }
}

/// The items of the array in `value`.
pub(crate) fn as_seq(value: &Value) -> Result<&[Value], String> {
    match value {
        Value::Seq(items) => Ok(items),
        _ => Err(format!("expected an array, found {}", describe(value))),
    }
}

/// The key and value of an object with exactly one entry.
pub(crate) fn single_entry(value: &Value) -> Result<(&str, &Value), String> {
    match as_map(value)? {
        [(key, value)] => Ok((key, value)),
        _ => Err("expected an object with a single entry".to_owned()),
    }
}

/// The entries of the object in `value`.
pub(crate) fn as_map(value: &Value) -> Result<&[(String, Value)], String> {
    match value {
        Value::Map(entries) => Ok(entries),
        _ => Err(format!("expected an object, found {}", describe(value))),
    }
}

/// A short description of `value` for error messages.
fn describe(value: &Value) -> String {
    match value {
        Value::Str(s) => format!("the string `{s}`"),
        Value::Num(n) => format!("the number {n}"),
        Value::Seq(_) => "an array".to_owned(),
        Value::Map(_) => "an object".to_owned(),
    }
}