Ada-Style Range Types with Value Range Propagation in Arithmetic

2014-03-19 Thread Nordlöw
I'm trying to extend my wrapper class Bound to support CTFE Value 
Range Propagation in primary in opUnary and opBinary similar to 
what Ada's Range Types.


My current try so far for binary arithmetic is

auto opBinary(string op, U,
  U lower_rhs = U.min,
  U upper_rhs = U.max,
  string file = __FILE__,
  int line = __LINE__)(Bound!(U, lower_rhs, 
upper_rhs) rhs)

{
Bound!(CommonType!(T, U),
   min(T.min, U.min),
   max(T.max + U.max)) tmp = void; // TODO: Needs fix
mixin("tmp = _value " ~ op ~ "rhs;");
mixin(check());
return tmp;
}

but this errors as

bound.d(120,19): Error: undefined identifier U and I don't 
understand why I can't parameterize opBinary in this way.


Ideas anyone?

For reference, this older version

https://github.com/nordlow/justd/blob/master/bound.d

compiles but do not do value range propagation in arithmetic.


Re: Ada-Style Range Types with Value Range Propagation in Arithmetic

2014-03-19 Thread Nordlöw

Ideas anyone?


I cracked it:


auto opBinary(string op, U,
  string file = __FILE__,
  int line = __LINE__)(U rhs)
{
static if (is(U == Bound))
{
alias C = CommonType!(T, U.type);
Bound!(C,
   cast(C)min + cast(C)U.min,
   cast(C)max + cast(C)U.max) tmp = void; // 
TODO: Needs fix

}
else
{
CommonType!(T, U) tmp = void;
}
mixin("tmp = _value " ~ op ~ "rhs;");
mixin(check());
return tmp;
}


Re: Ada-Style Range Types with Value Range Propagation in Arithmetic

2014-03-22 Thread bearophile

Nordlöw:


I cracked it:


This is important stuff. So if you find that you can't do 
something in D, consider asking for it in the main D newsgroup 
and in Bugzilla.


Bye,
bearophile