<?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>new -- new objects and new types</title> <link rel="stylesheet" type="text/css" href="../../../../Macaulay2/Style/doc.css"/> </head> <body> <table class="buttons"> <tr> <td><div><a href="_new__Class.html">next</a> | <a href="_making_spnew_spclasses.html">previous</a> | <a href="_printing_spand_spformatting_spfor_spnew_spclasses.html">forward</a> | <a href="_making_spnew_spclasses.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="_new.html" title="new objects and new types">new</a></div> <hr/> <div><h1>new -- new objects and new types</h1> <div class="single"><h2>Description</h2> <div><p>In this reference section we discuss how to make new types of object and new objects of those types.</p> <div><h2>installing a new method for new-of-from</h2> <ul><li><div class="list"><dl class="element"><dt class="heading">Usage: </dt><dd class="value"><div><tt>new AA of BB from C := (A,B,c) -> ...</tt></div> </dd></dl> </div> </li> <li>Inputs:<ul><li><span><tt>AA</tt>, <span>a <a href="___Hash__Table.html">hash table</a></span></span></li> <li><span><tt>BB</tt>, <span>a <a href="___Hash__Table.html">hash table</a></span></span></li> <li><span><tt>C</tt>, <span>a <a href="___Hash__Table.html">hash table</a></span></span></li> <li><span><tt>(A,B,c) -> ...</tt>, a function of 3 arguments: <tt>AA</tt> will be an ancestor of <tt>A</tt>, <tt>BB</tt> will be an ancestor of <tt>B</tt>, and <tt>C</tt> will be an ancestor of the class of <tt>c</tt>. Alternatively, <tt>A</tt> will be a type of <tt>AA</tt>, <tt>B</tt> will be a type of <tt>BB</tt>, and <tt>c</tt> will be an instance of <tt>C</tt>.</span></li> </ul> </li> <li>Consequences:<ul><li>the function will be installed as the method for <tt>new AA of BB from C</tt>. It will be stored under the key <tt>(NewOfFromMethod,AA,BB,C)</tt> in the youngest of the hash tables <tt>AA</tt>, <tt>BB</tt>, and <tt>C</tt>.</li> </ul> </li> <li>Outputs:<ul><li><span>the function is returned as the value of the expression</span></li> </ul> </li> </ul> <p>In this example we install a creation method for new types of types from functions. The function is used to construct the method for converting instances of the new type to nets for display.</p> <table class="examples"><tr><td><pre>i1 : new Type of BasicList from Function := (A,B,f) -> hashTable { net => f };</pre> </td></tr> </table> <p>The hash tables <tt>AA</tt>, <tt>BB</tt>, and <tt>C</tt> will normally be instances of <a href="___Type.html" title="the class of all types">Type</a>.</p> </div> <div><h2>new-of-from</h2> <ul><li><div class="list"><dl class="element"><dt class="heading">Usage: </dt><dd class="value"><div><tt>new A of B from c</tt></div> </dd></dl> </div> </li> <li>Inputs:<ul><li><span><tt>A</tt>, <span>a <a href="___Hash__Table.html">hash table</a></span></span></li> <li><span><tt>B</tt>, <span>a <a href="___Hash__Table.html">hash table</a></span></span></li> <li><span><tt>c</tt>, an instance of <tt>C</tt></span></li> </ul> </li> <li>Consequences:<ul><li>the function previously installed as the method for <tt>new A of B from C</tt> will be called with arguments <tt>(A,B,c)</tt>.</li> <li>if no such method has been installed, then ancestors of <tt>A</tt>, <tt>B</tt>, and <tt>C</tt>, will be consulted, searching lexicographically for a method; see <a href="_inheritance.html" title="">inheritance</a>.</li> <li>if no method is found by searching the ancestors, then the function <tt>(A,B,c) -> c</tt> will be used</li> <li>the value returned by the function, (or <tt>c</tt>, if no method was found), will have its class set to <tt>A</tt> and its parent set to <tt>B</tt>; see <a href="_new__Class.html" title="set the class and parent of an object">newClass</a>.</li> </ul> </li> <li>Outputs:<ul><li><span>the new object of class <tt>A</tt> and with parent <tt>B</tt> described above</span></li> </ul> </li> </ul> <p>We use the creation method installed above to create a new type of list.</p> <table class="examples"><tr><td><pre>i2 : f = s -> "--list of type X--" o2 = f o2 : FunctionClosure</pre> </td></tr> <tr><td><pre>i3 : X = new Type of List from f o3 = X o3 : Type</pre> </td></tr> <tr><td><pre>i4 : class X o4 = Type o4 : Type</pre> </td></tr> <tr><td><pre>i5 : parent X o5 = List o5 : Type</pre> </td></tr> <tr><td><pre>i6 : peek X o6 = Type of List{net => f}</pre> </td></tr> </table> <p>Now we use <a href="_new.html" title="new objects and new types">new</a> to create a new list of type <tt>X</tt> from a list. The system knows how to convert lists to lists of type <tt>X</tt>, so no creation method need be installed for <tt>new X from List</tt>.</p> <table class="examples"><tr><td><pre>i7 : x = new X from {1,3,11,12} o7 = --list of type X-- o7 : X</pre> </td></tr> <tr><td><pre>i8 : class x o8 = X o8 : Type</pre> </td></tr> <tr><td><pre>i9 : parent x o9 = Nothing o9 : Type</pre> </td></tr> <tr><td><pre>i10 : peek x o10 = {1, 3, 11, 12}</pre> </td></tr> </table> </div> <div><h2>installing a new method for new-of</h2> <ul><li><div class="list"><dl class="element"><dt class="heading">Usage: </dt><dd class="value"><div><tt>new AA of BB := (A,B) -> ...</tt></div> </dd></dl> </div> </li> <li>Inputs:<ul><li><span><tt>AA</tt>, <span>a <a href="___Hash__Table.html">hash table</a></span></span></li> <li><span><tt>BB</tt>, <span>a <a href="___Hash__Table.html">hash table</a></span></span></li> <li><span><tt>(A,B) -> ...</tt>, a function of 2 arguments: <tt>AA</tt> will be an ancestor of <tt>A</tt>, and <tt>BB</tt> will be an ancestor of <tt>B</tt>. Alternatively, <tt>A</tt> will be a type of <tt>AA</tt>, and <tt>B</tt> will be a type of <tt>BB</tt>.</span></li> </ul> </li> <li>Consequences:<ul><li>the function will be installed as the method for <tt>new AA of BB</tt>. It will be stored under the key <tt>(NewOfMethod,AA,BB)</tt> in the younger of the hash tables <tt>AA</tt> and <tt>BB</tt>.</li> </ul> </li> <li>Outputs:<ul><li><span>the function is returned as the value of the expression</span></li> </ul> </li> </ul> <p>This operation turns out to be needed infrequently, because there is no <tt>from</tt>-clause to provide data for initializing the instance of <tt>A</tt>.</p> <table class="examples"><tr><td><pre>i11 : new Type of BasicList := (type,array) -> ( stderr << "--new " << type << " of " << array << " being made" << endl; new MutableHashTable) o11 = {*Function[stdio:11:38-14:24]*} o11 : FunctionClosure</pre> </td></tr> </table> </div> <div><h2>new-of</h2> <ul><li><div class="list"><dl class="element"><dt class="heading">Usage: </dt><dd class="value"><div><tt>new A of B</tt></div> </dd></dl> </div> </li> <li>Inputs:<ul><li><span><tt>A</tt>, <span>a <a href="___Hash__Table.html">hash table</a></span></span></li> <li><span><tt>B</tt>, <span>a <a href="___Hash__Table.html">hash table</a></span></span></li> </ul> </li> <li>Consequences:<ul><li>the function previously installed as the method for <tt>new A of B</tt> will be called with arguments <tt>(A,B)</tt>.</li> <li>if no such method has been installed, then ancestors of <tt>A</tt> and <tt>B</tt> will be consulted, searching lexicographically for a method; see <a href="_inheritance.html" title="">inheritance</a>.</li> <li>the value returned by the function will have its class set to <tt>A</tt> and its parent set to <tt>B</tt>; see <a href="_new__Class.html" title="set the class and parent of an object">newClass</a>.</li> <li>if no method is found by searching the ancestors, then a new empty instance of <tt>A</tt> with parent <tt>B</tt> will be created</li> </ul> </li> <li>Outputs:<ul><li><span>the new object of class <tt>A</tt> and parent <tt>B</tt> described above</span></li> </ul> </li> </ul> <p>We illustrate this operation by making a new type of basic list, and then by making a list of that type.</p> <table class="examples"><tr><td><pre>i12 : M = new Type of BasicList --new Type of BasicList being made o12 = M o12 : Type</pre> </td></tr> <tr><td><pre>i13 : m = new M from {3,4,5} o13 = M{3, 4, 5} o13 : M</pre> </td></tr> <tr><td><pre>i14 : class m o14 = M o14 : Type</pre> </td></tr> <tr><td><pre>i15 : m#1 o15 = 4</pre> </td></tr> </table> <p>Now let's define a method for reversing the elements of a list of class <tt>M</tt>, using the unary operator <tt>-</tt>.</p> <table class="examples"><tr><td><pre>i16 : - M := reverse o16 = reverse o16 : CompiledFunction</pre> </td></tr> <tr><td><pre>i17 : - m o17 = M{5, 4, 3} o17 : M</pre> </td></tr> </table> </div> <div><h2>installing a new method for new-from</h2> <ul><li><div class="list"><dl class="element"><dt class="heading">Usage: </dt><dd class="value"><div><tt>new AA from C := (A,c) -> ...</tt></div> </dd></dl> </div> </li> <li>Inputs:<ul><li><span><tt>AA</tt>, <span>a <a href="___Hash__Table.html">hash table</a></span></span></li> <li><span><tt>C</tt>, <span>a <a href="___Type.html">type</a></span></span></li> <li><span><tt>(A,c) -> ...</tt>, a function of 2 arguments: <tt>AA</tt> will be an ancestor of <tt>A</tt>, and <tt>C</tt> will be an ancestor of the class of <tt>c</tt>. Alternatively, <tt>A</tt> will be a type of <tt>AA</tt> and <tt>c</tt> will be an instance of <tt>C</tt>.</span></li> </ul> </li> <li>Consequences:<ul><li>the function will be installed as the method for <tt>new AA from C</tt>. It will be stored under the key <tt>(NewFromMethod,AA,C)</tt> in the younger of the hash tables <tt>AA</tt> and <tt>C</tt>.</li> </ul> </li> <li>Outputs:<ul><li><span>the function is returned as the value of the expression</span></li> </ul> </li> </ul> <p>Let's use the class <tt>M</tt> defined above, and introduce a method for creating lists of class <tt>M</tt> from integers. Then we use it in the subsection below.</p> <table class="examples"><tr><td><pre>i18 : new M from ZZ := (M',i) -> 0 .. i o18 = {*Function[stdio:21:24-21:32]*} o18 : FunctionClosure</pre> </td></tr> </table> </div> <div><h2>new-from</h2> <ul><li><div class="list"><dl class="element"><dt class="heading">Usage: </dt><dd class="value"><div><tt>new A from c</tt></div> </dd></dl> </div> </li> <li>Inputs:<ul><li><span><tt>A</tt>, <span>a <a href="___Hash__Table.html">hash table</a></span></span></li> <li><span><tt>c</tt>, an instance of <tt>C</tt></span></li> </ul> </li> <li>Consequences:<ul><li>the function previously installed as the method for <tt>new A from C</tt> will be called with arguments <tt>(A,c)</tt>.</li> <li>if no such method has been installed, then ancestors of <tt>A</tt> and <tt>C</tt>, will be consulted, searching lexicographically for a method; see <a href="_inheritance.html" title="">inheritance</a>.</li> <li>if no method is found by searching the ancestors, then the function <tt>(A,c) -> c</tt> will be used</li> <li>the value returned by the function, (or <tt>c</tt>, if no method was found), will have its class set to <tt>A</tt> and its parent retained; see <a href="_new__Class.html" title="set the class and parent of an object">newClass</a></li> </ul> </li> <li>Outputs:<ul><li><span>the new object of class <tt>A</tt> initialized from <tt>c</tt> described above</span></li> </ul> </li> </ul> <p>We use the new-from method for <tt>new M from ZZ</tt> installed above.</p> <table class="examples"><tr><td><pre>i19 : n = new M from 13 o19 = M{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13} o19 : M</pre> </td></tr> <tr><td><pre>i20 : - n o20 = M{13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0} o20 : M</pre> </td></tr> </table> </div> <div><h2>installing a new method for new</h2> <ul><li><div class="list"><dl class="element"><dt class="heading">Usage: </dt><dd class="value"><div><tt>new AA := (A) -> ...</tt></div> </dd></dl> </div> </li> <li>Inputs:<ul><li><span><tt>AA</tt>, <span>a <a href="___Hash__Table.html">hash table</a></span></span></li> <li><span><tt>(A) -> ...</tt>, a function of 1 argument: <tt>AA</tt> will be an ancestor of <tt>A</tt>. Alternatively, <tt>A</tt> will be a type of <tt>AA</tt>.</span></li> </ul> </li> <li>Consequences:<ul><li>the function will be installed as the method for <tt>new AA</tt>. It will be stored under the key <tt>NewMethod</tt> in the hash table <tt>AA</tt>.</li> </ul> </li> <li>Outputs:<ul><li><span>the function is returned as the value of the expression</span></li> </ul> </li> </ul> <p>We use the class <tt>M</tt> introduced above, and install a method for <tt>new M</tt>, and we use it in the next subsection.</p> <table class="examples"><tr><td><pre>i21 : new M := (M') -> {a,b,c} o21 = {*Function[stdio:24:14-24:22]*} o21 : FunctionClosure</pre> </td></tr> </table> </div> <div><h2>new</h2> <ul><li><div class="list"><dl class="element"><dt class="heading">Usage: </dt><dd class="value"><div><tt>new A</tt></div> </dd></dl> </div> </li> <li>Inputs:<ul><li><span><tt>A</tt>, <span>a <a href="___Hash__Table.html">hash table</a></span></span></li> </ul> </li> <li>Consequences:<ul><li>the function previously installed as the method for <tt>new A</tt> will be called with argument <tt>A</tt>.</li> <li>if no such method has been installed, then ancestors of <tt>A</tt> will be consulted, searching for a method; see <a href="_inheritance.html" title="">inheritance</a>.</li> <li>the value returned by the function will have its class set to <tt>A</tt>; see <a href="_new__Class.html" title="set the class and parent of an object">newClass</a></li> <li>if no method is found by searching the ancestors, then a new empty instance of <tt>A</tt> will be created, if possible</li> </ul> </li> <li>Outputs:<ul><li><span>the new object of class <tt>A</tt> described above</span></li> </ul> </li> </ul> <p>We use the method for <tt>new M</tt> installed above.</p> <table class="examples"><tr><td><pre>i22 : new M o22 = M{a, b, c} o22 : M</pre> </td></tr> </table> </div> </div> </div> <div class="waystouse"><h2>For the programmer</h2> <p>The object <a href="_new.html" title="new objects and new types">new</a> is <span>a <a href="___Keyword.html">keyword</a></span>.</p> </div> <div><h3>Menu</h3> <ul><li><span><a href="_new__Class.html" title="set the class and parent of an object">newClass</a> -- set the class and parent of an object</span></li> </ul> </div> </div> </body> </html>