/// <summary> /// Resolve all variables in expression to either literal values /// or to orignal declared nameSegments /// </summary> /// <param name="guard"></param> /// <param name="state"></param> /// <returns></returns> public static IEnumerable <ExpressionTree> ResolveExpression(ExpressionTree guard, ProofState state) { Contract.Requires <ArgumentNullException>(guard != null, "guard"); Contract.Requires <ArgumentNullException>(state != null, "state"); if (guard.IsLeaf()) { if (!(guard.Data is NameSegment)) { yield return(guard.Copy()); } var newGuard = guard.Copy(); var result = EvaluateLeaf(newGuard, state); foreach (var item in result) { Contract.Assert(result != null); Expression newNs = null; // potential encapsulation problems if (item is MemberDecl) { var md = item as MemberDecl; newNs = new StringLiteralExpr(new Token(), md.Name, true); } else if (item is Formal) { var tmp = item as Formal; newNs = new NameSegment(tmp.tok, tmp.Name, null); } else if (item is NameSegment) { newNs = item as NameSegment; } else { newNs = item as Expression; // Dafny.LiteralExpr; } newGuard.Data = newNs; yield return(newGuard); } } else { foreach (var lChild in ResolveExpression(guard.LChild, state)) { if (guard.RChild != null) { foreach (var rChild in ResolveExpression(guard.RChild, state)) { yield return(new ExpressionTree(guard.Data, null, lChild, rChild)); } } else { yield return(new ExpressionTree(guard.Data, null, lChild, null)); } } } }
private IEnumerable<Expression> ReplaceConstants(ExpressionTree expression, List<Expression> constants, List<IVariable> vars) { if (expression == null) yield break; if (expression.Root == null) expression.SetRoot(); if (expression.IsLeaf()) { if (HasConstant(expression.Data, constants)) { foreach (var var in vars) { if (!ValidateType(var, expression.Parent.TreeToExpression() as BinaryExpr)) continue; var newVal = ExpressionTree.ExpressionToTree(VariableToExpression(var)); var copy = expression.Root.Copy(); var newTree = ExpressionTree.FindAndReplaceNode(copy, newVal, expression.Copy()); yield return newTree.Root.TreeToExpression(); foreach (var item in ReplaceConstants(newTree.Root, constants, vars)) yield return item; } } } else { foreach (var item in ReplaceConstants(expression.LChild, constants, vars)) yield return item; foreach (var item in ReplaceConstants(expression.RChild, constants, vars)) yield return item; } }
private void ReplaceTerm(Expression old_singleton, Expression new_term, ExpressionTree formula, ref List <Expression> nexp) { Contract.Requires(nexp != null); Contract.Requires(old_singleton != null); Contract.Requires(new_term != null); NameSegment curNs = null; NameSegment oldNs = null; if (formula == null) { return; } if (formula.isLeaf()) { if (formula.data.GetType() == old_singleton.GetType() && formula.was_replaced == false) { if (formula.data is NameSegment) { curNs = (NameSegment)formula.data; oldNs = (NameSegment)old_singleton; } else if (formula.data is UnaryOpExpr) { curNs = (NameSegment)((UnaryOpExpr)formula.data).E; oldNs = (NameSegment)((UnaryOpExpr)old_singleton).E; } else { Contract.Assert(false, Util.Error.MkErr(formula.data, -1)); } if (curNs.Name == oldNs.Name) { ExpressionTree nt = formula.Copy(); nt.data = new_term; if (nt.parent.lChild == nt) { nt.parent.lChild = nt; } else { nt.parent.rChild = nt; } nexp.Add(nt.root.TreeToExpression()); } } return; } ReplaceTerm(old_singleton, new_term, formula.lChild, ref nexp); ReplaceTerm(old_singleton, new_term, formula.rChild, ref nexp); }
private IEnumerable<Expression> ReplaceVar(Dictionary<Expression, List<Expression>> vars, ExpressionTree expression) { if (expression == null) yield break; if (expression.Root == null) expression.SetRoot(); if (expression.IsLeaf()) { foreach (var kvp in vars) { if (SingletonEquality(expression.Data, kvp.Key)) { foreach (var var in kvp.Value) { // safeguard against infinite loop if (SingletonEquality(kvp.Key, var)) continue; ExpressionTree newVal = RewriteExpression(expression, kvp.Key, var); if (newVal == null) break; var copy = expression.Root.Copy(); var newTree = ExpressionTree.FindAndReplaceNode(copy, newVal, expression.Copy()); yield return newTree.Root.TreeToExpression(); foreach (var item in ReplaceVar(vars, newTree.Root)) yield return item; } } } } else { foreach (var item in ReplaceVar(vars, expression.LChild)) yield return item; foreach (var item in ReplaceVar(vars, expression.RChild)) yield return item; } }
private void ReplaceTerm(Expression old_singleton, Expression new_term, ExpressionTree formula, ref List<Expression> nexp) { Contract.Requires(nexp != null); Contract.Requires(old_singleton != null); Contract.Requires(new_term != null); NameSegment curNs = null; NameSegment oldNs = null; if (formula == null) return; if (formula.isLeaf()) { if (formula.data.GetType() == old_singleton.GetType() && formula.was_replaced == false) { if (formula.data is NameSegment) { curNs = (NameSegment)formula.data; oldNs = (NameSegment)old_singleton; } else if (formula.data is UnaryOpExpr) { curNs = (NameSegment)((UnaryOpExpr)formula.data).E; oldNs = (NameSegment)((UnaryOpExpr)old_singleton).E; } else Contract.Assert(false, Util.Error.MkErr(formula.data, -1)); if (curNs.Name == oldNs.Name) { ExpressionTree nt = formula.Copy(); nt.data = new_term; if (nt.parent.lChild == nt) nt.parent.lChild = nt; else nt.parent.rChild = nt; nexp.Add(nt.root.TreeToExpression()); } } return; } ReplaceTerm(old_singleton, new_term, formula.lChild, ref nexp); ReplaceTerm(old_singleton, new_term, formula.rChild, ref nexp); }
/// <summary> /// Resolve all variables in expression to either literal values /// or to orignal declared nameSegments /// </summary> /// <param name="guard"></param> /// <param name="state"></param> /// <returns></returns> public static IEnumerable<ExpressionTree> ResolveExpression(ExpressionTree guard, ProofState state) { Contract.Requires<ArgumentNullException>(guard != null, "guard"); Contract.Requires<ArgumentNullException>(state != null, "state"); if (guard.IsLeaf()) { if (!(guard.Data is NameSegment)) { yield return guard.Copy(); } var newGuard = guard.Copy(); var result = EvaluateLeaf(newGuard, state); foreach (var item in result) { Contract.Assert(result != null); Expression newNs = null; // potential encapsulation problems if (item is MemberDecl) { var md = item as MemberDecl; newNs = new StringLiteralExpr(new Token(), md.Name, true); } else if (item is Formal) { var tmp = item as Formal; newNs = new NameSegment(tmp.tok, tmp.Name, null); } else if (item is NameSegment) { newNs = item as NameSegment; } else { newNs = item as Expression; // Dafny.LiteralExpr; } newGuard.Data = newNs; yield return newGuard; } } else { foreach (var lChild in ResolveExpression(guard.LChild, state)) { if (guard.RChild != null) { foreach (var rChild in ResolveExpression(guard.RChild, state)) { yield return new ExpressionTree(guard.Data, null, lChild, rChild); } } else { yield return new ExpressionTree(guard.Data, null, lChild, null); } } } }