package prolog.builtins

import prolog.Answers
import prolog.Substitutions
import prolog.ast.terms.Operator
import prolog.ast.terms.Term
import prolog.logic.*

class Atomic(private val term: Term) : Operator("atomic", rightOperand = term) {
    override fun satisfy(subs: Substitutions): Answers {
        if (atomic(term, subs)) {
            return sequenceOf(Result.success(emptyMap()))
        }
        return emptySequence()
    }

    override fun applySubstitution(subs: Substitutions): Atomic = Atomic(applySubstitution(term, subs))
}

class Compound(private val term: Term) : Operator("compound", rightOperand = term) {
    override fun satisfy(subs: Substitutions): Answers {
        if (compound(term, subs)) {
            return sequenceOf(Result.success(emptyMap()))
        }
        return emptySequence()
    }

    override fun applySubstitution(subs: Substitutions): Compound = Compound(applySubstitution(term, subs))
}

class NonVar(private val term: Term) : Operator("nonvar", rightOperand = term) {
    override fun satisfy(subs: Substitutions): Answers {
        if (nonvariable(term, subs)) {
            return sequenceOf(Result.success(emptyMap()))
        }

        return emptySequence()
    }

    override fun applySubstitution(subs: Substitutions): NonVar = NonVar(applySubstitution(term, subs))
}

class Var(private val term: Term) : Operator("var", rightOperand = term) {
    override fun satisfy(subs: Substitutions): Answers {
        if (variable(term, subs)) {
            return sequenceOf(Result.success(emptyMap()))
        }
        return emptySequence()
    }

    override fun applySubstitution(subs: Substitutions): Var = Var(applySubstitution(term, subs))
}
