Example #1
0
        /// <summary>
        ///  Yield an expression that is 'inexpr' with 'var' replaced by 'subst'.
        /// </summary>
        /// <param name="subst">The expression to substitute.</param>
        /// <param name="var">The variable to substitute for.</param>
        /// <param name="inexpr">The expression to substitute into.</param>
        public static IExpr /*!*/ Substitute(IExpr /*!*/ subst, IVariable /*!*/ var, IExpr /*!*/ inexpr)
        {
            Contract.Requires(inexpr != null);
            Contract.Requires(var != null);
            Contract.Requires(subst != null);
            Contract.Ensures(Contract.Result <IExpr>() != null);
            IExpr result = null;

            if (inexpr is IVariable)
            {
                result = inexpr.Equals(var) ? subst : inexpr;
            }
            else if (inexpr is IFunApp)
            {
                IFunApp /*!*/ funapp  = (IFunApp /*!*/)cce.NonNull(inexpr);
                IList         newargs = null;

                var x = new System.Collections.Generic.List <IExpr>();
                foreach (IExpr arg in funapp.Arguments)
                {
                    x.Add(Substitute(subst, var, arg));
                }
                newargs = new ArrayList(x);
                //newargs = new ArrayList{ IExpr/*!*/ arg in funapp.Arguments; Substitute(subst, var, arg) };
                result = funapp.CloneWithArguments(newargs);
            }
            else if (inexpr is IFunction)
            {
                IFunction /*!*/ fun = (IFunction /*!*/)cce.NonNull(inexpr);

                if (fun.Param.Equals(var))
                {
                    result = fun;
                }
                else
                {
                    result = fun.CloneWithBody(Substitute(subst, var, fun.Body));
                }
            }
            else if (inexpr is IUnknown)
            {
                result = inexpr;
            }
            else
            {
                { Contract.Assert(false); throw new cce.UnreachableException(); }
            }

            return(result);
        }
Example #2
0
        /// <summary>
        ///  Yield an expression that is 'inexpr' with 'var' replaced by 'subst'.
        /// </summary>
        /// <param name="subst">The expression to substitute.</param>
        /// <param name="var">The variable to substitute for.</param>
        /// <param name="inexpr">The expression to substitute into.</param>
        public static IExpr/*!*/ Substitute(IExpr/*!*/ subst, IVariable/*!*/ var, IExpr/*!*/ inexpr){
Contract.Requires(inexpr != null);
Contract.Requires(var != null);
Contract.Requires(subst != null);
Contract.Ensures(Contract.Result<IExpr>() != null);
            IExpr result = null;

            if (inexpr is IVariable)
            {
                result = inexpr.Equals(var) ? subst : inexpr;
            }
            else if (inexpr is IFunApp)
            {
                IFunApp/*!*/ funapp = (IFunApp/*!*/)cce.NonNull(inexpr);
                IList newargs = null;
              
              var x = new System.Collections.Generic.List<IExpr>();
              foreach (IExpr arg in funapp.Arguments){
                x.Add(Substitute(subst,var, arg));
              }
              newargs = new ArrayList(x);
                //newargs = new ArrayList{ IExpr/*!*/ arg in funapp.Arguments; Substitute(subst, var, arg) };
                result = funapp.CloneWithArguments(newargs);
            }
            else if (inexpr is IFunction)
            {
                IFunction/*!*/ fun = (IFunction/*!*/)cce.NonNull(inexpr);

                if (fun.Param.Equals(var))
                    result = fun;
                else
                    result = fun.CloneWithBody(Substitute(subst, var, fun.Body));
            }
            else if (inexpr is IUnknown)
            {
                result = inexpr;
            }
            else
            {
                {Contract.Assert(false);throw new cce.UnreachableException();}
            }

            return result;
        }