[MPL] More lazy evaluation, for ease of use?

According to Boost MPL documentation, "Every Integral Constant is also a nullary Metafunction, returning itself." Put another way: redundant evaluation of an Integral Constant is harmless. This handy guarantee encourages Metafunctions to evaluate their integral arguments upon demand. As others have pointed out before, such lazy evaluation reduces "typename ...::type" syntactic clutter, makes it easier to avoid instantiating a template with parameters it does not support, and tends to increase compilation speed. However, other Boost MPL concepts do not impose this self-return requirement, and many Boost MPL standard data types do not provide such a guarantee. In particular, I think it would be helpful if all Sequences, Iterators, and Lambda Expressions were also nullary Metafunctions returning themselves. (In particular, it would be nice if generic containers designed for lazy data could accommodate Lambda Expressions.) Furthermore, it would be helpful if more standard Metafunctions evaluated their arguments. In particular, if boost::mpl::eval_if were to evaluate its condition argument, then nested ifs would be much easier to write, because nested conditions would not have to be legal for all template parameters. Perhaps the reason for not evaluating certain arguments is backward compatibility? Personally, I think the convenience of widespread lazy evaluation would outweigh the costs of a transition to new Metafunctions and concepts. In particular, uniform lazy evaluation would reduce the burden on beginners to figure out which arguments must be evaluated, and which must *not* be evaluated. Instead, beginners could learn the simple rule to leave arguments unevaluated unless there is some good, documented reason to do otherwise. One special note: It is useful for MPL Metafunctions to be able to reason about arbitrary C++ types, even if those types do not happen to be nullary Metafunctions. For example, a canonical use of MPL is to compute the type returned by a function template. To that end, we could wrap arbitrary types just as we wrap integral values in Integral Constant types, and then unwrap them only at need. For example, we could define: template< typename X > struct box { typedef box type; typedef X content; }; template< typename B > struct open : B::type::content {}; and then compute with "box" instances instead of raw types, and apply "open" only when a raw type is actually required. What do you all think?

On 31/07/10 00:48, jcarey@jcarey.best.vwh.net wrote:
What do you all think? I like it somehow as I spend a lot of time myself doing box<> around for a lot of tasks. It also make sense that , the same way a constant can be seen a a nualry function, a type is nullary meta-function what so ever.

On Jul 30, 2010, at 6:48 PM, jcarey@jcarey.best.vwh.net wrote:
I think it would be helpful if all Sequences, Iterators, and Lambda Expressions were also nullary Metafunctions returning themselves. (In particular, it would be nice if generic containers designed for lazy data could accommodate Lambda Expressions.)
Not sure what you mean by that last part, but you might have a look at Vesa Karvonen's post: http://lists.boost.org/Archives/boost/2004/10/74984.php and the ensuing thread, oh, and the update. Actually, maybe http://tinyurl.com/lazympl is the best place to look. I did some work on this myself a couple of years ago and had lots of fun at it, but had some trouble figuring out how to make sure it was efficient. It seemed like compilers were not well-adapted to full laziness. A message I sent to Vesa about it is enclosed. -- David Abrahams BoostPro Computing http://boostpro.com

On 07/31/10 08:26, David Abrahams wrote: [snip]
Not sure what you mean by that last part, but you might have a look at Vesa Karvonen's post: http://lists.boost.org/Archives/boost/2004/10/74984.php and the ensuing thread, oh, and the update. Actually, maybe http://tinyurl.com/lazympl is the best place to look.
The http://groups.yahoo.com/group/boost/files/LazyMPL/ mentioned in that post is no longer there. Anybody have a copy they'd care to post to vault so other's can have a look? TIA. -Larry

On Jul 31, 2010, at 2:46 PM, Larry Evans wrote:
The http://groups.yahoo.com/group/boost/files/LazyMPL/ mentioned in that post is no longer there. Anybody have a copy they'd care to post to vault so other's can have a look?
Really wanting to close the vault—Anyone can get a GitHub account. Here's a link: http://github.com/dabrahams/mpl0x/downloads -- David Abrahams BoostPro Computing http://boostpro.com
participants (4)
-
David Abrahams
-
jcarey@jcarey.best.vwh.net
-
joel falcou
-
Larry Evans