/* conser.java Copyright (C) 2002 Quowong P Liu (GNU GPL at end of file).

The run the interpreter in repl mode: java conser

To run programs: java conser program.cons

The conser programming language
--------------------------------
The only data type is sets of pairs (of sets).

A program has to cons for any data other than the empty set, so it's a
conser.  Also, conser is sort of a combination of Conway and surreal.

It's a declarative language.

A program consists of function definitions, which may be mutually
recursive.  The function main must take zero or one argument.  When
the program is run, the main function is evaluated.  If it takes an
argument, the argument is 1 appended with the contents of stdin,
encoded as a surreal number.  The result of evaluating main, if it is
an integral surreal number, gets decoded into a byte stream with the
first byte discarded, and sent to stdout.  The extra byte needs to be
added or removed in case the input or the output begins with 0 or 255.

Example functions:
---------------------------------------------------------------------------
"Infinite loop."
loop = loop.
---------------------------------------------------------------------------
"Iteration.  Assumes f takes a single argument.  If n is a positive
 integer in a canonical form, gives the result of applying f n times
 to the argument."

2 a b = b.
iterate-f n a = (iterate-f <n f a (a ! 2 <n a)).
---------------------------------------------------------------------------

Example programs:
---------------------------------------------------------------------------
"cat: Copy stdin to stdout."
main a = a.
---------------------------------------------------------------------------
"H: Prints the first character of Hello world! to stdout.  Much shorter
 than printing the full Hello world! and doesn't take forever to run."

main = H.

H = (((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((
    (((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((
    (((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((
    (((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((
    (((((((((((((((((((((((((((((((((((((,),),),),),),),),),),),),),),),),),)
    ,),),),),),),),),),),),),),),),),),),),),),),),),),),),),),),),),),),),),
    ),),),),),),),),),),),),),),),),),),),),),),),),),),),),),),),),),),),),)
    ,),),),),),),),),),),),),),),),),),),),),),),),),),),),),),),),),),),),),
    ),),),),),),),),),),),),),),),),),),),),),),),),),),),),),),),),),),),),)
    ,),),),),),),),),),),),),),),),),),),),),),),),),),),),),),),),),),),),),
    ),),),),),),),),),),),),),),),),),),),),),),),),),),),),),),),),),),),),)
    ,),),),),),),),),),),),),),),),),),),),),),),),),),),),),),),),),),),),),
    ),),),),),),),),),),),),),),),),),),),),),),),),),),),),),),),),),),),),)
    ,),),),),),),),),),),),),),),),),),),).
---------------------------------------------------------------------------

Tokenization:

Whitespace separates tokens.

Comments are surrounded by double quotes.  Comments cannot contain double
quotes.  Comments are lexically whitespace.

The characters < > ( ) , & ! = . are special tokens.  Whitespace is not
needed to distinguish them.

Unbroken strings of any printable character other than the special tokens
or double quotes form identifier tokens.

A function definition consists of
 an identifier token, which names the function
 zero or more identifier tokens, which are the formal arguments
 the "=" token, which separates the specification from the body
 an expression, which consists of identifier tokens and/or
   "<" ">" "(" ")" "," "&" "!" tokens, and must be a valid expression
 the "." token, which ends the definition

Examples:
 empty = ().
 0 = (,).
 1 = (0,).
 -1 = (,0).
 bits = (0 1).
 f a b = b.

The ( ) , & ! characters can only be used to form cons, union, intersection,
or set subtraction expressions.

A cons, intersection, and set subtraction expressions consist of
 the "(" token
 zero or more expressions
 the "," token for cons, "&" for intersection, or "!" for set subtraction
 zero or more expressions
 the ")" token.

A union expression consists of
 the "(" token
 zero or more expressions
 the ")" token

Predefined functions:
 < is car.
 > is cdr.
 (,) is cons.
 () is union.
 (&) is intersection.
 (!) is set subtraction.

Examples:
---------------------------------------------------------------------------
union a b = (a b).
intersection a b = (a&b).
subtract a b = (a!b).
3-union a b c = (a b c).
---------------------------------------------------------------------------

Since there are no higher-order functions, there is no need to explicitly
specify precedence, so there is no way to do so.

Some things to note:
 <(a b) is equivalent to (<a <b).
 ((a b)) is equivalent to (a b).
 ((a b) c) is equivalent to (a b c).
 f (a b) c is equivalent to (f a c  f b c).
 f (a b) (c d) is equivalent to (f a c  f a d  f b c  f b d).
 f () is always ().
 f () a is always ().
 f a () is always ().

Examples:
---------------------------------------------------------------------------
"List processing."
car l = <l.
cdr l = >l.

map-f l = (f<l,map-f>l).

? a = (>a!2<a>a). 2 a b = b.
concat' a b = (<a,concat'>a b ?(>a,b)).

"concat' evaluates to () if a is () or b is ()."
concat a = (? a ?(>a,<a)concat'<a>a).
---------------------------------------------------------------------------

Hello world example:
---------------------------------------------------------------------------
"Hello world!: Prints Hello world! to stdout.  Takes forever to run."

"Addition."
+ a b = (+<a b + a<b,+>a b + a>b).

"Inverse under addition."
- a = (->a,-<a).

"Multiplication."
* a b = (+ + *<a b * a<b - *<a<b + + *>a b * a>b - *>a>b,
         + + *<a b * a>b - *<a>b + + *>a b * a<b - *>a<b).

"Small numbers."
0=(,).1=(0,).2=(1,).3=(2,).4=(3,).5=(4,).6=(5,).7=(6,).8=(7,).9=(8,).

"Factors of 10."
10 a = *(9,)a.
100 a = 10 10 a.
1000 a = 100 10 a.
10000 a = 1000 10 a.
100000 a = 10000 10 a.
1000000 a = 100000 10 a.
10000000 a = 1000000 10 a.
100000000 a = 10000000 10 a.
1000000000 a = 100000000 10 a.
10000000000 a = 1000000000 10 a.
100000000000 a = 10000000000 10 a.
1000000000000 a = 100000000000 10 a.
10000000000000 a = 1000000000000 10 a.
100000000000000 a = 10000000000000 10 a.
1000000000000000 a = 100000000000000 10 a.
10000000000000000 a = 1000000000000000 10 a.
100000000000000000 a = 10000000000000000 10 a.
1000000000000000000 a = 100000000000000000 10 a.
10000000000000000000 a = 1000000000000000000 10 a.
100000000000000000000 a = 10000000000000000000 10 a.
1000000000000000000000 a = 100000000000000000000 10 a.
10000000000000000000000 a = 1000000000000000000000 10 a.
100000000000000000000000 a = 10000000000000000000000 10 a.
1000000000000000000000000 a = 100000000000000000000000 10 a.
10000000000000000000000000 a = 1000000000000000000000000 10 a.
100000000000000000000000000 a = 10000000000000000000000000 10 a.
1000000000000000000000000000 a = 100000000000000000000000000 10 a.
10000000000000000000000000000 a = 1000000000000000000000000000 10 a.
100000000000000000000000000000 a = 10000000000000000000000000000 10 a.
1000000000000000000000000000000 a = 100000000000000000000000000000 10 a.
10000000000000000000000000000000 a = 1000000000000000000000000000000 10 a.

"Hello world! encoded as a number."
26018226366724685165746607890698 = +
10000000000000000000000000000000 2 +
 1000000000000000000000000000000 6 +
  100000000000000000000000000000 0 +
   10000000000000000000000000000 1 +
    1000000000000000000000000000 8 +
     100000000000000000000000000 2 +
      10000000000000000000000000 2 +
       1000000000000000000000000 6 +
        100000000000000000000000 3 +
         10000000000000000000000 6 +
          1000000000000000000000 6 +
           100000000000000000000 7 +
            10000000000000000000 2 +
             1000000000000000000 4 +
              100000000000000000 6 +
               10000000000000000 8 +
                1000000000000000 5 +
                 100000000000000 1 +
                  10000000000000 6 +
                   1000000000000 5 +
                    100000000000 7 +
                     10000000000 4 +
                      1000000000 6 +
                       100000000 6 +
                        10000000 0 +
                         1000000 7 +
                          100000 8 +
                           10000 9 +
                            1000 0 +
                             100 6 +
                              10 9 8.

main = 26018226366724685165746607890698.
---------------------------------------------------------------------------

Lazy evaluation.

Should conser programs be lazily evaluated?

With lazy evaluation, and an alternative, more tractable, definition of
the argument and result of main as lists of numbers rather than single
big numbers, interactive programs are possible.  However, I still like
the one big number method.

If, when forcing f a b, if a evaluates to (), then f a b = () and b never
needs to be evaluated.  However, if a never terminates, and b would
evaluate to (), then, if the order of evaluation is left to right, f a b
would never terminate, even if b evaluates to ().  If the evaluation of a
and b are done in parallel, then f a b = () if either a or b is ().

Still, with lazy evaluation,
 (finite-set & infinite-set) either is finite-set or doesn't terminate
 (finite-set ! infinite-set) either is () or doesn't terminate

Example of infinite sets:
---------------------------------------------------------------------------
+'s a = (a +'s(a,)).  -'s a = (a -'s(,a)).
Z = (+'s(,)-'s(,)).
inf = (Z,).
---------------------------------------------------------------------------
 */

/* This is not good code.  This is not intended to be good code.
   This is not even intended to be a correct implementation of conser. */
import java.io.*;
import java.math.*;
import java.util.*;

public class conser
{
    static boolean spew = false;
    static HashMap definitions = new HashMap();
    static final String VERSION = "(,)";

    public static void main(String[] args) throws Exception
    {
        if (args.length == 0) {
            spew = true;
            System.out.println("The conser interpreter, version " + VERSION + ", Copyright (C) 2002 Quowong P Liu");
            System.out.println("This program is free software; you can redistribute it and/or modify");
            System.out.println("it under the terms of the GNU General Public License as published by");
            System.out.println("the Free Software Foundation; either version 2 of the License, or");
            System.out.println("(at your option) any later version.");
            System.out.println();
            repl();
        } else {
            for (int i = 0; i < args.length; i++)
                loadFile(args[i]);
            interpretMain();
        }
    }

    private static void loadFile(String filename) throws Exception
    {
        Tokenizer tokenizer = new Tokenizer(new FileReader(filename));
        ArrayList current = new ArrayList();
        for (String tok = tokenizer.next(); tok != null; tok = tokenizer.next()) {
            if (tok.equals(".")) {
                addDefinition(current);
                current.clear();
            } else {
                current.add(tok);
            }
        }
        if (!current.isEmpty())
            throw new Exception("illegal tokens at the end of " + filename);
    }

    private static void interpretMain() throws Exception
    {
        Function main = (Function) definitions.get("main");
        if (main == null || main.nargs > 1)
            throw new Exception("no valid definition for main");

        ArrayList args = new ArrayList();
        if (main.nargs == 1) {
            ByteArrayOutputStream b = new ByteArrayOutputStream();
            b.write(1);
            byte[] buf = new byte[8196];
            for (;;) {
                int c = System.in.read(buf);
                if (c < 0)
                    break;
                b.write(buf, 0, c);
            }
            args.add(new ValBigInt(new BigInteger(b.toByteArray())));
        }

        Val result = main.eval(args);
        BigInteger resultVal = result.toBigInteger();
        if (resultVal != null) {
            byte[] resultBytes = resultVal.toByteArray();
            System.out.write(resultBytes, 1, resultBytes.length-1);
        }
    }

    private static void repl() throws Exception
    {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in, "ISO-8859-1"));
        ArrayList current = new ArrayList();
        boolean isDefinition = false;
        for (;;) {
            if (current.isEmpty())
                System.out.print("- ");
            else
                System.out.print("+ ");
            System.out.flush();
            String line = in.readLine();
            if (line == null)
                break;
            /* in repl(), comments can't span lines, not worth fixing */
            Tokenizer tokenizer = new Tokenizer(new StringReader(line));
            for (String tok = tokenizer.next(); tok != null; tok = tokenizer.next()) {
                if (tok.equals(".")) {
                    try {
                        if (isDefinition) {
                            addDefinition(current);
                        } else {
                            evaluatePrint(current);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    current.clear();
                    isDefinition = false;
                } else if (tok.equals("=")) {
                    isDefinition = true;
                    current.add(tok);
                } else {
                    current.add(tok);
                }
            }
        }
    }

    private static void addDefinition(ArrayList tokens) throws Exception
    {
        if (tokens.isEmpty())
            return;

        if (spew) {
            System.out.print("Adding definition:");
            for (Iterator i = tokens.iterator(); i.hasNext(); )
                System.out.print(" " + i.next());
            System.out.println();
        }

        Function f = new Function(tokens);
        if (definitions.keySet().contains(f.name))
            throw new Exception("repeated definition: " + f.name);
        definitions.put(f.name, f);
        f.resolve();
    }

    private static void evaluatePrint(ArrayList tokens) throws Exception
    {
        if (tokens.size() == 0)
            return;

        if (spew) {
            System.out.print("Evaluating:");
            for (Iterator i = tokens.iterator(); i.hasNext(); )
                System.out.print(" " + i.next());
            System.out.println();
        }

        if (tokens.size() == 1) {
            String token = (String) tokens.get(0);
            Function f = (Function) definitions.get(token);
            if (f == null) {
                if (token.equals("<") || token.equals(">"))
                    System.out.println("builtin: " + token);
                else if (Tokenizer.isSpecialToken(token))
                    System.out.println("syntax error: " + token);
                else
                    System.out.println("not defined: " + token);
            } else {
                f.resolve();
                if (f.nargs == 0 && f.resolved())
                    System.out.println(f.toString() + " = " + f.eval(new ArrayList()).toString());
                else
                    System.out.println(f);
            }
            return;
        }

        Expr expr = Expr.parse(new ArrayList(), tokens);
        ArrayList resolved = expr.resolve();
        if (resolved != null) {
            if (resolved.size() == 1) {
                expr = (Expr) resolved.get(0);
            } else {
                System.out.println("illegal expression: " + expr);
                return;
            }
        }
        if (!expr.resolved()) {
            System.out.println("unresolvable expression: " + expr);
            return;
        }

        System.out.println(expr.toString() + " = " + expr.eval(new ArrayList()));
    }
}

class Tokenizer
{
    private Reader in;
    private StringBuffer sb = new StringBuffer();
    Tokenizer(Reader in) { this.in = in; }

    static boolean isSpecialToken(String token)
    {
        return token.length() == 1 && isSpecialToken((int) token.charAt(0));
    }

    private static boolean isSpecialToken(int c)
    {
        switch (c) {
        case '=': case '.':
        case '<': case '>':
        case '(': case ')': case ',': case '&': case '!':
            return true;
        default:
            return false;
        }
    }

    private boolean isIdentifier(int c)
    {
        return c >= 0 && !Character.isWhitespace((char) c)
            && !isSpecialToken(c) && c != '"';
    }

    private void skipComment() throws Exception
    {
        int c = in.read();
        while (c >= 0 && c != '"')
            c = in.read();
        if (c < 0)
            throw new Exception("unterminated comment");
    }

    private String getBuffer()
    {
        String result = sb.toString();
        sb.setLength(0);
        return result;
    }

    String next() throws Exception
    {
        if (sb.length() > 0) {
            return getBuffer();
        }
        int c = in.read();
        for (;; c = in.read()) {
            if (c < 0) {
                return null;
            } else if (c == '"') {
                skipComment();
                continue;
            } else if (isSpecialToken(c)) {
                return String.valueOf((char) c);
            } else if (isIdentifier(c)) {
                sb.append((char) c);
                for (c = in.read();; c = in.read()) {
                    if (c < 0) {
                        return getBuffer();
                    } else if (c == '"') {
                        skipComment();
                        return getBuffer();
                    } else if (isSpecialToken(c)) {
                        String result = getBuffer();
                        sb.append((char) c);
                        return result;
                    } else if (!isIdentifier(c)) {
                        return getBuffer();
                    } else {
                        sb.append((char) c);
                    } 
                }
            }
        }
    }
}

class Function
{
    String name;
    int nargs;
    private Expr body;
    private boolean resolved = false;

    Function(ArrayList tokens) throws Exception
    {
        if (tokens.isEmpty())
            throw new RuntimeException();
        name = (String) tokens.remove(0);
        if (Tokenizer.isSpecialToken(name))
            throw new Exception("illegal function name: " + name);
        ArrayList formals = new ArrayList();
        for (;;) {
            if (tokens.isEmpty())
                throw new Exception("illegal function specification: " + name);
            String token = (String) tokens.remove(0);
            if (token.equals("="))
                break;
            if (Tokenizer.isSpecialToken(token))
                throw new Exception("illegal function parameter: " + name + ": " + token);
            if (token.equals(name) || formals.contains(token))
                throw new Exception("ambiguous function parameter: " + name + ": " + token);
            formals.add(token);
        }
        nargs = formals.size();
        body = Expr.parse(formals, tokens);
        if (body == null || !tokens.isEmpty())
            throw new Exception("illegal function definition: " + name);
    }

    private boolean resolving = false;
    void resolve() throws Exception
    {
        if (resolving)
            return;
        try {
            resolving = true;
            if (resolved)
                return;
            if (body.resolved()) {
                resolved = true;
                return;
            }
            if (conser.spew)
                System.out.println("resolving function: " + this);
            ArrayList resolvedBody = body.resolve();
            if (resolvedBody != null) {
                if (resolvedBody.size() != 1)
                    throw new Exception("resolve error: " + name);
                body = (Expr) resolvedBody.get(0);
            }
            if (body.resolved()) {
                resolved = true;
            }
        } finally {
            resolving = false;
        }
    }

    boolean resolved()
    {
        if (!resolving)
            return resolved;
        return true;
    }

    private Val memo = null;
    Val eval(ArrayList params) throws Exception
    {
        resolve();
        if (!resolved())
            throw new Exception("unresolved function body: " + name);
        if (nargs != 0)
            return body.eval(params);
        if (memo == null)
            memo = body.eval(params);
        return memo;
    }

    public String toString()
    {
        StringBuffer sb = new StringBuffer();
        sb.append(name).append(' ');
        for (int i = 0; i < nargs; i++)
            sb.append('"').append(i).append(' ');
        sb.append("= ").append(body.toString());
        return sb.toString();
    }
}

abstract class Expr
{
    abstract boolean resolved() throws Exception;
    abstract ArrayList resolve() throws Exception;
    abstract Val eval(ArrayList params) throws Exception;

    static Expr parse(ArrayList formals, ArrayList tokens) throws Exception
    {
        ArrayList unresolved = new ArrayList();
        for (;;) {
            if (tokens.isEmpty())
                break;
            String token = (String) tokens.get(0);
            if (formals.contains(token)) {
                tokens.remove(0);
                Expr expr = new ExprParam(formals.indexOf(token));
                if (unresolved.isEmpty())
                    return expr;
                unresolved.add(expr);
            } else if (token.equals(",")
                       || token.equals("&")
                       || token.equals("!")
                       || token.equals(")"))
            {
                if (unresolved.isEmpty())
                    return null;
                break;
            } else if (!token.equals("(")) {
                tokens.remove(0);
                unresolved.add(token);
            } else {
                tokens.remove(0);
                ArrayList left = new ArrayList();
                for (Expr arg = parse(formals, tokens);
                     arg != null;
                     arg = parse(formals, tokens))
                    left.add(arg);
                if (tokens.isEmpty())
                    throw new Exception("unmatched (");
                token = (String) tokens.remove(0);
                Expr expr;
                if (token.equals(")")) {
                    expr = new ExprUnion(left);
                } else {
                    ArrayList right = new ArrayList();
                    for (Expr arg = parse(formals, tokens);
                         arg != null;
                         arg = parse(formals, tokens))
                        right.add(arg);
                    if (tokens.isEmpty())
                        throw new Exception("unmatched (");
                    if (!tokens.remove(0).equals(")"))
                        throw new Exception("unmatched (");
                    if (token.equals(","))
                        expr = new ExprCons(left, right);
                    else if (token.equals("&"))
                        expr = new ExprIntersect(left, right);
                    else if (token.equals("!"))
                        expr = new ExprDifference(left, right);
                    else
                        throw new RuntimeException();
                }
                if (unresolved.isEmpty())
                    return expr;
                unresolved.add(expr);
            }
        }
        return new ExprUnresolved(unresolved);
    }

    static ArrayList resolveArgs(ArrayList args) throws Exception
    {
        ArrayList resolvedArgs = new ArrayList();
        for (Iterator i = args.iterator(); i.hasNext(); ) {
            Expr expr = (Expr) i.next();
            ArrayList resolvedExpr = expr.resolve();
            if (resolvedExpr == null)
                resolvedArgs.add(expr);
            else
                resolvedArgs.addAll(resolvedExpr);
        }
        return resolvedArgs;
    }

    static Val evalUnion(ArrayList params, ArrayList exprs) throws Exception
    {
        ArrayList vals = new ArrayList();
        for (Iterator i = exprs.iterator(); i.hasNext(); )
            vals.add(((Expr) i.next()).eval(params));
        return Val.union(vals);
    }
}

class ExprParam extends Expr
{
    private int pos;
    ExprParam(int pos) { this.pos = pos; }
    boolean resolved() { return true; }
    ArrayList resolve() { return null; }
    Val eval(ArrayList params) { return (Val) params.get(pos); }
    public String toString() { return "\"" + pos; }
}

class ExprUnion extends Expr
{
    private ArrayList args;

    ExprUnion(ArrayList args) { this.args = args; }

    boolean resolved() throws Exception
    {
        for (Iterator i = args.iterator(); i.hasNext(); )
            if (!((Expr) i.next()).resolved()) {
                if (conser.spew)
                    System.out.println("unresolved union expr: " + this);
                return false;
            }
        return true;
    }

    ArrayList resolve() throws Exception
    {
        if (conser.spew)
            System.out.println("resolving (): " + this);
        args = resolveArgs(args);
        return null;
    }

    Val eval(ArrayList params) throws Exception
    {
        return evalUnion(params, args);
    }

    public String toString()
    {
        StringBuffer sb = new StringBuffer();
        sb.append('(');
        String sp = "";
        for (Iterator i = args.iterator(); i.hasNext(); ) {
            sb.append(sp).append(i.next().toString());
            sp = " ";
        }
        sb.append(')');
        return sb.toString();
    }
}

abstract class ExprPair extends Expr
{
    private ArrayList left;
    private ArrayList right;

    ExprPair(ArrayList left, ArrayList right)
    {
        this.left = left;
        this.right = right;
    }

    boolean resolved() throws Exception
    {
        for (Iterator i = left.iterator(); i.hasNext(); )
            if (!((Expr) i.next()).resolved()) {
                if (conser.spew)
                    System.out.println("unresolved pair expr (left): " + this);
                return false;
            }
        for (Iterator i = right.iterator(); i.hasNext(); )
            if (!((Expr) i.next()).resolved()) {
                if (conser.spew)
                    System.out.println("unresolved pair expr (right): " + this);
                return false;
            }
        return true;
    }

    ArrayList resolve() throws Exception
    {
        if (conser.spew)
            System.out.println("resolving (,) or (!) or (&): " + this);
        left = resolveArgs(left);
        right = resolveArgs(right);
        return null;
    }

    abstract char separator();

    public String toString()
    {
        StringBuffer sb = new StringBuffer();
        sb.append('(');
        String sp = "";
        for (Iterator i = left.iterator(); i.hasNext(); ) {
            sb.append(sp).append(i.next().toString());
            sp = " ";
        }
        sb.append(separator());
        sp = "";
        for (Iterator i = right.iterator(); i.hasNext(); ) {
            sb.append(sp).append(i.next().toString());
            sp = " ";
        }
        sb.append(')');
        return sb.toString();
    }

    Val eval(ArrayList params) throws Exception
    {
        return eval(evalUnion(params, left), evalUnion(params, right));
    }

    abstract Val eval(Val left, Val right) throws Exception;
}

class ExprCons extends ExprPair
{
    ExprCons(ArrayList left, ArrayList right) { super(left, right); }

    char separator() { return ','; }

    Val eval(Val left, Val right) throws Exception
    {
        return Val.cons(left, right);
    }
}

class ExprIntersect extends ExprPair
{
    ExprIntersect(ArrayList left, ArrayList right) { super(left, right); }

    char separator() { return '&'; }

    Val eval(Val left, Val right) throws Exception
    {
        return Val.intersect(left, right);
    }
}

class ExprDifference extends ExprPair
{
    ExprDifference(ArrayList left, ArrayList right) { super(left, right); }

    char separator() { return '!'; }

    Val eval(Val left, Val right) throws Exception
    {
        return Val.difference(left, right);
    }
}

abstract class ExprCarCdr extends Expr
{
    private Expr arg;

    ExprCarCdr(Expr arg)
    {
        if (arg instanceof ExprUnresolved)
            throw new RuntimeException();
        this.arg = arg;
    }

    boolean resolved() throws Exception
    {
        boolean resolved = arg.resolved();
        if (!resolved)
            if (conser.spew)
                System.out.println("unresolved < or >: " + this);
        return resolved;
    }

    ArrayList resolve() throws Exception
    {
        if (conser.spew)
            System.out.println("resolving < or >: " + this);
        arg.resolve();
        return null;
    }

    abstract char operator();

    public String toString() { return operator() + arg.toString(); }

    Val eval(ArrayList params) throws Exception
    {
        return eval(arg.eval(params));
    }

    abstract Val eval(Val val) throws Exception;
}

class ExprCar extends ExprCarCdr
{
    ExprCar(Expr arg) { super(arg); }
    char operator() { return '<'; }
    Val eval(Val val) throws Exception { return val.left(); }
}

class ExprCdr extends ExprCarCdr
{
    ExprCdr(Expr arg) { super(arg); }
    char operator() { return '>'; }
    Val eval(Val val) throws Exception { return val.right(); }
}

class ExprUnresolved extends Expr
{
    private ArrayList exprs;

    ExprUnresolved(ArrayList exprs) { this.exprs = exprs; }

    boolean resolved()
    {
        if (conser.spew)
            System.out.println("unresolved: " + this);
        return false;
    }

    ArrayList resolve() throws Exception
    {
        if (conser.spew)
            System.out.println("resolving unresolved: " + this);

        if (!(exprs.get(0) instanceof String))
            throw new RuntimeException();

        ArrayList stack = new ArrayList();
        for (int i = exprs.size()-1; i >= 0; i--) {
            if (exprs.get(i) instanceof Expr) {
                Expr expr = (Expr) exprs.get(i);
                ArrayList exprResolved = expr.resolve();
                if (exprResolved == null) {
                    stack.add(expr);
                } else {
                    Collections.reverse(exprResolved);
                    stack.addAll(exprResolved);
                }
                continue;
            }
            String id = (String) exprs.get(i);
            if (id.equals("<") || id.equals(">")) {
                if (stack.size() < 1)
                    throw new Exception("resolve error: not enough arguments for: " + id + " in: " + this);
                if (!(stack.get(stack.size()-1) instanceof Expr)) {
                    stack.add(id);
                    continue;
                }
                Expr arg = (Expr) stack.remove(stack.size()-1);
                if (id.equals("<"))
                    stack.add(new ExprCar(arg));
                else
                    stack.add(new ExprCdr(arg));
                continue;
            }
            Function f = (Function) conser.definitions.get(id);
            if (f == null) {
                stack.add(id);
                continue;
            }
            if (stack.size() < f.nargs)
                throw new Exception("resolve error: not enough arguments for: " + id + " in: " + this);
            boolean fresolved = true;
            for (int ii = 0; ii < f.nargs; ii++)
                if (!(stack.get(stack.size()-ii-1) instanceof Expr)) {
                    fresolved = false;
                    break;
                }
            if (!fresolved) {
                stack.add(id);
                continue;
            }
            ArrayList fargs = new ArrayList();
            for (int ii = 0; ii < f.nargs; ii++)
                fargs.add(stack.remove(stack.size()-1));
            stack.add(new ExprFunction(f, fargs));
        }

        Collections.reverse(stack);
        if (stack.get(0) instanceof String) {
            exprs = stack;
            return null;
        }

        for (int i = 1; i < stack.size(); i++)
            if (stack.get(i) instanceof String) {
                exprs.clear();
                while (stack.size() > i)
                    exprs.add(stack.remove(i));
                stack.add(this);
                break;
            }
        return stack;
    }

    Val eval(ArrayList params) throws Exception
    {
        throw new Exception("cannot evaluate unresolved subexpression: " + this);
    }

    public String toString()
    {
        StringBuffer sb = new StringBuffer();
        sb.append("\"unresolved");
        for (Iterator i = exprs.iterator(); i.hasNext(); )
            sb.append(' ').append(i.next().toString());
        return sb.toString();
    }
}

class ExprFunction extends Expr
{
    private Function func;
    private ArrayList args;

    ExprFunction(Function func, ArrayList args) throws Exception
    {
        this.func = func;
        this.args = args;
        for (Iterator i = args.iterator(); i.hasNext(); )
            if (i.next() instanceof ExprUnresolved)
                throw new RuntimeException();
    }

    boolean resolved() throws Exception
    {
        if (!func.resolved()) {
            if (conser.spew)
                System.out.println("unresolved function: " + this);
            return false;
        }
        for (Iterator i = args.iterator(); i.hasNext(); )
            if (!((Expr) i.next()).resolved()) {
                if (conser.spew)
                    System.out.println("unresolved function argument: " + this);
                return false;
            }
        return true;
    }

    ArrayList resolve() throws Exception
    {
        if (conser.spew)
            System.out.println("resolving function: " + this);
        func.resolve();
        for (Iterator i = args.iterator(); i.hasNext(); )
            ((Expr) i.next()).resolve();
        return null;
    }

    Val eval(ArrayList params) throws Exception
    {
        ArrayList myParams = new ArrayList();
        for (Iterator i = args.iterator(); i.hasNext(); ) {
            Val param = ((Expr) i.next()).eval(params);
            if (param.empty())
                return Val.EMPTY;
            myParams.add(param);
        }
        ArrayList results = new ArrayList();
        evalPermutations(myParams, new ArrayList(), results);
        return Val.union(results);
    }

    private void evalPermutations
        (ArrayList params, ArrayList currentPermutation, ArrayList results)
        throws Exception
    {
        if (currentPermutation.size() == params.size()) {
            results.add(func.eval(currentPermutation));
            return;
        }
        Val param = (Val) params.get(currentPermutation.size());
        if (!(param instanceof ValSet)) {
            currentPermutation.add(param);
            evalPermutations(params, currentPermutation, results);
            currentPermutation.remove(currentPermutation.size()-1);
        } else {
            for (Iterator i = ((ValSet) param).set.iterator(); i.hasNext(); ) {
                currentPermutation.add(i.next());
                evalPermutations(params, currentPermutation, results);
                currentPermutation.remove(currentPermutation.size()-1);
            }
        }
    }

    public String toString()
    {
        StringBuffer sb = new StringBuffer();
        sb.append(func.name);
        for (Iterator i = args.iterator(); i.hasNext(); )
            sb.append(' ').append(((Expr) i.next()).toString());
        return sb.toString();
    }
}

abstract class Val
{
    static ValSet EMPTY = new ValSet(new HashSet());
    static ValBigInt ZERO = new ValBigInt(BigInteger.ZERO);

    BigInteger toBigInteger() { return null; }
    abstract boolean empty();
    abstract Val left() throws Exception;
    abstract Val right() throws Exception;

    static Val cons(Val val1, Val val2) throws Exception
    {
        if (val1.empty() && val2.empty())
            return Val.ZERO;

        /* incorrect: try to canonicalize some surreal integers.
           however, it's useful for playing with integer arithmetic --
           addition, subtraction, multiplication.  16 + 16 and 4*4 still
           take forever though.

           to be correct, only represent (,), ((,),), (((,),),), ...
           and (,(,)), (,(,(,))), ... as ValBigInt, since the correct
           left() and right() will be returned for those. */

        BigInteger lmin = findMin(val1);
        BigInteger lmax = findMax(val1);
        BigInteger rmin = findMin(val2);
        BigInteger rmax = findMax(val2);

        if (val2.empty() && lmax != null && lmax.signum() != -1)
            return new ValBigInt(lmax.add(BigInteger.ONE));

        if (val1.empty() && rmin != null && rmin.signum() != 1)
            return new ValBigInt(rmin.subtract(BigInteger.ONE));

        if (lmax == null || rmin == null || lmax.compareTo(rmin) != -1)
            return new ValPair(val1, val2);

        if (lmax.signum() == -1 && rmin.signum() == 1)
            return Val.ZERO;

        if (lmax.signum() != -1) {
            BigInteger val = lmax.add(BigInteger.ONE);
            if (val.compareTo(rmin) == -1)
                return new ValBigInt(val);
        } else if (rmin.signum() != 1) {
            BigInteger val = rmin.subtract(BigInteger.ONE);
            if (val.compareTo(lmax) == 1)
                return new ValBigInt(val);
        }

        return new ValPair(val1, val2);
    }

    private static BigInteger findMin(Val val)
    {
        if (val instanceof ValBigInt)
            return val.toBigInteger();
        if (!(val instanceof ValSet))
            return null;
        BigInteger min = null;
        for (Iterator i = ((ValSet) val).set.iterator(); i.hasNext(); ) {
            Val v = (Val) i.next();
            if (v instanceof ValSet)
                throw new RuntimeException();
            if (!(v instanceof ValBigInt))
                return null;
            ValBigInt vbi = (ValBigInt) v;
            if (min == null)
                min = vbi.toBigInteger();
            else
                min = min.min(vbi.toBigInteger());
        }
        return min;
    }

    private static BigInteger findMax(Val val)
    {
        if (val instanceof ValBigInt)
            return val.toBigInteger();
        if (!(val instanceof ValSet))
            return null;
        BigInteger max = null;
        for (Iterator i = ((ValSet) val).set.iterator(); i.hasNext(); ) {
            Val v = (Val) i.next();
            if (v instanceof ValSet)
                throw new RuntimeException();
            if (!(v instanceof ValBigInt))
                return null;
            ValBigInt vbi = (ValBigInt) v;
            if (max == null)
                max = vbi.toBigInteger();
            else
                max = max.max(vbi.toBigInteger());
        }
        return max;
    }

    static Val union(List vals) throws Exception
    {
        if (vals.isEmpty())
            return Val.EMPTY;

        HashSet set = new HashSet();
        /* not quite correct, since hashCode() and equals() aren't
           properly defined.  if they were, this would be correct. */
        for (Iterator i = vals.iterator(); i.hasNext(); ) {
            Val val = (Val) i.next();
            if (!(val instanceof ValSet))
                set.add(val);
            else
                for (Iterator ii = ((ValSet) val).set.iterator(); ii.hasNext(); ) {
                    Val valval = (Val) ii.next();
                    if (valval instanceof ValSet)
                        throw new RuntimeException();
                    set.add(valval);
                }
        }

        switch (set.size()) {
        case 0:
            return Val.EMPTY;
        case 1:
            return (Val) set.iterator().next();
        default:
            return new ValSet(set);
        }
    }

    static Val intersect(Val val1, Val val2) throws Exception
    {
        if (val1.empty() || val2.empty())
            return Val.EMPTY;

        if (val1.equals(val2))
            return val1;

        /* the rest of this function is incorrect,
           since hashCode() and equals() aren't properly defined. */

        HashSet set = new HashSet();

        if (val1 instanceof ValSet)
            set.addAll(((ValSet) val1).set);
        else
            set.add(val1);

        if (val2 instanceof ValSet)
            set.retainAll(((ValSet) val2).set);
        else if (!set.contains(val2))
            return Val.EMPTY;
        else {
            set.clear();
            set.add(val2);
        }

        return new ValSet(set);
    }

    static Val difference(Val val1, Val val2) throws Exception
    {
        if (val1.empty() || val2.empty())
            return val1;

        if (val1.equals(val2))
            return Val.EMPTY;

        /* the rest of this function is incorrect,
           since hashCode() and equals() aren't properly defined. */

        HashSet set = new HashSet();

        if (val1 instanceof ValSet)
            set.addAll(((ValSet) val1).set);
        else
            set.add(val1);

        if (val2 instanceof ValSet)
            set.removeAll(((ValSet) val2).set);
        else
            set.remove(val2);

        return new ValSet(set);
    }

    abstract void toString(StringBuffer sb);
}

class ValBigInt extends Val
{
    private BigInteger val;

    ValBigInt(BigInteger val) { this.val = val; }

    public boolean equals(Object o)
    {
        if (o instanceof ValBigInt)
            return ((ValBigInt) o).val.equals(val);
        return false;
    }

    BigInteger toBigInteger() { return val; }

    boolean empty() { return false; }

    Val left()
    {
        if (val.signum() == 1)
            return new ValBigInt(val.subtract(BigInteger.ONE));
        else
            return Val.EMPTY;
    }

    Val right()
    {
        if (val.signum() == -1)
            return new ValBigInt(val.add(BigInteger.ONE));
        else
            return Val.EMPTY;
    }

    public String toString() { return val.toString(); }

    void toString(StringBuffer sb) { sb.append(val.toString()); }
}

class ValSet extends Val
{
    Set set;

    ValSet(Set set) { this.set = set; }

    boolean empty() { return set.isEmpty(); }

    Val left() throws Exception
    {
        if (empty())
            return this;

        ArrayList vals = new ArrayList();
        for (Iterator i = set.iterator(); i.hasNext(); )
            vals.add(((Val) i.next()).left());
        return union(vals);
    }

    Val right() throws Exception
    {
        if (empty())
            return this;

        ArrayList vals = new ArrayList();
        for (Iterator i = set.iterator(); i.hasNext(); )
            vals.add(((Val) i.next()).right());
        return union(vals);
    }

    public String toString()
    {
        StringBuffer sb = new StringBuffer();
        sb.append('(');
        toString(sb);
        sb.append(')');
        return sb.toString();
    }

    void toString(StringBuffer sb)
    {
        String sp = "";
        for (Iterator i = set.iterator(); i.hasNext(); ) {
            sb.append(sp);
            ((Val) i.next()).toString(sb);
            sp = " ";
        }
    }
}

class ValPair extends Val
{
    private Val left;
    private Val right;

    ValPair(Val left, Val right) { this.left = left; this.right = right; }

    boolean empty() { return false; }

    Val left() { return left; }

    Val right() { return right; }

    public String toString()
    {
        StringBuffer sb = new StringBuffer();
        toString(sb);
        return sb.toString();
    }

    void toString(StringBuffer sb)
    {
        sb.append('(');
        left.toString(sb);
        sb.append(',');
        right.toString(sb);
        sb.append(')');
    }
}

/*
		    GNU GENERAL PUBLIC LICENSE
		       Version 2, June 1991

 Copyright (C) 1989, 1991 Free Software Foundation, Inc.
     59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 Everyone is permitted to copy and distribute verbatim copies
 of this license document, but changing it is not allowed.

			    Preamble

  The licenses for most software are designed to take away your
freedom to share and change it.  By contrast, the GNU General Public
License is intended to guarantee your freedom to share and change free
software--to make sure the software is free for all its users.  This
General Public License applies to most of the Free Software
Foundation's software and to any other program whose authors commit to
using it.  (Some other Free Software Foundation software is covered by
the GNU Library General Public License instead.)  You can apply it to
your programs, too.

  When we speak of free software, we are referring to freedom, not
price.  Our General Public Licenses are designed to make sure that you
have the freedom to distribute copies of free software (and charge for
this service if you wish), that you receive source code or can get it
if you want it, that you can change the software or use pieces of it
in new free programs; and that you know you can do these things.

  To protect your rights, we need to make restrictions that forbid
anyone to deny you these rights or to ask you to surrender the rights.
These restrictions translate to certain responsibilities for you if you
distribute copies of the software, or if you modify it.

  For example, if you distribute copies of such a program, whether
gratis or for a fee, you must give the recipients all the rights that
you have.  You must make sure that they, too, receive or can get the
source code.  And you must show them these terms so they know their
rights.

  We protect your rights with two steps: (1) copyright the software, and
(2) offer you this license which gives you legal permission to copy,
distribute and/or modify the software.

  Also, for each author's protection and ours, we want to make certain
that everyone understands that there is no warranty for this free
software.  If the software is modified by someone else and passed on, we
want its recipients to know that what they have is not the original, so
that any problems introduced by others will not reflect on the original
authors' reputations.

  Finally, any free program is threatened constantly by software
patents.  We wish to avoid the danger that redistributors of a free
program will individually obtain patent licenses, in effect making the
program proprietary.  To prevent this, we have made it clear that any
patent must be licensed for everyone's free use or not licensed at all.

  The precise terms and conditions for copying, distribution and
modification follow.

		    GNU GENERAL PUBLIC LICENSE
   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION

  0. This License applies to any program or other work which contains
a notice placed by the copyright holder saying it may be distributed
under the terms of this General Public License.  The "Program", below,
refers to any such program or work, and a "work based on the Program"
means either the Program or any derivative work under copyright law:
that is to say, a work containing the Program or a portion of it,
either verbatim or with modifications and/or translated into another
language.  (Hereinafter, translation is included without limitation in
the term "modification".)  Each licensee is addressed as "you".

Activities other than copying, distribution and modification are not
covered by this License; they are outside its scope.  The act of
running the Program is not restricted, and the output from the Program
is covered only if its contents constitute a work based on the
Program (independent of having been made by running the Program).
Whether that is true depends on what the Program does.

  1. You may copy and distribute verbatim copies of the Program's
source code as you receive it, in any medium, provided that you
conspicuously and appropriately publish on each copy an appropriate
copyright notice and disclaimer of warranty; keep intact all the
notices that refer to this License and to the absence of any warranty;
and give any other recipients of the Program a copy of this License
along with the Program.

You may charge a fee for the physical act of transferring a copy, and
you may at your option offer warranty protection in exchange for a fee.

  2. You may modify your copy or copies of the Program or any portion
of it, thus forming a work based on the Program, and copy and
distribute such modifications or work under the terms of Section 1
above, provided that you also meet all of these conditions:

    a) You must cause the modified files to carry prominent notices
    stating that you changed the files and the date of any change.

    b) You must cause any work that you distribute or publish, that in
    whole or in part contains or is derived from the Program or any
    part thereof, to be licensed as a whole at no charge to all third
    parties under the terms of this License.

    c) If the modified program normally reads commands interactively
    when run, you must cause it, when started running for such
    interactive use in the most ordinary way, to print or display an
    announcement including an appropriate copyright notice and a
    notice that there is no warranty (or else, saying that you provide
    a warranty) and that users may redistribute the program under
    these conditions, and telling the user how to view a copy of this
    License.  (Exception: if the Program itself is interactive but
    does not normally print such an announcement, your work based on
    the Program is not required to print an announcement.)

These requirements apply to the modified work as a whole.  If
identifiable sections of that work are not derived from the Program,
and can be reasonably considered independent and separate works in
themselves, then this License, and its terms, do not apply to those
sections when you distribute them as separate works.  But when you
distribute the same sections as part of a whole which is a work based
on the Program, the distribution of the whole must be on the terms of
this License, whose permissions for other licensees extend to the
entire whole, and thus to each and every part regardless of who wrote it.

Thus, it is not the intent of this section to claim rights or contest
your rights to work written entirely by you; rather, the intent is to
exercise the right to control the distribution of derivative or
collective works based on the Program.

In addition, mere aggregation of another work not based on the Program
with the Program (or with a work based on the Program) on a volume of
a storage or distribution medium does not bring the other work under
the scope of this License.

  3. You may copy and distribute the Program (or a work based on it,
under Section 2) in object code or executable form under the terms of
Sections 1 and 2 above provided that you also do one of the following:

    a) Accompany it with the complete corresponding machine-readable
    source code, which must be distributed under the terms of Sections
    1 and 2 above on a medium customarily used for software interchange; or,

    b) Accompany it with a written offer, valid for at least three
    years, to give any third party, for a charge no more than your
    cost of physically performing source distribution, a complete
    machine-readable copy of the corresponding source code, to be
    distributed under the terms of Sections 1 and 2 above on a medium
    customarily used for software interchange; or,

    c) Accompany it with the information you received as to the offer
    to distribute corresponding source code.  (This alternative is
    allowed only for noncommercial distribution and only if you
    received the program in object code or executable form with such
    an offer, in accord with Subsection b above.)

The source code for a work means the preferred form of the work for
making modifications to it.  For an executable work, complete source
code means all the source code for all modules it contains, plus any
associated interface definition files, plus the scripts used to
control compilation and installation of the executable.  However, as a
special exception, the source code distributed need not include
anything that is normally distributed (in either source or binary
form) with the major components (compiler, kernel, and so on) of the
operating system on which the executable runs, unless that component
itself accompanies the executable.

If distribution of executable or object code is made by offering
access to copy from a designated place, then offering equivalent
access to copy the source code from the same place counts as
distribution of the source code, even though third parties are not
compelled to copy the source along with the object code.

  4. You may not copy, modify, sublicense, or distribute the Program
except as expressly provided under this License.  Any attempt
otherwise to copy, modify, sublicense or distribute the Program is
void, and will automatically terminate your rights under this License.
However, parties who have received copies, or rights, from you under
this License will not have their licenses terminated so long as such
parties remain in full compliance.

  5. You are not required to accept this License, since you have not
signed it.  However, nothing else grants you permission to modify or
distribute the Program or its derivative works.  These actions are
prohibited by law if you do not accept this License.  Therefore, by
modifying or distributing the Program (or any work based on the
Program), you indicate your acceptance of this License to do so, and
all its terms and conditions for copying, distributing or modifying
the Program or works based on it.

  6. Each time you redistribute the Program (or any work based on the
Program), the recipient automatically receives a license from the
original licensor to copy, distribute or modify the Program subject to
these terms and conditions.  You may not impose any further
restrictions on the recipients' exercise of the rights granted herein.
You are not responsible for enforcing compliance by third parties to
this License.

  7. If, as a consequence of a court judgment or allegation of patent
infringement or for any other reason (not limited to patent issues),
conditions are imposed on you (whether by court order, agreement or
otherwise) that contradict the conditions of this License, they do not
excuse you from the conditions of this License.  If you cannot
distribute so as to satisfy simultaneously your obligations under this
License and any other pertinent obligations, then as a consequence you
may not distribute the Program at all.  For example, if a patent
license would not permit royalty-free redistribution of the Program by
all those who receive copies directly or indirectly through you, then
the only way you could satisfy both it and this License would be to
refrain entirely from distribution of the Program.

If any portion of this section is held invalid or unenforceable under
any particular circumstance, the balance of the section is intended to
apply and the section as a whole is intended to apply in other
circumstances.

It is not the purpose of this section to induce you to infringe any
patents or other property right claims or to contest validity of any
such claims; this section has the sole purpose of protecting the
integrity of the free software distribution system, which is
implemented by public license practices.  Many people have made
generous contributions to the wide range of software distributed
through that system in reliance on consistent application of that
system; it is up to the author/donor to decide if he or she is willing
to distribute software through any other system and a licensee cannot
impose that choice.

This section is intended to make thoroughly clear what is believed to
be a consequence of the rest of this License.

  8. If the distribution and/or use of the Program is restricted in
certain countries either by patents or by copyrighted interfaces, the
original copyright holder who places the Program under this License
may add an explicit geographical distribution limitation excluding
those countries, so that distribution is permitted only in or among
countries not thus excluded.  In such case, this License incorporates
the limitation as if written in the body of this License.

  9. The Free Software Foundation may publish revised and/or new versions
of the General Public License from time to time.  Such new versions will
be similar in spirit to the present version, but may differ in detail to
address new problems or concerns.

Each version is given a distinguishing version number.  If the Program
specifies a version number of this License which applies to it and "any
later version", you have the option of following the terms and conditions
either of that version or of any later version published by the Free
Software Foundation.  If the Program does not specify a version number of
this License, you may choose any version ever published by the Free Software
Foundation.

  10. If you wish to incorporate parts of the Program into other free
programs whose distribution conditions are different, write to the author
to ask for permission.  For software which is copyrighted by the Free
Software Foundation, write to the Free Software Foundation; we sometimes
make exceptions for this.  Our decision will be guided by the two goals
of preserving the free status of all derivatives of our free software and
of promoting the sharing and reuse of software generally.

			    NO WARRANTY

  11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW.  EXCEPT WHEN
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS
TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE
PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
REPAIR OR CORRECTION.

  12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGES.

		     END OF TERMS AND CONDITIONS

	    How to Apply These Terms to Your New Programs

  If you develop a new program, and you want it to be of the greatest
possible use to the public, the best way to achieve this is to make it
free software which everyone can redistribute and change under these terms.

  To do so, attach the following notices to the program.  It is safest
to attach them to the start of each source file to most effectively
convey the exclusion of warranty; and each file should have at least
the "copyright" line and a pointer to where the full notice is found.

    <one line to give the program's name and a brief idea of what it does.>
    Copyright (C) <year>  <name of author>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA


Also add information on how to contact you by electronic and paper mail.

If the program is interactive, make it output a short notice like this
when it starts in an interactive mode:

    Gnomovision version 69, Copyright (C) year  name of author
    Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
    This is free software, and you are welcome to redistribute it
    under certain conditions; type `show c' for details.

The hypothetical commands `show w' and `show c' should show the appropriate
parts of the General Public License.  Of course, the commands you use may
be called something other than `show w' and `show c'; they could even be
mouse-clicks or menu items--whatever suits your program.

You should also get your employer (if you work as a programmer) or your
school, if any, to sign a "copyright disclaimer" for the program, if
necessary.  Here is a sample; alter the names:

  Yoyodyne, Inc., hereby disclaims all copyright interest in the program
  `Gnomovision' (which makes passes at compilers) written by James Hacker.

  <signature of Ty Coon>, 1 April 1989
  Ty Coon, President of Vice

This General Public License does not permit incorporating your program into
proprietary programs.  If your program is a subroutine library, you may
consider it more useful to permit linking proprietary applications with the
library.  If this is what you want to do, use the GNU Library General
Public License instead of this License.
*/
