package recursion

import (
	"logik/aussagenlogik/formulae"
	"logik/core/utils"
)

/* ---------------------------------------------------------------- *
 * METHOD: Evaluation of fomulae in models
 * ---------------------------------------------------------------- */

func Eval(tree formulae.Formula, I []string) int {
	// Definiere (parameterisiertes) Schema:
	var schema = func(_I []string) func(formulae.Formula, []int) int {
		return func(tree formulae.Formula, prevValues []int) int {
			if tree.IsAtom() || tree.IsGeneric() {
				return utils.BoolToInt(utils.StrListContains(_I, tree.GetExpr()))
			} else if tree.IsTautologySymbol() {
				return 1
			} else if tree.IsContradictionSymbol() {
				return 0
			} else if tree.IsNegation() {
				return 1 - prevValues[0]
			} else if tree.IsConjunction2() {
				return utils.Min2(prevValues[0], prevValues[1])
			} else if tree.IsConjunction() {
				return utils.MinList(prevValues)
			} else if tree.IsDisjunction2() {
				return utils.Max2(prevValues[0], prevValues[1])
			} else if tree.IsDisjunction() {
				return utils.MaxList(prevValues)
			} else if tree.IsImplication() {
				return utils.BoolToInt(prevValues[0] <= prevValues[1])
			} else {
				panic("Could not evaluate expression!")
			}
		}
	}
	// Erzeuge Funktion aus Schema und berechne Wert:
	fn := formulae.CreateFromSchemeIntValued(schema(I))
	return fn(tree)
}
