<html><head><base href="x-msg://92/"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0; "><span class="Apple-style-span" style="border-collapse: separate; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; -webkit-text-decorations-in-effect: none; text-indent: 0px; -webkit-text-size-adjust: auto; text-transform: none; orphans: 2; white-space: normal; widows: 2; word-spacing: 0px; "><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><span class="Apple-style-span" style="border-collapse: separate; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; -webkit-text-decorations-in-effect: none; text-indent: 0px; -webkit-text-size-adjust: auto; text-transform: none; orphans: 2; white-space: normal; widows: 2; word-spacing: 0px; "><span class="Apple-style-span" style="border-collapse: separate; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; -webkit-text-decorations-in-effect: none; text-indent: 0px; -webkit-text-size-adjust: auto; text-transform: none; orphans: 2; white-space: normal; widows: 2; word-spacing: 0px; "><span class="Apple-style-span" style="border-collapse: separate; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; -webkit-text-decorations-in-effect: none; text-indent: 0px; -webkit-text-size-adjust: auto; text-transform: none; orphans: 2; white-space: normal; widows: 2; word-spacing: 0px; "><span class="Apple-style-span" style="border-collapse: separate; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; -webkit-text-decorations-in-effect: none; text-indent: 0px; -webkit-text-size-adjust: auto; text-transform: none; orphans: 2; white-space: normal; widows: 2; word-spacing: 0px; "><span class="Apple-style-span" style="border-collapse: separate; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; -webkit-text-decorations-in-effect: none; text-indent: 0px; -webkit-text-size-adjust: auto; text-transform: none; orphans: 2; white-space: normal; widows: 2; word-spacing: 0px; "><span class="Apple-style-span" style="border-collapse: separate; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; -webkit-text-decorations-in-effect: none; text-indent: 0px; -webkit-text-size-adjust: auto; text-transform: none; orphans: 2; white-space: normal; widows: 2; word-spacing: 0px; "><span class="Apple-style-span" style="border-collapse: separate; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; -webkit-text-decorations-in-effect: none; text-indent: 0px; -webkit-text-size-adjust: auto; text-transform: none; orphans: 2; white-space: normal; widows: 2; word-spacing: 0px; "><span class="Apple-style-span" style="border-collapse: separate; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; -webkit-text-decorations-in-effect: none; text-indent: 0px; -webkit-text-size-adjust: auto; text-transform: none; orphans: 2; white-space: normal; widows: 2; word-spacing: 0px; "><div><span class="Apple-style-span" style="font-size: medium;"><font class="Apple-style-span" color="#0000FF" face="'Gill Sans'">I still don't understand what is broken here...</font></span></div></span></span></span></span></span></span></span></span></div></span></span></div><div><br class="webkit-block-placeholder"></div><div><div>On 23 Jan 2010, at 14:30, Jay K wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><span class="Apple-style-span" style="border-collapse: separate; font-family: Helvetica; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; "><div class="hmmessage" style="font-size: 10pt; font-family: Verdana; ">Once I fix Lex.m3, should we continue to allow -FIRST(INTEGER) or not?<br>As well, should we go the extra bit and disallow it even if it doesn't overflow?<br> ie: on one's complement?<br> <br> <br>I already disallow it in some code -- depending on which code gets<br>hit in the backend. But this disallowing is new.<br> <br> - Jay<br><br> <br><hr id="stopSpelling">From:<span class="Apple-converted-space"> </span><a href="mailto:jay.krell@cornell.edu">jay.krell@cornell.edu</a><br>To:<span class="Apple-converted-space"> </span><a href="mailto:hosking@cs.purdue.edu">hosking@cs.purdue.edu</a><br>CC:<span class="Apple-converted-space"> </span><a href="mailto:rodney_bates@lcwb.coop">rodney_bates@lcwb.coop</a>;<span class="Apple-converted-space"> </span><a href="mailto:m3devel@elegosoft.com">m3devel@elegosoft.com</a><br>Subject: RE: [M3devel] the meaning of -FIRST(INTEGER)?<br>Date: Sat, 23 Jan 2010 05:00:34 +0000<br><br>Sorry, not literals.<br> <br>How do I write correct portable code to test if a Word is greater than -FIRST(INTEGER)?<br> <br>How do I express -FIRST(INTEGER)? Without incurring signed overflow<br>while evaluating the constant expression?<br> <br>For a one's complement system, nothing wrong with -FIRST(INTEGER).<br> It equals LAST(INTEGER).<br> <br>But for a two's complement system, evaluating -FIRST(INTEGER)<br> incurs overflow.<br> <br>Maybe:<br> Word.Add(-(FIRST(INTEGER) + 1)), 1)<br> <br>?<br> <br>Probably. I'll try that.<br> <br>You know, C has the same dilemna and similar solution.<br>not a great idea to write<br>unsigned u = (unsigned)-INT_MIN;<br> <br>nor<br> <br>unsigned u = -(unsigned)INT_MIN;<br> <br>though the second is maybe ok.<br>The first incurs signed overflow in C as well.<br>Which isn't defined. The first also gets a warning<br>with some compilers: "negating unsigned is still unsigned".<br> <br>An idiom is<br>unsigned u = ((unsigned)-(INT_MIN + 1)) + 1;<br> <br>derivation:<br>INT_MIN + 1 yields a negative number that can be safely negated.<br>Which yields a positive number one less than the desired value.<br> <br> <br> - Jay<br><br><hr id="ecxstopSpelling">Subject: Re: [M3devel] the meaning of -FIRST(INTEGER)?<br>From:<span class="Apple-converted-space"> </span><a href="mailto:hosking@cs.purdue.edu">hosking@cs.purdue.edu</a><br>Date: Fri, 22 Jan 2010 18:52:05 -0500<br>CC:<span class="Apple-converted-space"> </span><a href="mailto:rodney_bates@lcwb.coop">rodney_bates@lcwb.coop</a>;<span class="Apple-converted-space"> </span><a href="mailto:m3devel@elegosoft.com">m3devel@elegosoft.com</a><br>To:<span class="Apple-converted-space"> </span><a href="mailto:jay.krell@cornell.edu">jay.krell@cornell.edu</a><br><br><span class="ecxecxApple-style-span" style="font-family: Times; "><div><span class="ecxecxApple-style-span" style="font-family: Helvetica; "><div><span class="ecxecxApple-style-span" style="text-transform: none; text-indent: 0px; border-collapse: separate; font: normal normal normal medium/normal Helvetica; white-space: normal; letter-spacing: normal; word-spacing: 0px; "><div class="ecxecxhmmessage" style="font-family: Verdana; font-size: 10pt; "><div>There's no language hole! Unsigned literals and unsigned constant expressions can easily be handled.</div><div><br></div><div>Literals:</div></div></span></div></span></div><div><br></div>If no explicit base is present, the value of the literal must be at most <tt>LAST(INTEGER)</tt>. If an explicit base is present, the value of the literal must be less than<tt>2^Word.Size</tt>, and its interpretation uses the convention of the <a href="http://www.cs.purdue.edu/homes/hosking/m3/reference/word-intf.html"><tt>Word</tt></a> interface. For example, on a sixteen-bit two's complement machine, <tt>16_FFFF</tt> and <tt>-1</tt>represent the same value.</span><br><br><div><div>Constant expressions:</div><div><br></div><div>CONST x = Word.Plus(16_FFFF, 1)</div><div><br></div><div><br></div><div>On 22 Jan 2010, at 17:51, Jay K wrote:</div><br class="ecxecxApple-interchange-newline"><blockquote><span class="ecxecxApple-style-span" style="text-transform: none; text-indent: 0px; border-collapse: separate; font: normal normal normal medium/normal Helvetica; white-space: normal; letter-spacing: normal; word-spacing: 0px; "><div class="ecxecxhmmessage" style="font-family: Verdana; font-size: 10pt; ">I think there is a language hole here.<div>The language should perhaps allow for</div><div>unsigned literals and unsigned constant</div><div>expressions.</div><div><br></div><div><br></div><div>Something I don't quite have my head around as well,</div><div>maybe a language/library hole, is how to do</div><div>e.g. the below, without assuming two's complement</div><div>representation of signed integers.</div><div><br></div><div><br></div><div>Can we maybe add Word.AbsoluteValueOfInteger?</div><div>The implementation would, roughly:</div><div> IF i < 0 THEN </div><div> i := -i;</div><div> END;</div><div> RETURN i;</div><div> END;</div><div><br></div><div>with the extra qualification that overflow is silent</div><div><br></div><div><br></div><div> > But surely, we could agree that compile time<br style="text-indent: 0in !important; "> > arithmetic should do the same thing as runtime would, for a given<br style="text-indent: 0in !important; "> > implementation.</div><div><br></div><div>Uh huh. This is why you need *different types* (or interfaces)</div><div>for the variety of integer overflow behavior and not a runtime</div><div>setting. Otherwise the compiler can't know what the</div><div>runtime behavior is.</div><div><br></div><div><br></div><div>As well, using static typing instead of a runtime setting,</div><div>allows for efficiency. Imagine, say, if x86 does require</div><div>extra code to check for overflow.</div><div>Well, if using "integer-with-silent-overflow", that</div><div>would be omitted. If using "integer-with-overflow"</div><div>the code would be included.</div><div><br></div><div><br></div><div>I introduce the error. It used to be silent.</div><div>I changed it to a warning, for the</div><div>very specific case of negating FIRST(INTEGER).</div><div>Any other overflow here, which is probably not</div><div>possible, will still error.</div><div><br></div><div> - Jay<br><br><br>> Date: Fri, 22 Jan 2010 19:56:37 +0000<br>> From:<span class="ecxecxApple-converted-space"> </span><a href="mailto:rodney_bates@lcwb.coop">rodney_bates@lcwb.coop</a><br>> To:<span class="ecxecxApple-converted-space"> </span><a href="mailto:m3devel@elegosoft.com">m3devel@elegosoft.com</a><br>> Subject: Re: [M3devel] the meaning of -FIRST(INTEGER)?<br>><span class="ecxecxApple-converted-space"> </span><br>> ><br>> > So..I have m3back using Target.Int a bunch.<br>> ><br>> > And converting back and forth some between Target.Int and<br>> ><br>> > INTEGER and doing match with Target.Int.<br>> ><br>> > And various operations can fail.<br>> ><br>> ><br>> ><br>> ><br>> ><br>> > And my current diff results in:<br>> ><br>> ><br>> ><br>> ><br>> ><br>> > new source -> compiling Lex.m3<br>> > "..\src\fmtlex\Lex.m3", line 227: doneg: Negate overflowed<br>> > "..\src\fmtlex\Lex.m3", line 343: doneg: Negate overflowed<br>> > 2 errors encountered<br>> > new source -> compiling Scan.i3<br>> ><br>> ><br>> ><br>> ><br>> ><br>> > which is nice to see, it means my code is actually running.<br>> ><br>> ><br>> ><br>> ><br>> ><br>> > So I look at the code in question:<br>> ><br>> ><br>> ><br>> ><br>> ><br>> > PROCEDURE ReadNumber(rd: Rd.T; defaultBase: [2..16]; signed: BOOLEAN):<br>> > Word.T<br>> > VAR c: CHAR; sign: [0..1]; res: Word.T; BEGIN<br>> > ...<br>> ><br>> > IF signed AND<br>> > ((sign = 0 AND Word.GT(res, LAST(INTEGER))) OR<br>> > (sign = 1 AND Word.GT(res, -FIRST(INTEGER)))) THEN<br>> > RAISE FloatMode.Trap(FloatMode.Flag.IntOverflow)<br>> > ....<br>> ><br>> ><br>> ><br>> ><br>> ><br>> > -FIRST(INTEGER).<br>> ><br>> ><br>> ><br>> ><br>> ><br>> > What is that supposed to do?<br>> ><br>> ><br>> ><br>> ><br>> ><br>> > I mean, I kind of know, I'm slightly playing stupid, partly not.<br>> ><br>> > Does the compiler know what is an INTEGER vs. what is a "Word"?<br>> ---------------------------------------------------------Word.T?<br>><span class="ecxecxApple-converted-space"> </span><br>> No, they are the same type.<br>><span class="ecxecxApple-converted-space"> </span><br>> > Or it is just obligated to assume everything is a Word?<br>><span class="ecxecxApple-converted-space"> </span><br>> It is obligated to do the builtin operators (unary - being one<br>> of these) using signed interpretation of the value and functions<br>> in Word using unsigned interpretation.<br>><span class="ecxecxApple-converted-space"> </span><br>> The signed operators don't assume anything about the machine<br>> representation. The functions in Word do assume two-s complement<br>> binary, in order to be defined. This is a low-level facility.<br>><span class="ecxecxApple-converted-space"> </span><br>> > To do the negation at compile time and ignore the overflow?<br>><span class="ecxecxApple-converted-space"> </span><br>> This may be poorly specified. The code sample you cite clearly assumes<br>> this is what it does. The compile errors indicate the assumption<br>> has become wrong.<br>><span class="ecxecxApple-converted-space"> </span><br>> > Does the language need work here?<br>><span class="ecxecxApple-converted-space"> </span><br>> Overflow detection wars! But surely, we could agree that compile time<br>> arithmetic should do the same thing as runtime would, for a given<br>> implementation.<br>><span class="ecxecxApple-converted-space"> </span><br>> > I mean, like, -FIRST(INTEGER), that is a problematic expression, isn't it?<br>><span class="ecxecxApple-converted-space"> </span><br>> Yes, it's a statically unconditional overflow, and the language doesn't<br>> specify what happens.<br>><span class="ecxecxApple-converted-space"> </span><br>> As long as we are assuming two-s complement binary, I would just remove<br>> the - in front of FIRST(INTEGER). If the compiler does not consider it<br>> and overflow error and wraps around, in this particular case, the - is<br>> an identity operation on the bits. Maybe the writer intended the -<br>> to hint to a human reader that unsigned interpretation is about to be<br>> applied to this value.<br>><span class="ecxecxApple-converted-space"> </span><br>> ><br>> ><br>> ><br>> ><br>> > - Jay<br>> ><br>> ><br>> ><br>><span class="ecxecxApple-converted-space"> </span><br>><span class="ecxecxApple-converted-space"> </span><br></div></div></span></blockquote></div><br></div></span><br class="Apple-interchange-newline"></blockquote></div><br></body></html>