// BDDL, the BEHAVIOR Domain Definition Language: a task is a problem with typed objects, an initial state // (:init) and a goal (:goal), written as s-expressions, as in BEHAVIOR-1K: // // (define (problem picking_up_trash-0) // (:domain xlerobot-kitchen) // (:objects can__of__soda.n.01_1 can__of__soda.n.01_2 - can__of__soda.n.01 ashcan.n.01_1 - ashcan.n.01) // (:init (ontop can__of__soda.n.01_1 countertop.n.01_1) ...) // (:goal (and (forall (?can__of__soda.n.01 - can__of__soda.n.01) (inside ?can__of__soda.n.01 ?ashcan.n.01_1))))) // // This module parses that and grounds a goal into the literals that are scored: the goal's conjuncts, with // each `forall` expanded over its objects, so partial credit is the share of these that hold (the // BEHAVIOR Challenge's score). It knows nothing about the simulation: predicates are evaluated by a // function the caller gives. /** Tokens: parentheses and atoms; `;` starts a comment to the end of the line. */ function tokens(text) { return text.replace(/;[^\n]*/g, " ").replace(/\(/g, " ( ").replace(/\)/g, " ) ").split(/\s+/).filter(Boolean); } /** The s-expression in `text` as nested arrays of strings. */ export function parseSexpr(text) { const list = tokens(text); let i = 0; const read = () => { const t = list[i++]; if (t === undefined) throw new Error("BDDL: unexpected end"); if (t === ")") throw new Error("BDDL: unexpected )"); if (t !== "(") return t; const out = []; while (list[i] !== ")") { if (i >= list.length) throw new Error("BDDL: missing )"); out.push(read()); } i++; return out; }; const expr = read(); if (i < list.length) throw new Error("BDDL: text after the problem"); return expr; } /** Typed names, "a b - type c - type2", as {name: type}. */ function typed(items) { const out = {}; let pending = []; for (let k = 0; k < items.length; k++) { if (items[k] === "-") { for (const n of pending) out[n] = items[k + 1]; pending = []; k++; } else pending.push(items[k]); } for (const n of pending) out[n] = "object"; return out; } /** A BDDL problem: {name, domain, objects: {name: type}, init: [expr], goal: expr, text}. */ export function parseProblem(text) { const e = parseSexpr(text); if (e[0] !== "define" || e[1]?.[0] !== "problem") throw new Error("BDDL: not a (define (problem ...))"); const problem = { name: e[1][1], domain: null, objects: {}, init: [], goal: ["and"], text }; for (const part of e.slice(2)) { const [head, ...rest] = part; if (head === ":domain") problem.domain = rest[0]; else if (head === ":objects") problem.objects = typed(rest); else if (head === ":init") problem.init = rest; else if (head === ":goal") problem.goal = rest[0] ?? ["and"]; } return problem; } /** The type (category) of an instance name: "can__of__soda.n.01_2" -> "can__of__soda.n.01". */ export const typeOf = (name) => name.replace(/_\d+$/, ""); /** An expression with variables bound (`?x` -> object) and constants' leading `?` dropped. */ function bind(expr, vars, objects) { if (typeof expr === "string") { if (expr in vars) return vars[expr]; if (expr.startsWith("?") && expr.slice(1) in objects) return expr.slice(1); return expr; } if (expr[0] === "forall" || expr[0] === "exists") { // a quantifier's own variable stays a variable inside it const [, decl, body] = expr; const own = new Set(Object.keys(typed(decl))); const inner = Object.fromEntries(Object.entries(vars).filter(([k]) => !own.has(k))); return [expr[0], decl, bind(body, inner, objects)]; } return expr.map((x) => bind(x, vars, objects)); } /** The objects of a quantifier's type. */ function domainOf(decl, objects) { const [[variable, type]] = Object.entries(typed(decl)); return { variable, members: Object.keys(objects).filter((n) => objects[n] === type || typeOf(n) === type) }; } /** * The goal's scored literals: its conjuncts, `and` flattened and `forall` expanded over its objects, * each a ground expression (a literal, a negated one, or an `or`/`exists` kept whole). */ export function groundGoal(problem) { const out = []; const visit = (expr) => { if (expr[0] === "and") for (const x of expr.slice(1)) visit(x); else if (expr[0] === "forall") { const { variable, members } = domainOf(expr[1], problem.objects); for (const m of members) visit(bind(expr[2], { [variable]: m }, problem.objects)); } else out.push(expr); }; visit(bind(problem.goal, {}, problem.objects)); return out; } /** * Whether a ground expression holds, with `holds(predicate, args)` for the literals; quantifiers range * over the problem's objects. */ export function evaluate(expr, holds, objects) { const [head, ...rest] = expr; switch (head) { case "and": return rest.every((x) => evaluate(x, holds, objects)); case "or": return rest.some((x) => evaluate(x, holds, objects)); case "not": return !evaluate(rest[0], holds, objects); case "imply": return !evaluate(rest[0], holds, objects) || evaluate(rest[1], holds, objects); case "forall": case "exists": { const { variable, members } = domainOf(rest[0], objects); const each = (m) => evaluate(bind(rest[1], { [variable]: m }, objects), holds, objects); return head === "forall" ? members.every(each) : members.some(each); } default: return holds(head, rest); } } /** An expression written back as BDDL. */ export function show(expr) { return typeof expr === "string" ? expr : `(${expr.map(show).join(" ")})`; }