<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE message PUBLIC
  "-//MLarc//DTD MLarc output files//EN"
  "../../mlarc.dtd"[
  <!ATTLIST message
    listname CDATA #REQUIRED
    title CDATA #REQUIRED
  >
]>

  <?xml-stylesheet href="../../mlarc.xsl" type="text/xsl"?>


<message 
  url="2002/07/e97792a8a64614e000b7a7096a57651b"
  from="Johan Baltié &lt;johan.baltie@w...&gt;"
  author="Johan Baltié"
  date="2002-07-17T06:19:57"
  subject="Re: [Caml-list] Statically detecting arrays bound exceptions ?? (was: Universal Serializer)"
  prev="2002/07/30f01411c409ed781c4bc76264d690f4"
  next="2002/07/04c53f5edda840f0c2342942bbacead0"
  next-in-thread="2002/07/04c53f5edda840f0c2342942bbacead0"
  prev-thread="2002/07/559d8c8c619d2fdc4d87eb6f282dca61"
  next-thread="2002/07/6f9d163bb49127c0bf96895d22c05290"
  root="../../"
  period="month"
  listname="caml-list"
  title="Archives of the Caml mailing list">

<thread subject="Re: [Caml-list] Statically detecting arrays bound exceptions ?? (was: Universal Serializer)">
<msg 
  url="2002/07/e97792a8a64614e000b7a7096a57651b"
  from="Johan Baltié &lt;johan.baltie@w...&gt;"
  author="Johan Baltié"
  date="2002-07-17T06:19:57"
  subject="Re: [Caml-list] Statically detecting arrays bound exceptions ?? (was: Universal Serializer)">
<msg 
  url="2002/07/04c53f5edda840f0c2342942bbacead0"
  from="Jacques Garrigue &lt;garrigue@k...&gt;"
  author="Jacques Garrigue"
  date="2002-07-17T06:47:04"
  subject="Re: [Caml-list] Statically detecting arrays bound exceptions ?? (was: Universal Serializer)">
<msg 
  url="2002/07/455bfaf53e9277eca0b090e59a694608"
  from="Johan Baltié &lt;johan.baltie@w...&gt;"
  author="Johan Baltié"
  date="2002-07-17T07:15:07"
  subject="Re: [Caml-list] Statically detecting arrays bound exceptions ?? (was: Universal Serializer)">
<msg 
  url="2002/07/c2a08e45551c889a92cdde75e853bd34"
  from="Jacques Garrigue &lt;garrigue@k...&gt;"
  author="Jacques Garrigue"
  date="2002-07-17T07:33:10"
  subject="Re: [Caml-list] Statically detecting arrays bound exceptions ?? (was: Universal Serializer)">
<msg 
  url="2002/07/16de9bda0d6614f523023e419ef92d08"
  from="Johan Baltié &lt;johan.baltie@w...&gt;"
  author="Johan Baltié"
  date="2002-07-17T07:53:47"
  subject="Re: [Caml-list] Sub{range,type} (was: Statically detecting arrays bound exceptions ??)">
</msg>
</msg>
</msg>
</msg>
</msg>
</thread>

<contents>
Ok as there were the two same comments on my post I do a single answer.

Here my first post:

&gt;&gt; What about defining type that are subranges of int ?
&gt;&gt; à la ADA...


Here the comments:
-----
&gt;Then how do you make sure that the result of an arithmetic expression is
&gt;still within that sub-range? For example, m.( i + j ) ?
&gt;
&gt;Cheers,
&gt;Hao-yang Wang

-----

&gt;as i said to john skaller, won't you then get index incrementation exceptions ?
&gt;anyway, it's a trade-off.
&gt;
&gt;-- 
&gt;Berke
&gt;mayur_naik@my-deja.com writes:

-----

Now, as I am not an ADA guru I just show you my source:

http://groups.google.com/groups?hl=fr&amp;lr=&amp;ie=UTF-8&amp;oe=UTF-8&amp;frame=right&amp;th=5c68b368acf1e99&amp;seekm=00-04-194%40comp.compilers#link5

------------- BEGIN OF FOLLOWUP -------------

&gt; Does any language or any machine provide some mechanism to:
&gt;
&gt; 1. index an array without checking its bounds
&gt; 2. throw an exception if the index was actually out of range
&gt; 3. allow the programmer to catch and handle the exception rather than
&gt;    terminate the program

Well, Ada does. The strong typing gives information to the compiler for it to
deduce when range checking is not needed:

declare
  subtype Index is Integer range 1..10;
  type Arr is array (Index) of Integer;
  a : Arr;
  element : Integer;
  j : Index := 1;
  k : Integer := 11;
begin
  for i in a'Range loop
    element := a(i); -- no range checking needed, i is in range by definition
  end loop;
  a(j); -- range checking not needed, j is within Index by definition
  a(k); -- range checking needed due possibility of k being outside of Index
exception
  when Constraint_Error =&gt;
     -- process the out-of-range error from a(k)
end;

--
Cheers,                                        The Rhythm is around me,
                                               The Rhythm has control.
Ray Blaak                                      The Rhythm is inside me,
blaak@infomatch.com                            The Rhythm has my soul.

------------- END OF FOLLOWUP -------------

IMHO, the use of subtypes help to avoid a lot of bound check, maybe not all of
them, but I do not think that eliminating *all* the bounds check is useful.

Another point is that using subranges you're not allowed to do many errors as
when you are using bare int.

Ciao

Jo
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners

</contents>

</message>

