// Path: engine/jprol-core/src/main/java/com/igormaznitsa/jprol/data/Terms.java
// public static TermList newList(final Term term) {
//   return new TermList(term);
// }
// 
// Path: engine/jprol-core/src/main/java/com/igormaznitsa/jprol/data/Terms.java
// public static TermStruct newStruct(final Term functor) {
//   return new TermStruct(functor);
// }
// 
// Path: engine/jprol-core/src/main/java/com/igormaznitsa/jprol/utils/Utils.java
// public static TermList createOrAppendToList(final TermList nullableList, final Term term) {
//   final TermList newList = Terms.newList(term);
// 
//   if (nullableList != null && !nullableList.isNullList()) {
//     newList.setTail(nullableList.getTail());
//     nullableList.setTail(newList);
//   }
// 
//   return newList;
// }
// 
// Path: engine/jprol-core/src/main/java/com/igormaznitsa/jprol/exceptions/ProlCriticalError.java
// public class ProlCriticalError extends Error {
// 
//   private static final long serialVersionUID = 1340153646373377400L;
// 
//   public ProlCriticalError(final Throwable cause) {
//     super(cause);
//   }
// 
//   public ProlCriticalError(final String message, final Throwable cause) {
//     super(message, cause);
//   }
// 
//   public ProlCriticalError(final String message) {
//     super(message);
//   }
// 
//   public ProlCriticalError() {
//     super();
//   }
// 
// }


import static com.igormaznitsa.jprol.data.TermType.LIST;
import static com.igormaznitsa.jprol.data.TermType.VAR;
import static com.igormaznitsa.jprol.data.Terms.newList;
import static com.igormaznitsa.jprol.data.Terms.newStruct;
import static com.igormaznitsa.jprol.utils.Utils.createOrAppendToList;
import com.igormaznitsa.jprol.exceptions.ProlCriticalError;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Stream;
        curlist = (TermList) nextList;
      } else {
        arraylist.add(nextList);
        break;
      }
    }
    return arraylist.toArray(new Term[0]);
  }

  public Term toAtom() {
    if (this.isNullList()) {
      return new Term("<empty>");
    }
    if (this.getTail() == Terms.NULL_LIST) {
      return this.getHead();
    } else {
      final int length = this.calculateLength();
      if (length == 3) {
        if (this.getHead() == Terms.LIST_FUNCTOR) {
          final TermList secondElement = this.getTail();
          final TermList thirdElement = secondElement.getTail();

          if (thirdElement.getHead().getTermType() == LIST) {
            return newList(secondElement.getHead(), thirdElement.getHead());
          }
        }
      }

      final TermStruct result;
      if (length == 1) {
// Path: engine/jprol-core/src/main/java/com/igormaznitsa/jprol/data/Terms.java
// public static TermList newList(final Term term) {
//   return new TermList(term);
// }
// 
// Path: engine/jprol-core/src/main/java/com/igormaznitsa/jprol/data/Terms.java
// public static TermStruct newStruct(final Term functor) {
//   return new TermStruct(functor);
// }
// 
// Path: engine/jprol-core/src/main/java/com/igormaznitsa/jprol/utils/Utils.java
// public static TermList createOrAppendToList(final TermList nullableList, final Term term) {
//   final TermList newList = Terms.newList(term);
// 
//   if (nullableList != null && !nullableList.isNullList()) {
//     newList.setTail(nullableList.getTail());
//     nullableList.setTail(newList);
//   }
// 
//   return newList;
// }
// 
// Path: engine/jprol-core/src/main/java/com/igormaznitsa/jprol/exceptions/ProlCriticalError.java
// public class ProlCriticalError extends Error {
// 
//   private static final long serialVersionUID = 1340153646373377400L;
// 
//   public ProlCriticalError(final Throwable cause) {
//     super(cause);
//   }
// 
//   public ProlCriticalError(final String message, final Throwable cause) {
//     super(message, cause);
//   }
// 
//   public ProlCriticalError(final String message) {
//     super(message);
//   }
// 
//   public ProlCriticalError() {
//     super();
//   }
// 
// }


// Path: engine/jprol-core/src/main/java/com/igormaznitsa/jprol/data/TermList.java
import static com.igormaznitsa.jprol.data.TermType.LIST;
import static com.igormaznitsa.jprol.data.TermType.VAR;
import static com.igormaznitsa.jprol.data.Terms.newList;
import static com.igormaznitsa.jprol.data.Terms.newStruct;
import static com.igormaznitsa.jprol.utils.Utils.createOrAppendToList;
import com.igormaznitsa.jprol.exceptions.ProlCriticalError;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Stream;

        curlist = (TermList) nextList;
      } else {
        arraylist.add(nextList);
        break;
      }
    }
    return arraylist.toArray(new Term[0]);
  }

  public Term toAtom() {
    if (this.isNullList()) {
      return new Term("<empty>");
    }
    if (this.getTail() == Terms.NULL_LIST) {
      return this.getHead();
    } else {
      final int length = this.calculateLength();
      if (length == 3) {
        if (this.getHead() == Terms.LIST_FUNCTOR) {
          final TermList secondElement = this.getTail();
          final TermList thirdElement = secondElement.getTail();

          if (thirdElement.getHead().getTermType() == LIST) {
            return newList(secondElement.getHead(), thirdElement.getHead());
          }
        }
      }

      final TermStruct result;
      if (length == 1) {
        result = newStruct(this.getHead());