From: Jeffrey Carter on
On 08/09/2010 06:48 AM, Robert A Duff wrote:
>
> Look at how unsigned types are used in C. size_t is a good
> example. It's used to count up the sizes of things.
> If I have 1000 objects of size 10_000_000, the total
> size is 1000*10_000_000 = 10_000_000_000. If that
> calculation wraps around on a 32-bit machine, the
> answer is just plain wrong. I'd rather get Constraint_Error.

It seems to me there are 2 orthogonal issues: signed or unsigned representation,
and overflow checking on or off. In this case, we'd like an unsigned
representation (because we don't need negative values but do need as large a
range as possible) with overflow checking on (we almost always want overflow
checking on).

If integer types had been approached this way, perhaps "wrap around" would only
happen on the base type of types without overflow checking.

--
Jeff Carter
"Run away! Run away!"
Monty Python and the Holy Grail
58

--- news://freenews.netfront.net/ - complaints: news(a)netfront.net ---
From: Jeffrey Carter on
On 08/09/2010 11:32 AM, Natacha Kerensikova wrote:
>
> In C for such things I usually use a struct bitfield. I guess the Ada
> equivalent would be a record of Booleans, with a pragma or something
> to have them packed together. Would there be anything wrong with that?
> It seems to me more readable than the above IFs, though from what I
> understood from the rest of the thread it would make CASE on multiple
> bit difficult, and it won't allow slicing (which I'm not sure is a bad
> thing, readability-wise). Is there anything else?

Typically, I would address it using:

type Mode_ID is (Alarm, ...);

type Mode_Set is array (Mode_ID) of Boolean;
for Mode_Set'Component_Size use 1;

Mode : Mode_Set;

if Mode (Alarm) then ...

Of course, I'd usually leave off the 'Component_Size definition; memory is
usually cheap and plentiful these days. Only if I had to match an existing
bit-packed representation, or Mode_ID was very large [array (Integer) of
Boolean, anyone?] would I worry about the representation.

--
Jeff Carter
"Run away! Run away!"
Monty Python and the Holy Grail
58

--- news://freenews.netfront.net/ - complaints: news(a)netfront.net ---
From: Robert A Duff on
Natacha Kerensikova <lithiumcat(a)gmail.com> writes:

> That's something I'm quite uncomfortable with. However I see the point
> of the other related posts here. So my question is, if I assume
> Stream_Element is an octet, is there a way to make compilation fail
> with an explicit error message when the assumption doesn't hold? C's
> preprocessor allows this through a #if on CHAR_BITS and a #error to
> stop compilation with a custom message. Any Ada equivalent?

Put:

pragma Assert(Stream_Element'Modulus = 2**8);
pragma Assert(Stream_Element'Size = 8);

in any library package that depends on this fact.

This will fail at run time if it's wrong. But it will fail on
EVERY execution of the program, so it's pretty-much just as
good as a compile-time error. Also, if you're lucky,
the compiler will warn you at compile time (and you can
use warnings-as-errors mode if you like).

You might also want to look up the GNAT-specific Compile_Time_Error
and Compile_Time_Warning pragmas.

- Bob
From: Robert A Duff on
Georg Bauhaus <rm.dash-bauhaus(a)futureapps.de> writes:

> Always takes a while until my slow head grasps Dmitry's
> non-Ada semantics.

;-)

Dmitry has many "interesting" ideas about what Ada ought to be like,
some (many?) of which I agree with. But it's important to keep
straight the difference between "Ada as it is" and "Ada as Dmitry
(or Bob, or ...) would like it to be". ;-)

> case {T0 x T1 x T3} is
> when ... => ...
>
> Static dispatch tables on composite types' values
> switching to non-subprogram sequences of instructions
> wrapped in a case statement that's likely to have
> impressive combinatorial properties...

I can think of several reasonable ways to implement such a
feature. It would, of course, require a rather massive
change to what "static expression" means.

You can already do "case" on a 64-bit integer,
so the compiler already has to deal with cases where
a simple jump table is infeasible.

> Is there no better way than this?
>
> Experimenting further, what will be a useful type of
> case_statement_alternative's discrete choice list, then?
> One that can be explored programmatically?

Any non-limited type, I guess.

- Bob
From: Robert A Duff on
Natacha Kerensikova <lithiumcat(a)gmail.com> writes:

> On Aug 8, 11:08�pm, Robert A Duff <bobd...(a)shell01.TheWorld.com>
> wrote:
>> "Dmitry A. Kazakov" <mail...(a)dmitry-kazakov.de> writes:
>> By the way, one defense of modular types I've heard is that
>> they are used in mathematics. �True. �But mathematicians do
>> not use _implicit_ mod. �They say things like "X = Y (mod N)",
>> which is pronounced "X is congruent to Y (modulo N)".
>> Congruent, not equal.
>
> Actually past a certain level, they don't explicit mod anymore.

Interesting. I never reached that level of math!

>... I
> don't remember hearing many "modulo" in Galois field classes. And
> neither when dealing with stuff like an n-bit integer representing a
> member of Z/2Z [X] quotiented by a n+1 degree polynomial (Reed-Solomon
> stuff). And then you're overloading "+" with something very xor-like

- Bob
First  |  Prev  |  Next  |  Last
Pages: 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
Prev: GPRbuild compatibility
Next: Irony?