static MuFormula RewriteBoxDia(MuFormula form) { // We extended our grammar from the assignment by allowing regular formulas instead of single-letter actions. // Here, we rewrite such extended formulas to their more primitive versions for which we impemented evaluators. if (form is Diamond) { var dia = form as Diamond; if (dia.RegularFormula.Multiplier == "+") { // <R+>f = <R><R*>f var rf2 = dia.RegularFormula.Clone(); rf2.Multiplier = "*"; dia.RegularFormula.Multiplier = ""; dia.Formula = new Diamond(dia.RegularFormula, new Diamond(rf2, dia.Formula)); } else if (dia.RegularFormula.Multiplier == "*") { // <R*>f = mu X . (<R> X || f) var var = new Variable(GetFreeVar()); dia.RegularFormula.Multiplier = ""; return Rewrite(new Mu(var, new Disjunction(new Diamond(dia.RegularFormula, var), dia.Formula))); } dia.Formula = Rewrite(dia.Formula); } else if (form is Box) { var box = form as Box; if (box.RegularFormula.Multiplier == "+") { // [R+]f = [R][R*]f var rf2 = box.RegularFormula.Clone(); rf2.Multiplier = "*"; box.RegularFormula.Multiplier = ""; return new Box(box.RegularFormula, new Box(rf2, box.Formula)); } else if (box.RegularFormula.Multiplier == "*") { // [R*]f = nu X . ([R] X && f) var var = new Variable(GetFreeVar()); box.RegularFormula.Multiplier = ""; return Rewrite(new Nu(var, new Disjunction(new Box(box.RegularFormula, var), box.Formula))); } box.Formula = Rewrite(box.Formula); } // we also need to perform this on all subformulas else if (form is Disjunction) form = new Disjunction(Rewrite(((Disjunction)form).Left), Rewrite(((Disjunction)form).Right)); else if (form is Conjunction) form = new Conjunction(Rewrite(((Conjunction)form).Left), Rewrite(((Conjunction)form).Right)); else if (form is Mu) form = new Mu(((Mu)form).Variable, Rewrite(((Mu)form).Formula)); else if (form is Nu) form = new Nu(((Nu)form).Variable, Rewrite(((Nu)form).Formula)); else if (form is Negation) form = new Negation(Rewrite(((Negation)form).Formula)); return form; }
private object CreateNewObject(GOLD.Reduction r) { object result = null; switch ((ProductionIndex)r.Parent.TableIndex()) { case ProductionIndex.Nl_Newline: // <nl> ::= NewLine <nl> break; case ProductionIndex.Nl_Newline2: // <nl> ::= NewLine break; case ProductionIndex.Regform_Pipepipe: // <regForm> ::= <regForm> '||' <regFormSeq> result = new UnionFormula((RegularFormula)r[0].Data, (RegularFormula)r[2].Data); break; case ProductionIndex.Regform: // <regForm> ::= <regFormSeq> result = r[0].Data; break; case ProductionIndex.Regformseq_Dot: // <regFormSeq> ::= <regFormSeq> '.' <regFormValue> result = new SequenceFormula((RegularFormula)r[0].Data, (RegularFormula)r[2].Data); break; case ProductionIndex.Regformseq: // <regFormSeq> ::= <regFormValue> result = r[0].Data; break; case ProductionIndex.Regformvalue_Identifier: // <regFormValue> ::= Identifier <Multiplier> result = new SingleAction((string)r[0].Data, (string)r[1].Data); break; case ProductionIndex.Regformvalue_Not: // <regFormValue> ::= not <regForm> result = new NegateAction((RegularFormula)r[1].Data); break; case ProductionIndex.Regformvalue_Lparen_Rparen: // <regFormValue> ::= '(' <regForm> ')' <Multiplier> if ((string)r[3].Data != "") return new NestedFormula((RegularFormula)r[1].Data, (string)r[3].Data); else // small optimization result = r[1].Data; break; case ProductionIndex.Multiplier_Plus: // <Multiplier> ::= '+' result = "+"; break; case ProductionIndex.Multiplier_Times: // <Multiplier> ::= '*' result = "*"; break; case ProductionIndex.Multiplier: // <Multiplier> ::= result = ""; break; case ProductionIndex.Muform_Identifier: // <muForm> ::= Identifier result = new Proposition((string)r[0].Data); break; case ProductionIndex.Muform_Variable: // <muForm> ::= Variable result = new Variable((string)r[0].Data); break; case ProductionIndex.Muform_Lparen_Pipepipe_Rparen: // <muForm> ::= '(' <muForm> '||' <muForm> ')' result = new Disjunction((MuFormula)r[1].Data, (MuFormula)r[3].Data); break; case ProductionIndex.Muform_Lparen_Ampamp_Rparen: // <muForm> ::= '(' <muForm> '&&' <muForm> ')' result = new Conjunction((MuFormula)r[1].Data, (MuFormula)r[3].Data); break; case ProductionIndex.Muform_Lt_Gt: // <muForm> ::= '<' <regForm> '>' <muForm> result = new Diamond((RegularFormula)r[1].Data, (MuFormula)r[3].Data); break; case ProductionIndex.Muform_Lbracket_Rbracket: // <muForm> ::= '[' <regForm> ']' <muForm> result = new Box((RegularFormula)r[1].Data, (MuFormula)r[3].Data); break; case ProductionIndex.Muform_Mu_Variable_Dot: // <muForm> ::= mu Variable '.' <muForm> result = new Mu(new Variable((string)r[1].Data), (MuFormula)r[3].Data); break; case ProductionIndex.Muform_Nu_Variable_Dot: // <muForm> ::= nu Variable '.' <muForm> result = new Nu(new Variable((string)r[1].Data), (MuFormula)r[3].Data); break; case ProductionIndex.Muform_Not: // <muForm> ::= not <muForm> result = new Negation((MuFormula)r[1].Data); break; case ProductionIndex.Muform_Lparen_Rparen: result = r[1].Data as MuFormula; break; case ProductionIndex.Line_Commentl: // <Line> ::= CommentL <nl> break; case ProductionIndex.Line: // <Line> ::= <muForm> <nl> var form = (MuFormula)r[0].Data; FormulaRewriter.ResetFreeVars(); form = FormulaRewriter.Rewrite(form); form.SetParents(null /* root has no parent */); formulas.Add(form); break; case ProductionIndex.Line2: // <Line> ::= <nl> break; case ProductionIndex.Lines: // <Lines> ::= <Line> <Lines> break; case ProductionIndex.Lines2: // <Lines> ::= break; } //switch return result; }
// rewrite regular formulas inside box/diamond to multiple boxes/diamonds static MuFormula RewriteRFs(MuFormula form) { // connectives on regular formulas // <a.rf>f = <a><rf>f if (form is Box) { var box = form as Box; // [rf1+rf2]f = [rf1]f || [rf2]f /*if (box.RegularFormula is UnionFormula) { var un = box.RegularFormula as UnionFormula; return Rewrite(new Disjunction(new Box(un.Left, box.Formula), new Box(un.Right, box.Formula))); }*/ // directly supported by RegularFormula now! // [a.rf]f = [a][rf]f if (box.RegularFormula is SequenceFormula) { var seq = box.RegularFormula as SequenceFormula; return Rewrite(new Box(seq.First, new Box(seq.Sequence, RewriteRFs(box.Formula)))); } // by now all *s should be gone, so we can rewrite // [(a.b)] to [a.b] else if (box.RegularFormula is NestedFormula) { var nested = box.RegularFormula as NestedFormula; if (nested.Multiplier != "") throw new InvalidProgramException("by now all multipliers should have been removed"); box.RegularFormula = nested.Inner; return Rewrite(box); } } else if (form is Diamond) { var dia = form as Diamond; // <rf1+rf2>f = <[rf1>f || <rf2>f /*if (dia.RegularFormula is UnionFormula) { var un = dia.RegularFormula as UnionFormula; return Rewrite(new Disjunction(new Diamond(un.Left, dia.Formula), new Diamond(un.Right, dia.Formula))); }*/ // directly supported now! // <a.rf>f = <a><rf>f if (dia.RegularFormula is SequenceFormula) { var seq = dia.RegularFormula as SequenceFormula; dia.RegularFormula = seq.First; dia.Formula = new Diamond(seq.Sequence, dia.Formula); return Rewrite(dia); } else if (dia.RegularFormula is NestedFormula) { var nested = dia.RegularFormula as NestedFormula; if (nested.Multiplier != "") throw new InvalidProgramException("by now all multipliers should have been removed"); dia.RegularFormula = nested.Inner; return Rewrite(dia); } } // we also need to perform this on all subformulas else if (form is Disjunction) form = new Disjunction(Rewrite(((Disjunction)form).Left), Rewrite(((Disjunction)form).Right)); else if (form is Conjunction) form = new Conjunction(Rewrite(((Conjunction)form).Left), Rewrite(((Conjunction)form).Right)); else if (form is Mu) form = new Mu(((Mu)form).Variable, Rewrite(((Mu)form).Formula)); else if (form is Nu) form = new Nu(((Nu)form).Variable, Rewrite(((Nu)form).Formula)); return form; }
private object CreateNewObject(GOLD.Reduction r) { object result = null; switch ((ProductionIndex)r.Parent.TableIndex()) { case ProductionIndex.Nl_Newline: // <nl> ::= NewLine <nl> break; case ProductionIndex.Nl_Newline2: // <nl> ::= NewLine break; case ProductionIndex.Regform_Pipepipe: // <regForm> ::= <regForm> '||' <regFormSeq> result = new UnionFormula((RegularFormula)r[0].Data, (RegularFormula)r[2].Data); break; case ProductionIndex.Regform: // <regForm> ::= <regFormSeq> result = r[0].Data; break; case ProductionIndex.Regformseq_Dot: // <regFormSeq> ::= <regFormSeq> '.' <regFormValue> result = new SequenceFormula((RegularFormula)r[0].Data, (RegularFormula)r[2].Data); break; case ProductionIndex.Regformseq: // <regFormSeq> ::= <regFormValue> result = r[0].Data; break; case ProductionIndex.Regformvalue_Identifier: // <regFormValue> ::= Identifier <Multiplier> result = new SingleAction((string)r[0].Data, (string)r[1].Data); break; case ProductionIndex.Regformvalue_Not: // <regFormValue> ::= not <regForm> result = new NegateAction((RegularFormula)r[1].Data); break; case ProductionIndex.Regformvalue_Lparen_Rparen: // <regFormValue> ::= '(' <regForm> ')' <Multiplier> if ((string)r[3].Data != "") { return(new NestedFormula((RegularFormula)r[1].Data, (string)r[3].Data)); } else // small optimization { result = r[1].Data; } break; case ProductionIndex.Multiplier_Plus: // <Multiplier> ::= '+' result = "+"; break; case ProductionIndex.Multiplier_Times: // <Multiplier> ::= '*' result = "*"; break; case ProductionIndex.Multiplier: // <Multiplier> ::= result = ""; break; case ProductionIndex.Muform_Identifier: // <muForm> ::= Identifier result = new Proposition((string)r[0].Data); break; case ProductionIndex.Muform_Variable: // <muForm> ::= Variable result = new Variable((string)r[0].Data); break; case ProductionIndex.Muform_Lparen_Pipepipe_Rparen: // <muForm> ::= '(' <muForm> '||' <muForm> ')' result = new Disjunction((MuFormula)r[1].Data, (MuFormula)r[3].Data); break; case ProductionIndex.Muform_Lparen_Ampamp_Rparen: // <muForm> ::= '(' <muForm> '&&' <muForm> ')' result = new Conjunction((MuFormula)r[1].Data, (MuFormula)r[3].Data); break; case ProductionIndex.Muform_Lt_Gt: // <muForm> ::= '<' <regForm> '>' <muForm> result = new Diamond((RegularFormula)r[1].Data, (MuFormula)r[3].Data); break; case ProductionIndex.Muform_Lbracket_Rbracket: // <muForm> ::= '[' <regForm> ']' <muForm> result = new Box((RegularFormula)r[1].Data, (MuFormula)r[3].Data); break; case ProductionIndex.Muform_Mu_Variable_Dot: // <muForm> ::= mu Variable '.' <muForm> result = new Mu(new Variable((string)r[1].Data), (MuFormula)r[3].Data); break; case ProductionIndex.Muform_Nu_Variable_Dot: // <muForm> ::= nu Variable '.' <muForm> result = new Nu(new Variable((string)r[1].Data), (MuFormula)r[3].Data); break; case ProductionIndex.Muform_Not: // <muForm> ::= not <muForm> result = new Negation((MuFormula)r[1].Data); break; case ProductionIndex.Muform_Lparen_Rparen: result = r[1].Data as MuFormula; break; case ProductionIndex.Line_Commentl: // <Line> ::= CommentL <nl> break; case ProductionIndex.Line: // <Line> ::= <muForm> <nl> var form = (MuFormula)r[0].Data; FormulaRewriter.ResetFreeVars(); form = FormulaRewriter.Rewrite(form); form.SetParents(null /* root has no parent */); formulas.Add(form); break; case ProductionIndex.Line2: // <Line> ::= <nl> break; case ProductionIndex.Lines: // <Lines> ::= <Line> <Lines> break; case ProductionIndex.Lines2: // <Lines> ::= break; } //switch return(result); }
// rewrite regular formulas inside box/diamond to multiple boxes/diamonds static MuFormula RewriteRFs(MuFormula form) { // connectives on regular formulas // <a.rf>f = <a><rf>f if (form is Box) { var box = form as Box; // [rf1+rf2]f = [rf1]f || [rf2]f /*if (box.RegularFormula is UnionFormula) { * var un = box.RegularFormula as UnionFormula; * return Rewrite(new Disjunction(new Box(un.Left, box.Formula), new Box(un.Right, box.Formula))); * }*/ // directly supported by RegularFormula now! // [a.rf]f = [a][rf]f if (box.RegularFormula is SequenceFormula) { var seq = box.RegularFormula as SequenceFormula; return(Rewrite(new Box(seq.First, new Box(seq.Sequence, RewriteRFs(box.Formula))))); } // by now all *s should be gone, so we can rewrite // [(a.b)] to [a.b] else if (box.RegularFormula is NestedFormula) { var nested = box.RegularFormula as NestedFormula; if (nested.Multiplier != "") { throw new InvalidProgramException("by now all multipliers should have been removed"); } box.RegularFormula = nested.Inner; return(Rewrite(box)); } } else if (form is Diamond) { var dia = form as Diamond; // <rf1+rf2>f = <[rf1>f || <rf2>f /*if (dia.RegularFormula is UnionFormula) { * var un = dia.RegularFormula as UnionFormula; * return Rewrite(new Disjunction(new Diamond(un.Left, dia.Formula), new Diamond(un.Right, dia.Formula))); * }*/ // directly supported now! // <a.rf>f = <a><rf>f if (dia.RegularFormula is SequenceFormula) { var seq = dia.RegularFormula as SequenceFormula; dia.RegularFormula = seq.First; dia.Formula = new Diamond(seq.Sequence, dia.Formula); return(Rewrite(dia)); } else if (dia.RegularFormula is NestedFormula) { var nested = dia.RegularFormula as NestedFormula; if (nested.Multiplier != "") { throw new InvalidProgramException("by now all multipliers should have been removed"); } dia.RegularFormula = nested.Inner; return(Rewrite(dia)); } } // we also need to perform this on all subformulas else if (form is Disjunction) { form = new Disjunction(Rewrite(((Disjunction)form).Left), Rewrite(((Disjunction)form).Right)); } else if (form is Conjunction) { form = new Conjunction(Rewrite(((Conjunction)form).Left), Rewrite(((Conjunction)form).Right)); } else if (form is Mu) { form = new Mu(((Mu)form).Variable, Rewrite(((Mu)form).Formula)); } else if (form is Nu) { form = new Nu(((Nu)form).Variable, Rewrite(((Nu)form).Formula)); } return(form); }
static MuFormula RewriteBoxDia(MuFormula form) { // We extended our grammar from the assignment by allowing regular formulas instead of single-letter actions. // Here, we rewrite such extended formulas to their more primitive versions for which we impemented evaluators. if (form is Diamond) { var dia = form as Diamond; if (dia.RegularFormula.Multiplier == "+") { // <R+>f = <R><R*>f var rf2 = dia.RegularFormula.Clone(); rf2.Multiplier = "*"; dia.RegularFormula.Multiplier = ""; dia.Formula = new Diamond(dia.RegularFormula, new Diamond(rf2, dia.Formula)); } else if (dia.RegularFormula.Multiplier == "*") { // <R*>f = mu X . (<R> X || f) var var = new Variable(GetFreeVar()); dia.RegularFormula.Multiplier = ""; return(Rewrite(new Mu(var, new Disjunction(new Diamond(dia.RegularFormula, var), dia.Formula)))); } dia.Formula = Rewrite(dia.Formula); } else if (form is Box) { var box = form as Box; if (box.RegularFormula.Multiplier == "+") { // [R+]f = [R][R*]f var rf2 = box.RegularFormula.Clone(); rf2.Multiplier = "*"; box.RegularFormula.Multiplier = ""; return(new Box(box.RegularFormula, new Box(rf2, box.Formula))); } else if (box.RegularFormula.Multiplier == "*") { // [R*]f = nu X . ([R] X && f) var var = new Variable(GetFreeVar()); box.RegularFormula.Multiplier = ""; return(Rewrite(new Nu(var, new Disjunction(new Box(box.RegularFormula, var), box.Formula)))); } box.Formula = Rewrite(box.Formula); } // we also need to perform this on all subformulas else if (form is Disjunction) { form = new Disjunction(Rewrite(((Disjunction)form).Left), Rewrite(((Disjunction)form).Right)); } else if (form is Conjunction) { form = new Conjunction(Rewrite(((Conjunction)form).Left), Rewrite(((Conjunction)form).Right)); } else if (form is Mu) { form = new Mu(((Mu)form).Variable, Rewrite(((Mu)form).Formula)); } else if (form is Nu) { form = new Nu(((Nu)form).Variable, Rewrite(((Nu)form).Formula)); } else if (form is Negation) { form = new Negation(Rewrite(((Negation)form).Formula)); } return(form); }