How to resolve the algorithm Amb step by step in the PL/I programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Amb step by step in the PL/I 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 PL/I programming language

Source code in the pl/i programming language

*process or(!) source attributes xref;
 amb: Proc Options(main);
 /*********************************************************************
 * 25.08.2013 Walter Pachl
 *********************************************************************/
 Dcl w(4,10) Char(40) Var
     Init('the','that','a','if',(6)(1)' ',
          'frog','elephant','thing',(7)(1)' ',
          'walked','treaded','grows','trots',(6)(1)' ',
          'slowly','quickly',(8)(1)' ');
 Dcl ns Char(40) Var;
 Dcl (i,k,j,ii,jj,m,n) Bin Fixed(31);
 n=hbound(w,1);                        /* number of sets             */
 m=hbound(w,2);                        /* max number of words in set */
 Call show;                            /* show the input             */
 Do i=1 To n-1;                        /* loop over sets             */
   k=i+1;                              /* the following set          */
   Do ii=1 To m;                       /* loop over elements in set k*/
     If words(w(i,ii))=i Then Do;      /* a sentence part found      */
       Do jj=1 To m;                   /* loop over following words  */
         If right(w(i,ii),1)=left(w(k,jj),1) Then Do; /* fitting     */
           ns=w(i,ii)!!' '!!w(k,jj);   /* build new sentence (part)  */
           If words(ns)=k Then         /* 'complete' part            */
             Call add(k,ns);           /* add to set k               */
         End;
       End;
     End;
   End;
 Do jj=1 To m;                         /* show the results           */
   If words(w(4,jj))=4 Then
     put edit('--> ',w(4,jj))(Skip,a,a);
   End;

 add: Proc(ni,s);
 /*********************************************************************
 * add a sentence (part) to set ni
 *********************************************************************/
 Dcl (i,ni) Bin Fixed(31);
 Dcl s  Char(40) Var;
 Do i=1 To m While(w(ni,i)>'');        /* look for an empty slot     */
   End;
 w(ni,i)=s;                            /* add the sentence (part)    */
 End;

 words: Proc(s) Returns(Bin Fixed(31));
 /*********************************************************************
 * return the number of blank separated words in s
 *********************************************************************/
 Dcl s  Char(40) Var;
 Dcl nw Bin Fixed(31) Init(0);
 Dcl i  Bin Fixed(31) Init(1);
 If s>'' Then Do;
   nw=1;
   Do i=1 To length(s);
     If substr(s,i,1)=' ' Then
       nw+=1;
     End;
   End;
 Return(nw);
 End;

 show: Proc;
 /*********************************************************************
 * show the input sets
 *********************************************************************/
 Dcl (i,j,mm) Bin Fixed(31) Init(0);
 Dcl l(4) Bin Fixed(31) Init((4)0);
 Do i=1 To n;
   Do j=1 To m;
     If w(i,j)>'' Then Do;
       mm=max(mm,j);               /* max number of words in any set */
       l(i)=max(l(i),length(w(i,j)));  /* max word length in set i   */
       End;
     End;
   End;
 Put Edit('Input:')(Skip,a);
 Do j=1 To mm;                         /* output lines               */
   Put Skip;
   Do i=1 To n;
     Put Edit(w(i,j),' ')(a(l(i)),a);
     End;
   End;
 Put Skip;
 End;

 End;

  

You may also check:How to resolve the algorithm Flipping bits game step by step in the PL/I programming language
You may also check:How to resolve the algorithm Digital root step by step in the PL/I programming language
You may also check:How to resolve the algorithm Reverse words in a string step by step in the PL/I programming language
You may also check:How to resolve the algorithm Averages/Root mean square step by step in the PL/I programming language
You may also check:How to resolve the algorithm Arithmetic/Complex step by step in the PL/I programming language