package foreign;

import nova.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * Warranty & Liability
 * To the extent permitted by applicable law and unless explicitly
 * otherwise agreed upon, XLOG Technologies AG makes no warranties
 * regarding the provided information. XLOG Technologies AG assumes
 * no liability that any problems might be solved with the information
 * provided by XLOG Technologies AG.
 *
 * Rights & License
 * All industrial property rights regarding the information - copyright
 * and patent rights in particular - are the sole property of XLOG
 * Technologies AG. If the company was not the originator of some
 * excerpts, XLOG Technologies AG has at least obtained the right to
 * reproduce, change and translate the information.
 *
 * Reproduction is restricted to the whole unaltered document. Reproduction
 * of the information is only allowed for non-commercial uses. Selling,
 * giving away or letting of the execution of the library is prohibited.
 * The library can be distributed as part of your applications and libraries
 * for execution provided this comment remains unchanged.
 *
 * Restrictions
 * Only to be distributed with programs that add significant and primary
 * functionality to the library. Not to be distributed with additional
 * software intended to replace any components of the library.
 *
 * Trademarks
 * Jekejeke is a registered trademark of XLOG Technologies AG.
 */
public final class cyclib {

    /******************************************************************/
    /* subsumes/2                                                     */
    /******************************************************************/

    /**
     * subsumes(X, Y): [N208 8.2.4]
     * The built-in succeeds if X subsumes Y.
     */
    private static boolean test_subsumes(Object[] args) {
        Object alpha = Machine.exec_build(args[0]);
        Object beta = Machine.exec_build(args[1]);
        return subsumes(beta, alpha, beta);
    }

    /**
     * Determine whether two terms single side unify.
     * As a side effect the trail is extended, even if unification fails.
     * Tail recursive solution.
     *
     * @param root   The root of the second term.
     * @param first  The first term.
     * @param second The second term.
     * @return boolean True if the two terms single side unify, otherwise false.
     */
    private static boolean subsumes(Object root, Object first, Object second) {
        for (; ; ) {
            first = Store.deref(first);
            second = Store.deref(second);
            if (Store.is_variable(first)) {
                if (Store.is_variable(second) && first == second)
                    return true;
                if (has_var((Store.Variable) first, root))
                    return false;
                Machine.bind(second, (Store.Variable) first);
                return true;
            }
            if (Store.is_variable(second))
                return false;
            if (!Store.is_structure(first))
                return Objects.equals(first, second);
            if (!Store.is_structure(second))
                return false;
            Object[] t1 = ((Store.Structure) first).args;
            Object[] t2 = ((Store.Structure) second).args;
            if (t1.length != t2.length)
                return false;
            if (!((Store.Structure) first).functor.equals(((Store.Structure) second).functor))
                return false;
            int i = 0;
            for (; i < t1.length - 1; i++) {
                if (!subsumes(root, t1[i], t2[i]))
                    return false;
            }
            first = t1[i];
            second = t2[i];
        }
    }

    /******************************************************************/
    /* unify_with_occurs_check/2                                      */
    /******************************************************************/

    /**
     * unify_with_occurs_check(S, T): [ISO 8.2.2]
     * The built-in succeeds when the Prolog terms S and T unify
     * with occurs check, otherwise the built-in fails.
     */
    private static boolean test_unify_checked(Object[] args) {
        Object alpha = Machine.exec_build(args[0]);
        Object beta = Machine.exec_build(args[1]);
        return unify_checked(alpha, beta);
    }

    /**
     * Determine whether two terms unify with occurs check.
     * As a side effect the trail is extended, even if unification fails.
     * Can handle cyclic terms.
     *
     * @param first  The first term.
     * @param second The second term.
     * @return boolean True if the two terms unify, otherwise false.
     */
    private static boolean unify_checked(Object first, Object second) {
        List stack = null;
        List log = null;
        try {
            for (; ; ) {
                first = Store.deref(first);
                second = Store.deref(second);
                if (Store.is_variable(first)) {
                    if (!Store.is_variable(second) || first != second) {
                        if (has_var((Store.Variable) first, second))
                            break;
                        Machine.bind(second, (Store.Variable) first);
                    }
                } else if (Store.is_variable(second)) {
                    if (has_var((Store.Variable) second, first))
                        break;
                    Machine.bind(first, (Store.Variable) second);
                } else if (!Store.is_structure(first)) {
                    if (!Objects.equals(first, second))
                        break;
                } else if (!Store.is_structure(second)) {
                    break;
                } else if (((Store.Structure) first).args.length !=
                        ((Store.Structure) second).args.length) {
                    break;
                } else {
                    first = Machine.union_find((Store.Structure) first);
                    second = Machine.union_find((Store.Structure) second);
                    if (!((Store.Structure) first).functor.equals(
                            ((Store.Structure) second).functor)) {
                        break;
                    } else if (first != second) {
                        log = Machine.union_add(log, (Store.Structure) first,
                                (Store.Structure) second);
                        if (0 != ((Store.Structure) first).args.length - 1) {
                            Machine.Item item = new Machine.Item((Store.Structure)first, second, 0);
                            stack = Store.stack_push(stack, item);
                        }
                        first = ((Store.Structure) first).args[0];
                        second = ((Store.Structure) second).args[0];
                        continue;
                    }
                }
                Machine.Item item = (Machine.Item)Store.stack_peek(stack);
                if (item == null) {
                    return true;
                } else {
                    item.idx++;
                    first = item.first.args[item.idx];
                    second = ((Store.Structure)item.second).args[item.idx];
                    if (item.idx == item.first.args.length - 1)
                        Store.stack_pop(stack);
                }
            }
            return false;
        } finally {
            Machine.union_undo(log);
        }
    }

    /******************************************************************/
    /* occurs_check/2                                                 */
    /******************************************************************/

    /**
     * occurs_check(S, T): [ISO 7.3.3]
     * The built-in succeeds when the Prolog variable S occurs
     * in the Prolog term T, otherwise the built-in fails.
     */
    private static boolean test_has_var(Object[] args) {
        Object alpha = Machine.exec_build(args[0]);
        Object beta = Machine.exec_build(args[1]);
        return Store.is_variable(alpha) && has_var((Store.Variable)alpha, beta);
    }

    /**
     * Check whether a variable occurs in a term.
     * Can handle cyclic terms and deep recursion.
     *
     * @param vterm The Prolog variable.
     * @param first The Prolog term.
     * @return boolean True if vterm occurs in first, otherwise false.
     */
    private static boolean has_var(Store.Variable vterm, Object first) {
        has_var2 h = new has_var2(vterm);
        boolean res = special.walk_vars(first, h, Store.VAR_MASK_SEEN);
        special.walk_vars(first, h, 0);
        return res;
    }

    private static class has_var2 implements Handler.Acceptor {
        private final Store.Variable vterm;

        private has_var2(Store.Variable vterm)  {
            this.vterm = vterm;
        }

        public boolean run(Object node) {
            return (node == vterm);
        }

    }

    /******************************************************************/
    /* acyclic_term/1                                                 */
    /******************************************************************/

    /**
     * acyclic_term(T): [TC2 8.3.11]
     * The predicate succeeds when the Prolog term T is an acyclic term,
     * otherwise the predicate fails.
     */
    private static boolean test_acyclic_term(Object[] args) {
        Object alpha = Machine.exec_build(args[0]);
        boolean res = walk_acyclic(alpha, Store.VAR_MASK_SEEN);
        walk_acyclic(alpha, 0);
        return !res;
    }

    /**
     * Walk a Prolog term completely or partially.
     *
     * @param first The Prolog term.
     * @param state The desired state.
     * @return True if the walk was stopped, otherwise false.
     */
    private static boolean walk_acyclic(Object first, int state) {
        List stack = null;
        for (; ; ) {
            first = Store.deref(first);
            if (Store.is_compound(first)) {
                if ((((Store.Compound) first).walk & Store.VAR_MASK_SEEN) != state) {
                    ((Store.Compound)first).walk = (((Store.Compound)first).walk & ~Store.VAR_MASK_SEEN) | state;
                    ((Store.Compound) first).walk &= ~Store.VAR_MASK_SERNO;
                    stack = Store.stack_push(stack, first);
                    first = ((Store.Structure) first).args[0];
                    continue;
                } else if ((((Store.Compound)first).walk & Store.VAR_MASK_SERNO)
                        != ((Store.Structure) first).args.length) {
                    return true;
                }
            }
            Store.Compound item = (Store.Compound)Store.stack_peek(stack);
            while (item != null &&
                    (item.walk & Store.VAR_MASK_SERNO) == item.args.length - 1) {
                item.walk++;
                Store.stack_pop(stack);
                item = (Store.Compound)Store.stack_peek(stack);
            }
            if (item == null) {
                return false;
            } else {
                item.walk++;
                first = item.args[item.walk & Store.VAR_MASK_SERNO];
            }
        }
    }

    /******************************************************************/
    /* sys_acyclic_decompose/3                                        */
    /******************************************************************/

    /**
     * sys_acyclic_decompose(S, T):
     * The built-in succeeds in R with skeleton and substitution
     * list cells so that the substitions applied to the skeletons
     * gives identical the elements of the Prolog list L.
     */
    private static boolean test_sys_acyclic_decompose(Object[] args) {
        Object alpha = Machine.exec_build(args[0]);
        Map map = new HashMap();
        Store.Structure back = null;
        Object res = null;
        while (Store.is_structure(alpha) &&
                ".".equals(((Store.Structure) alpha).functor) &&
                ((Store.Structure) alpha).args.length == 2) {
            Object temp = Store.deref(((Store.Structure) alpha).args[0]);
            temp = walk_decom(map, temp);
            temp = new Store.Compound(".",
                    new Object[]{temp, Store.UNDEF_OBJ});
            if (back == null) {
                res = temp;
            } else {
                back.args[1] = temp;
            }
            back = (Store.Structure)temp;
            alpha = Store.deref(((Store.Structure) alpha).args[1]);
        }
        if (back == null) {
            res = "[]";
        } else {
            back.args[1] = "[]";
        }
        return Machine.exec_unify(args[1], res);
    }

    /**
     * Walk a Prolog term completely.
     *
     * @param map The compound map.
     * @param first The Prolog term.
     * @return The decomp.
     */
    private static Object walk_decom(Map map, Object first) {
        List stack = null;
        Object subst = "[]";
        for (; ; ) {
            first = Store.deref(first);
            if (Store.is_compound(first)) {
                Object peek = Store.assoc_get(map, first);
                if (peek == null) {
                    map = Store.assoc_set(map, first, Store.UNDEF_OBJ);
                    peek = new Store.Compound(((Store.Structure) first).functor,
                            new Object[((Store.Structure) first).args.length]);
                    Machine.Item item = new Machine.Item((Store.Structure)first, peek, 0);
                    stack = Store.stack_push(stack, item);
                    first = ((Store.Structure) first).args[0];
                    continue;
                } else {
                    if (peek == Store.UNDEF_OBJ) {
                        peek = new Store.Variable();
                        map = Store.assoc_set(map, first, peek);
                    }
                    first = peek;
                }
            }
            Machine.Item item = (Machine.Item)Store.stack_peek(stack);
            while (item != null &&
                    item.idx  == item.first.args.length - 1) {
                ((Store.Structure)item.second).args[item.idx] = first;
                first = item.second;
                Object peek = Store.assoc_get(map, item.first);
                if (peek == Store.UNDEF_OBJ) {
                    map = Store.assoc_set(map, item.first, first);
                } else {
                    first = new Store.Compound("=", new Object[]{peek, first});
                    subst = new Store.Compound(".", new Object[]{first, subst});
                    first = peek;
                }
                Store.stack_pop(stack);
                item = (Machine.Item)Store.stack_peek(stack);
            }
            if (item == null) {
                return new Store.Compound(".", new Object[]{first,subst});
            } else {
                ((Store.Structure)item.second).args[item.idx] = first;
                item.idx++;
                first = item.first.args[item.idx];
            }
        }
    }

    /******************************************************************/
    /* Cyc Lib Init                                                   */
    /******************************************************************/

    public static void main() {
        Store.add("subsumes", 2, special.make_check(cyclib::test_subsumes));
        Store.add("unify_with_occurs_check", 2, special.make_check(cyclib::test_unify_checked));
        Store.add("occurs_check", 2, special.make_check(cyclib::test_has_var));
        Store.add("acyclic_term", 1, special.make_check(cyclib::test_acyclic_term));
        Store.add("sys_acyclic_decompose", 2, special.make_check(cyclib::test_sys_acyclic_decompose));
    }

}
