Re: extra data in expressions
- To: mathgroup at smc.vnet.net
- Subject: [mg53409] Re: [mg53367] extra data in expressions
- From: "Wolf, Hartmut" <Hartmut.Wolf at t-systems.com>
- Date: Tue, 11 Jan 2005 01:30:38 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
>-----Original Message----- >From: joshuahorowitz at comcast.net [mailto:joshuahorowitz at comcast.net] To: mathgroup at smc.vnet.net >Sent: Saturday, January 08, 2005 8:40 AM >Subject: [mg53409] [mg53367] extra data in expressions > >Here's a rather abstract foundations question: > >I once unthinkingly assumed that an expression was determined >by its head and parts. This is of course very wrong: any two >expressions with head Real, no matter how different, have head >Real and no parts. Same goes for Symbol, Integer, String, and >all other "atomic" expression heads. This means that certain >expressions can (and must) hold hidden extra data. > >My question is whether there are any consistent, logical >semantics for this extra data. Can an expression contain both >parts AND extra data? >Which built-in symbols (or rather, expressions with head one >of these built-in symbols) contain extra data; is there a list >somewhere? Can user-defined symbols contain user-defined extra >data? Why does Head[String @@ 1] return Integer? What else >could it possibly return? > >I'm starting to suspect that "everything is an expression" >involves some pretty ugly exceptions. > > Things stay simple, if we avoid implementation fantasies like "hold hidden extra data". A Mathematica expression either is atomic or composed by a head (which in turn may be any Mathematica expression) and a sequence (which might be empty) of elements (each in turn may be any Mathematica expression). The Mathematica function Head returns the head-component if it is composed, else returns a Symbol which characterizes the atom (think of it as a "data type"), which is most important for pattern matching purposes. In[23]:= Head["hello world"[1][2]] Out[23]= "hello world"[1] The implementation of an atom (even if it appears to be "composed" to our eyes, as e.g. Rational[7,8] or 2.1 + 0.2\[ImaginaryI]) is completely unaccessible to us. You can recognize an atomic expression by the function AtomQ: In[15]:= AtomQ /@ {3, 2.1 + 0.2\[ImaginaryI], 7/8, Anything, "{1,2}", \[Pi], {1, 2}, 1[2]} Out[15]= {True, True, True, True, True, True, False, False} In[31]:= Head /@ {3, 2.1 + 0.2\[ImaginaryI], 7/8, Anything, "{1,2}", \[Pi], {1, 2}, 1[2]} Out[31]= {Integer, Complex, Rational, Symbol, String, Symbol, List, 1} Access of components only is possible if the expression is composed: In[17]:= Off[First::"normal"] In[19]:= First /@ {3, 2.1 + 0.2\[ImaginaryI], 7/8, Anything, "{1,2}", \[Pi], {1, 2}, 1[2]} Out[19]= {First[3], First[2.1 + 0.2*I], First[7/8], First[Anything], First["{1,2}"], First[Pi], 1, 2} (* but Part[expression, 0] just gives Head[expression] *) Apply at atoms reproduces the atom (for any "function"): In[14]:= Anything @@@ {3, 2.1 + 0.2\[ImaginaryI], 7/8, Anything, "{1,2}", \[Pi], {1, 2}, 1[2]} Out[14]= {3, 2.1 + 0.2*I, 7/8, Anything, "{1,2}", Pi, Anything[1, 2], Anything[2]} (This explains why Head[String @@ 1] returns Integer.) -- Hartmut Wolf