<commit_before>package com.github.prologdb.runtime.unification

import com.github.prologdb.runtime.VariableMapping
import com.github.prologdb.runtime.term.Term
import com.github.prologdb.runtime.term.Variable

class VariableBucket private constructor(
        /**
         * Each known variable gets a key in this map; The value however is not present if the variable
         * has not been instantiated yet.
         */
        private val variableMap: MutableMap<Variable, Term?>
) {
    constructor() : this(mutableMapOf<Variable, Term?>())

    val isEmpty
        get() = variableMap.isEmpty()

    private val substitutionMapper: (Variable) -> Term = { variable ->
        if (isInstantiated(variable) && this[variable] != variable) {
            this[variable].substituteVariables(this.asSubstitutionMapper())
        }
        else {
            variable
        }
    }

    fun asSubstitutionMapper(): (Variable) -> Term = substitutionMapper

    operator fun get(v: Variable): Term {
        if (isInstantiated(v)) {
            return variableMap[v]!!
        }
        else {
            throw NameError("Variable $v has not been instantiated yet.")
        }
    }

    fun instantiate(variable: Variable, value: Term) {
        if (isInstantiated(variable)) {
            throw NameError("Variable $variable is already instantiated in this bucket.")
        }
<commit_msg>Runtime: prevent the anonymous variable from being instantiated<commit_after>package com.github.prologdb.runtime.unification

import com.github.prologdb.runtime.VariableMapping
import com.github.prologdb.runtime.term.Term
import com.github.prologdb.runtime.term.Variable

class VariableBucket private constructor(
        /**
         * Each known variable gets a key in this map; The value however is not present if the variable
         * has not been instantiated yet.
         */
        private val variableMap: MutableMap<Variable, Term?>
) {
    constructor() : this(mutableMapOf<Variable, Term?>())

    val isEmpty
        get() = variableMap.isEmpty()

    private val substitutionMapper: (Variable) -> Term = { variable ->
        if (isInstantiated(variable) && this[variable] != variable) {
            this[variable].substituteVariables(this.asSubstitutionMapper())
        }
        else {
            variable
        }
    }

    fun asSubstitutionMapper(): (Variable) -> Term = substitutionMapper

    operator fun get(v: Variable): Term {
        if (isInstantiated(v)) {
            return variableMap[v]!!
        }
        else {
            throw NameError("Variable $v has not been instantiated yet.")
        }
    }

    fun instantiate(variable: Variable, value: Term) {
        if (variable == Variable.ANONYMOUS) return

        if (isInstantiated(variable)) {
            throw NameError("Variable $variable is already instantiated in this bucket.")
        }

{'fraction_non_alphanumeric': 0.09728978457261987, 'fraction_numerical': 0.0, 'mean_word_length': 2.6032540675844804, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6543991', 'n_tokens_mistral': 774, 'n_tokens_neox': 737, 'n_words': 209}