static public Expr genMMA(Expr ep0, ArrayExpr ep1, String op, Parser.ParseResult pr) { if (op == "All") { if (!(ep0 is CompositeExpr)) return ep0; CompositeExpr ce = ep0 as CompositeExpr; if (ce.Head == WellKnownSym.power && ce.Args[1] is WordSym && ce.Args[0] is ArrayExpr) { pr.matrixOperationResult = new CompositeExpr(ce.Args[1], new ArrayExpr((Array)(ce.Args[0] as ArrayExpr).Elts.Clone())); return pr.matrixOperationResult; } int c = ce.Args.Length; Expr[] newExprElts = new Expr[c]; for (int i = 0; i < c; i++) newExprElts[i] = genMMA(ce.Args[i], null, "All", pr); return new CompositeExpr(ce.Head, newExprElts); } Expr newExpr = new CompositeExpr(new WordSym(op), new ArrayExpr((Array)ep1.Elts.Clone())); if (ep0.Equals(ep1) && op == "No Matrix Operation") return ep1; else if (ep0.Equals(ep1) && op != "No Matrix Operation") return newExpr; if (ep0 is CompositeExpr) { CompositeExpr ce = ep0 as CompositeExpr; if (ce.Head == new LetterSym('⇒') || ce.Head == new LetterSym('→')) { if (ce.Args[0].Equals(ep1) || ce.Args[0] is CompositeExpr && (ce.Args[0] as CompositeExpr).Head == WellKnownSym.power && (ce.Args[0] as CompositeExpr).Args[0].Equals(ep1)) return op != "No Matrix Operation" ? newExpr : ep1; else return genMMA(ce.Args[0], ep1, op, null); } else if (ce.Head == WellKnownSym.power && ce.Args[0] is ArrayExpr) { if (ce.Args[0].Equals(ep1)) return op != "No Matrix Operation" ? new CompositeExpr(new WordSym(op), ce.Args[0]) : ce.Args[0]; else if (!(ce.Args[1] is WordSym)) return ce; else return (ce.Args[1] as WordSym).Word != "No Matrix Operation" ? new CompositeExpr(ce.Args[1], ce.Args[0]) : ce.Args[0]; } int c = (ep0 as CompositeExpr).Args.Length; Expr[] newExprElts = new Expr[c]; for (int i = 0; i < c; i++) newExprElts[i] = genMMA((ep0 as CompositeExpr).Args[i], ep1, op, null); return new CompositeExpr((ep0 as CompositeExpr).Head, newExprElts); } return ep0; }
public static ArrayExpr Set_Value_To_Mem(BitVecExpr value, BitVecExpr address, string key, Context ctx) { if (address.SortSize < 64) { address = ctx.MkZeroExt(64 - address.SortSize, address); } Debug.Assert(address.SortSize == 64); uint nBytes = value.SortSize >> 3; ArrayExpr mem = Create_Mem_Key(key, ctx); for (uint i = 0; i < nBytes; ++i) { BitVecExpr address2 = ctx.MkBVAdd(address, ctx.MkBV(i, 64)); mem = ctx.MkStore(mem, address2, ctx.MkExtract((8 * (i + 1)) - 1, 8 * i, value)); } return(mem); }
public static BitVecExpr Create_Value_From_Mem(BitVecExpr address, int nBytes, string key, Context ctx) { Contract.Requires(ctx != null); Contract.Requires(nBytes > 0, "Number of bytes has to larger than zero. nBytes=" + nBytes); using (ArrayExpr mem = Create_Mem_Key(key, ctx)) { BitVecExpr result = ctx.MkSelect(mem, address) as BitVecExpr; for (uint i = 1; i < nBytes; ++i) { BitVecExpr result2 = ctx.MkSelect(mem, ctx.MkBVAdd(address, ctx.MkBV(i, 64))) as BitVecExpr; result = ctx.MkConcat(result2, result); } Debug.Assert(result.SortSize == (nBytes * 8)); return(result); } }
/// <summary> /// Evaluates an array type declaration. /// </summary> /// <returns></returns> public object VisitArray(ArrayExpr expr) { var arrayExprs = expr.Exprs; // Case 1: array type if (arrayExprs != null) { var items = new List <object>(); foreach (var exp in arrayExprs) { object result = exp == null ? null : exp.Evaluate(this); items.Add(result); } var array = new LArray(items); return(array); } return(new LArray(new List <object>())); }
public override IExpr Build(BuildContext ctx, IExprBuilder initiator, Func <IExpr> next) { if (ctx.Token is ArrayParenthesisToken openT && openT.IsOpen) { ctx.NextIndex(); var arrayExpr = new ArrayExpr(Env) { OpenToken = openT, }; IExpr expr; while (true) { expr = next(); ThrowIfExprIsNull(expr, ctx.Token); if (ctx.Token is ArrayParenthesisToken token && !token.IsOpen) { arrayExpr.Elements.Add(expr); arrayExpr.CloseToken = token; break; } if (ctx.Token is SeparatorToken) { arrayExpr.Elements.Add(expr); ctx.NextIndex(); continue; } ctx.PushExpr(expr); } ctx.NextIndex(); arrayExpr.IdentifyExactReturnType(); return(arrayExpr); } return(null); }
public static ArrayExpr Set_Value_To_Mem(BitVecExpr value, BitVecExpr address, string key, Context ctx) { Contract.Requires(value != null); Contract.Requires(address != null); Contract.Requires(ctx != null); BitVecExpr address2 = (address.SortSize < 64) ? ctx.MkZeroExt(64 - address.SortSize, address) : address; Contract.Assume(address2 != null); Contract.Assume(address2.SortSize == 64); uint nBytes = value.SortSize >> 3; ArrayExpr mem = Create_Mem_Key(key, ctx); for (uint i = 0; i < nBytes; ++i) { BitVecExpr address3 = ctx.MkBVAdd(address2, ctx.MkBV(i, 64)); mem = ctx.MkStore(mem, address3, ctx.MkExtract((8 * (i + 1)) - 1, 8 * i, value)); } return(mem); }
public void Run() { using (Context ctx = new Context()) { ArrayExpr a = ctx.MkArrayConst("a", ctx.IntSort, ctx.IntSort); FuncDecl f = ctx.MkFuncDecl("f", ctx.IntSort, ctx.IntSort); ArrayExpr m = ctx.MkMap(f, a); Console.WriteLine(m); Console.WriteLine(m.IsArrayMap); Console.WriteLine(a.IsArrayMap); Console.WriteLine(m.IsSelect); Console.WriteLine(ctx.MkSelect(m, ctx.MkInt(0)).IsSelect); Console.WriteLine(ctx.MkStore(m, ctx.MkInt(0), ctx.MkInt(1)).IsStore); Console.WriteLine(ctx.MkStore(m, ctx.MkInt(0), ctx.MkInt(1))); Console.WriteLine(m.IsStore); Console.WriteLine(m.FuncDecl); Console.WriteLine(m.FuncDecl.Parameters[0].FuncDecl); Console.WriteLine(m.FuncDecl.Parameters[0].FuncDecl[ctx.MkInt(0)]); Console.WriteLine(ctx.MkSelect(m, ctx.MkInt(10))); } }
public void Run() { Dictionary <string, string> cfg = new Dictionary <string, string>() { { "AUTO_CONFIG", "true" }, { "MODEL", "true" } }; using (Context ctx = new Context(cfg)) { ArrayExpr X = ctx.MkArrayConst("A", ctx.IntSort, ctx.IntSort); Expr q = ctx.MkGe( ctx.MkAdd((IntExpr)ctx.MkSelect(X, ctx.MkInt(0)), (IntExpr)ctx.MkSelect(X, ctx.MkInt(1)), (IntExpr)ctx.MkSelect(X, ctx.MkInt(2))), ctx.MkInt(0)); Console.WriteLine(q); Console.WriteLine(q.Simplify()); } }
public void Run() { Dictionary <string, string> cfg = new Dictionary <string, string>() { { "AUTO_CONFIG", "true" }, { "MODEL", "true" } }; using (Context ctx = new Context(cfg)) { ArrayExpr A = ctx.MkArrayConst("A", ctx.IntSort, ctx.IntSort); IntExpr x = ctx.MkIntConst("x"); IntExpr y = ctx.MkIntConst("y"); Solver s = ctx.MkSolver(); s.Assert(ctx.MkEq(ctx.MkSelect(A, x), x)); s.Assert(ctx.MkEq(ctx.MkStore(A, x, y), A)); Console.WriteLine(s); Console.WriteLine(s.Check()); Console.WriteLine(s.Model); } }
public void Run() { Dictionary <string, string> cfg = new Dictionary <string, string>() { { "AUTO_CONFIG", "true" }, { "MODEL", "true" } }; using (Context ctx = new Context(cfg)) { Sort I = ctx.IntSort; ArrayExpr A = ctx.MkArrayConst("A", I, I); IntExpr x = ctx.MkIntConst("x"); Console.WriteLine(ctx.MkSelect(A, x)); Console.WriteLine(ctx.MkStore(A, x, ctx.MkInt(10))); Expr q = ctx.MkSelect(ctx.MkStore(A, ctx.MkInt(2), ctx.MkAdd(x, ctx.MkInt(1))), ctx.MkInt(2)); Console.WriteLine(q); Console.WriteLine(q.Simplify()); } }
private ArrayExpr TempHackNewAE(Array a) { ArrayExpr ae = new ArrayExpr(a); ae.Annotations["Force Parentheses"] = 1; return ae; }
private static IEnumerable <ILineObject> GenerateNormalSkillFunction(PlayerExporter exporter, SkillGeneratorEnv env, string id, bool stateLabelAsUpdate) { List <ILineObject> ret = new List <ILineObject>(); var action = exporter.GetAction(id); var ae = new Pat.ActionEffects(action); foreach (var b in action.Behaviors) { b.MakeEffects(ae); } exporter.SSERecorder.AddAction(ae, env.GetActionID(id), env); ret.AddRange(ae.InitEffects.Select(e => e.Generate(env))); var list2 = ae.UpdateEffects.Select(e => e.Generate(env)); list2 = new ILineObject[] { GenerateNormalSkillUpdateSSEChecker() }.Concat(list2); if (stateLabelAsUpdate) { list2 = list2.Concat(new ILineObject[] { new SimpleLineObject("return true;") }); } var updateFunc = new FunctionBlock("", new string[0], list2).AsExpression(); ILineObject setUpdate; if (stateLabelAsUpdate) { setUpdate = ThisExpr.Instance.MakeIndex("SetUpdateFunction").Call(updateFunc).Statement(); } else { setUpdate = ThisExpr.Instance.MakeIndex("stateLabel").Assign(updateFunc).Statement(); } ret.Add(setUpdate); var keys = ae.SegmentFinishEffects.Select( keyEffect => new FunctionBlock("", new string[0], keyEffect.Select(e => e.Generate(env))).AsExpression()); var keyCount = action.Segments.Count - 1; if (ae.SegmentFinishEffects.Count < keyCount) { keyCount = ae.SegmentFinishEffects.Count; } var arrayObj = new ArrayExpr(keys.Take(keyCount).ToArray()); var setKey = ThisExpr.Instance.MakeIndex("keyAction").Assign(arrayObj).Statement(); ret.Add(new SimpleLineObject("this.SetEndTakeCallbackFunction(this.KeyActionCheck);")); ret.Add(setKey); if (ae.SegmentFinishEffects.Count >= action.Segments.Count) { var effects = ae.SegmentFinishEffects[action.Segments.Count - 1].Select(e => e.Generate(env)); var funcEndMotion = new FunctionBlock("", new string[0], effects).AsExpression(); var setEndMotion = ThisExpr.Instance.MakeIndex("SetEndMotionCallbackFunction").Call(funcEndMotion).Statement(); ret.Add(setEndMotion); } return(ret); }
public Expr Reformat(Expr e) { if (e is DoubleNumber) { double truncated = Math.Truncate((e as DoubleNumber).Num); double decimals = Math.Round(((e as DoubleNumber).Num - truncated) * Math.Pow(10, 15)) / Math.Pow(10, 15); return truncated + decimals; } if (e is ArrayExpr) { Array a = (Array)((ArrayExpr)e).Elts.Clone(); ArrayExpr ae = new ArrayExpr(a); foreach (int[] ix in ae.Indices) ae[ix] = Reformat(ae[ix]); if (!e.Annotations.Contains("Force Parentheses") || !e.Annotations["Force Parentheses"].Equals(0)) ae.Annotations["Force Parentheses"] = 1; return ae; } if (e is CompositeExpr) { List<Expr> args = new List<Expr>(); foreach (Expr a in Args(e)) args.Add(Reformat(a)); if (head(e) == WKSID.power && IsNum(args[1])) { Expr n = Num(args[1]); if (n is IntegerNumber && (int)n < 0) { if ((int)n == -1) return Divide(args[0]); return Divide(Power(args[0], -(int)n)); } } if (head(e) == WKSID.power) { if (Ag(e, 1) is CompositeExpr && head(Ag(e, 1)) == WKSID.divide && Ag(Ag(e, 1), 0) is IntegerNumber && (Ag(Ag(e, 1), 0) as IntegerNumber).Num == 2) return new CompositeExpr(WellKnownSym.root, 2, Reformat(Ag(e, 0))); if (head(Ag(e, 1)) == WKSID.divide && Ag(Ag(e, 1), 0) is IntegerNumber && (Ag(Ag(e, 1), 0) as IntegerNumber).Num < 0) if ((Ag(Ag(e, 1), 0) as IntegerNumber).Num == -2) return new CompositeExpr(WellKnownSym.root, 2, Reformat(Divide(Ag(e, 0)))); else return Divide(Power(Ag(e, 0), new IntegerNumber(-(Ag(Ag(e, 1), 0) as IntegerNumber).Num))); } else if (head(e) == WKSID.plus) { bool reorder = false; List<Expr> negs = new List<Expr>(); List<Expr> pluses = new List<Expr>(); foreach (Expr t in args) { if (head(t) == WKSID.minus || head(t) == WKSID.plusminus || ((t is DoubleNumber && (double)t < 0) || (t is IntegerNumber && (int)t < 0))) negs.Add(t); else if (head(t) == WKSID.times && head(Ag(t, 0)) == WKSID.minus) { Expr[] remainder = new Expr[(t as CompositeExpr).Args.Length]; for (int i = 1; i < (t as CompositeExpr).Args.Length; i++) remainder[i] = (t as CompositeExpr).Args[i]; remainder[0] = Ag(Ag(t, 0), 0); negs.Add(Minus(Mult(remainder))); reorder = true; } else if (head(t) == WKSID.times && (Ag(t, 0) is IntegerNumber && (int)Ag(t, 0) < 0)) { Expr[] remainder = new Expr[(t as CompositeExpr).Args.Length]; for (int i = 0; i < (t as CompositeExpr).Args.Length; i++) remainder[i] = (t as CompositeExpr).Args[i]; remainder[0] = -(int)(Ag(t, 0) as IntegerNumber).Num; negs.Add(Minus(Mult(remainder))); reorder = true; } else { pluses.Add(t); if (negs.Count > 0) reorder = true; } } if (reorder) { foreach (Expr t in negs) pluses.Add(t); return Reformat(new CompositeExpr(WellKnownSym.plus, pluses.ToArray())); } } else if (head(e) == WKSID.times) { bool reorder = false; List<Expr> divs = new List<Expr>(); List<Expr> ndivs = new List<Expr>(); List<Expr> nargs = new List<Expr>(); foreach (Expr t in args) if (head(t) == WKSID.times) foreach (Expr tt in Args(t)) nargs.Add(tt); else nargs.Add(t); foreach (Expr t in nargs) { if (head(t) == WKSID.divide || (head(t) == WKSID.minus && head(Ag(t, 0)) == WKSID.divide)) divs.Add(t); else { ndivs.Add(t); if (divs.Count > 0) reorder = true; } } if (divs.Count > 1 || reorder) { if (divs.Count > 1) { for (int i = 0; i < divs.Count; i++) divs[i] = Ag(divs[i], 0); ndivs.Add(new CompositeExpr(WellKnownSym.divide, new CompositeExpr(WellKnownSym.times, divs.ToArray()))); } else ndivs.Add(divs[0]); return Reformat(new CompositeExpr(WellKnownSym.times, ndivs.ToArray())); } } if (head(e) == WKSID.divide && args[0] is IntegerNumber && (int)args[0] < 0) { return Mult(-1, Divide(-(int)args[0])); } if (head(e) == WKSID.times && args[0] is IntegerNumber && (int)args[0] == 1) { List<Expr> terms = new List<Expr>(); terms.Add(args[1]); for (int i = 2; i < args.Count; i++) terms.Add(args[i]); if (terms.Count == 1) return terms[0]; else return Reformat(new CompositeExpr(WellKnownSym.times, terms.ToArray())); } if (head(e) == WKSID.times && args[0] is IntegerNumber && (int)args[0] == -1) { List<Expr> terms = new List<Expr>(); terms.Add(Minus(args[1])); for (int i = 2; i < args.Count; i++) terms.Add(args[i]); if (terms.Count == 1) return terms[0]; else return Reformat(new CompositeExpr(WellKnownSym.times, terms.ToArray())); } return new CompositeExpr((e as CompositeExpr).Head.Clone(), args.ToArray()); } return e; }
/// <summary> /// This converts from an Expr 1-dimensional array of indices (1-based) to a c# 1-dimensional array of indices (0-based), /// or returns null if not all the indices are known. /// </summary> /// <param name="ae"></param> /// <param name="converter"></param> /// <returns></returns> int[] ConvertToInd(ArrayExpr ae, Converter<Expr, int?> converter) { Debug.Assert(ae.Elts.Rank == 1); int[] inds = new int[ae.Dims[0]]; for (int i = 0; i < inds.Length; i++) { int? ix = converter(ae[i]); if (ix == null) return null; else inds[i] = ix.Value; } return inds; }
private Expr _Numericize(CompositeExpr e) { Expr[] args = new Expr[e.Args.Length]; for (int i = 0; i < e.Args.Length; i++) if ((i != 0 || head(e) != WKSID.index) && !(i == 0 && e.Head == WellKnownSym.summation && e.Args.Length > 1)) args[i] = Numericize(e.Args[i]); else args[i] = e.Args[i]; Expr val = Ag(e, 0); if (e.Head is WellKnownSym) { DoubleNumber dn1 = null; DoubleNumber dn2 = null; IntegerNumber in1 = null; IntegerNumber in2 = null; ComplexNumber cn1 = null; ComplexNumber cn2 = null; if (args.Length > 0) { dn1 = args[0] as DoubleNumber; cn1 = args[0] as ComplexNumber; in1 = args[0] as IntegerNumber; } if (args.Length > 1) { dn2 = args[1] as DoubleNumber; cn2 = args[1] as ComplexNumber; in2 = args[1] as IntegerNumber; } double d1 = 0; double d2 = 0; if (in2 != null) d2 = (double)in2.Num; if (in1 != null) d1 = (double)in1.Num; if (dn2 != null) d2 = dn2.Num; if (dn1 != null) d1 = dn1.Num; bool num1 = dn1 != null || in1 != null; bool num2 = dn2 != null || in2 != null; WKSID id = ((WellKnownSym)e.Head).ID; switch (id) { case WKSID.im: Trace.Assert(args.Length == 1); if (args[0] is RealNumber) return 0.0; else if (args[0] is ComplexNumber) return ((ComplexNumber)args[0]).Im; break; case WKSID.magnitude: if (in1 != null) return in1.Num.abs(); if (dn1 != null) return Math.Abs(dn1.Num); if (cn1 != null) return Numericize(new CompositeExpr(WellKnownSym.root, 2, new CompositeExpr(WellKnownSym.plus, new CompositeExpr(WellKnownSym.power, cn1.Re, 2), new CompositeExpr(WellKnownSym.power, cn1.Im, 2)))); break; case WKSID.minus: Trace.Assert(args.Length == 1); if (num1) if (in1 != null) return new IntegerNumber(-in1.Num); else return -d1; else if (args[0] is ComplexNumber) return NMinus((ComplexNumber)args[0]); else if (args[0] is ArrayExpr) { ArrayExpr ae = new ArrayExpr((Array)((ArrayExpr)args[0]).Elts.Clone()); foreach (int[] i in ae.Indices) ae[i] = Numericize(Minus(ae[i])); if (!args[0].Annotations.Contains("Force Parentheses") || !args[0].Annotations["Force Parentheses"].Equals(0)) ae.Annotations["Force Parentheses"] = 1; return ae; } break; case WKSID.plus: { Trace.Assert(args.Length > 0); if (args.Length == 1) { return args[0]; } if (Array.TrueForAll(args, delegate(Expr a) { return a is ArrayExpr; })) { ArrayExpr[] aes = Array.ConvertAll<Expr, ArrayExpr>(args, delegate(Expr a) { return (ArrayExpr)a; }); int[] dims = aes[0].Dims; bool isok = true; for (int i = 1; isok && i < args.Length; i++) { int[] dd = aes[i].Dims; if (dd.Length != dims.Length) isok = false; for (int j = 0; isok && j < dims.Length; j++) if (dims[j] != dd[j]) isok = false; } if (isok) { ArrayExpr newae = new ArrayExpr((Array)aes[0].Elts.Clone()); foreach (int[] ix in newae.Indices) { newae[ix] = Numericize(Plus(Array.ConvertAll<ArrayExpr, Expr>(aes, delegate(ArrayExpr ae) { return ae[ix]; }))); } newae.Annotations["Force Parentheses"] = 1; return newae; } } List<Expr> leftover = new List<Expr>(); double rsum = 0; double isum = 0; IntegerNumber risum = 0; bool anyd = false, anyc = false, anyi = false; foreach (Expr p in args) { DoubleNumber dn = p as DoubleNumber; IntegerNumber inn = p as IntegerNumber; ComplexNumber cn = p as ComplexNumber; if (dn != null) { if (anyi) { rsum = dn.Num + (double)risum.Num; anyi = false; } else rsum += dn.Num; anyd = true; } else if (inn != null) { if (anyd) rsum += (double)inn.Num; else { risum = risum.Num + inn.Num; anyi = true; } } else if (cn != null) { DoubleNumber red = cn.Re as DoubleNumber; DoubleNumber imd = cn.Im as DoubleNumber; if (red != null && imd != null) { if (anyi) rsum = red.Num + (double)risum.Num; else rsum += red.Num; isum += imd.Num; anyd = true; anyc = true; anyi = false; } else leftover.Add(p); } else leftover.Add(p); } Number rn = anyd ? (Number)new DoubleNumber(rsum) : (Number)risum; Number n = anyc ? (Number)new ComplexNumber(rsum, isum) : (Number)rn; if (leftover.Count == 0) return n; else { if (anyd || anyi || anyc) leftover.Add(n); return new CompositeExpr(e.Head, leftover.ToArray()); } } case WKSID.mod: if (args.Length != 2) break; if (in1 != null && in2 != null) return new IntegerNumber(in1.Num % in2.Num); else if (in1 != null && dn2 != null) return Math.IEEERemainder(in1.Num.AsDouble(), dn2.Num); else if (dn1 != null && in2 != null) return Math.IEEERemainder(dn1.Num, in2.Num.AsDouble()); else if (dn1 != null && dn2 != null) return Math.IEEERemainder(dn1.Num, dn2.Num); break; case WKSID.power: if (args.Length < 2) break; Trace.Assert(args.Length == 2); if (num1 && (d1 >= 0 || in2 != null) && num2) { double pow = Math.Pow(d1, d2); return in1 != null && in2 != null && d2 > 0 ? (Expr)new IntegerNumber((int)pow) : (Expr)new DoubleNumber(pow); } else { if (num1) cn1 = new ComplexNumber(d1, 0.0); if (num2) cn2 = new ComplexNumber(d2, 0.0); if (cn1 != null && cn2 != null) return NPower(cn1, cn2); } if (num2 && d2 == 0) return new DoubleNumber(1); if (num2 && d2 == 1) return args[0]; if (args[0] is ArrayExpr && args[1] == new LetterSym('T')) { // matrix transpose // FIXME: actually, this should probably be turned into a wellknownsym "transpose" by parse2. Issue there is how to know T isn't being // used as a variable? ArrayExpr ae = (ArrayExpr)args[0]; if (ae.Elts.Rank == 1) { Expr[,] n = new Expr[ae.Elts.Length, 1]; for (int i = 0; i < ae.Elts.Length; i++) { n[i, 0] = ae[i]; } ae = new ArrayExpr(n); ae.Annotations["Force Parentheses"] = 1; } else if (ae.Elts.Rank == 2) { int h = ae.Elts.GetLength(0); int w = ae.Elts.GetLength(1); Expr[,] n = new Expr[w, h]; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { n[j, i] = ae[i, j]; } } ae = new ArrayExpr(n); ae.Annotations["Force Parentheses"] = 1; return ae; } } break; case WKSID.re: Trace.Assert(args.Length == 1); if (num1) return args[0]; else if (cn1 != null) return cn1.Re; break; case WKSID.times: Trace.Assert(args.Length > 0); if (args.Length == 1) { return args[0]; } else { List<Expr> leftover = new List<Expr>(); int start = 0; while (start < args.Length) { int end; bool isarray = args[start] is ArrayExpr; for (end = start + 1; end < args.Length; end++) { if (isarray != (args[end] is ArrayExpr)) break; } leftover.AddRange(isarray ? NMultiplyArrays(args, start, end) : NMultiplyScalars(args, start, end)); start = end; } if (leftover.Count == 1) { return leftover[0]; } else { leftover = this.NMultipyAll(args, e); if (leftover.Count == 1) return leftover[0]; else return new CompositeExpr(e.Head, leftover.ToArray()); } //original code before matrix scalar computations //if(leftover.Count == 1) return leftover[0]; //else //return new CompositeExpr(e.Head, leftover.ToArray()); } case WKSID.divide: Trace.Assert(args.Length == 1); if (num1) return 1 / d1; else if (args[0] is ComplexNumber) return NReciprocal((ComplexNumber)args[0]); break; case WKSID.ln: Trace.Assert(args.Length == 1); if (num1) { if (d1 >= 0) return Math.Log(d1); return NLog(d1); } else if (args[0] is ComplexNumber) return NLog((ComplexNumber)args[0]); break; case WKSID.log: /* log takes base then value to take the logarithm of */ if (args.Length == 1) { dn2 = dn1; in2 = in1; cn2 = cn1; d2 = d1; num2 = num1; d1 = 10; dn1 = 10; in1 = null; cn1 = null; num1 = true; } if (num1 && num2) { if (d1 >= 0 && num2 && d2 >= 0) return Math.Log(d2, d1); else return double.NaN; } else { if (num1) cn1 = new ComplexNumber(d1, 0.0); if (num2) cn2 = new ComplexNumber(d2, 0.0); if (cn1 != null && cn2 != null) return NTimes(NLog(cn2), NReciprocal(NLog(cn1))); } break; case WKSID.root: /* takes root number (eg 2 for square root), value to take the root of */ Trace.Assert(args.Length == 2); if (num1 && num2 && d2 >= 0) return Math.Pow(d2, 1 / d1); else { if (num1) cn1 = new ComplexNumber(d1, 0.0); if (num2) cn2 = new ComplexNumber(d2, 0.0); if (cn1 != null && cn2 != null) return NPower(cn2, NReciprocal(cn1)); } if (head(args[1]) == WKSID.power) return Numericize(Power(Ag(args[1], 0), Mult(Ag(args[1], 1), Divide(args[0])))); break; case WKSID.index: { bool didnumericize = false; if (!(args[0] is ArrayExpr)) { args[0] = Numericize(args[0]); didnumericize = true; } ArrayExpr aex = null, aix = null; if (!IsArrayIndex(e, ref aex, ref aix)) { if (!didnumericize) args[0] = Numericize(args[0]); break; } int[] ix = ConvertToInd(aix, ExprToInd); if (ix == null) { if (!didnumericize) args[0] = Numericize(args[0]); break; } return Numericize(aex[ix]); } case WKSID.sin: Trace.Assert(args.Length == 1); if (head(val) == WKSID.times && Ag(val, 1) == _degree) return Numericize(new CompositeExpr(WellKnownSym.sin, Mult(WellKnownSym.pi, Num(Mult(Ag(val, 0), Divide(180)))))); if (num1) return Math.Sin(d1); else if (cn1 != null) return NSin(cn1); break; case WKSID.cos: Trace.Assert(args.Length == 1); if (head(val) == WKSID.times && Ag(val, 1) == _degree) return Numericize(new CompositeExpr(WellKnownSym.cos, Mult(WellKnownSym.pi, Num(Mult(Ag(val, 0), Divide(180)))))); if (num1) return Math.Cos(d1); else if (cn1 != null) return NCos(cn1); break; case WKSID.tan: Trace.Assert(args.Length == 1); if (head(val) == WKSID.times && Ag(val, 1) == _degree) return Numericize(new CompositeExpr(WellKnownSym.tan, Mult(WellKnownSym.pi, Num(Mult(Ag(val, 0), Divide(180)))))); if (num1) return Math.Tan(d1); else if (cn1 != null) return NTan(cn1); break; case WKSID.sec: Trace.Assert(args.Length == 1); if (head(val) == WKSID.times && Ag(val, 1) == _degree) return Numericize(new CompositeExpr(WellKnownSym.sec, Mult(WellKnownSym.pi, Num(Mult(Ag(val, 0), Divide(180)))))); if (num1) return 1 / Math.Cos(d1); else if (cn1 != null) return NReciprocal(NCos(cn1)); break; case WKSID.csc: Trace.Assert(args.Length == 1); if (head(val) == WKSID.times && Ag(val, 1) == _degree) return Numericize(new CompositeExpr(WellKnownSym.csc, Mult(WellKnownSym.pi, Num(Mult(Ag(val, 0), Divide(180)))))); if (num1) return 1 / Math.Sin(d1); else if (args[0] is ComplexNumber) return NReciprocal(NSin(cn1)); break; case WKSID.cot: Trace.Assert(args.Length == 1); if (head(val) == WKSID.times && Ag(val, 1) == _degree) return Numericize(new CompositeExpr(WellKnownSym.cot, Mult(WellKnownSym.pi, Num(Mult(Ag(val, 0), Divide(180)))))); if (num1) return 1 / Math.Tan(d1); if (cn1 != null) return NReciprocal(NTan(cn1)); break; case WKSID.asin: Trace.Assert(args.Length == 1); if (num1) return Math.Asin(d1); if (cn1 != null) return NArcSin(cn1); break; case WKSID.acos: Trace.Assert(args.Length == 1); if (head(val) == WKSID.times && Ag(val, 1) == _degree) return Numericize(new CompositeExpr(WellKnownSym.acos, Mult(WellKnownSym.pi, Num(Mult(Ag(val, 0), Divide(180)))))); if (num1) return Math.Acos(d1); if (cn1 != null) return NArcCos(cn1); break; case WKSID.atan: Trace.Assert(args.Length == 1); if (num1) return Math.Atan(d1); if (cn1 != null) return NArcTan(cn1); break; case WKSID.asec: Trace.Assert(args.Length == 1); if (num1) return Math.Acos(1 / d1); if (cn1 != null) return NArcCos(NReciprocal(cn1)); break; case WKSID.acsc: Trace.Assert(args.Length == 1); if (num1) return Math.Asin(1 / d1); if (cn1 != null) return NArcSin(NReciprocal(cn1)); break; case WKSID.acot: Trace.Assert(args.Length == 1); if (num1) return Math.Atan(1 / d1); if (cn1 != null) return NArcTan(NReciprocal(cn1)); break; case WKSID.sinh: Trace.Assert(args.Length == 1); if (num1) return Math.Sinh(d1); if (cn1 != null) return NSinH(cn1); break; case WKSID.cosh: Trace.Assert(args.Length == 1); if (num1) return Math.Cosh(d1); if (cn1 != null) return NCosH(cn1); break; case WKSID.tanh: Trace.Assert(args.Length == 1); if (num1) return Math.Tanh(d1); if (cn1 != null) return NTanH(cn1); break; case WKSID.sech: Trace.Assert(args.Length == 1); if (num1) return 1 / Math.Cosh(d1); if (cn1 != null) return NReciprocal(NCosH(cn1)); break; case WKSID.csch: Trace.Assert(args.Length == 1); if (num1) return 1 / Math.Sinh(d1); if (cn1 != null) return NReciprocal(NSinH(cn1)); break; case WKSID.coth: Trace.Assert(args.Length == 1); if (num1) return 1 / Math.Tanh(d1); if (cn1 != null) return NReciprocal(NTanH(cn1)); break; case WKSID.asinh: case WKSID.acosh: case WKSID.atanh: case WKSID.asech: case WKSID.acsch: case WKSID.acoth: /* C# library doesn't contain these functions */ break; case WKSID.factorial: Trace.Assert(args.Length == 1); if (in1 != null && in1.Num >= 0) return Factorial(in1.Num); break; case WKSID.summation: Trace.Assert(args.Length > 0 && args.Length < 4); if (args.Length == 3 && num2 && e.Args[0] is CompositeExpr && !(e.Args[2] is NullExpr)) { CompositeExpr ce = e.Args[0] as CompositeExpr; Expr key = new NullExpr(); d1 = 0; if (ce != null && ce.Head == WellKnownSym.equals && (ce.Args[0] is LetterSym || ce.Args[0] is WellKnownSym)) { key = ce.Args[0].Clone(); Expr start = Numericize(ce.Args[1]); IntegerNumber sinn = start as IntegerNumber; DoubleNumber sdn = start as DoubleNumber; d1 = (sinn != null) ? (double)sinn.Num : sdn != null ? sdn.Num : double.NaN; } Expr res = new IntegerNumber(0); for (int d = (int)d1; d1 <= d2 && d <= (int)d2; d++) { Expr newB = (Expr)e.Args[2].Clone(); newB = Numericize(_Substitute(newB, key, d)); res = Plus(res, newB); res = Numericize(_Simplify(res)); } return res; } break; } } return new CompositeExpr(Numericize(e.Head), args); }
private Expr _Substitute(ArrayExpr e) { Array a = (Array)e.Elts.Clone(); ArrayExpr ae = new ArrayExpr(a); foreach (int[] ix in ae.Indices) ae[ix] = Substitute(ae[ix]); if (!e.Annotations.Contains("Force Parentheses") || !e.Annotations["Force Parentheses"].Equals(0)) ae.Annotations["Force Parentheses"] = 1; return ae; }
private Expr _Canonicalize(ArrayExpr e) { return e; }
public void Run() { using (Context ctx = new Context()) { RealExpr x = ctx.MkRealConst("x"); RealExpr y = ctx.MkRealConst("y"); RealExpr z = ctx.MkRealConst("z"); FuncDecl f = ctx.MkFuncDecl("f", ctx.RealSort, ctx.RealSort); Solver s = ctx.MkSolver(); s.Assert(ctx.MkGt(x, ctx.MkReal(10)), ctx.MkEq(y, ctx.MkAdd(x, ctx.MkReal(3))), ctx.MkLt(y, ctx.MkReal(15)), ctx.MkGt((RealExpr)f[x], ctx.MkReal(2)), ctx.MkNot(ctx.MkEq(f[y], f[x]))); Console.WriteLine(s.Check()); Model m = s.Model; foreach (FuncDecl fd in m.Decls) { Console.Write(" " + fd.Name); } Console.WriteLine(); foreach (FuncDecl fd in m.Decls) { if (fd.DomainSize == 0) { Console.WriteLine(fd.Name + " -> " + m.ConstInterp(fd)); } else { Console.WriteLine(fd.Name + " -> " + m.FuncInterp(fd)); } } Console.WriteLine(m.Evaluate(ctx.MkAdd(z, ctx.MkReal(1)))); Console.WriteLine(m.Evaluate(ctx.MkAdd(z, ctx.MkReal(1)), true)); Console.WriteLine(m.Evaluate(z)); FuncInterp fi = m.FuncInterp(f); Console.WriteLine(fi.Else); Console.WriteLine(fi.NumEntries); Console.WriteLine(fi.Entries[0]); Console.WriteLine(fi.Entries[0].NumArgs); Console.WriteLine(fi.Entries[0].Args[0]); Console.WriteLine(fi.Entries[0].Value); ArrayExpr a = ctx.MkArrayConst("a", ctx.RealSort, ctx.RealSort); s.Assert(ctx.MkGt((RealExpr)ctx.MkSelect(a, x), ctx.MkReal(10)), ctx.MkGt((RealExpr)ctx.MkSelect(a, y), ctx.MkReal(20))); Console.WriteLine(s); Console.WriteLine(s.Check()); Console.WriteLine(s.Model); Console.WriteLine(s.Model.Evaluate(a)); Console.WriteLine(s.Model.FuncInterp(a.FuncDecl)); } }
static public Expr genMMA(Expr ep0, ArrayExpr ep1, String op, Parser.ParseResult pr) { if (op == "All") { if (!(ep0 is CompositeExpr)) { return(ep0); } CompositeExpr ce = ep0 as CompositeExpr; if (ce.Head == WellKnownSym.power && ce.Args[1] is WordSym && ce.Args[0] is ArrayExpr) { pr.matrixOperationResult = new CompositeExpr(ce.Args[1], new ArrayExpr((Array)(ce.Args[0] as ArrayExpr).Elts.Clone())); return(pr.matrixOperationResult); } int c = ce.Args.Length; Expr[] newExprElts = new Expr[c]; for (int i = 0; i < c; i++) { newExprElts[i] = genMMA(ce.Args[i], null, "All", pr); } return(new CompositeExpr(ce.Head, newExprElts)); } Expr newExpr = new CompositeExpr(new WordSym(op), new ArrayExpr((Array)ep1.Elts.Clone())); if (ep0.Equals(ep1) && op == "No Matrix Operation") { return(ep1); } else if (ep0.Equals(ep1) && op != "No Matrix Operation") { return(newExpr); } if (ep0 is CompositeExpr) { CompositeExpr ce = ep0 as CompositeExpr; if (ce.Head == new LetterSym('⇒') || ce.Head == new LetterSym('→')) { if (ce.Args[0].Equals(ep1) || ce.Args[0] is CompositeExpr && (ce.Args[0] as CompositeExpr).Head == WellKnownSym.power && (ce.Args[0] as CompositeExpr).Args[0].Equals(ep1)) { return(op != "No Matrix Operation" ? newExpr : ep1); } else { return(genMMA(ce.Args[0], ep1, op, null)); } } else if (ce.Head == WellKnownSym.power && ce.Args[0] is ArrayExpr) { if (ce.Args[0].Equals(ep1)) { return(op != "No Matrix Operation" ? new CompositeExpr(new WordSym(op), ce.Args[0]) : ce.Args[0]); } else if (!(ce.Args[1] is WordSym)) { return(ce); } else { return((ce.Args[1] as WordSym).Word != "No Matrix Operation" ? new CompositeExpr(ce.Args[1], ce.Args[0]) : ce.Args[0]); } } int c = (ep0 as CompositeExpr).Args.Length; Expr[] newExprElts = new Expr[c]; for (int i = 0; i < c; i++) { newExprElts[i] = genMMA((ep0 as CompositeExpr).Args[i], ep1, op, null); } return(new CompositeExpr((ep0 as CompositeExpr).Head, newExprElts)); } return(ep0); }
public virtual void Visit(ArrayExpr expr) { }
public void Test_Z3_MemWithArray_4() { #region Definitions uint nBits = 8; Context ctx = new Context(); BitVecExpr bv_0 = ctx.MkBV(0, nBits); BitVecExpr bv_16 = ctx.MkBV(16, nBits); BitVecExpr bv_32 = ctx.MkBV(32, nBits); BitVecExpr rax = ctx.MkBVConst("RAX!0", nBits); BitVecExpr rbx = ctx.MkBVConst("RBX!0", nBits); BitVecExpr rcx = ctx.MkBVConst("RCX!0", nBits); BitVecExpr rdx = ctx.MkBVConst("RDX!0", nBits); IList <(BitVecExpr, BitVecExpr)> writes = new List <(BitVecExpr, BitVecExpr)>(); Goal state = ctx.MkGoal(); ArrayExpr mem = ctx.MkArrayConst("mem", ctx.MkBitVecSort(nBits), ctx.MkBitVecSort(nBits)); #endregion // Test if overwriting a address works Console.WriteLine("mov rbx, 16"); state.Assert(ctx.MkEq(rbx, bv_16)); Console.WriteLine("mov rcx, 32"); state.Assert(ctx.MkEq(rcx, bv_32)); // Console.WriteLine("mov rax, 0"); // state.Assert(ctx.MkEq(rax, bv_0)); Console.WriteLine("mov qword ptr[rax], rbx"); mem = ctx.MkStore(mem, rax, rbx); Console.WriteLine("mov qword ptr[rax], rcx"); mem = ctx.MkStore(mem, rax, rcx); Console.WriteLine("mov rdx, qword ptr[rax]"); state.Assert(ctx.MkEq(rdx, ctx.MkSelect(mem, rax))); #region Write to console Solver solver = ctx.MkSolver(); Solver solver_U = ctx.MkSolver(); solver.Assert(state.Formulas); Console.WriteLine(string.Empty); Console.WriteLine("state1=" + state); if (true) { Tactic tactic1 = ctx.MkTactic("propagate-values"); Goal state2 = tactic1.Apply(state).Subgoals[0]; Console.WriteLine("state2=" + state2.ToString()); } Console.WriteLine(string.Empty); Tv[] raxTV = ToolsZ3.GetTvArray(rax, (int)nBits, solver, solver_U, ctx); Console.WriteLine("rax = " + ToolsZ3.ToStringBin(raxTV) + " = " + ToolsZ3.ToStringHex(raxTV)); Tv[] rbxTV = ToolsZ3.GetTvArray(rbx, (int)nBits, solver, solver_U, ctx); Console.WriteLine("rbx = " + ToolsZ3.ToStringBin(rbxTV) + " = " + ToolsZ3.ToStringHex(rbxTV)); Tv[] rcxTV = ToolsZ3.GetTvArray(rcx, (int)nBits, solver, solver_U, ctx); Console.WriteLine("rcx = " + ToolsZ3.ToStringBin(rcxTV) + " = " + ToolsZ3.ToStringHex(rcxTV)); Tv[] rdxTV = ToolsZ3.GetTvArray(rdx, (int)nBits, solver, solver_U, ctx); Console.WriteLine("rdx = " + ToolsZ3.ToStringBin(rdxTV) + " = " + ToolsZ3.ToStringHex(rdxTV)); #endregion }
public object VisitArray(ArrayExpr expr) { return(null); }
bool IsArrayIndex(Expr e, ref ArrayExpr ae, ref ArrayExpr ai) { if (Args(e).Length != 2) return false; if (!(Ag(e, 0) is ArrayExpr)) return false; if (!(Ag(e, 1) is ArrayExpr && ((ArrayExpr)Ag(e, 1)).Elts.Rank == 1)) return false; ae = (ArrayExpr)Ag(e, 0); ai = (ArrayExpr)Ag(e, 1); if (ai.Dims[0] != ae.Elts.Rank) return false; return true; }