<?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/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)"
  prev="2002/07/04c53f5edda840f0c2342942bbacead0"
  next="2002/07/54f51c27ee0b81ae764b1dd088861b17"
  prev-in-thread="2002/07/04c53f5edda840f0c2342942bbacead0"
  next-in-thread="2002/07/c2a08e45551c889a92cdde75e853bd34"
  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>
&gt; From: "Johan Baltié" &lt;johan.baltie@wanadoo.fr&gt;
&gt; 
&gt; &gt; Well, Ada does. The strong typing gives information to the compiler for it to
&gt; &gt; deduce when range checking is not needed:
&gt; &gt; 
&gt; &gt; declare
&gt; &gt;   subtype Index is Integer range 1..10;
&gt; &gt;   type Arr is array (Index) of Integer;
&gt; &gt;   a : Arr;
&gt; &gt;   element : Integer;
&gt; &gt;   j : Index := 1;
&gt; &gt;   k : Integer := 11;
&gt; &gt; begin
&gt; &gt;   for i in a'Range loop
&gt; &gt;     element := a(i); -- no range checking needed, i is in range by definition
&gt; &gt;   end loop;
&gt; &gt;   a(j); -- range checking not needed, j is within Index by definition
&gt; &gt;   a(k); -- range checking needed due possibility of k being outside of Index
&gt; &gt; exception
&gt; &gt;   when Constraint_Error =&gt;
&gt; &gt;      -- process the out-of-range error from a(k)
&gt; &gt; end;
&gt; 
&gt; This is similar to Pascal ranges.
&gt; The trouble is that typical code doesn't look like that, but rather
&gt; 
&gt;     let bubble_once arr =
&gt;       for i = 0 to Array.length arr - 2 do
&gt;         if arr.(i) &gt; arr.(i+1) then begin
&gt;           let tmp = arr.(i) in
&gt;           arr.(i) &lt;- arr.(i+1);
&gt;           arr.(i+1) &lt;- tmp
&gt;         end
&gt;       done
&gt; 
&gt; or worse
&gt; 
&gt;     let bubble_one arr last =
&gt;       assert (last &lt; Array.length arr);
&gt;       let swap i =
&gt;         let tmp = arr.(i) in
&gt;         arr.(i) &lt;- arr.(i+1);
&gt;         arr.(i+1) &lt;- tmp
&gt;       in
&gt;       for i = 0 to last - 1 do
&gt;         if arr.(i) &lt; arr.(i+1) then swap i
&gt;       done
&gt; 
&gt; In the first case, that's not too difficult: you just have to know
&gt; that Array.length returns the length of an array, and do a bit of
&gt; arithmetic.
&gt; 
&gt; In the second case, you should propagate the information from the
&gt; assertion to the loop bound, and additionally treat swap as if it were
&gt; inlined (we know it is its only call context).  

No it's not such a mess.
A subrange is  a type. As ocaml is a bit strong on is types it solves itself the
problem

     let bubble_one arr last =
       assert (last &lt; Array.length arr);
       let swap i =
         let tmp = arr.(i) in
         arr.(i) &lt;- arr.(i+1);
         arr.(i+1) &lt;- tmp
       in
       for i = 0 to last - 1 do
         if arr.(i) &lt; arr.(i+1) then swap i
       done

the {..} are my subranges types

bubble_one: a' {0..b'} array -&gt; int -&gt; unit
swap: {0..b'} -&gt; unit

the "for" should be like in Ada, working with range types and no problem will
ever occur.

&gt; And it's fragile:
&gt; if you move "swap" out of the function, then it might be used on any
&gt; array, and you have to do the bound check.

If you move swap out of the function, in another one using another array, the
type will change and a warning/error/check will occur if the types are incompatible.


&gt; [snipped]
&gt;     Jacques Garrigue


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>

