public override Function substitute(TypeParameterInstantiation s)
 {
     return(new BasicFunctionTemplateInstance(
                template,
                (from a in typeArguments select a.substitute(s)).ToArray()
                ));
 }
Пример #2
0
 private TypeConstructorInstance substituteOriginalType(TypeParameterInstantiation instantiation)
 {
     if (originalType == null)
     {
         return(null);
     }
     return(originalType.substitute(instantiation) as TypeConstructorInstance);
 }
Пример #3
0
 public Function substitute(TypeParameterInstantiation s)
 {
     return(makeMapWrite(
                (from a in mapTypeArguments select a.substitute(s)).ToArray(),
                (from a in argumentTypes select a.substitute(s)).ToArray(),
                resultType.substitute(s)
                ));
 }
Пример #4
0
 public Function substitute(TypeParameterInstantiation s)
 {
     return(new BasicMapRead(
                mapType.substituteMap(s),
                (from ta in mapTypeArguments select ta.substitute(s)).ToArray(),
                (from it in mapIndexTypes select it.substitute(s)).ToArray(),
                resultType.substitute(s)
                ));
 }
Пример #5
0
        ////////////////////////////////////////////////////////////////////
        ////////////////////////////////////////////////////////////////////
        public IType substitute(TypeParameterInstantiation instantiation)
        {
            IType result = instantiation.map(typeVariable);

            if (result == null)
            {
                result = this;
            }
            return(result);
        }
Пример #6
0
        public static BasicMapRead mapRead(MapType mapType, ITypeTuple typeArguments)
        {
            Debug.Assert(mapType != null);
            Debug.Assert(typeArguments != null);
            Debug.Assert(mapType.typeParameters.Length == typeArguments.Count());

            var s             = new TypeParameterInstantiation(mapType.typeParameters, typeArguments);
            var argumentTypes = TypeTuple.make(new [] { mapType }.Concat(from a in mapType.domain select a.substitute(s)));
            var resultType    = mapType.range.substitute(s);

            return(mapRead(typeArguments, argumentTypes, resultType));
        }
Пример #7
0
        /////////////////////////////////////////////////////////////////////////////////////////////////////
        public MapType substituteMap(TypeParameterInstantiation instantiation)
        {
            foreach (var tp in typeParameters)
            {
                Debug.Assert(instantiation.map(tp) == null);
            }

//            Debug.Assert(mapTypeArguments.Length == mapTypeArguments.Length);
            bool clone = false;

            var newDomain = new IType[domain.Count()];

            for (int i = 0; i < domain.Count(); i++)
            {
                newDomain[i] = domain[i].substitute(instantiation);
                if (newDomain[i] != domain[i])
                {
                    clone = true;
                }
            }
            IType newRange = range.substitute(instantiation);

            if (newRange != range)
            {
                clone = true;
            }

            TypeConstructorInstance newOriginalType = substituteOriginalType(instantiation);

            if (newOriginalType != originalType)
            {
                clone = true;
            }

            MapType result;

            if (clone)
            {
                result = new MapType((TypeVariable[])typeParameters.Clone(), newDomain, newRange, newOriginalType);
                result.originalSubstitution = this;
            }
            else
            {
                result = this;
            }

            return(result);
        }
Пример #8
0
        private Expression makeMapWriteMap(FAE fae)
        {
            Debug.Assert(fae.function is MapWrite);
            Debug.Assert(fae.arguments[0].type is MapType);
            var        mu = fae.function as MapWrite;
            Expression result;

            if (!versionMap.TryGetValue(fae.ToString(), out result))
            {
                var ot = fae.arguments[0].type as MapType;
                Debug.Assert(ot != null);
                var s = new TypeParameterInstantiation(ot.typeParameters, mu.typeArguments.Skip(1).ToArray());
//                var ots = new MapType(new TypeVariable[0], (from m in ot.domain select m.substitute(s)).ToArray(), ot.range.substitute(s));
                Expression   m = fae.arguments[0];
                Expression[] i = fae.arguments.Take(fae.arguments.count - 1).Skip(1).ToArray();

                if (fae.freeVariables.Count > 0)
                {
//m(fv)[i(fv):=x(fv)] ==> v(fv)  (assume v(fv)[i]==x, assume forall fv : forall j : j!=i(fv) ==> v(fv)[j] == m(fv)[j]
                    var    fvt = TypeTuple.make(from fv in fae.freeVariables select fv.type);
                    string fn  = getFreshMUName("mu");
                    var    ft  = new BFunctionTemplate(fn, "", new TypeVariable[0], new BasicFunctionSignature(ot, fvt),
                                                       null);
                    Function f = ft.getInstance();
                    IEnumerable <BasicBoundVariableExpression> fve = from fv in fae.freeVariables
                                                                     select new BasicBoundVariableExpression(fv);
                    result = new BasicFAE(f, new ExpressionList(fve));
                }
                else
                {
                    //m[i:=x] ==> v  (assume v[i]==x, assume forall j : j!=i ==> v[j] == m[j]
                    string nvn = getFreshMUName("mu");
                    var    nv  = new ProgramVariable(nvn, ot, false, false, false, false);
                    procedure.addVariable(nv);
                    result = new BasicProgramVariableExpression(nv);
                }
                Expression[] j =
                    (from e in i select new BasicBoundVariableExpression(makeBoundVariable(e.type))).ToArray();
                //forall j : j!=i ==> v[j]==m[j]
                addConditionalMapEqualityAxiom(i, result, m, mu.typeArguments);
                //v[i]==x;
                Expression x = fae.arguments.Last();
                addEqualityAxiom(ml(result, i, mu.typeArguments.Skip(1).ToArray(), x.type), fae.arguments.Last());
            }
            return(result);
        }
Пример #9
0
        public static BasicMapRead mapRead(IEnumerable <IType> typeArguments, IEnumerable <IType> argumentTypes, IType resultType)
        {
            Debug.Assert(typeArguments != null);
            Debug.Assert(argumentTypes != null);
            Debug.Assert(argumentTypes.Count() > 0);
            var mapType = argumentTypes.First() as MapType;

            Debug.Assert(mapType != null);
            Debug.Assert(typeArguments.Count() == mapType.typeParameters.Length);
            Debug.Assert(mapType.domain.Count() == argumentTypes.Count() - 1);

            var   substitution = new TypeParameterInstantiation(mapType.typeParameters, typeArguments);
            IType sresultType  = mapType.range.substitute(substitution);

            Debug.Assert(resultType.ToStringN() == sresultType.ToStringN());
            Debug.Assert(argumentTypes.Skip(1).Zip(mapType.domain, (at, mtD) => at.isEquivalent(mtD.substitute(substitution))).All(x => x));
            string indexString = makeIndexString(TypeTuple.make(typeArguments), TypeTuple.make(argumentTypes));

            BasicMapRead result = null;

            bool foundInstance = instances.TryGetValue(indexString, out result);

            if (result != null)
            {
                if (!ReferenceEquals(resultType, result.resultType))
                {
                    foundInstance = false;
                }
                else
                if (argumentTypes.Zip(result.argumentTypes, (x, y) => x.isEquivalent(y)).Any(x => !x))
                {
                    foundInstance = false;
                }
            }
            if (!foundInstance)
            {
                var indexTypes = argumentTypes.Skip(1);
                result = new BasicMapRead(mapType, typeArguments, indexTypes, resultType);
                instances[indexString] = result;
            }
            return(result);
        }
Пример #10
0
        private void addConditionalMapEqualityAxiom(Expression[] i, Expression mu, Expression m,
                                                    ITypeTuple typeArguments)
        {
            Debug.Assert(mu.type.ToStringN() == m.type.ToStringN());
            string mlIndex = TypeInstanceCollector.makeString(typeArguments);

            Debug.Assert(tic.functionInstanceMap["MapRead"].ContainsKey(mlIndex));
            Debug.Assert(m.type is MapType);
            var mt = m.type as MapType;

            TypeInstanceCollector.GFunctionInstance[] tpis = (from tpi in tic.functionInstanceMap["MapRead"].Values
                                                              where
                                                              tpi.function.typeArguments.First().ToStringN() ==
                                                              mt.ToStringN()
                                                              select tpi).ToArray();
            Debug.Assert(
                (from tpi in tpis select TypeInstanceCollector.makeString(tpi.function.typeArguments)).Contains(
                    TypeInstanceCollector.makeString(typeArguments)));
            foreach (var tpi in tpis)
            {
                var ta = tpi.function.typeArguments;
                Debug.Assert(ta.Count() == mt.typeParameters.Count() + 1);
                Debug.Assert(ta.First().ToStringN() == mt.ToStringN());
                var mta  = ta.Skip(1).ToArray();
                var ts   = new TypeParameterInstantiation(mt.typeParameters, mta);
                var at   = (from a in mt.domain select a.substitute(ts)).ToArray();
                var rt   = mt.range.substitute(ts);
                var bvs  = (from a in at select procedure.makeFreshBoundVariable(a)).ToArray();
                var bves =
                    (from bv in bvs select new BasicBoundVariableExpression(bv)).ToArray();

                if (TypeInstanceCollector.makeString(ta) == TypeInstanceCollector.makeString(typeArguments))
                {
                    addAxiom(
                        sentence(implication(inEquality(bves, i), equality(ml(m, bves, mta, rt), ml(mu, bves, mta, rt)))));
                }
                else
                {
                    addAxiom(sentence(equality(ml(m, bves, mta, rt), ml(mu, bves, mta, rt))));
                }
            }
        }
Пример #11
0
        /////////////////////////////////////////////////////////////////////////////////////////////////////
        /////////////////////////////////////////////////////////////////////////////////////////////////////
        public IType substitute(TypeParameterInstantiation instantiation)
        {
            bool clone        = false;
            var  newArguments = new IType[arguments.Count()];

            for (int i = 0; i < arguments.Count(); i++)
            {
                newArguments[i] = arguments[i].substitute(instantiation);
                Debug.Assert(newArguments[i] != null);
                if (newArguments[i] != arguments[i])
                {
                    clone = true;
                }
            }

            if (clone)
            {
                return(new TypeConstructorInstance(typeConstructor, newArguments));
            }

            return(this);
        }
Пример #12
0
        private BasicMapRead(MapType mapType, IEnumerable <IType> mapTypeArguments, IEnumerable <IType> indexTypes, IType resultType)
        {
            Debug.Assert(mapType != null);
            Debug.Assert(indexTypes != null);
            Debug.Assert(resultType != null);
            Debug.Assert(mapType.domain.Count() == indexTypes.Count());
            Debug.Assert(mapTypeArguments != null);
            Debug.Assert(mapType.typeParameters.Length == mapTypeArguments.Count());

            var substitution = new TypeParameterInstantiation(mapType.typeParameters, mapTypeArguments);

            Debug.Assert(resultType.ToStringN() == mapType.range.substitute(substitution).ToStringN());
            Debug.Assert(mapType.domain.Zip(indexTypes, (x, y) => x.substitute(substitution).isEquivalent(y)).All(x => x));

/*            for (int i = 0; i < indexTypes.Count(); i++)
 *              Debug.Assert(indexTypes[i].ToStringExpanded() ==
 *                           mapType.domain[i].substitute(substitution).ToStringExpanded());
 */
            this.mapType          = mapType;
            mapIndexTypes         = TypeTuple.make(indexTypes);
            this.resultType       = resultType;
            this.mapTypeArguments = TypeTuple.make(mapTypeArguments);
        }
Пример #13
0
        public bool isEquivalent(IType other)
        {
            var omt = other as MapType;

            if (omt == null)
            {
                return(false);
            }
            if (omt.typeParameters.Count() != typeParameters.Count())
            {
                return(false);
            }
            if (omt.domain.Count() != domain.Count())
            {
                return(false);
            }

            var ttv      = canonicalParameterArray();
            var otherSub = new TypeParameterInstantiation(omt.typeParameters, ttv);
            var thisSub  = new TypeParameterInstantiation(typeParameters, ttv);

//            var omtUnified = omt.substitute(new TypeParameterInstantiation(omt.mapTypeArguments, ttv)) as MapType;
//            var thisUnified = substitute(new TypeParameterInstantiation(mapTypeArguments, ttv)) as MapType;

            for (int i = 0; i < domain.Count(); i++)
            {
                if (!domain[i].substitute(thisSub).isEquivalent(omt.domain[i].substitute(otherSub)))
                {
                    return(false);
                }
            }
            if (!range.substitute(thisSub).isEquivalent(omt.range.substitute(otherSub)))
            {
                return(false);
            }
            return(true);
        }
 public override Function substitute(TypeParameterInstantiation s)
 {
     return(this);
 }
Пример #15
0
 public ExpressionSubstitution()
 {
     typeSubstitution = new TypeParameterInstantiation();
 }
Пример #16
0
 /////////////////////////////////////////////////////////////////////////////////////////////////////
 /////////////////////////////////////////////////////////////////////////////////////////////////////
 public IType substitute(TypeParameterInstantiation instantiation)
 {
     return(substituteMap(instantiation));
 }
Пример #17
0
 public abstract Function substitute(TypeParameterInstantiation s);
        ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        private List <List <IType> > getTypesToInstantiate(Expression e)
        {
            List <TypeVariable> ftvl = e.freeTypeVariables.ToList();

            var gtl = new List <IType>();

            foreach (var t in qtc.types)
            {
                if (t.Value.type.freeTypeVariables.Count == 0)
                {
                    gtl.Add(t.Value.type);
                }
            }

            var indices = new int[ftvl.Count];

            for (int i = 0; i < ftvl.Count; i++)
            {
                indices[i] = 0;
            }

            var                  result = new List <List <IType> >();
            HashSet <IType>      ngts   = getNonGroundTypes(e);
            IEnumerable <string> programGroundTypeNames = from p in qtc.types where p.Value.type.isGround select p.Key;

/*            {
 *              Console.Write("Program Types = {");
 *              foreach (var t in programGroundTypeNames)
 *                  Console.Write(t + " ");
 *              Console.WriteLine("}");
 *          }
 */
            while (true)
            {
                var l = new List <IType>();
                for (int i = 0; i < indices.Length; i++)
                {
                    l.Add(gtl[indices[i]]);
                }

                {
                    var tpi = new TypeParameterInstantiation(ftvl.ToArray(), l.ToArray());

/*                    {
 *                      Console.Write("{");
 *                      foreach (var ngt in ngts)
 *                          Console.Write(ngt.substitute(tpi).ToStringExpanded() + "|");
 *                      Console.WriteLine("}");
 *                  }
 */
                    foreach (var ngt in ngts)
                    {
                        Debug.Assert(ngt.substitute(tpi).isGround);
                        if (!programGroundTypeNames.Contains(ngt.substitute(tpi).ToStringN()))
                        {
                            goto skipAdd;
                        }
                    }
//                    Console.WriteLine("Accepted");
                    result.Add(l);
skipAdd:
                    ;
                }

                Debug.Assert(indices.Length > 0);
                int j = indices.Length - 1;
                while (true)
                {
                    Debug.Assert(indices[j] < gtl.Count);
                    if (indices[j] == gtl.Count - 1)
                    {
                        if (j == 0)
                        {
                            goto endLoop;
                        }
                        else
                        {
                            indices[j] = 0;
                            j--;
                        }
                    }
                    else
                    {
                        indices[j]++;
                        break;
                    }
                }
            }
endLoop:

            return(result);
        }
Пример #19
0
 public IType substitute(TypeParameterInstantiation instantiation)
 {
     return(this);
 }