next up previous contents
Next: Special Purpose Functions Up: Primitives Previous: Functions on Type t_list   Contents


Functions on type t_binding

t_bool
operator==(t_binding b1, t_binding b2)

Returns true if b1 and b2 are bindings of the same length containing the same names (in order) bound to (recursively) equal values, and false otherwise.

t_bool
operator!=(t_binding b1, t_binding b2) =
  operator!(operator==(b1, b2))

t_binding
_bind1(t_text n, t_value v)

If n is not empty, returns a binding with the single (name, value) pair (n, v). If n is empty, the evaluation halts with a runtime error.

t_binding
_head(t_binding b)

Returns a binding with one (name, value) pair equal to the first element of b. If b is empty, the evaluation halts with a runtime error.

t_binding
_tail(t_binding b)

Returns the binding consisting of all elements of b, in order, except the first. If b is empty, the evaluation halts with a runtime error.

t_int
_length(t_binding b)

Returns the number of (name, value) pairs in b.

t_binding
_elem(t_binding b, t_int i)

Returns a binding consisting solely of the i-th (name, value) pair in the binding b. If no such pair exists, the evaluation halts with a runtime error. The first pair of a binding has index 0.

t_text
_n(t_binding b)

If _length(b) = 1, returns the name part of the (name, value) pair that constitutes b. Otherwise, the evaluation halts with a runtime error.

t_value
_v(t_binding b)

If _length(b) differs from 1, the evaluation halts with a runtime error. Otherwise, let v be the value part of the (name, value) pair that constitutes b. This function returns v.

t_bool
_defined(t_binding b, t_text name)

If name is empty, the evaluation halts with a runtime error. Otherwise, the function returns true if the binding b contains a pair (n, v) with n identical to name, and false otherwise.

t_value
_lookup(t_binding b, t_text name)

If name is nonempty and is defined in b, returns the value associated with it; otherwise, the evaluation halts with a runtime error.

t_binding
_append(t_binding b1, t_binding b2)

Returns a binding formed by appending b2 to b1, but only if all the names in b1 and b2 are distinct. Otherwise, the evaluation halts with a runtime error.

t_binding
operator+(t_binding b1, t_binding b2) =
{
  val r = emptybinding;
  for (; !(b1 == emptybinding); b1 = _tail(b1)) {
    val n = _n(_head(b1));
    val v;
    if (_defined(b2, n) == true)
      v = _lookup(b2, n);
    else v = _v(_head(b1));
    r = _append(r, _bind1(n, v));
  }
  for (; !(b2 == emptybinding); b2 = _tail(b2)) {
    if (_defined(b1, _n(_head(b2)) == false)
      r = _append(r, _head(b2));
  }
  return r;
}

Returns a binding formed by appending b2 to b1, giving precedence to b2 when both b1 and b2 contain (name, value) pairs with the same name.

t_binding
operator++(t_binding b1, t_binding b2) =
{
  val r = emptybinding;
  for (; !(b1 == emptybinding); b1 = _tail(b1)) {
    val n = _n(_head(b1));
    val v;
    if (_defined(b2, n) == true) {
      val v2 = _lookup(b2, n);
      if (_is_binding(v2) == true) {
        v = _v(_head(b1);
        if (_is_binding(v) == true)
          v = operator++(v, v2);
        else v = v2;
      }
      else v = v2;
    }
    else v = _v(_head(b1));
    r = _append(r, _bind1(n, v));
  }
  for (; !(b2 == emptybinding); b2 = _tail(b2)) {
    if (_defined(r, _n(_head(b2)) == false)
      r = _append(r, _head(b2));
  }
  return r;
}

Similar to operator+, but performs the operation recursively for each name n that is associated with a binding value in both b1 and b2.

t_binding
operator-(t_binding b1, t_binding b2) =
{
  val r = emptybinding;
  for (; !(b1 = emptybinding); b1 = _tail(b1)) {
    val n = _n(_head(b1));
    if (_defined(b2, n) == false)
      r = _append(r, _head(b1));
  }
  return r;
}

Returns a binding formed by removing from b1 any pair (n, v) such that the name n is defined in b2. The value v associated with n in b2 is irrelevant.

t_binding
_sub(t_binding b, t_int start = 0, t_int len = _length(b))
{
  int w = _length(b);
  int i = _min(_max(start, 0)), w);
  int j = _min(i + _max(len, 0), w);
  // 0 <= i <= j <= _length(b); extract [i..j)
  t_binding r = emptybinding;
  for (; i < j; i++) r = _append(r, _elem(b, i));
  return r;
}

Returns the sub-binding of b of length len starting at element start. Note the boundary cases defined by the pseudo-code; _sub produces a runtime error only if it is passed arguments of the wrong type.

t_binding
_map(t_closure f, t_binding b) =
{
  t_binding res = emptybinding;
  for (; !(b == emptybinding); b = _tail(l)) {
    // apply the closure "f"
    t_binding b1 = f(_n(_head(b)), _v(_head(b)));
    res = _append(res, b1);
  }
  return res;
}

Returns the binding that results from applying the closure f to each (name, value) pair of the binding b, and appending the resulting bindings together. The closure f should take the name (of type t_text) and value (of type t_value) as arguments, and return a value of type t_binding. If f has the wrong signature, the evaluation halts with a runtime error.

t_binding
_par_map(t_closure f, t_binding b)

Formally equivalent to _map, but the implementation may perform each application of f in a separate parallel thread. External tools invoked by _run_tool in different threads may be run simultaneously on different machines. If a runtime error occurs in one thread, the other threads may still run to completion before the evaluation terminates.


next up previous contents
Next: Special Purpose Functions Up: Primitives Previous: Functions on Type t_list   Contents
Allan Heydon, Roy Levin, Timothy Mann, Yuan Yu