From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from weis@localhost) by pauillac.inria.fr (8.6.10/8.6.6) id RAA28496 for caml-redistribution; Fri, 27 Sep 1996 17:57:13 +0200 Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.6.10/8.6.6) with ESMTP id OAA18524 for ; Fri, 27 Sep 1996 14:44:30 +0200 Received: from margaux.inria.fr (margaux.inria.fr [128.93.8.2]) by concorde.inria.fr (8.7.6/8.7.1) with ESMTP id OAA07984 for ; Fri, 27 Sep 1996 14:44:30 +0200 (MET DST) Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by margaux.inria.fr (8.6.10/8.6.6) with ESMTP id OAA19910 for ; Fri, 27 Sep 1996 14:44:29 +0200 Received: from lasmea.univ-bpclermont.fr (lasmea.univ-bpclermont.fr [193.54.50.15]) by concorde.inria.fr (8.7.6/8.7.1) with ESMTP id OAA07980 for ; Fri, 27 Sep 1996 14:44:27 +0200 (MET DST) Message-Id: <199609271244.OAA07980@concorde.inria.fr> Received: by lasmea.univ-bpclermont.fr (1.37.109.16/16.2) id AA223998396; Fri, 27 Sep 1996 14:46:36 +0200 From: Jocelyn Serot Subject: Re: native code compiler and exceptions To: caml-list@margaux.inria.fr Date: Fri, 27 Sep 1996 14:46:35 METDST X-Mailer: Elm [revision: 109.17] Sender: weis In his answer to my original question, P. Weis (Pierre.Weis@inria.fr) says: > Wao! To ``rely heavily on array bounds violation'' is a very ugly style > of programming. I could not imagine why you need to use this style. Well, maybe the formulation ``rely heavily on array bounds violation'' was a bit misleading. My code relies on the correct handling of exceptions that, at the lowest level may be trapped as array bound violation. I will try yo explain my pb on a small example. Suppose that images are represented as pixel 2d-arrays (in fact, images are implemented as ADTs, with their internal representation hidden). # type image = int array array # let get_pixel i y x = i.(y).(x) # let set_pixel i y x p = i.(y).(x) <- p # let create nr nc = Array.new_matrix nr nc 0 What i want to do is to compute, for example is a simple 2d-convolution, turning pixel(x,y) into (p(x-1,y)+p(x,y-1)+p(x+1,y)+p(x,y+1))/4. A possible manner to handle the clipping effect at image boundaries is to set that p(x,y) = 0 whenever x<0 ot y<0. So it does not seem to me so "ugly" to write my convolution fn like that: # let conv im = # let nr = nb_rows im and nc = nb_cols im in # let im' = create nr nc in # for y = 0 to pred nr do for x = 0 to pred nc do # let p1 = try_get_pixel im (x-1) y 0 in # let p2 = try_get_pixel im x (y-1) 0 in # let p3 = try_get_pixel im (x+1) y 0 in # let p4 = try_get_pixel im x (y+1) 0 in # set_pixel im' (y,x) ((p1+p2+p3+p4)/4) done done; # m' making use of the given access fn: # let try_get_pixel im y x clip = # try get_pixel im y x with Invalid_argument _ -> clip (btw, note that is this case clip might be a _fn_ of the "faulty" coordinates, like: # let try_get_pixel im y x clip = # try get_pixel im y x with Invalid_argument _ -> clip y x ) Is this not "good" fnal programming style ?.. Or does the "ugliness" lies only in the [... with Invalid_argument _ ->...] construct ? In this case, i guess i should test _explicitely_ whether (y,x) are in bounds instead of relying of the exception [Invalid_argument "Array.get"].Ie: # let try_get_pixel im y x clip = # if (y<0 || y>=(nb_rows im) || x<0 || x>=(nb_cols im)) then clip # else get_pixel im y x Is it what you mean ? Jocelyn -- E-mail: Jocelyn.Serot@lasmea.univ-bpclermont.fr ............................. S-mail: LASMEA - URA 1793 CNRS, Universite Blaise Pascal, 63177 Aubiere cedex Tel: (33) 73.40.73.30 - Fax: (33) 73.40.72.62 ............................... .... http://wwwlasmea.univ-bpclermont.fr/Personnel/Jocelyn.Serot/Welcome.html