comp.lang.ada
 help / color / mirror / Atom feed
* non-consecutive ranges
@ 1999-04-30  0:00 vlight
  1999-04-30  0:00 ` Tucker Taft
                   ` (5 more replies)
  0 siblings, 6 replies; 12+ messages in thread
From: vlight @ 1999-04-30  0:00 UTC (permalink / raw)




is there a way to define a variable to have a non-consecutive range?  for
instance, let's say i wanted to define variable X with a range from 2 to 1024
and 4096 to 8192.

  X : Integer range (1 .. 1024) and (4096 .. 8192);

or something similar to this declaration.  in addition to define
non-consecutive ranges, could i define a range of odd numbers? even numbers?

how would i go about declaring this?

joe

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own    




^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: non-consecutive ranges
  1999-04-30  0:00 non-consecutive ranges vlight
@ 1999-04-30  0:00 ` Tucker Taft
  1999-04-30  0:00   ` dennison
  1999-04-30  0:00 ` dennison
                   ` (4 subsequent siblings)
  5 siblings, 1 reply; 12+ messages in thread
From: Tucker Taft @ 1999-04-30  0:00 UTC (permalink / raw)


vlight@my-dejanews.com wrote:
> 
> is there a way to define a variable to have a non-consecutive range?  for
> instance, let's say i wanted to define variable X with a range from 2 to 1024
> and 4096 to 8192.
> 
>   X : Integer range (1 .. 1024) and (4096 .. 8192);
> 
> or something similar to this declaration.  in addition to define
> non-consecutive ranges, could i define a range of odd numbers? even numbers?
> 
> how would i go about declaring this?

There is nothing like this built into the language.
However, you could define a private type and a "convert_from_int"
function which did an arbitrarily complicated test before
agreeing to convert the integer to a value of the private type.
You could "inline" this function to get roughly the same
performance as if the capability were supported directly
through some special syntax.

If you had a general requirement for types like this, you
could go so far as to write a generic package to be used
for defining such "holey" types, with generic formal parameters
indicating the gap(s) in the range.  E.g.:

generic
    type Underlying_Type is (<>);
    First, Gap_First, Gap_Last, Last : Underlying_Type;
package Holey_Types is
    type Holey is private;
    function Make_Holey(Val : Underlying_Type) return Holey;
    function Val(H : Holey) return Underlying_Type;
private
    pragma Inline(Make_Holey, Val);
    type Holey is new Underlying_Type range First .. Last;
end Holey_Types;

package body Holey_Types is
    function Make_Holey(Val : Underlying_Type) return Holey is
    begin
        if Val in Gap_First .. Gap_Last then
            raise Constraint_Error;
        end if;
        return Holey(Val);  -- Constraint_Error if not in First .. Last
    end Make_Holey;
    function Val(H : Holey) return Underlying_Type is
    begin
        return Underlying_Type(H);
    end Val;
end Holey_Types;

> joe

-- 
-Tucker Taft   stt@averstar.com   http://www.averstar.com/~stt/
Technical Director, Distributed IT Solutions  (www.averstar.com/tools)
AverStar (formerly Intermetrics, Inc.)   Burlington, MA  USA




^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: non-consecutive ranges
  1999-04-30  0:00 non-consecutive ranges vlight
  1999-04-30  0:00 ` Tucker Taft
@ 1999-04-30  0:00 ` dennison
  1999-05-01  0:00 ` Robert Dewar
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 12+ messages in thread
From: dennison @ 1999-04-30  0:00 UTC (permalink / raw)


In article <7gct90$7hr$1@nnrp1.dejanews.com>,
  vlight@my-dejanews.com wrote:
>
>
> is there a way to define a variable to have a non-consecutive range?  for
> instance, let's say i wanted to define variable X with a range from 2 to 1024
> and 4096 to 8192.
>
>   X : Integer range (1 .. 1024) and (4096 .. 8192);
>
> or something similar to this declaration.  in addition to define
> non-consecutive ranges, could i define a range of odd numbers? even numbers?
>
> how would i go about declaring this?

There is no language mechanism for inserting "holes" in numeric types.

Enumerations with rep clauses can be used to skip over certian actual values
(although conceptually there are no "holes" in the enumeration itself). But
trying to define an "Odd" type by enumerating every odd integer would be
beyond painful.

Another possibilty would be to declare a controlled type, and write
initialization and all the mathematical and conversion operations you need to
ensure that Constraint_Error is raised if the number ever goes even.

A third possibility is to bag it and move on...

--
T.E.D.

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own    




^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: non-consecutive ranges
  1999-04-30  0:00 ` Tucker Taft
@ 1999-04-30  0:00   ` dennison
  0 siblings, 0 replies; 12+ messages in thread
From: dennison @ 1999-04-30  0:00 UTC (permalink / raw)


In article <3729FE8E.4C1B2091@averstar.com>,
  Tucker Taft <stt@averstar.com> wrote:

> If you had a general requirement for types like this, you
> could go so far as to write a generic package to be used
> for defining such "holey" types, with generic formal parameters
> indicating the gap(s) in the range.  E.g.:

Leave it to Tucker to create holy types. :-)

--
T.E.D.

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own    




^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: non-consecutive ranges
  1999-04-30  0:00 non-consecutive ranges vlight
                   ` (3 preceding siblings ...)
  1999-05-01  0:00 ` Robert B. Love 
@ 1999-05-01  0:00 ` Ehud Lamm
  1999-05-01  0:00   ` bglbv
  1999-05-03  0:00 ` Josh Highley
  5 siblings, 1 reply; 12+ messages in thread
From: Ehud Lamm @ 1999-05-01  0:00 UTC (permalink / raw)


The idea of non consecutive ranges come up many times. I find it very
remarkable. Consider how costly vvalidity checks for such types can be.
Consider a type which is based on integer, but consists of only the prime
numbers...

This is a nic example of "over abstracting." This idea ofranges is so
appealing in many situations that we tend to quite simply SEE how the
abstraction is logically extended to such "sets of ranges." Many don't
even see this as new, and are surprised to see that some syntax they come
up with, doesn't compile.

This is one of example of the amazing power of good abstractions - we lose
site of the implementation details altogether, and so don't know where the
applicability of the abstraction ends.

We can connect this to the idea of abstraction in general. Great
scientists are usually regarded to be those that aside from using various
scientific abstractions (like differntial equations, newtonian mechanics,
thermodynamics etc.), grasp the inner details - thus knowing when things
are applicable and when they are not and the theory etc. needs to be
modified.

Analogy can many times be the key here, but knowing where the analogies
end is the crucial part. (This sentence may be seen as a pointer to Doug
Hofstadter's theories of analogy making).

Ehud Lamm     mslamm@pluto.mscc.huji.ac.il





^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: non-consecutive ranges
  1999-04-30  0:00 non-consecutive ranges vlight
  1999-04-30  0:00 ` Tucker Taft
  1999-04-30  0:00 ` dennison
@ 1999-05-01  0:00 ` Robert Dewar
  1999-05-01  0:00   ` dvdeug
  1999-05-01  0:00 ` Robert B. Love 
                   ` (2 subsequent siblings)
  5 siblings, 1 reply; 12+ messages in thread
From: Robert Dewar @ 1999-05-01  0:00 UTC (permalink / raw)


In article <7gct90$7hr$1@nnrp1.dejanews.com>,
  vlight@my-dejanews.com wrote:
>
>
> is there a way to define a variable to have a non-consecutive range?
> for instance, let's say i wanted to define variable X with a range
> from 2 to 1024 and 4096 to 8192.
>
>   X : Integer range (1 .. 1024) and (4096 .. 8192);
>
> or something similar to this declaration.  in addition to define
> non-consecutive ranges, could i define a range of odd numbers? even
> numbers?

Or in general numbers for which the arbitrarily complicated predicate
P holds. If you put it this way, you see that the language has to
define some more or less arbitrary cut off point on what is implemented
as part of the fundamental syntax. In C, not even simple ranges make
the cutoff. In Ada, simple ranges do, but not split ranges (or odd or
even etc).

In ABC, Lambert Meerten's teaching language, if I remember right, you
can define arbitrary assertions that are associated with types. Any
attempt to assign a value executes the assertion, and causes a run time
failure if the assertion fails.

That's actually quite a nice extension, which can be done as an
attribute (and therefore is an "allowed" extension).

  type Odd_Integer is new Integer;

  procedure Check (X : Odd_Integer) is
  begin
     if X mod 2 = 1 then
        Raise_Exception
          (Constraint_Error'Identity,
           "even value assigned to Odd_Integer");
     end if;
  end Check;

  for Odd_Integer'Domain_Check use Check;

Then Domain_Check would be called whenever a value of type Odd_Integer
was modified. Another formulation would be

  function Check (X : Odd_Integer) return Odd_Integer;

which would have the opportunity of modifying the assigned value before
the assignment.

Thoughts?

Useful feature?
Silly bell and whistle?

[Above my desk when working on SPITBOL was a large, very elaborate
 sign in caligrophy that said

        DO NOT EMBELLISH

          :-) ]

Robert Dewar

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own    




^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: non-consecutive ranges
  1999-05-01  0:00 ` Ehud Lamm
@ 1999-05-01  0:00   ` bglbv
  1999-05-02  0:00     ` Ehud Lamm
  0 siblings, 1 reply; 12+ messages in thread
From: bglbv @ 1999-05-01  0:00 UTC (permalink / raw)


Ehud Lamm <mslamm@mscc.huji.ac.il> writes:

> We can connect this to the idea of abstraction in general. Great
> scientists are usually regarded to be those that aside from using various
> scientific abstractions (like differntial equations, newtonian mechanics,
> thermodynamics etc.), grasp the inner details - thus knowing when things
> are applicable and when they are not and the theory etc. needs to be
> modified.

No, that's just the definition of a _competent_ scientist. To qualify
as great, a scientist must also have had at least two good original ideas
in his/her career. [Bad ideas don't count, as one tends not to keep track
of their originality.]

Back to programming: a competent programmer also needs some understanding
of the underlying details. In order to successfully use floating point
arithmetic, you need to know about rounding and about the fact that
not all real numbers are representable. If you want to write efficient
code, you need to know both about the properties of the algorithms
that apply to your problem and about existing constraints on how
various features of your programming language are implemented.
The role of abstraction isn't to let us permanently forget the
details, but rather to help us keep the interdependencies between the
various parts of a large program at a manageable level.




^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: non-consecutive ranges
  1999-04-30  0:00 non-consecutive ranges vlight
                   ` (2 preceding siblings ...)
  1999-05-01  0:00 ` Robert Dewar
@ 1999-05-01  0:00 ` Robert B. Love 
  1999-05-04  0:00   ` fraser
  1999-05-01  0:00 ` Ehud Lamm
  1999-05-03  0:00 ` Josh Highley
  5 siblings, 1 reply; 12+ messages in thread
From: Robert B. Love  @ 1999-05-01  0:00 UTC (permalink / raw)


In <7gct90$7hr$1@nnrp1.dejanews.com> vlight@my-dejanews.com wrote:
> 
> 
> is there a way to define a variable to have a non-consecutive range?  
for
> instance, let's say i wanted to define variable X with a range from 2 
to 1024
> and 4096 to 8192.
> 
>   X : Integer range (1 .. 1024) and (4096 .. 8192);
> 
> or something similar to this declaration.  in addition to define
> non-consecutive ranges, could i define a range of odd numbers? even 
numbers?

Didn't Modula-2 have a "Set" datatype that could have non-consecutive
ranges?  Anybody have a good generic set implementation?

--
----------------------------------------------------------------
 Bob Love                                   MIME & NeXT Mail OK
 rlove@neosoft.com                            
----------------------------------------------------------------





^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: non-consecutive ranges
  1999-05-01  0:00 ` Robert Dewar
@ 1999-05-01  0:00   ` dvdeug
  0 siblings, 0 replies; 12+ messages in thread
From: dvdeug @ 1999-05-01  0:00 UTC (permalink / raw)


In article <7gesqf$pfc$1@nnrp1.dejanews.com>,
  Robert Dewar <robert_dewar@my-dejanews.com> wrote:
> That's actually quite a nice extension, which can be done as an
> attribute (and therefore is an "allowed" extension).
[...]
> Then Domain_Check would be called whenever a value of type Odd_Integer
> was modified. Another formulation would be
>
>   function Check (X : Odd_Integer) return Odd_Integer;
>
> which would have the opportunity of modifying the assigned value before
> the assignment.
>
> Thoughts?
>
> Useful feature?
> Silly bell and whistle?

It's interesting. I think the second one is more useful, and if it existed, I
would probably use it at one point. But it is redundant with Ada's typing
system, and non-portable, so I'd head towards the bell and whistle side.

--
David Starner - dstarner98@aasaa.ofe.org

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own    




^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: non-consecutive ranges
  1999-05-01  0:00   ` bglbv
@ 1999-05-02  0:00     ` Ehud Lamm
  0 siblings, 0 replies; 12+ messages in thread
From: Ehud Lamm @ 1999-05-02  0:00 UTC (permalink / raw)


On Sat, 1 May 1999 bglbv@my-dejanews.com wrote:

> > We can connect this to the idea of abstraction in general. Great
> > scientists are usually regarded to be those that aside from using various
> > scientific abstractions (like differntial equations, newtonianmechanics,
> > thermodynamics etc.), grasp the inner details - thus knowing when things
> > are applicable and when they are not and the theory etc. needs to be
> > modified.
> 
> No, that's just the definition of a _competent_ scientist. To qualify
> as great, a scientist must also have had at least two good original ideas
> in his/her career. [Bad ideas don't count, as one tends not to keep track
> of their originality.]

I tend to agree, though it isn't all that clear cut. But really
off-topic... 

> 
> Back to programming: a competent programmer also needs some understanding
> of the underlying details. In order to successfully use floating point
> arithmetic, you need to know about rounding and about the fact that
> not all real numbers are representable. If you want to write efficient
> code, you need to know both about the properties of the algorithms
> that apply to your problem and about existing constraints on how
> various features of your programming language are implemented.
> The role of abstraction isn't to let us permanently forget the
> details, but rather to help us keep the interdependencies between the
> various parts of a large program at a manageable level.
> 

You mean that if someone sells you and ADT, let's say with some complexity
attributes of the code, you must know the implementation? Surely not.

"Abstraction" is too general a word here. You can program a computer, and
even do it reasonably good without knowing how recursion (an abstraction)
is done on each type of machine your code compiles on. 
However I agree, that in many cases understanding the abstraction means
understanding what's under it.

Consider how many programming books, give "urban legends" as explanations
of how various language abstractions are really handled. 
In many cases it is good enough.

Ehud Lamm     mslamm@pluto.mscc.huji.ac.il






^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: non-consecutive ranges
  1999-04-30  0:00 non-consecutive ranges vlight
                   ` (4 preceding siblings ...)
  1999-05-01  0:00 ` Ehud Lamm
@ 1999-05-03  0:00 ` Josh Highley
  5 siblings, 0 replies; 12+ messages in thread
From: Josh Highley @ 1999-05-03  0:00 UTC (permalink / raw)



<vlight@my-dejanews.com> wrote in message
news:7gct90$7hr$1@nnrp1.dejanews.com...
>
>
> is there a way to define a variable to have a non-consecutive range?  for
> instance, let's say i wanted to define variable X with a range from 2 to
1024
> and 4096 to 8192.
>
>   X : Integer range (1 .. 1024) and (4096 .. 8192);
>
> or something similar to this declaration.  in addition to define
> non-consecutive ranges, could i define a range of odd numbers? even
numbers?
>
> how would i go about declaring this?
>
> joe

How about this:

X             : integer range 2..8192;
X_hole     : integer range 1025..4096;
; other "holes" . . .

. . .

if an_integer not in X or else an_integer in X_hole then
    raise contraint_error;
end if;

. . .

; odd integers
if (an_integer not in X) or else (an_integer mod 2 = 0) then
    raise constrain_error;
end if;


Josh Highley
joshhighley@hotmail.com






^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: non-consecutive ranges
  1999-05-01  0:00 ` Robert B. Love 
@ 1999-05-04  0:00   ` fraser
  0 siblings, 0 replies; 12+ messages in thread
From: fraser @ 1999-05-04  0:00 UTC (permalink / raw)


rlove@antispam.neosoft.com (Robert B. Love ) wrote:

>Didn't Modula-2 have a "Set" datatype that could have non-consecutive
>ranges?  Anybody have a good generic set implementation?

You can find one at http://www.whitelion.org/ada/wl.tar.gz ... it's GPLd,
and it contains bounded and unbounded sets (WL.Sets.Bounded,
WL.Sets.Unbounded).

There's a bunch of other stuff in there as well [1], and I'll get around
to documenting and releasing it eventually.

Fraser.

[1] lists, dynamic arrays, a command line interface, a hash table, that
sort of thing.




^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~1999-05-04  0:00 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1999-04-30  0:00 non-consecutive ranges vlight
1999-04-30  0:00 ` Tucker Taft
1999-04-30  0:00   ` dennison
1999-04-30  0:00 ` dennison
1999-05-01  0:00 ` Robert Dewar
1999-05-01  0:00   ` dvdeug
1999-05-01  0:00 ` Robert B. Love 
1999-05-04  0:00   ` fraser
1999-05-01  0:00 ` Ehud Lamm
1999-05-01  0:00   ` bglbv
1999-05-02  0:00     ` Ehud Lamm
1999-05-03  0:00 ` Josh Highley

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox