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)

View File

@ -12,13 +12,14 @@ RCURLYBRACE: '}';
SYMB_NOT: ('!'|'~'|'not');
SYMB_AND: ('&'+|'^'|'and');
SYMB_OR: ('|'+|'v'|'or');
SYMB_IMPL: ('->'|'=>');
SYMB_IFF: ([<][-]+[>]|[<][=]+[>]|'iff');
SYMB_IMPL: (~[<][-]+[>]|~[<][=]+[>]);
// Rules
start: open <EOF> | closed <EOF>;
closed: atomic | not | LBRACE open RBRACE;
open: and2 | and | or2 | or | implies;
open: and2 | and | or2 | or | implies | iff;
// Schemata für atomische Ausdrücke
atomic: taut | contradiction | atom; //| generic;
@ -26,8 +27,8 @@ taut: ('1'|'true');
contradiction: ('0'|'false');
atom: 'A0' | 'A1' | 'A' NUMBER; // muss A0, A1 wegen falsum/verum extra auflisten
// // als 'generische' Formeln schreibe bspw. {F}, {G}, {F1}, usw.
// // generic: LCURLYBRACE (.*?) RCURLYBRACE;
// FIXME: dieses Schema führt zu Konflikten
// generic: LCURLYBRACE (~[{])+ RCURLYBRACE;
// // FIXME: dieses Schema führt zu Konflikten
// Schema für Negation: ¬ F
not: symb=SYMB_NOT closed;
@ -39,3 +40,5 @@ or2: closed symb=SYMB_OR closed;
or: ( closed ( symb=SYMB_OR closed )+ );
// Schema für Implikation: F1 ⟶ F2
implies: closed symb=SYMB_IMPL closed;
// Schema für Implikation: F1 ⟷ F2
iff: closed symb=SYMB_IFF closed;