Пример #1
0
        public override bool Reduce(IVariableMap<Expression> Map, ref Expression Reduced)
        {
            // Is the source tuple an actual tuple?
            TupleExpression te = this.SourceTuple as TupleExpression;
            if (te != null)
            {
                // Now we got this :D
                Reduced = this.InnerExpression.Substitute(new SimpleMap<Expression>(this.BreakIndex, te.Parts));
                return true;
            }

            // Wouldn't it be hilarious if the inner expression never even used the tuple's data?
            Expression cire = this.InnerExpression.Compress(this.BreakIndex, this.TupleSize);
            if (cire != null)
            {
                Reduced = cire;
                return true;
            }

            // Nope, guess i'll have to do it the normal way :(
            Expression tre = this.SourceTuple;
            Expression ire = this.InnerExpression;
            if (tre.Reduce(Map, ref tre) | ire.Reduce(this._CreateInner(Map), ref ire))
            {
                Reduced = new TupleBreakExpression(this.BreakIndex, this.TupleSize, tre, ire);
                return true;
            }

            return false;
        }
Пример #2
0
 public override Expression Substitute(IVariableMap<Expression> Map)
 {
     return Expression.Lookup(this.Index, Map);
 }
Пример #3
0
 public override bool Reduce(IVariableMap<Expression> Map, ref Expression Reduced)
 {
     // Only possible way to reduce a variable is to replace it with its value.
     Expression possible = null;
     if (Map.Lookup(this.Index, ref possible))
     {
         VariableExpression ve = possible as VariableExpression;
         if (ve != null)
         {
             if (ve.Index == this.Index)
             {
                 return false;
             }
         }
         Reduced = possible;
         return true;
     }
     return false;
 }
Пример #4
0
 public override Expression Substitute(IVariableMap<Expression> Map)
 {
     return this;
 }
Пример #5
0
 public override Expression Substitute(IVariableMap<Expression> Map)
 {
     return new FunctionDefineExpression(
         this.ArgumentIndex,
         this.ArgumentType.Substitute(Map),
         this.Function.Substitute(new SubsetMap<Expression>(0, this.ArgumentIndex, Map)));
 }
Пример #6
0
 public override Expression Substitute(IVariableMap<Expression> Map)
 {
     return new FunctionCallExpression(this.Function.Substitute(Map), this.Argument.Substitute(Map));
 }
Пример #7
0
        public override bool Reduce(IVariableMap<Expression> Map, ref Expression Reduced)
        {
            // Beta reduction (substituting an argument in a function definition)
            FunctionDefineExpression fde = this.Function as FunctionDefineExpression;
            if (fde != null)
            {
                Reduced = fde.SubstituteCall(this.Argument);
                return true;
            }

            // Recursive reduction
            Expression fre = this.Function;
            Expression are = this.Argument;
            if (fre.Reduce(Map, ref fre) | are.Reduce(Map, ref are))
            {
                Reduced = new FunctionCallExpression(fre, are);
                return true;
            }

            return false;
        }
Пример #8
0
 /// <summary>
 /// Substitutes all variables in the expression with their corresponding expression in the specified map. This guarntees all variables in the stack's range
 /// to be removed (If all variables only reference variables below them in stack).
 /// </summary>
 public virtual Expression Substitute(IVariableMap<Expression> Map)
 {
     return this;
 }
Пример #9
0
 /// <summary>
 /// Tries to gradually simplifies the expression. Can possibly access the map to dereference a variable.
 /// </summary>
 public virtual bool Reduce(IVariableMap<Expression> Map, ref Expression Reduced)
 {
     return false;
 }
Пример #10
0
 /// <summary>
 /// Looks up or "dereferences" a variable with the given index in the specified stack.
 /// </summary>
 public static Expression Lookup(int Index, IVariableMap<Expression> Map)
 {
     Expression res = null;
     if (Map.Lookup(Index, ref res))
     {
         return res.Substitute(Map);
     }
     else
     {
         return Expression.Variable(Index);
     }
 }
Пример #11
0
 public override Expression Substitute(IVariableMap<Expression> Map)
 {
     if (this.Parts != null)
     {
         Expression[] subs = new Expression[this.Parts.Length];
         for (int t = 0; t < this.Parts.Length; t++)
         {
             subs[t] = this.Parts[t].Substitute(Map);
         }
         return new TupleExpression(subs);
     }
     else
     {
         return Empty;
     }
 }
Пример #12
0
 private IVariableMap<Expression> _CreateInner(IVariableMap<Expression> Source)
 {
     Expression[] stackappend = new Expression[this.TupleSize];
     for (int t = 0; t < stackappend.Length; t++)
     {
         stackappend[t] = Expression.Variable(this.BreakIndex + t);
     }
     return new SwitchMap<Expression>(
         this.BreakIndex,
         Source,
         new SimpleMap<Expression>(this.BreakIndex, stackappend));
 }
Пример #13
0
 public override Expression Substitute(IVariableMap<Expression> Map)
 {
     return new TupleBreakExpression(
         this.BreakIndex,
         this.TupleSize,
         this.SourceTuple.Substitute(Map),
         this.InnerExpression.Substitute(this._CreateInner(Map)));
 }