simple Chair/Keyboard issue...

Apr 1, 2009 at 5:17 AM

I'm trying to build a simple expression tree example, creating a lambda that will return double it's input, but I'm getting this error:

System.ArgumentException was unhandled
  Message="must be reducible node\r\nParameter name: this"
  Source="Microsoft.Scripting.Core"
  ParamName="this"
  StackTrace:
       at Microsoft.Scripting.Utils.ContractUtils.Requires(Boolean precondition, String paramName, String message) in C:\Users\mwkohout\Documents\dlr\DLR_Main\Runtime\Src\Microsoft.Scripting.Core\Utils\ContractUtils.cs:line 54
       at Microsoft.Linq.Expressions.Expression.ReduceAndCheck() in C:\Users\mwkohout\Documents\dlr\DLR_Main\Runtime\Src\Microsoft.Scripting.Core\Ast\Expression.cs:line 186
       at Microsoft.Linq.Expressions.Expression.ReduceExtensions() in C:\Users\mwkohout\Documents\dlr\DLR_Main\Runtime\Src\Microsoft.Scripting.Core\Ast\Expression.cs:line 205
       at Microsoft.Linq.Expressions.Compiler.StackSpiller.RewriteExtensionExpression(Expression expr, Stack stack) in C:\Users\mwkohout\Documents\dlr\DLR_Main\Runtime\Src\Microsoft.Scripting.Core\Compiler\StackSpiller.cs:line 902
       at Microsoft.Linq.Expressions.Compiler.StackSpiller.RewriteExpression(Expression node, Stack stack) in C:\Users\mwkohout\Documents\dlr\DLR_Main\Runtime\Src\Microsoft.Scripting.Core\Compiler\StackSpiller.Generated.cs:line 187
       at Microsoft.Linq.Expressions.Compiler.StackSpiller.RewriteExpressionFreeTemps(Expression expression, Stack stack) in C:\Users\mwkohout\Documents\dlr\DLR_Main\Runtime\Src\Microsoft.Scripting.Core\Compiler\StackSpiller.cs:line 154
       at Microsoft.Linq.Expressions.Compiler.StackSpiller.Rewrite[T](Expression`1 lambda) in C:\Users\mwkohout\Documents\dlr\DLR_Main\Runtime\Src\Microsoft.Scripting.Core\Compiler\StackSpiller.cs:line 105
       at Microsoft.Linq.Expressions.Expression`1.Accept(StackSpiller spiller) in C:\Users\mwkohout\Documents\dlr\DLR_Main\Runtime\Src\Microsoft.Scripting.Core\Ast\LambdaExpression.cs:line 162
       at Microsoft.Linq.Expressions.Compiler.StackSpiller.AnalyzeLambda(LambdaExpression lambda) in C:\Users\mwkohout\Documents\dlr\DLR_Main\Runtime\Src\Microsoft.Scripting.Core\Compiler\StackSpiller.cs:line 93
       at Microsoft.Linq.Expressions.Compiler.StackSpiller.RewriteLambdaExpression(Expression expr, Stack stack) in C:\Users\mwkohout\Documents\dlr\DLR_Main\Runtime\Src\Microsoft.Scripting.Core\Compiler\StackSpiller.cs:line 304
       at Microsoft.Linq.Expressions.Compiler.StackSpiller.RewriteExpression(Expression node, Stack stack) in C:\Users\mwkohout\Documents\dlr\DLR_Main\Runtime\Src\Microsoft.Scripting.Core\Compiler\StackSpiller.Generated.cs:line 97
       at Microsoft.Linq.Expressions.Compiler.StackSpiller.ChildRewriter.Add(Expression node) in C:\Users\mwkohout\Documents\dlr\DLR_Main\Runtime\Src\Microsoft.Scripting.Core\Compiler\StackSpiller.Temps.cs:line 150
       at Microsoft.Linq.Expressions.Compiler.StackSpiller.ChildRewriter.AddArguments(IArgumentProvider expressions) in C:\Users\mwkohout\Documents\dlr\DLR_Main\Runtime\Src\Microsoft.Scripting.Core\Compiler\StackSpiller.Temps.cs:line 166
       at Microsoft.Linq.Expressions.Compiler.StackSpiller.RewriteDynamicExpression(Expression expr, Stack stack) in C:\Users\mwkohout\Documents\dlr\DLR_Main\Runtime\Src\Microsoft.Scripting.Core\Compiler\StackSpiller.cs:line 167
       at Microsoft.Linq.Expressions.Compiler.StackSpiller.RewriteExpression(Expression node, Stack stack) in C:\Users\mwkohout\Documents\dlr\DLR_Main\Runtime\Src\Microsoft.Scripting.Core\Compiler\StackSpiller.Generated.cs:line 184
       at Microsoft.Linq.Expressions.Compiler.StackSpiller.RewriteExpressionFreeTemps(Expression expression, Stack stack) in C:\Users\mwkohout\Documents\dlr\DLR_Main\Runtime\Src\Microsoft.Scripting.Core\Compiler\StackSpiller.cs:line 154
       at Microsoft.Linq.Expressions.Compiler.StackSpiller.Rewrite[T](Expression`1 lambda) in C:\Users\mwkohout\Documents\dlr\DLR_Main\Runtime\Src\Microsoft.Scripting.Core\Compiler\StackSpiller.cs:line 105
       at Microsoft.Linq.Expressions.Expression`1.Accept(StackSpiller spiller) in C:\Users\mwkohout\Documents\dlr\DLR_Main\Runtime\Src\Microsoft.Scripting.Core\Ast\LambdaExpression.cs:line 162
       at Microsoft.Linq.Expressions.Compiler.StackSpiller.AnalyzeLambda(LambdaExpression lambda) in C:\Users\mwkohout\Documents\dlr\DLR_Main\Runtime\Src\Microsoft.Scripting.Core\Compiler\StackSpiller.cs:line 93
       at Microsoft.Linq.Expressions.Compiler.LambdaCompiler.AnalyzeLambda(LambdaExpression& lambda) in C:\Users\mwkohout\Documents\dlr\DLR_Main\Runtime\Src\Microsoft.Scripting.Core\Compiler\LambdaCompiler.cs:line 214
       at Microsoft.Linq.Expressions.Compiler.LambdaCompiler.Compile(LambdaExpression lambda) in C:\Users\mwkohout\Documents\dlr\DLR_Main\Runtime\Src\Microsoft.Scripting.Core\Compiler\LambdaCompiler.cs:line 180
       at Microsoft.Linq.Expressions.LambdaExpression.Compile() in C:\Users\mwkohout\Documents\dlr\DLR_Main\Runtime\Src\Microsoft.Scripting.Core\Ast\LambdaExpression.cs:line 106
       at XtraDLRgeTest.Program.Main(String[] args) in C:\Users\mwkohout\Documents\dlr\DLR_Main\XtraDLRgeTest\Program.cs:line 38
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException:


This is the code I've got:

           LambdaBuilder doubleLB = Utils.Lambda(typeof(Int32), "double", new SourceSpan());
            ParameterExpression param = doubleLB.ClosedOverParameter(typeof(Int32),"i");
            doubleLB.Body = Expression.Multiply(param, Expression.Constant(2));
            LambdaBuilder lb = Utils.Lambda(typeof(object), "test");
           
            lb.Body = Expression.Dynamic(new MemoizingFunctionCallBinder(), typeof(Object), doubleLB.MakeLambda(), Expression.Constant(1), Expression.Constant(new Object()));

            var output = lb.MakeLambda().Compile().DynamicInvoke(new Object[] { });
    
            System.Console.Out.Write(output);


I'm sure my malfunction is simple, but random trial and error hasn't exposed what exactly it is I'm doing wrong.  Do any of you have a suggestion?
thanks again,
Mike Kohout
Apr 1, 2009 at 5:31 AM
and for those curious, here's the complete (nonworking) example, with an untested memoizing call binder:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

using Microsoft.Scripting.Ast;
using Microsoft.Scripting;

using AstUtils = Microsoft.Scripting.Ast.Utils;
using System.Collections.Generic;
using Microsoft.Linq.Expressions;

namespace XtraDLRgeTest
{
    class Program
    {
        static void Main(string[] args)
        {
           
            LambdaBuilder doubleLB = Utils.Lambda(typeof(Int32), "double", new SourceSpan());
            ParameterExpression param = doubleLB.ClosedOverParameter(typeof(Int32),"i");
            doubleLB.Body = Expression.Multiply(param, Expression.Constant(2));
            LambdaBuilder lb = Utils.Lambda(typeof(object), "test");
           
            lb.Body = Expression.Dynamic(new MemoizingFunctionCallBinder(), typeof(Object), doubleLB.MakeLambda(), Expression.Constant(1), Expression.Constant(new Object()));

            var output = lb.MakeLambda().Compile().DynamicInvoke(new Object[] { });
    
            System.Console.Out.Write(output);
            System.Console.In.ReadLine();
        }

        public class MemoizingFunctionCallBinder : Microsoft.Scripting.DynamicMetaObjectBinder
        {
            public MemoizingFunctionCallBinder()
                : base()
            {
           
            }

            public override DynamicMetaObject Bind(DynamicMetaObject target, DynamicMetaObject[] args)
            {
                Microsoft.Func<object> funct = target.Value as Microsoft.Func<object>;
                var arguments = from param in args select param.Value;
                var argExpressions = from param in args select param.Expression;

                return new DynamicMetaObject(
                    Expression.Constant(funct.DynamicInvoke(arguments) ),
                    BindingRestrictions.GetExpressionRestriction(   // always valid for instances of this name
                    Expression.And(
                        Expression.Equal( target.Expression, Expression.Constant(target)),
                        buildEquals( args )
                    ))
                );
           
            }

            private Expression buildEquals(DynamicMetaObject[] args)
            {
                var exp = args[0].Expression;
                var val = args[0].Value;

                Expression lastExp;
                if( args.Length>0)
                {
                    DynamicMetaObject[] cdr = new DynamicMetaObject[args.Length-1];
                    Array.Copy(args, 1, cdr,0, args.Length-1);
                    lastExp = buildEquals( cdr );
                }
                else
                {
                    lastExp = Expression.Constant(true);
                }
                return Expression.And(
                    Expression.Equal(exp, Expression.Constant(val)),
                    lastExp);
            }
        }
    }
}


On Wed, Apr 1, 2009 at 12:18 AM, mwkohout <notifications@codeplex.com> wrote:

From: mwkohout


I'm trying to build a simple expression tree example, creating a lambda that will return double it's input, but I'm getting this error:

System.ArgumentException was unhandled
  Message="must be reducible node\r\nParameter name: this"
  Source="Microsoft.Scripting.Core"
  ParamName="this"
  StackTrace:
       at Microsoft.Scripting.Utils.ContractUtils.Requires(Boolean precondition, String paramName, String message) in C:\Users\mwkohout\Documents\dlr\DLR_Main\Runtime\Src\Microsoft.Scripting.Core\Utils\ContractUtils.cs:line 54
       at Microsoft.Linq.Expressions.Expression.ReduceAndCheck() in C:\Users\mwkohout\Documents\dlr\DLR_Main\Runtime\Src\Microsoft.Scripting.Core\Ast\Expression.cs:line 186
       at Microsoft.Linq.Expressions.Expression.ReduceExtensions() in C:\Users\mwkohout\Documents\dlr\DLR_Main\Runtime\Src\Microsoft.Scripting.Core\Ast\Expression.cs:line 205
       at Microsoft.Linq.Expressions.Compiler.StackSpiller.RewriteExtensionExpression(Expression expr, Stack stack) in C:\Users\mwkohout\Documents\dlr\DLR_Main\Runtime\Src\Microsoft.Scripting.Core\Compiler\StackSpiller.cs:line 902
       at Microsoft.Linq.Expressions.Compiler.StackSpiller.RewriteExpression(Expression node, Stack stack) in C:\Users\mwkohout\Documents\dlr\DLR_Main\Runtime\Src\Microsoft.Scripting.Core\Compiler\StackSpiller.Generated.cs:line 187
       at Microsoft.Linq.Expressions.Compiler.StackSpiller.RewriteExpressionFreeTemps(Expression expression, Stack stack) in C:\Users\mwkohout\Documents\dlr\DLR_Main\Runtime\Src\Microsoft.Scripting.Core\Compiler\StackSpiller.cs:line 154
       at Microsoft.Linq.Expressions.Compiler.StackSpiller.Rewrite[T](Expression`1 lambda) in C:\Users\mwkohout\Documents\dlr\DLR_Main\Runtime\Src\Microsoft.Scripting.Core\Compiler\StackSpiller.cs:line 105
       at Microsoft.Linq.Expressions.Expression`1.Accept(StackSpiller spiller) in C:\Users\mwkohout\Documents\dlr\DLR_Main\Runtime\Src\Microsoft.Scripting.Core\Ast\LambdaExpression.cs:line 162
       at Microsoft.Linq.Expressions.Compiler.StackSpiller.AnalyzeLambda(LambdaExpression lambda) in C:\Users\mwkohout\Documents\dlr\DLR_Main\Runtime\Src\Microsoft.Scripting.Core\Compiler\StackSpiller.cs:line 93
       at Microsoft.Linq.Expressions.Compiler.StackSpiller.RewriteLambdaExpression(Expression expr, Stack stack) in C:\Users\mwkohout\Documents\dlr\DLR_Main\Runtime\Src\Microsoft.Scripting.Core\Compiler\StackSpiller.cs:line 304
       at Microsoft.Linq.Expressions.Compiler.StackSpiller.RewriteExpression(Expression node, Stack stack) in C:\Users\mwkohout\Documents\dlr\DLR_Main\Runtime\Src\Microsoft.Scripting.Core\Compiler\StackSpiller.Generated.cs:line 97
       at Microsoft.Linq.Expressions.Compiler.StackSpiller.ChildRewriter.Add(Expression node) in C:\Users\mwkohout\Documents\dlr\DLR_Main\Runtime\Src\Microsoft.Scripting.Core\Compiler\StackSpiller.Temps.cs:line 150
       at Microsoft.Linq.Expressions.Compiler.StackSpiller.ChildRewriter.AddArguments(IArgumentProvider expressions) in C:\Users\mwkohout\Documents\dlr\DLR_Main\Runtime\Src\Microsoft.Scripting.Core\Compiler\StackSpiller.Temps.cs:line 166
       at Microsoft.Linq.Expressions.Compiler.StackSpiller.RewriteDynamicExpression(Expression expr, Stack stack) in C:\Users\mwkohout\Documents\dlr\DLR_Main\Runtime\Src\Microsoft.Scripting.Core\Compiler\StackSpiller.cs:line 167
       at Microsoft.Linq.Expressions.Compiler.StackSpiller.RewriteExpression(Expression node, Stack stack) in C:\Users\mwkohout\Documents\dlr\DLR_Main\Runtime\Src\Microsoft.Scripting.Core\Compiler\StackSpiller.Generated.cs:line 184
       at Microsoft.Linq.Expressions.Compiler.StackSpiller.RewriteExpressionFreeTemps(Expression expression, Stack stack) in C:\Users\mwkohout\Documents\dlr\DLR_Main\Runtime\Src\Microsoft.Scripting.Core\Compiler\StackSpiller.cs:line 154
       at Microsoft.Linq.Expressions.Compiler.StackSpiller.Rewrite[T](Expression`1 lambda) in C:\Users\mwkohout\Documents\dlr\DLR_Main\Runtime\Src\Microsoft.Scripting.Core\Compiler\StackSpiller.cs:line 105
       at Microsoft.Linq.Expressions.Expression`1.Accept(StackSpiller spiller) in C:\Users\mwkohout\Documents\dlr\DLR_Main\Runtime\Src\Microsoft.Scripting.Core\Ast\LambdaExpression.cs:line 162
       at Microsoft.Linq.Expressions.Compiler.StackSpiller.AnalyzeLambda(LambdaExpression lambda) in C:\Users\mwkohout\Documents\dlr\DLR_Main\Runtime\Src\Microsoft.Scripting.Core\Compiler\StackSpiller.cs:line 93
       at Microsoft.Linq.Expressions.Compiler.LambdaCompiler.AnalyzeLambda(LambdaExpression& lambda) in C:\Users\mwkohout\Documents\dlr\DLR_Main\Runtime\Src\Microsoft.Scripting.Core\Compiler\LambdaCompiler.cs:line 214
       at Microsoft.Linq.Expressions.Compiler.LambdaCompiler.Compile(LambdaExpression lambda) in C:\Users\mwkohout\Documents\dlr\DLR_Main\Runtime\Src\Microsoft.Scripting.Core\Compiler\LambdaCompiler.cs:line 180
       at Microsoft.Linq.Expressions.LambdaExpression.Compile() in C:\Users\mwkohout\Documents\dlr\DLR_Main\Runtime\Src\Microsoft.Scripting.Core\Ast\LambdaExpression.cs:line 106
       at XtraDLRgeTest.Program.Main(String[] args) in C:\Users\mwkohout\Documents\dlr\DLR_Main\XtraDLRgeTest\Program.cs:line 38
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException:


This is the code I've got:

           LambdaBuilder doubleLB = Utils.Lambda(typeof(Int32), "double", new SourceSpan());
            ParameterExpression param = doubleLB.ClosedOverParameter(typeof(Int32),"i");
            doubleLB.Body = Expression.Multiply(param, Expression.Constant(2));
            LambdaBuilder lb = Utils.Lambda(typeof(object), "test");
           
            lb.Body = Expression.Dynamic(new MemoizingFunctionCallBinder(), typeof(Object), doubleLB.MakeLambda(), Expression.Constant(1), Expression.Constant(new Object()));

            var output = lb.MakeLambda().Compile().DynamicInvoke(new Object[] { });
    
            System.Console.Out.Write(output);


I'm sure my malfunction is simple, but random trial and error hasn't exposed what exactly it is I'm doing wrong.  Do any of you have a suggestion?
thanks again,
Mike Kohout

Read the full discussion online.

To add a post to this discussion, reply to this email (dlr@discussions.codeplex.com)

To start a new discussion for this project, email dlr@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com


Apr 1, 2009 at 12:31 PM
There's some code in LambdaBuilder that's effectively obsolete.  Setting "lb.DoNotAddContext = true" before compiling the lambda should disable it.
Coordinator
Apr 1, 2009 at 2:55 PM

You may also just not want to use LambdBuilder and instead use Expression.Lambda directly.  In your particular example you’re not really doing anything that LambdaBuilder would make easier and the primary reason it exists was to simplify changes at the DLR level w/o having to change existing languages (IronPython and IronRuby) much.  But I think both IronPython and IronRuby have stopped using it as well.

From: curthagenlocher [mailto:notifications@codeplex.com]
Sent: Wednesday, April 01, 2009 5:31 AM
To: Dino Viehland
Subject: Re: simple Chair/Keyboard issue... [dlr:51918]

From: curthagenlocher

There's some code in LambdaBuilder that's effectively obsolete. Setting "lb.DoNotAddContext = true" before compiling the lambda should disable it.

Read the full discussion online.

To add a post to this discussion, reply to this email (dlr@discussions.codeplex.com)

To start a new discussion for this project, email dlr@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com

Apr 2, 2009 at 1:31 AM
Curt, Dinov, I took your advice.  For posterity, this worked like a charm:

ParameterExpression param = Expression.Parameter(typeof(int), "param");
LambdaExpression doubleLE = Expression.Lambda( Expression.Multiply(param, Expression.Constant(2)),
                                                                                     new ParameterExpression[] { param });

var output = doubleLE.Compile().DynamicInvoke(1);

thanks again,
Mike