sicp.io
4.3.2 · Implementing the amb evaluator

Failure resumes the next saved alternative.

An explicit evaluator passes a success continuation and a failure continuation through guest evaluation. Amb saves remaining choices in the failure path.

Guiding question

How can an evaluator turn failure from a terminal error into a request to resume an earlier choice point?

  • Evaluate guest expressions with explicit success and failure continuations
  • Represent guest compound procedures with lexical environments
  • Propagate alternative continuations through operator and operand evaluation
  • Implement amb by trying each choice with a failure path to the rest
  • Implement require by invoking the current alternative when its predicate is false
  • Collect every finite solution or stop at an explicit solution limit
  • Keep omitted assignment rollback and fairness policies visible

ambeval receives expression, environment, succeed, and fail. A deterministic expression calls succeed with its value and the failure continuation that should be used if later work rejects that value. An amb form evaluates its first choice and replaces failure with a procedure that tries the remaining choices. Operand evaluation threads these continuations from left to right, so a failure inside a procedure body can revisit an earlier nondeterministic argument.

require evaluates its predicate in the same continuation system. A true predicate succeeds with ok; a false predicate invokes the next predicate alternative, which ultimately returns to the most recent amb choice. all-values repeatedly calls the next alternative supplied by each success. The first run exhausts the selected finite search and reports complete. The second deliberately stops after four solutions and reports truncated. The lesson evaluator covers ordered amb choices, require, procedure application, and success or failure continuation transfer.

SICP code7,667 of 1,048,576 UTF-8 bytes
Examples
Result
Output
Value
Diagnostic
Execution trace0 / 0 events
    Programs run in the browser with their result and execution trace.
    Expected result

    The pair search returns (complete ((1 4) (2 3))). The capped triple search returns (truncated ((1 2 3) (1 2 4) (1 2 5) (1 3 4))).

    Trace focus

    Follow each amb choice installing a failure continuation for the remaining choices. Then trace get-arguments as a rejected require in the body resumes a later third argument, later second argument, or later first argument. The search stops when it exhausts its choices or reaches the configured solution cap.

    Try it yourself

    Change the program and compare the result.

    Change the pair program so left and right are chosen from 1 through 6, require their product to be 12, and collect every solution where left is smaller. Predict the solution order before running it.

    Show hint

    The left-to-right amb order tests all right choices for left 1 before moving to left 2. Only ordered factor pairs survive both require forms.

    Complete this lesson

    0 of 23 lessons complete in this chapter0%