From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by yquem.inria.fr (Postfix) with ESMTP id B9DAFBC69 for ; Fri, 9 Mar 2007 13:08:43 +0100 (CET) Received: from postar.fmf.uni-lj.si (vega.fmf.uni-lj.si [193.2.67.45]) by concorde.inria.fr (8.13.6/8.13.6) with ESMTP id l29C8gQJ004517 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO) for ; Fri, 9 Mar 2007 13:08:43 +0100 Received: from localhost (unknown [192.168.5.1]) by postar.fmf.uni-lj.si (Postfix) with ESMTP id 917A3F6A34 for ; Fri, 9 Mar 2007 13:08:42 +0100 (CET) X-Virus-Scanned: amavisd-new at spam.fmf.uni-lj.si Received: from postar.fmf.uni-lj.si ([192.168.5.5]) by localhost (spam.fmf.uni-lj.si [192.168.5.1]) (amavisd-new, port 10024) with ESMTP id NHnebTj1IkdN for ; Fri, 9 Mar 2007 13:24:39 +0100 (CET) Received: from [193.2.67.88] (unknown [193.2.67.88]) by postar.fmf.uni-lj.si (Postfix) with ESMTP id 58B1AE7FC7 for ; Fri, 9 Mar 2007 13:08:42 +0100 (CET) Message-ID: <45F14E4E.6040200@fmf.uni-lj.si> Date: Fri, 09 Mar 2007 13:08:46 +0100 From: Andrej Bauer Reply-To: Andrej.Bauer@andrej.com User-Agent: Icedove 1.5.0.9 (X11/20061220) MIME-Version: 1.0 To: caml-list@yquem.inria.fr Subject: Re: [Caml-list] Operator overloading References: <3D1E4D9CA9BCE04D8F2B55F203AE4CE30666AB74@selma.roomandboard.com> In-Reply-To: <3D1E4D9CA9BCE04D8F2B55F203AE4CE30666AB74@selma.roomandboard.com> Content-Type: text/plain; charset=ISO-8859-2; format=flowed Content-Transfer-Encoding: 7bit X-Miltered: at concorde with ID 45F14E4A.001 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; andrej:01 andrej:01 overloading:01 overloading:01 0,1:01 0,1:01 vectors:01 matrices:01 multiplying:01 vectors:01 matrices:01 toplevel:01 ambiguities:01 caml-list:01 hmm:02 To all who like overloading, I dearly suggest that they explain to themselves the following bit of Mathematica: In[1]:= f[v_] = {{1,0}, {1,1}} . (v + {0,1}); In[2]:= f[{0,0}] Out[2]= {{0, 0}, {1, 1}} Firstly, if you do not think about the above, you won't even see what's wrong. Secondly, once you do see what is wrong, you won't know why it's wrong. The problem is caused by the meaning of + and Mathematica evaluation strategy. Explanation of what is wrong: 1) {0,1} is a two-dimensional vector 2) {{1,0},{1,1}} is the 2x2 matrix with rows (1,0) and (1,1) 3) The operator . means, according to the help system: In[4]:= ?. a.b.c or Dot[a, b, c] gives products of vectors, matrices and tensors. For example, multiplying a 2x2 matrix and a 2D vector gives a 2D vector: In[5]:= {{1,0},{1,1}} . {0,1} Out[5]= {0, 1} 4) Clearly, + is addition :-) but in case you do not trust me: In[6]:= ?+ x + y + z represents a sum of terms. 5) The definition of f then says: take v, add to it the vector {0,1}, then multiply by the 2x2 matrix {{1,0},{1,1}}. 6) Thus we compute f[{0,0}] by hand according to 5): f[{0,0}] = {{1,0},{1,1}} . ({0,0} + {0,1}) = {{1,0},{1,1}} . {0,1} = {0,1} 7) Hmm, strange, Mathematica thinks the answer is a 2x2 matrix, and we think it's a 2D vector. Have fun overloading! I will repeat my suggestion again: when writing serious and complicated code, overloading can cause a lot of harm. When manipulating expressions such as vectors and matrices interactively, it is convenient to have overloaded + etc. Perhaps the right way to solve the dilemma is to have a "toplevel on streoids" which intelligently resolves ambiguities. The intelligence can then go much further than simple overloading. Andrej