// Path: java/gulava/Goal.java
// public interface Goal {
//   Stream run(Subst s);
// }
// 
// Path: java/gulava/Var.java
// public final class Var implements Comparable<Var> {
//   private static int nextC = 0;
//   private final int c = nextC++;
// 
//   @Override
//   public String toString() {
//     return String.format("_.%x", c);
//   }
// 
//   @Override
//   public int compareTo(Var other) {
//     return c - other.c;
//   }
// }


import static gulava.Goals.UNIT;
import static gulava.Goals.same;
import gulava.Goal;
import gulava.Var;
import gulava.annotation.MakeLogicValue;
import gulava.annotation.MakePredicates;
/*
 *  Copyright (c) 2015 The Gulava Authors
 *
 *  Permission is hereby granted, free of charge, to any person obtaining a copy
 *  of this software and associated documentation files (the "Software"), to deal
 *  in the Software without restriction, including without limitation the rights
 *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *  copies of the Software, and to permit persons to whom the Software is
 *  furnished to do so, subject to the following conditions:
 *
 *  The above copyright notice and this permission notice shall be included in all
 *  copies or substantial portions of the Software.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 *  SOFTWARE.
 */
package gulava.util;



/**
 * Represents a numeric count. This structure is useful if you are not performing any non-trivial
 * calculations except adding or subtracting small integers.
 *
 * <p>This has a single field - the counter is equal to 1 + the "count value" of the field. The
 * count value of the field is either 0 (for null) or some natural number (for instances of this
 * class).
 */
@MakeLogicValue
public abstract class Count<C> {
  /**
   * The value whose count is one less than this one.
   */
  public abstract C oneLess();

  public static Count<?> fromInt(int value) {
    if (value < 0) {
      throw new IllegalArgumentException("Must be non-negative: " + value);
    }

    Count<?> result = null;
    while (value > 0) {
      result = Count.of(result);
      value--;
    }

    return result;
  }

// Path: java/gulava/Goal.java
// public interface Goal {
//   Stream run(Subst s);
// }
// 
// Path: java/gulava/Var.java
// public final class Var implements Comparable<Var> {
//   private static int nextC = 0;
//   private final int c = nextC++;
// 
//   @Override
//   public String toString() {
//     return String.format("_.%x", c);
//   }
// 
//   @Override
//   public int compareTo(Var other) {
//     return c - other.c;
//   }
// }


// Path: java/gulava/util/Count.java
import static gulava.Goals.UNIT;
import static gulava.Goals.same;
import gulava.Goal;
import gulava.Var;
import gulava.annotation.MakeLogicValue;
import gulava.annotation.MakePredicates;

/*
 *  Copyright (c) 2015 The Gulava Authors
 *
 *  Permission is hereby granted, free of charge, to any person obtaining a copy
 *  of this software and associated documentation files (the "Software"), to deal
 *  in the Software without restriction, including without limitation the rights
 *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *  copies of the Software, and to permit persons to whom the Software is
 *  furnished to do so, subject to the following conditions:
 *
 *  The above copyright notice and this permission notice shall be included in all
 *  copies or substantial portions of the Software.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 *  SOFTWARE.
 */
package gulava.util;



/**
 * Represents a numeric count. This structure is useful if you are not performing any non-trivial
 * calculations except adding or subtracting small integers.
 *
 * <p>This has a single field - the counter is equal to 1 + the "count value" of the field. The
 * count value of the field is either 0 (for null) or some natural number (for instances of this
 * class).
 */
@MakeLogicValue
public abstract class Count<C> {
  /**
   * The value whose count is one less than this one.
   */
  public abstract C oneLess();

  public static Count<?> fromInt(int value) {
    if (value < 0) {
      throw new IllegalArgumentException("Must be non-negative: " + value);
    }

    Count<?> result = null;
    while (value > 0) {
      result = Count.of(result);
      value--;
    }

    return result;
  }

  public static <C> Count<C> of(C oneLess) {