home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / CLIPPER / MISC / BIPL.ZIP / PROCS.ZIP / ALLOF.ICN < prev    next >
Encoding:
Text File  |  1992-09-28  |  3.5 KB  |  109 lines

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