How to resolve the algorithm Amb step by step in the Seed7 programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Amb step by step in the Seed7 programming language

Table of Contents

Problem Statement

Define and give an example of the Amb operator. The Amb operator (short for "ambiguous") expresses nondeterminism. This doesn't refer to randomness (as in "nondeterministic universe") but is closely related to the term as it is used in automata theory ("non-deterministic finite automaton"). The Amb operator takes a variable number of expressions (or values if that's simpler in the language) and yields a correct one which will satisfy a constraint in some future computation, thereby avoiding failure. Problems whose solution the Amb operator naturally expresses can be approached with other tools, such as explicit nested iterations over data sets, or with pattern matching. By contrast, the Amb operator appears integrated into the language. Invocations of Amb are not wrapped in any visible loops or other search patterns; they appear to be independent. Essentially Amb(x, y, z) splits the computation into three possible futures: a future in which the value x is yielded, a future in which the value y is yielded and a future in which the value z is yielded. The future which leads to a successful subsequent computation is chosen. The other "parallel universes" somehow go away. Amb called with no arguments fails. For simplicity, one of the domain values usable with Amb may denote failure, if that is convenient. For instance, it is convenient if a Boolean false denotes failure, so that Amb(false) fails, and thus constraints can be expressed using Boolean expressions like Amb(x * y == 8) which unless x and y add to four. A pseudo-code program which satisfies this constraint might look like: The output is 2 4 because Amb(1, 2, 3) correctly chooses the future in which x has value 2, Amb(7, 6, 4, 5) chooses 4 and consequently Amb(x * y = 8) produces a success. Alternatively, failure could be represented using strictly Amb(): Or else Amb could take the form of two operators or functions: one for producing values and one for enforcing constraints: where Ambassert behaves like Amb() if the Boolean expression is false, otherwise it allows the future computation to take place, without yielding any value. The task is to somehow implement Amb, and demonstrate it with a program which chooses one word from each of the following four sets of character strings to generate a four-word sentence: The constraint to be satisfied is that the last character of each word (other than the last) is the same as the first character of its successor. The only successful sentence is "that thing grows slowly"; other combinations do not satisfy the constraint and thus fail. The goal of this task isn't to simply process the four lists of words with explicit, deterministic program flow such as nested iteration, to trivially demonstrate the correct output. The goal is to implement the Amb operator, or a facsimile thereof that is possible within the language limitations.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Amb step by step in the Seed7 programming language

Source code in the seed7 programming language

$ include "seed7_05.s7i";

const type: setListType is array array string;

const func array string: amb (in string: word1, in setListType: listOfSets) is func
  result
    var array string: ambResult is 0 times "";
  local
    var string: word2 is "";
  begin
    for word2 range listOfSets[1] do
      if length(ambResult) = 0 and word1[length(word1) len 1] = word2[1 len 1] then
        if length(listOfSets) = 1 then
          ambResult := [] (word1) & [] (word2);
        else
          ambResult := amb(word2, listOfSets[2 ..]);
          if length(ambResult) <> 0 then
            ambResult := [] (word1) & ambResult;
          end if;
        end if;
      end if;
    end for;
  end func;

const func array string: amb (in setListType: listOfSets) is func
  result
    var array string: ambResult is 0 times "";
  local
    var string: word1 is "";
  begin
    for word1 range listOfSets[1] do
      if length(ambResult) = 0 then
        ambResult := amb(word1, listOfSets[2 ..]);
      end if;
    end for;
  end func;

const proc: main is func
  local
    var array string: ambResult is 0 times "";
    var string: word is "";
  begin
    ambResult := amb([] ([] ("the", "that", "a"),
                         [] ("frog", "elephant", "thing"),
                         [] ("walked", "treaded", "grows"),
                         [] ("slowly", "quickly")));
    for word range ambResult do
      write(word <& " ");
    end for;
    writeln;
  end func;

  

You may also check:How to resolve the algorithm Spelling of ordinal numbers step by step in the Sidef programming language
You may also check:How to resolve the algorithm Spinning rod animation/Text step by step in the Action! programming language
You may also check:How to resolve the algorithm Infinity step by step in the Seed7 programming language
You may also check:How to resolve the algorithm Draw a pixel step by step in the Rust programming language
You may also check:How to resolve the algorithm Date format step by step in the ooRexx programming language