home *** CD-ROM | disk | FTP | other *** search
/ Turbo Toolbox / Turbo_Toolbox.iso / sharew / exoten / icon / allof.icn next >
Encoding:
Text File  |  1990-03-05  |  2.9 KB  |  102 lines

  1. ############################################################################
  2. #
  3. #    Name:    allof.icn
  4. #
  5. #    Title:    Iterative Conjunction Control Operation
  6. #
  7. #    Author:    Robert J. Alexander
  8. #
  9. #    Date:    November 3, 1989
  10. #
  11. ############################################################################
  12. #
  13. #  allof{expr1,expr2} -- Control operation that performs iterative
  14. #             conjunction.
  15. #
  16. #     Expr1 works like the control expression of "every-do"; it controls
  17. #  iteration by being resumed to produce all of its possible results.
  18. #  The allof{} expression produces the outcome of conjunction of all
  19. #  of the resulting expr2s, one instance of expr2 created for each
  20. #  iteration.
  21. #
  22. #     For example:
  23. #
  24. #    global c
  25. #    ...
  26. #    pattern := "ab*"
  27. #    "abcdef" ? {
  28. #       allof { c := !pattern ,
  29. #          if c == "*" then move(0 to *&subject - &pos + 1) else =c
  30. #          } & pos(0)
  31. #       }
  32. #
  33. #  This example will perform a wild card match on "abcdef" against
  34. #  pattern "ab*", where "*" in a pattern matches 0 or more characters.
  35. #  Since pos(0) will fail the first time it is evaluated, the allof{}
  36. #  expression will be resumed just as a conjunction expression would,
  37. #  and backtracking will propagate through all of the expr2s; the
  38. #  expression will ultimately succeed (as its conjunctive equivalent
  39. #  would).
  40. #
  41. #     Note that, due to the scope of variables in co-expressions,
  42. #  communication between expr1 and expr2 must be via global variables,
  43. #  hence c in the above example must be global.
  44. #
  45. #     The allof{} procedure models Icon's expression evaluation
  46. #  mechanism in that it explicitly performs backtracking.  The author of
  47. #  this procedure knows of no way to use Icon's built-in goal directed
  48. #  evaluation to perform conjunction of a arbitrary number of computed
  49. #  expressions (suggestions welcome).
  50. #
  51. ############################################################################
  52. #
  53. #  Requires:  co-expressions
  54. #
  55. ############################################################################
  56.  
  57. procedure allof(expr)
  58.    local elist,i,x,v
  59.    #
  60.    #  Initialize
  61.    #
  62.    elist := []    # expression list
  63.    i := 1    # expression list pointer
  64.    #
  65.    #  Loop until backtracking over all expr[2]s has failed.
  66.    #
  67.    while i > 0 do {
  68.       if not (x := elist[i]) then
  69.      #
  70.      #  If we're at the end of the list of expressions, attempt an
  71.      #  iteration to produce another expression.
  72.      #
  73.          if @expr[1] then
  74.         put(elist,x := ^expr[2])
  75.      else {
  76.         #
  77.         #  If no further iterations, suspend a result.
  78.         #
  79.         suspend v
  80.         #
  81.         #  We've been backed into -- back up to last expr[2].
  82.         #
  83.         i -:= 1
  84.         }
  85.       #
  86.       #  Evaluate the expression.
  87.       #
  88.       if v := @x then {
  89.      #
  90.      #  If success, move on to the refreshed next expression.
  91.      #
  92.          i +:= 1
  93.      elist[i] := ^elist[i]
  94.      }
  95.       else
  96.      #
  97.      #  If failure, back up.
  98.      #
  99.          i -:= 1
  100.       }
  101. end
  102.