Sophie

Sophie

distrib > Fedora > 15 > i386 > by-pkgid > 7ebd25ac536d248d499a3ce2acda963a > files > 4548

Macaulay2-1.3.1-8.fc15.i686.rpm

<?xml version="1.0" encoding="utf-8" ?>  <!-- for emacs: -*- coding: utf-8 -*- -->
<!-- Apache may like this line in the file .htaccess: AddCharset utf-8 .html -->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 plus MathML 2.0 plus SVG 1.1//EN"	 "http://www.w3.org/2002/04/xhtml-math-svg/xhtml-math-svg-flat.dtd" >
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head><title>inheritance</title>
<link rel="stylesheet" type="text/css" href="../../../../Macaulay2/Style/doc.css"/>
</head>
<body>
<table class="buttons">
  <tr>
    <td><div><a href="_making_spnew_spclasses.html">next</a> | <a href="_binary_spmethods.html">previous</a> | <a href="_making_spnew_spclasses.html">forward</a> | <a href="_binary_spmethods.html">backward</a> | <a href="___The_sp__Macaulay2_splanguage.html">up</a> | <a href="index.html">top</a> | <a href="master.html">index</a> | <a href="toc.html">toc</a> | <a href="http://www.math.uiuc.edu/Macaulay2/">Macaulay2 web site</a></div>

    </td>
  </tr>
</table>
<div><a href="index.html" title="">Macaulay2Doc</a> > <a href="___The_sp__Macaulay2_splanguage.html" title="">The Macaulay2 language</a> > <a href="_inheritance.html" title="">inheritance</a></div>
<hr/>
<div><h1>inheritance</h1>
<div>Each class has a parent class that can be used as a container for bits of code that apply to a more general class of objects.  In this section we show how this mechanism works in detail.<p/>
We begin by creating a new type of basic list.<table class="examples"><tr><td><pre>i1 : X = new Type of BasicList

o1 = X

o1 : Type</pre>
</td></tr>
<tr><td><pre>i2 : parent X

o2 = BasicList

o2 : Type</pre>
</td></tr>
</table>
The parent of <tt>X</tt> is <a href="___Basic__List.html" title="the class of all basic lists">BasicList</a>, as desired, thus methods applicable to basic lists will also apply also to instances of <tt>X</tt>.  One such method is the method for creating a net from a basic list; here is its code:<table class="examples"><tr><td><pre>i3 : code(net,BasicList)

o3 = -- code for method: net(BasicList)
     /builddir/build/BUILD/Macaulay2-1.3.1-r10737/Macaulay2/m2/nets.m2:177:20-180:47: --source code:
     net BasicList := x -> horizontalJoin deepSplice (
           net class x, 
           "{",
           toSequence between(comma,apply(toList x,netn)),</pre>
</td></tr>
</table>
This code is run automatically to display an instance of <tt>X</tt>, so if we make one, we'll be able to see what it is:<table class="examples"><tr><td><pre>i4 : x = new X from {2,3,4}

o4 = X{2, 3, 4}

o4 : X</pre>
</td></tr>
</table>
Now let's imagine we wish to treat instances of <tt>X</tt> as vectors, and to negate one by negating its entries.  As it happens, no method for this has been installed for basic lists, as we can check with <a href="_lookup.html" title="look up methods">lookup</a>.<table class="examples"><tr><td><pre>i5 : lookup(symbol -, X) === null

o5 = false</pre>
</td></tr>
</table>
We install and test a new method as described in <a href="_installing_spmethods.html" title="">installing methods</a>.<table class="examples"><tr><td><pre>i6 : - X := t -> apply(t,i -> -i);</pre>
</td></tr>
<tr><td><pre>i7 : - x

o7 = X{-2, -3, -4}

o7 : X</pre>
</td></tr>
</table>
This method will apply automatically to subclasses of <tt>X</tt>, as we see now.<table class="examples"><tr><td><pre>i8 : Y = new Type of X;</pre>
</td></tr>
<tr><td><pre>i9 : y = new Y from {4,5,6}

o9 = Y{4, 5, 6}

o9 : Y</pre>
</td></tr>
<tr><td><pre>i10 : - y

o10 = Y{-4, -5, -6}

o10 : Y</pre>
</td></tr>
</table>
For <tt>binary methods</tt>, there is an apparent ambiguity in deciding exactly how inheritance will work.  Let's illustrate by making a new subclass <tt>Z</tt> of <tt>X</tt>.<table class="examples"><tr><td><pre>i11 : Z = new Type of X;</pre>
</td></tr>
<tr><td><pre>i12 : z = new Z from {7,8,9}

o12 = Z{7, 8, 9}

o12 : Z</pre>
</td></tr>
</table>
Now let's install two methods, either of which might conceivably be applied to evaluate the expression <tt>y+z</tt>, and see what happens.<table class="examples"><tr><td><pre>i13 : Y + X := (a,b) -> YX;</pre>
</td></tr>
<tr><td><pre>i14 : X + Z := (a,b) -> XZ;</pre>
</td></tr>
<tr><td><pre>i15 : y + z

o15 = YX

o15 : Symbol</pre>
</td></tr>
</table>
The result is the symbol <tt>YX</tt>.  The reason is that after finding that no method applies directly for adding an instance of <tt>Y</tt> to an instance of <tt>Z</tt>, the search continues: <tt>Z</tt> is replaced by its parent <tt>X</tt>, and so on.  (After enough unsuccessful iterations of this, the second type is reset to <tt>Z</tt>, the first type is replaced by its parent, and the search continues.)<p/>
The same search order applies to method functions defined with <a href="_method.html" title="make a new method function">method</a>.</div>
</div>
</body>
</html>