anchor.diff:                `(|get-value| ,step-ops))))
   
   
  -(defun smt-plan-context ( &key
  -                            operators facts
  -                            state-vars
  -                            ground-actions
  -                            initial-true
  -                            initial-false
  -                            initial-state
  -                            goal
  -                            smt)
  +(defun smt-plan-context (&key
  +                           operators
  +                           facts
  +                           domain
  +                           goal
  +                           smt)
     "Fork an SMT solver and initialize with base plan definitions."
  -  (let* ((operators (when operators
  -                      (load-operators operators)))
  -         (facts (when facts (load-facts facts)))
  -         (type-map (compute-type-map (pddl-operators-types operators)
  -                                     (pddl-facts-objects facts)))
  -         (state-vars (or state-vars
  -                         (create-state-variables (pddl-operators-predicates operators)
  -                                                 type-map)))
  -         (ground-actions (or ground-actions
  -                               (smt-ground-actions (pddl-operators-actions operators)
  -                                                      type-map)))
  -         (initial-true (unless initial-state (or initial-true (pddl-facts-init facts))))
  -         (initial-false (unless initial-state (or initial-false
  -                                                  (set-difference  state-vars initial-true :test #'equal))))
  -         (initial-state (or initial-state
  -                            `(and ,@initial-true
  -                                  ,@(loop for v in initial-false collect `(not ,v)))))
  -
  -         (goal (or goal (pddl-facts-goal facts)))
  +  (let* ((domain (or domain (ground-domain operators facts :goal goal)))
  +         (state-vars (ground-domain-variables domain))
  +         (ground-actions (ground-domain-operators domain))
  +         (initial-state (ground-domain-start domain))
            (n-op (length ground-actions))
            (n-var (length state-vars)))
       (format t "~&ground actions: ~D" n-op)
  
positive.code:                (stmt-list (list)
                   (map nil #'stmt list)))
          ;; Per-step function
          (stmt-list (smt-plan-step-fun state-vars ground-actions))
          ;; initial state
          (stmt-list (smt-plan-step-vars state-vars 0))
          (stmt (smt-assert (rewrite-exp initial-state 0))))
  
        (make-smt-plan-context :smt smt
                               :ground-variables state-vars
                               :ground-actions ground-actions
                               :goal goal
                               :step 0))))
  
  
  
  
  (defun smt-plan ( &key
                      operators facts
                      state-vars
                      ground-actions
                      initial-true
                      initial-false
                      initial-state
                      goal
                      (max-steps 10))
    (let* ((operators (when operators
                        (load-operators operators)))
           (facts (when facts (load-facts facts)))
           (type-map (compute-type-map (pddl-operators-types operators)
                                       (pddl-facts-objects facts)))
           (state-vars (or state-vars
                           (create-state-variables (pddl-operators-predicates operators)
                                                   type-map)))
           (ground-actions (or ground-actions
                                 (smt-ground-actions (pddl-operators-actions operators)
                                                        type-map)))
           (initial-true (unless initial-state (or initial-true (pddl-facts-init facts))))
           (initial-false (unless initial-state (or initial-false
                                                    (set-difference  state-vars initial-true :test #'equal))))
           (initial-state (or initial-state
                              `(and ,@initial-true
                                    ,@(loop for v in initial-false collect `(not ,v)))))
  
           (goal (or goal (pddl-facts-goal facts)))
           (n-op (length ground-actions))
           (n-var (length state-vars)))
      (format t "~&ground actions: ~D" n-op)
      (format t "~&ground states: ~D" n-var)
      (let ((smt (smt-start)))
        (labels ((stmt (exp)
                   ;(print (list 'eval exp))
                   (smt-eval smt exp))
                 (stmt-list (list)
                   (map nil #'stmt list))
                 (plan-step (i)
                   (format t "~&trying step ~D" i)
                   ;; step declarations
                   (stmt-list (smt-plan-step-stmts state-vars ground-actions i))
                   ;; namespace
                   (stmt '(|push| 1))
                   ;; goal assertion
                   (stmt (smt-assert (rewrite-exp goal (1+ i))))
                   ;; check-sat
                   (let ((is-sat (smt-eval smt '(|check-sat|))))
                     (print is-sat)
                     (case is-sat
                       (sat
                        (result i))
                       (unsat
                        ;; pop
                        (stmt '(|pop| 1))
                        (when (< (1+ i) max-steps)
                          (plan-step (1+ i))))
                       (otherwise
                        (error "Unrecognized (check-sat) result: ~A" is-sat)))))
                 (result (i)
                   (let* ((step-ops (smt-plan-step-ops ground-actions (1+ i)))
                          (values (stmt `(|get-value| ,step-ops)))
                          (plan (smt-plan-parse values)))
                     plan)))
          ;; Per-step function
          (stmt-list (smt-plan-step-fun state-vars ground-actions))
          ;; initial state
          (stmt-list (smt-plan-step-vars state-vars 0))
          (stmt (smt-assert (rewrite-exp initial-state 0)))
          (prog1 (plan-step 0)
            (smt-stop smt))))))
  
  
  
  
  
  
negative.code:           ((false |false|)))))
      (map 'list #'cdr (sort plan (lambda (a b) (< (car a) (car b)))))))
  
  
  
  (defstruct smt-plan-context
    smt
    domain
    step
    values)
  
  (defun smt-plan-context-ground-variables (instance)
    (ground-domain-variables (smt-plan-context-domain instance)))
  (defun smt-plan-context-ground-actions (instance)
    (ground-domain-operators (smt-plan-context-domain instance)))
  (defun smt-plan-context-goal (instance)
    (ground-domain-goal (smt-plan-context-domain instance)))
  
  (defun smt-plan-check (cx &key max-steps)
    "Check if a plan exists for the current step, recurse if not."
  