master > master: codego - iff

This commit is contained in:
RD
2021-05-15 00:05:03 +02:00
parent 9b225b0551
commit 79bfcf57bb
5 changed files with 24 additions and 4 deletions

View File

@@ -145,3 +145,4 @@ func (fml Formula) IsConjunction() bool { return fml.kind == "and" || fm
func (fml Formula) IsDisjunction2() bool { return fml.kind == "or2" }
func (fml Formula) IsDisjunction() bool { return fml.kind == "or" || fml.kind == "or2" }
func (fml Formula) IsImplication() bool { return fml.kind == "implies" }
func (fml Formula) IsDoubleImplication() bool { return fml.kind == "iff" }

View File

@@ -115,3 +115,12 @@ func Implies(fml1 Formula, fml2 Formula) Formula {
subformulae: [](*Formula){&fml1, &fml2},
}
}
func DoubleImplication(fml1 Formula, fml2 Formula) Formula {
return Formula{
kind: "iff",
expr: "(" + fml1.expr + " <-> " + fml2.expr + ")",
valence: 2,
subformulae: [](*Formula){&fml1, &fml2},
}
}

View File

@@ -31,6 +31,8 @@ func Eval(fml formulae.Formula, I []string) int {
return utils.MaxList(prevValues)
} else if fml.IsImplication() {
return utils.BoolToInt(prevValues[0] <= prevValues[1])
} else if fml.IsDoubleImplication() {
return utils.BoolToInt(prevValues[0] == prevValues[1])
} else {
panic("Could not evaluate expression!")
}

View File

@@ -139,6 +139,11 @@ func (ant antlrTree) toFormula() formulae.Formula {
if nChildren == 3 {
return formulae.Implies(subants[0].toFormula(), subants[2].toFormula())
}
case "iff":
// NOTE: expr = expr <=> expr
if nChildren == 3 {
return formulae.DoubleImplication(subants[0].toFormula(), subants[2].toFormula())
}
case "and", "or":
// NOTE: expr = expr op expr op ... op expr
var n int = int((len(subants) + 1) / 2)