private CardExpr MkAnyUnion(DependencyNode node, AppFreeCanUnn arg) { CardExpr expr = MkConstantCount(arg); if (expr.IsInfinity) { return(expr); } foreach (var m in arg.NonRangeMembers) { if (m.Kind != SymbolKind.UserSortSymb) { continue; } var data = varMap[((UserSortSymb)m).DataSymbol]; if (data.Item1 == node) { return(new CardCnst(Cardinality.Infinity)); } else { expr += data.Item2; } } return(expr); }
/// <summary> /// The unn must contain some constant. /// In this case, returns a constant. /// </summary> /// <param name="unn"></param> /// <returns></returns> public Term GetSomeConstant(AppFreeCanUnn unn) { Contract.Requires(unn != null && unn.ContainsConstants); bool wasAdded; if (!unn.RangeMembers.IsEmpty()) { var rng = unn.RangeMembers.First(); return(Index.MkCnst(new Rational(rng.Key, BigInteger.One), out wasAdded)); } BaseSortSymb bs; foreach (var s in unn.NonRangeMembers) { switch (s.Kind) { case SymbolKind.BaseCnstSymb: case SymbolKind.UserCnstSymb: return(Index.MkApply(s, TermIndex.EmptyArgs, out wasAdded)); case SymbolKind.BaseSortSymb: { bs = (BaseSortSymb)s; switch (bs.SortKind) { case BaseSortKind.Real: case BaseSortKind.Integer: case BaseSortKind.Natural: return(Index.ZeroValue); case BaseSortKind.PosInteger: return(Index.OneValue); case BaseSortKind.NegInteger: return(Index.MkCnst(-Rational.One, out wasAdded)); case BaseSortKind.String: return(Index.EmptyStringValue); default: throw new NotImplementedException(); } } default: continue; } } throw new NotImplementedException(); }
public AppFreeCanUnn GetUnion(Term t) { Contract.Requires(t != null && t.Groundness != Groundness.Variable); AppFreeCanUnn unn; if (!termToUnn.TryFindValue(t, out unn)) { unn = new AppFreeCanUnn(t); termToUnn.Add(t, unn); } return(unn); }
private Z3Expr MkCoercion(Z3Expr t, AppFreeCanUnn unn, EnumEmbedding te) { var coercions = DefaultMember.Item2; foreach (var s in unn.NonRangeMembers) { Contract.Assert(s.IsNonVarConstant); coercions = t.Eq(Context, te.MkGround(s, null)).Ite( Context, MkGround(s, null), coercions); } return(coercions); }
private Z3Expr MkCoercion(Z3Expr t, AppFreeCanUnn unn, NaturalEmbedding te) { var zero = Context.MkInt(0); var one = Context.MkInt(1); var i = (Z3IntExpr)te.UnboxingFun.Apply(t); var negCase = BoxingFun.Apply(i.Neg(Context)); var coercion = i.Ge(Context, zero).Ite( Context, BoxingFun.Apply(one.Sub(Context, i)), negCase); return(coercion); }
private Z3Expr MkCoercion(Z3Expr t, AppFreeCanUnn unn, IntRangeEmbedding te) { Z3BVExpr translate; var unbox = (Z3BVExpr)te.UnboxingFun.Apply(t); if (te.Lower <= Lower) { translate = unbox.BVSub(Context, Context.MkBV((Lower - te.Lower).ToString(), unbox.SortSize)).FitBV(Context, bvSort.Size); } else { translate = unbox.FitBV(Context, bvSort.Size).BVAdd(Context, Context.MkBV((te.Lower - Lower).ToString(), bvSort.Size)); } return(BoxingFun.Apply(translate)); }
private Z3Expr MkCoercion(Z3Expr t, AppFreeCanUnn unn, UnionEmbedding te) { Z3Expr unbox; //// If the union contains string, then it cannot contain other strings. var test = te.MkTestAndUnbox(Index.SymbolTable.GetSortSymbol(BaseSortKind.String), t, out unbox); if (test != null) { return(test.Ite(Context, unbox, DefaultMember.Item2)); } ITypeEmbedding tep; Z3Expr coercions = DefaultMember.Item2; foreach (var s in unn.NonRangeMembers) { if (s.Kind != SymbolKind.BaseCnstSymb) { continue; } test = te.MkTestAndUnbox(s, t, out unbox); Contract.Assert(test != null); tep = Owner.GetEmbedding(unbox.Sort); switch (tep.Kind) { case TypeEmbeddingKind.Enum: coercions = test.And(Context, unbox.Eq(Context, tep.MkGround(s, null))).Ite( Context, MkGround(s, null), coercions); break; case TypeEmbeddingKind.Singleton: coercions = test.Ite( Context, MkGround(s, null), coercions); break; default: throw new NotImplementedException(); } } return(coercions); }
private Z3Expr MkCoercion(Z3Expr t, AppFreeCanUnn unn, UnionEmbedding te) { Z3BoolExpr test; Z3Expr unboxed; ITypeEmbedding ubxTE; var coercions = DefaultMember.Item2; foreach (var s in unn.NonRangeMembers) { test = te.MkTestAndUnbox(s, t, out unboxed); Contract.Assert(test != null); ubxTE = Owner.GetEmbedding(unboxed.FuncDecl.Range); test = test.And(Context, ubxTE.MkGround(s, null).Eq(Context, unboxed)); coercions = test.Ite(Context, MkGround(s, null), coercions); } return(coercions); }
private Z3Expr MkCoercion(Z3Expr t, AppFreeCanUnn unn, ITypeEmbedding te) { var coercions = DefaultMember.Item2; bool wasAdded; ITypeEmbedding boxedTE; foreach (var s in unn.NonRangeMembers) { var boxer = GetBoxer(s); Contract.Assert(s != null); boxedTE = Owner.GetEmbedding(boxer.AccessorDecls[0].Range); coercions = te.MkTest(t, Index.MkApply(s, TermIndex.EmptyArgs, out wasAdded)).Ite( Context, boxer.ConstructorDecl.Apply(boxedTE.MkCoercion(t)), coercions); } Term rngType; foreach (var kv in unn.RangeMembers) { foreach (var boxer in GetBoxers(kv.Key, kv.Value)) { boxedTE = Owner.GetEmbedding(boxer.AccessorDecls[0].Range); rngType = Index.MkApply( Index.RangeSymbol, new Term[] { Index.MkCnst(new Rational(kv.Key, BigInteger.One), out wasAdded), Index.MkCnst(new Rational(kv.Value, BigInteger.One), out wasAdded) }, out wasAdded); coercions = te.MkTest(t, rngType).Ite( Context, boxer.ConstructorDecl.Apply(boxedTE.MkCoercion(t)), coercions); } } Contract.Assert(coercions != null); return(coercions); }
private Z3Expr MkCoercion(Z3Expr t, AppFreeCanUnn unn, PosIntegerEmbedding te) { var zero = Context.MkInt(0); var one = Context.MkInt(1); var i = (Z3IntExpr)te.UnboxingFun.Apply(t); var negCase = BoxingFun.Apply(i.Neg(Context).Add(Context, one)); var coercion = i.Gt(Context, zero).Ite( Context, BoxingFun.Apply(i.Neg(Context)), negCase); coercion = i.Eq(Context, zero).Ite( Context, BoxingFun.Apply(one), coercion); return(coercion); }
private CardExpr MkRelationalUnion(DependencyNode node, AppFreeCanUnn arg) { CardExpr expr = MkConstantCount(arg); if (expr.IsInfinity) { return(expr); } foreach (var m in arg.NonRangeMembers) { if (m.Kind != SymbolKind.UserSortSymb) { continue; } var data = varMap[((UserSortSymb)m).DataSymbol]; Contract.Assert(data.Item1 != node); expr += data.Item3; } return(expr); }
/// <summary> /// Returns the number of constants accepted by the union /// </summary> private CardCnst MkConstantCount(AppFreeCanUnn unn) { BigInteger nCnsts = 0; foreach (var nr in unn.NonRangeMembers) { if (nr.Kind == SymbolKind.BaseCnstSymb || nr.Kind == SymbolKind.UserCnstSymb) { ++nCnsts; } else if (nr.Kind == SymbolKind.BaseSortSymb) { return(new CardCnst(Cardinality.Infinity)); } } foreach (var rng in unn.RangeMembers) { nCnsts += rng.Value - rng.Key + 1; } return(new CardCnst(new Cardinality(nCnsts))); }
private Z3Expr MkCoercion(Z3Expr t, AppFreeCanUnn unn, RealEmbedding te) { return(BoxingFun.Apply( ((Z3IntExpr)Context.MkSub(Context.MkReal2Int((Z3RealExpr)t), Context.MkInt(Lower.ToString()))).Int2BV(Context, bvSort.Size))); }
private Z3Expr MkCoercion(Z3Expr t, AppFreeCanUnn unn, NegIntegerEmbedding te) { return(te.MkIntCoercion(t)); }
/// <summary> /// If arg : argType must be a subtype of acceptingType, then returns /// a possibly coerced term satisfying the property. The type of the /// coerced term is also returned. /// /// Returns null and generates errors of if the term cannot be /// coerced. /// /// If computeType is false, then the type of the coerced /// term is not returned. /// </summary> private Tuple <Term, Term> Coerce(AppFreeCanUnn acceptingType, Term arg, Term argType, int argIndex, Node appNode, string appFun, List <Flag> flags) { bool wasAdded; Set <Namespace> spaces; Namespace maxPrefix = null; LiftedBool isCoercable = LiftedBool.True; Set <UserSymbol> dataSorts = null; Term resultType = null; UserSymbol us; UserSortSymb uss; //// Step 1. Check that all constants are accepted, and all data sorts //// can potentially be coerced. //// After this loop, resultType contains all constants. argType.Visit( x => x.Symbol == theUnnSymbol ? x.Args : null, t => { if (t.Symbol == theUnnSymbol) { return; } else if (t.Symbol.Kind == SymbolKind.UserSortSymb || t.Symbol.IsDataConstructor || t.Symbol.IsDerivedConstant || (t.Symbol.Kind == SymbolKind.UserCnstSymb && ((UserCnstSymb)t.Symbol).IsTypeConstant)) { if (t.Symbol.Kind == SymbolKind.UserSortSymb) { uss = (UserSortSymb)t.Symbol; us = uss.DataSymbol; } else if (t.Symbol.IsDataConstructor) { us = (UserSymbol)t.Symbol; uss = us.Kind == SymbolKind.ConSymb ? ((ConSymb)us).SortSymbol : ((MapSymb)us).SortSymbol; } else { uss = null; us = (UserSymbol)t.Symbol; } if (maxPrefix == null) { maxPrefix = us.Namespace; } else { us.Namespace.TryGetPrefix(maxPrefix, out maxPrefix); } if (dataSorts == null) { dataSorts = new Set <UserSymbol>(Symbol.Compare); } dataSorts.Add(us); if (!acceptingType.Contains(uss == null ? (Symbol)us : uss)) { if (!acceptingType.TryGetRenamings(us.Name, out spaces)) { var flag = new Flag( SeverityKind.Error, appNode, Constants.UnsafeArgType.ToString( argIndex + 1, appFun, t.Symbol.PrintableName), Constants.UnsafeArgType.Code); flags.Add(flag); isCoercable = LiftedBool.False; } else if (isCoercable == LiftedBool.True) { isCoercable = LiftedBool.Unknown; } } } else if (!acceptingType.AcceptsConstants(t)) { var flag = new Flag( SeverityKind.Error, appNode, Constants.UnsafeArgType.ToString( argIndex + 1, appFun, t.Symbol != theRngSymbol ? t.Symbol.PrintableName : (t.Args[0].Symbol.PrintableName + ".." + t.Args[1].Symbol.PrintableName)), Constants.UnsafeArgType.Code); flags.Add(flag); isCoercable = LiftedBool.False; } else { resultType = resultType == null ? t : Index.MkApply(theUnnSymbol, new Term[] { t, resultType }, out wasAdded); } }); if (isCoercable == false) { return(null); } else if (isCoercable == true) { return(new Tuple <Term, Term>(arg, argType)); } //// Step 2. Check that there is a unique coercion from the user sorts. Contract.Assert(dataSorts != null && maxPrefix != null); Set <Namespace> rnmgs = null, cndts; Namespace prefix; string[] suffix; foreach (var s in dataSorts) { suffix = s.Namespace.Split(maxPrefix); Contract.Assert(suffix != null); acceptingType.TryGetRenamings(s.Name, out spaces); cndts = new Set <Namespace>(Namespace.Compare); foreach (var ns in spaces) { if (ns.Split(suffix, out prefix)) { cndts.Add(prefix); } } if (rnmgs == null) { rnmgs = cndts; } else { rnmgs.IntersectWith(cndts); } if (rnmgs.Count == 0) { var flag = new Flag( SeverityKind.Error, appNode, Constants.UncoercibleArgType.ToString( argIndex + 1, appFun, s.PrintableName), Constants.UncoercibleArgType.Code); flags.Add(flag); return(null); } } if (rnmgs.Count != 1) { foreach (var ns in rnmgs) { var flag = new Flag( SeverityKind.Error, appNode, Constants.AmbiguousCoercibleArg.ToString( argIndex + 1, appFun, maxPrefix.FullName, ns.FullName), Constants.AmbiguousCoercibleArg.Code); flags.Add(flag); } return(null); } var from = maxPrefix; var to = rnmgs.GetSomeElement(); Symbol coerced; foreach (var ds in dataSorts) { if (ds.Kind == SymbolKind.UserCnstSymb) { if (!Index.SymbolTable.IsCoercible(ds, from, to, out coerced)) { coerced = null; } } else { uss = ds.Kind == SymbolKind.ConSymb ? ((ConSymb)ds).SortSymbol : ((MapSymb)ds).SortSymbol; if (!Index.SymbolTable.IsCoercible(uss, from, to, out coerced)) { coerced = null; } } if (coerced == null) { var flag = new Flag( SeverityKind.Error, appNode, Constants.UncoercibleArgType.ToString( argIndex + 1, appFun, ds.PrintableName), Constants.UncoercibleArgType.Code); flags.Add(flag); return(null); } else { resultType = resultType == null ? Index.MkApply(coerced, TermIndex.EmptyArgs, out wasAdded) : Index.MkApply( theUnnSymbol, new Term[] { Index.MkApply(coerced, TermIndex.EmptyArgs, out wasAdded), resultType }, out wasAdded); } } typeEnvironment.AddCoercion(appNode, argIndex, from.FullName, to.FullName); var coercedArg = Index.MkApply( Index.SymbolTable.GetOpSymbol(ReservedOpKind.Relabel), new Term[] { Index.MkCnst(from.FullName, out wasAdded), Index.MkCnst(to.FullName, out wasAdded), arg }, out wasAdded); return(new Tuple <Term, Term>(coercedArg, resultType)); }
private Z3Expr MkCoercion(Z3Expr t, AppFreeCanUnn unn, SingletonEmbedding te) { return(MkGround(te.Value.Symbol, null)); }
private Z3Expr MkCoercion(Z3Expr t, AppFreeCanUnn unn, SingletonEmbedding te) { return(Context.MkInt(((Rational)((BaseCnstSymb)te.Value.Symbol).Raw).Numerator.ToString())); }
private Z3Expr MkCoercion(Z3Expr t, AppFreeCanUnn unn, RealEmbedding te) { return(MkPosCoercion(Context.MkReal2Int((Z3.RealExpr)t))); }
private Z3Expr MkCoercion(Z3Expr t, AppFreeCanUnn unn, UnionEmbedding te) { return(CreationFun.Apply()); }
private Z3Expr MkCoercion(Z3Expr t, AppFreeCanUnn unn, IntRangeEmbedding te) { return(MkPosCoercion(te.MkIntCoercion(t))); }
public UnionEmbedding(TypeEmbedder owner, Term unnType, Map <Term, Tuple <uint, UserSymbol> > sortIndices) { Contract.Requires(owner != null); Owner = owner; Name = string.Format("Unn_{0}", sortIndices[unnType].Item1); Type = unnType; CanonicalUnion = new AppFreeCanUnn(unnType); var unn = CanonicalUnion; Z3Con con; LinkedList <ITypeEmbedding> intFacts, cnstFacts; LinkedList <Z3Con> allConstructors = new LinkedList <Z3Con>(); owner.GetFactorizations(unnType, out intFacts, out cnstFacts); foreach (var te in intFacts) { rngBoxings.Add( te.Type, con = Context.MkConstructor( string.Format("Box_{0}_{1}", Name, te.Representation.Name), string.Format("IsBox_{0}_{1}", Name, te.Representation.Name), new string[] { string.Format("Unbox_{0}_{1}", Name, te.Representation.Name) }, new Z3Sort[] { te.Representation })); allConstructors.AddLast(con); } foreach (var te in cnstFacts) { con = Context.MkConstructor( string.Format("Box_{0}_{1}", Name, te.Representation.Name), string.Format("IsBox_{0}_{1}", Name, te.Representation.Name), new string[] { string.Format("Unbox_{0}_{1}", Name, te.Representation.Name) }, new Z3Sort[] { te.Representation }); allConstructors.AddLast(con); foreach (var t in te.Type.Enumerate(x => x.Args)) { if (t.Symbol.Arity == 0) { otherBoxings.Add(t.Symbol, con); } } } bool wasAdded; foreach (var m in unn.NonRangeMembers) { if (m.Kind == SymbolKind.BaseSortSymb) { var te = Owner.GetEmbedding(Index.MkApply(m, TermIndex.EmptyArgs, out wasAdded)); otherBoxings.Add( m, con = Context.MkConstructor( string.Format("Box_{0}_{1}", Name, te.Representation.Name), string.Format("IsBox_{0}_{1}", Name, te.Representation.Name), new string[] { string.Format("Unbox_{0}_{1}", Name, te.Representation.Name) }, new Z3Sort[] { te.Representation })); allConstructors.AddLast(con); } else if (m.Kind == SymbolKind.UserSortSymb) { otherBoxings.Add( m, con = Context.MkConstructor( string.Format("Box_{0}_{1}", Name, ((UserSortSymb)m).DataSymbol.FullName), string.Format("IsBox_{0}_{1}", Name, ((UserSortSymb)m).DataSymbol.FullName), new string[] { string.Format("Unbox_{0}_{1}", Name, ((UserSortSymb)m).DataSymbol.FullName) }, new Z3Sort[] { null }, new uint[] { sortIndices[Index.MkApply(m, TermIndex.EmptyArgs, out wasAdded)].Item1 })); allConstructors.AddLast(con); } } Boxers = allConstructors.ToArray(allConstructors.Count); }
private Z3Expr MkCoercion(Z3Expr t, AppFreeCanUnn unn, IntegerEmbedding te) { return(MkPosCoercion((Z3IntExpr)t)); }
private Z3Expr MkCoercion(Z3Expr t, AppFreeCanUnn unn, IntegerEmbedding te) { return(Context.MkInt2Real((Z3.IntExpr)t)); }
private Z3Expr MkCoercion(Z3Expr t, AppFreeCanUnn unn, SingletonEmbedding te) { var val = ((Rational)((BaseCnstSymb)te.Value.Symbol).Raw).Numerator - Lower; return(BoxingFun.Apply(Context.MkBV(val.ToString(), bvSort.Size))); }
private Z3Expr MkCoercion(Z3Expr t, AppFreeCanUnn unn, UnionEmbedding te) { Z3Expr unbox; //// If the union contains real, then it cannot contain other integral numerics. var test = te.MkTestAndUnbox(Index.SymbolTable.GetSortSymbol(BaseSortKind.Real), t, out unbox); if (test != null) { return(test.Ite( Context, MkCoercion(unbox, unn, (RealEmbedding)Owner.GetEmbedding(BaseSortKind.Real)), DefaultMember.Item2)); } //// If the union contains int, then it cannot contain other integral numerics. test = te.MkTestAndUnbox(Index.SymbolTable.GetSortSymbol(BaseSortKind.Integer), t, out unbox); if (test != null) { return(test.Ite( Context, MkCoercion(unbox, unn, (IntegerEmbedding)Owner.GetEmbedding(BaseSortKind.Integer)), DefaultMember.Item2)); } //// Otherwise unn may contain subsorts of Integer or integer ranges. //// First handle the base subsorts var coercions = DefaultMember.Item2; test = te.MkTestAndUnbox(Index.SymbolTable.GetSortSymbol(BaseSortKind.Natural), t, out unbox); if (test != null) { coercions = test.Ite( Context, MkCoercion(unbox, unn, (NaturalEmbedding)Owner.GetEmbedding(BaseSortKind.Natural)), coercions); } test = te.MkTestAndUnbox(Index.SymbolTable.GetSortSymbol(BaseSortKind.PosInteger), t, out unbox); if (test != null) { coercions = test.Ite( Context, MkCoercion(unbox, unn, (PosIntegerEmbedding)Owner.GetEmbedding(BaseSortKind.PosInteger)), coercions); } test = te.MkTestAndUnbox(Index.SymbolTable.GetSortSymbol(BaseSortKind.NegInteger), t, out unbox); if (test != null) { coercions = test.Ite( Context, MkCoercion(unbox, unn, (NegIntegerEmbedding)Owner.GetEmbedding(BaseSortKind.NegInteger)), coercions); } //// Additionally, there may be integer ranges. Z3BoolExpr[] tests; Z3Expr[] unboxeds; ITypeEmbedding tep; foreach (var kv in unn.RangeMembers) { tests = te.MkTestAndUnbox(kv.Key, kv.Value, t, out unboxeds); for (int i = 0; i < tests.Length; ++i) { test = tests[i]; unbox = unboxeds[i]; tep = Owner.GetEmbedding(unbox.Sort); switch (tep.Kind) { case TypeEmbeddingKind.IntRange: coercions = test.Ite( Context, MkCoercion(unbox, unn, (IntRangeEmbedding)tep), coercions); break; case TypeEmbeddingKind.Singleton: coercions = test.Ite( Context, MkCoercion(unbox, unn, (SingletonEmbedding)tep), coercions); break; default: throw new NotImplementedException(); } } } return(coercions); }
private Z3Expr MkCoercion(Z3Expr t, AppFreeCanUnn unn, IntRangeEmbedding te) { return(Context.MkInt2Real(te.MkIntCoercion(t))); }