From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id IAA25051; Wed, 17 Jul 2002 08:19:57 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id IAA25085 for ; Wed, 17 Jul 2002 08:19:56 +0200 (MET DST) Received: from mel-rto2.wanadoo.fr (smtp-out-2.wanadoo.fr [193.252.19.254]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id g6H6JtX05787 for ; Wed, 17 Jul 2002 08:19:56 +0200 (MET DST) Received: from mel-rta9.wanadoo.fr (193.252.19.69) by mel-rto2.wanadoo.fr (6.5.007) id 3D1838B600C04829 for caml-list@inria.fr; Wed, 17 Jul 2002 08:19:55 +0200 Received: from AlphaSystem.dnsalias.net (80.11.74.121) by mel-rta9.wanadoo.fr (6.5.007) id 3D2A791A0042BB7C for caml-list@inria.fr; Wed, 17 Jul 2002 08:19:55 +0200 Received: from localhost ([127.0.0.1] helo=alphasystem.dnsalias.net) by AlphaSystem.dnsalias.net with esmtp (Exim 3.36 #1 (Debian)) id 17UiAE-000Icu-00 for ; Wed, 17 Jul 2002 08:19:54 +0200 From: "Johan Baltié" To: caml-list@inria.fr Subject: Re: [Caml-list] Statically detecting arrays bound exceptions ?? (was: Universal Serializer) Date: Wed, 17 Jul 2002 07:19:54 +0100 Message-Id: <20020717061954.M92611@wanadoo.fr> X-Mailer: Open WebMail 1.61 20020204 X-OriginatingIP: 192.6.2.192 (baltie_j) MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk Ok as there were the two same comments on my post I do a single answer. Here my first post: >> What about defining type that are subranges of int ? >> à la ADA... Here the comments: ----- >Then how do you make sure that the result of an arithmetic expression is >still within that sub-range? For example, m.( i + j ) ? > >Cheers, >Hao-yang Wang ----- >as i said to john skaller, won't you then get index incrementation exceptions ? >anyway, it's a trade-off. > >-- >Berke >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&lr=&ie=UTF-8&oe=UTF-8&frame=right&th=5c68b368acf1e99&seekm=00-04-194%40comp.compilers#link5 ------------- BEGIN OF FOLLOWUP ------------- > Does any language or any machine provide some mechanism to: > > 1. index an array without checking its bounds > 2. throw an exception if the index was actually out of range > 3. allow the programmer to catch and handle the exception rather than > 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 => -- 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