{"id":455,"date":"2018-11-13T18:19:20","date_gmt":"2018-11-13T18:19:20","guid":{"rendered":"https:\/\/books.compclassnotes.com\/elementarycomputing\/?page_id=455"},"modified":"2018-11-13T18:19:20","modified_gmt":"2018-11-13T18:19:20","slug":"nested-functions","status":"publish","type":"page","link":"https:\/\/books.compclassnotes.com\/elementarycomputing\/nested-functions\/","title":{"rendered":"Nested Functions"},"content":{"rendered":"<p>In the previous section we applied a test to see if a variable was bound in the body of a function. We haven&#8217;t seen many examples so far where this would be true, but consider function\u00a0<strong>(\u03bbx. + (\u03bby. + 3 y) x 2);\u00a0<\/strong>is y bound in it?<\/p>\n<p>We consider the function to be:\u00a0<strong>(\u03bbx. E)\u00a0<\/strong>so clearly,\u00a0<strong>y<\/strong> doesn&#8217;t appear in the parameter list. The other possibility is that it is bound in\u00a0<strong>E<\/strong>. Here,\u00a0<strong>E = (+ (\u03bby. + 3 y) x 2)<\/strong>. This time\u00a0<strong>y<\/strong> does appear in the parameter list, and it is free in the body\u00a0<strong>(+ 3 y)<\/strong>, meaning that it is bound in\u00a0<strong>E,\u00a0<\/strong>so it follows that\u00a0<strong>y\u00a0<\/strong>is bound in\u00a0<strong>(\u03bbx. + (\u03bby. + 3 y) x 2).<\/strong><\/p>\n<p>This is a fairly subtle thing, so make sure you&#8217;re satisfied with this before going on. What this tells us is that a variable doesn&#8217;t have to be bound by the\u00a0<em>first<\/em>\u00a0\u03bb in an expression, that it can be bound elsewhere in the expression.<\/p>\n<p>If we try evaluate that function by passing it the value 7 we get:<\/p>\n<p><strong>( (\u03bbx. + (\u03bby. + 3 y) x 2) 7)<\/strong><\/p>\n<p>Perform a \\(\\beta\\) conversion on <strong>x\u00a0<\/strong>to get:<\/p>\n<p><strong>(+ (\u03bby. + 3 y) 7 2)<\/strong><\/p>\n<p>Now we do another\u00a0\\(\\beta\\) conversion (on <strong>y<\/strong>) to get:<\/p>\n<p><strong>(+ (+ 3\u00a0 7) 2)<\/strong><\/p>\n<p>Here&#8217;s another example. Is\u00a0<strong>x\u00a0<\/strong>free or bound?<\/p>\n<p><strong>((\u03bbx. + x 3) x)<\/strong><\/p>\n<p>If we take\u00a0<strong>E\u00a0<\/strong>to be\u00a0<strong>(\u03bbx. + x 3) <\/strong>and<strong> F <\/strong>to be<strong> x<\/strong><strong>,\u00a0<\/strong>then\u00a0<strong>x<\/strong> is bound in <strong>E\u00a0<\/strong>and\u00a0free in\u00a0<strong>F,\u00a0<\/strong>which means that\u00a0<strong>x\u00a0<\/strong>is both free\u00a0<em>and<\/em> bound in\u00a0<strong>(E F)<\/strong>, so it is free and bound in the same expression! What does this mean? It means that these are two different <strong>x<\/strong>&#8216;s, so the same name doesn&#8217;t always mean the same variable. Take a look at the AST below; it&#8217;s much clearer there because the\u00a0\u03bb node shows us which\u00a0<strong>x\u00a0<\/strong>belongs to the parameter list.<\/p>\n<div id=\"attachment_459\" style=\"width: 291px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-459\" class=\"size-full wp-image-459\" src=\"https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-content\/uploads\/sites\/9\/2018\/11\/Free-02.png\" alt=\"\" width=\"281\" height=\"381\" srcset=\"https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-content\/uploads\/sites\/9\/2018\/11\/Free-02.png 281w, https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-content\/uploads\/sites\/9\/2018\/11\/Free-02-221x300.png 221w\" sizes=\"auto, (max-width: 281px) 100vw, 281px\" \/><p id=\"caption-attachment-459\" class=\"wp-caption-text\">An AST with two different variables that have the same name.<\/p><\/div>\n<p>Here&#8217;s another more complex example:<\/p>\n<p><strong>((\u03bbf. f (* 1 3) (+ 4 5) x) ((\u03bbxyz. (* 2 x)))<\/strong><\/p>\n<p>Which\u00a0<strong>x\u00a0<\/strong>is free and which bound? If we apply our rules then we can see that is is both. It is free in the\u00a0<strong>(\u03bbf. f (* 1 3) (+ 4 5) x)\u00a0<\/strong>part, and bound in the\u00a0<strong>(\u03bbxyz. (* 2 x))\u00a0<\/strong>part.<\/p>\n<p>Here it is as an AST:<\/p>\n<div id=\"attachment_464\" style=\"width: 623px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-464\" class=\"wp-image-464\" src=\"https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-content\/uploads\/sites\/9\/2018\/11\/Free-03.png\" alt=\"\" width=\"613\" height=\"368\" srcset=\"https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-content\/uploads\/sites\/9\/2018\/11\/Free-03.png 910w, https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-content\/uploads\/sites\/9\/2018\/11\/Free-03-300x180.png 300w, https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-content\/uploads\/sites\/9\/2018\/11\/Free-03-768x461.png 768w\" sizes=\"auto, (max-width: 613px) 100vw, 613px\" \/><p id=\"caption-attachment-464\" class=\"wp-caption-text\">A function calling another function. The orange x is a free variable that will need a value.<\/p><\/div>\n<p>The first thing we do is to bring in the value for\u00a0<strong>f<\/strong> as below:<\/p>\n<div id=\"attachment_463\" style=\"width: 650px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-463\" class=\" wp-image-463\" src=\"https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-content\/uploads\/sites\/9\/2018\/11\/Free-04.png\" alt=\"\" width=\"640\" height=\"395\" srcset=\"https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-content\/uploads\/sites\/9\/2018\/11\/Free-04.png 885w, https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-content\/uploads\/sites\/9\/2018\/11\/Free-04-300x185.png 300w, https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-content\/uploads\/sites\/9\/2018\/11\/Free-04-768x474.png 768w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><p id=\"caption-attachment-463\" class=\"wp-caption-text\">The variable to be replaced.<\/p><\/div>\n<p>This \\(\\beta\\) reduction gives us this:<\/p>\n<div id=\"attachment_462\" style=\"width: 537px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-462\" class=\"wp-image-462\" src=\"https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-content\/uploads\/sites\/9\/2018\/11\/Free-05.png\" alt=\"\" width=\"527\" height=\"283\" srcset=\"https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-content\/uploads\/sites\/9\/2018\/11\/Free-05.png 888w, https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-content\/uploads\/sites\/9\/2018\/11\/Free-05-300x161.png 300w, https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-content\/uploads\/sites\/9\/2018\/11\/Free-05-768x413.png 768w\" sizes=\"auto, (max-width: 527px) 100vw, 527px\" \/><p id=\"caption-attachment-462\" class=\"wp-caption-text\">The AST after applying the beta reduction.<\/p><\/div>\n<p>Now we&#8217;re stuck. We have a \u03bb expression that takes three parameters and, although we have three, the orange\u00a0<strong>x<\/strong> is free, so let&#8217;s assume it has been define somewhere else like this:<\/p>\n<pre>(define x 2)\r\n<\/pre>\n<p>That changes the AST to this, which has enough arguments so we can apply our \\(\\beta\\) reduction.<\/p>\n<div id=\"attachment_460\" style=\"width: 657px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-460\" class=\"wp-image-460\" src=\"https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-content\/uploads\/sites\/9\/2018\/11\/Free-07.png\" alt=\"\" width=\"647\" height=\"307\" srcset=\"https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-content\/uploads\/sites\/9\/2018\/11\/Free-07.png 1128w, https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-content\/uploads\/sites\/9\/2018\/11\/Free-07-300x142.png 300w, https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-content\/uploads\/sites\/9\/2018\/11\/Free-07-768x364.png 768w, https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-content\/uploads\/sites\/9\/2018\/11\/Free-07-1024x486.png 1024w\" sizes=\"auto, (max-width: 647px) 100vw, 647px\" \/><p id=\"caption-attachment-460\" class=\"wp-caption-text\">The final steps of the reduction.<\/p><\/div>\n<p>Recall from the section on <a href=\"https:\/\/books.compclassnotes.com\/elementarycomputing\/lifetime-diagrams\/\">Lifetime Diagrams<\/a>\u00a0that internally, the computer uses different names for variables:<\/p>\n<div id=\"attachment_410\" style=\"width: 770px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-410\" class=\" wp-image-410\" src=\"https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-content\/uploads\/sites\/9\/2018\/11\/02-Lifetime.png\" alt=\"\" width=\"760\" height=\"220\" srcset=\"https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-content\/uploads\/sites\/9\/2018\/11\/02-Lifetime.png 1216w, https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-content\/uploads\/sites\/9\/2018\/11\/02-Lifetime-300x87.png 300w, https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-content\/uploads\/sites\/9\/2018\/11\/02-Lifetime-768x222.png 768w, https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-content\/uploads\/sites\/9\/2018\/11\/02-Lifetime-1024x296.png 1024w\" sizes=\"auto, (max-width: 760px) 100vw, 760px\" \/><p id=\"caption-attachment-410\" class=\"wp-caption-text\">A Lifetime Diagram with multiple copies of the same function running.<\/p><\/div>\n<p>This shows how the names can be easily kept separate when there are multiple copies of the same function running. This is important because in that case, there will definitely be functions using the same name, but what if we pass a function to another function like we did above? It turns out that it&#8217;s the same thing; any time a function is actually running, a temporary variable is set up to keep that function&#8217;s values private to it. However, when we&#8217;re reading these functions written out in\u00a0\u03bb calculus, it won&#8217;t always be that clear, so let&#8217;s add some more formal notation to help us keep everything straight.<\/p>\n<p>We&#8217;ve been using \\(\\beta\\) reductions for a while now. Formally, a\u00a0\\(\\beta\\) reduction replaces the\u00a0<em>free<\/em> occurrences of a variable in the body of a function. For example, in the expresion\u00a0<strong>((\u03bbx. + x 1) 5)\u00a0<\/strong>we will replace all the free occurrences in the body\u00a0<strong>(+ x 1)<\/strong> with the value 5, giving us\u00a0<strong>(+ 5 1)<\/strong>. Formally, we write this as:<\/p>\n\\( ((\\lambda x. + x\\; 1)\\; 5) \\overrightarrow\\beta (+ 5\\; 1)\\)\n<p>Here&#8217;s a more complex example, this time with multiple\u00a0\u03bb expressions:<\/p>\n\\( ((\\lambda x. + (\\lambda y. +\\; 2\\; y)\\; x\\; 4)\\; 3)\\)\n<p>Our\u00a0\\(\\beta\\) reduction does the following:<\/p>\n\\( \\overrightarrow\\beta (+ (\\lambda y. +\\; 2\\; y)\\; 3\\; 4)\\)\n<p>Next we do another one to get:<\/p>\n\\( \\overrightarrow\\beta (+\\; (+\\; 2\\; 3)\\; 4)\\)\n<p>Note that we haven&#8217;t added anything new here, we&#8217;ve just formalised what we&#8217;ve been doing for a while now, which is bringing in values for parameters. Here&#8217;s a more confusing but\u00a0<em>identical<\/em> example:<\/p>\n\\( ((\\lambda x. + (\\lambda x. +\\; 2\\; x)\\; x\\; 4)\\; 3)\\)\n<p>When we do our\u00a0\u00a0\\(\\beta\\) reduction we only replace the\u00a0<em>free<\/em> occurrences of\u00a0<strong>x,\u00a0<\/strong>and the inner\u00a0\u03bb has a\u00a0<em>bound\u00a0<\/em><strong>x<\/strong>, so that doesn&#8217;t change, giving us:<\/p>\n\\( \\overrightarrow\\beta (+ (\\lambda x. +\\; 2\\; x)\\; 3\\; 4)\\)\n<p>Yes, it still looks slightly different to the last example, but only the names are different. Structurally and functionally it is the same, that is,\u00a0<em>semantically<\/em> it is the same even though the\u00a0<em>syntax<\/em> is different.<\/p>\n<p>We&#8217;ll finish up with one last mind-bending example of passing a\u00a0\u03bb expression to another\u00a0\u03bb expression. This might seems a strange thing to want to do, as so far we&#8217;ve only modified the functionality of something by passing it a different piece of\u00a0<em>data<\/em>; however, what this will let us do is modify the functionality of a piece of code by <em>passing it another piece of code!\u00a0<\/em>Think about the parallel processing examples we had before, this is a really great way to change the functionality of a system very quickly.<\/p>\n<p>Let&#8217;s start with\u00a0<strong>(\u03bbf. f 3) (\u03bbx. + x 1)<\/strong>.<\/p>\n<div id=\"attachment_479\" style=\"width: 441px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-479\" class=\"wp-image-479\" src=\"https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-content\/uploads\/sites\/9\/2018\/11\/Free-08.png\" alt=\"\" width=\"431\" height=\"245\" srcset=\"https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-content\/uploads\/sites\/9\/2018\/11\/Free-08.png 719w, https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-content\/uploads\/sites\/9\/2018\/11\/Free-08-300x171.png 300w\" sizes=\"auto, (max-width: 431px) 100vw, 431px\" \/><p id=\"caption-attachment-479\" class=\"wp-caption-text\">Passing a lambda expression into another lambda expression.<\/p><\/div>\n<p>Next we do our \\(\\beta\\) conversion<\/p>\n<div id=\"attachment_478\" style=\"width: 709px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-478\" class=\"wp-image-478\" src=\"https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-content\/uploads\/sites\/9\/2018\/11\/Free-09.png\" alt=\"\" width=\"699\" height=\"287\" srcset=\"https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-content\/uploads\/sites\/9\/2018\/11\/Free-09.png 1113w, https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-content\/uploads\/sites\/9\/2018\/11\/Free-09-300x123.png 300w, https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-content\/uploads\/sites\/9\/2018\/11\/Free-09-768x315.png 768w, https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-content\/uploads\/sites\/9\/2018\/11\/Free-09-1024x420.png 1024w\" sizes=\"auto, (max-width: 699px) 100vw, 699px\" \/><p id=\"caption-attachment-478\" class=\"wp-caption-text\">Perform a beta conversion (reduction) on the AST, followed by another one on the remaining tree.<\/p><\/div>\n<p>Well, that was actually disappointingly easy and straightforward, so let&#8217;s try a different one!<\/p>\n<p><strong>(\u03bbx. + 2 (\u03bby. y 5) x) (\u03bbz. + 1 z)<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<div id=\"attachment_477\" style=\"width: 659px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-477\" class=\"wp-image-477 size-full\" src=\"https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-content\/uploads\/sites\/9\/2018\/11\/Free-10.png\" alt=\"\" width=\"649\" height=\"709\" srcset=\"https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-content\/uploads\/sites\/9\/2018\/11\/Free-10.png 649w, https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-content\/uploads\/sites\/9\/2018\/11\/Free-10-275x300.png 275w\" sizes=\"auto, (max-width: 649px) 100vw, 649px\" \/><p id=\"caption-attachment-477\" class=\"wp-caption-text\">An expression with three lambda expressions!<\/p><\/div>\n<p>&nbsp;<\/p>\n<p>We apply the first one and perform a\u00a0\\(\\beta\\) conversion<\/p>\n<div id=\"attachment_475\" style=\"width: 621px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-475\" class=\"wp-image-475 size-full\" src=\"https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-content\/uploads\/sites\/9\/2018\/11\/Free-12.png\" alt=\"\" width=\"611\" height=\"496\" srcset=\"https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-content\/uploads\/sites\/9\/2018\/11\/Free-12.png 611w, https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-content\/uploads\/sites\/9\/2018\/11\/Free-12-300x244.png 300w\" sizes=\"auto, (max-width: 611px) 100vw, 611px\" \/><p id=\"caption-attachment-475\" class=\"wp-caption-text\">Perform a beta reduction. Notice that the + is now on top.<\/p><\/div>\n<p>Now we bring in the subtree for\u00a0<strong>y<\/strong><\/p>\n<div id=\"attachment_473\" style=\"width: 399px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-473\" class=\"wp-image-473 size-full\" src=\"https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-content\/uploads\/sites\/9\/2018\/11\/Free-14.png\" alt=\"\" width=\"389\" height=\"543\" srcset=\"https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-content\/uploads\/sites\/9\/2018\/11\/Free-14.png 389w, https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-content\/uploads\/sites\/9\/2018\/11\/Free-14-215x300.png 215w\" sizes=\"auto, (max-width: 389px) 100vw, 389px\" \/><p id=\"caption-attachment-473\" class=\"wp-caption-text\">Perform another beta reduction to leave us with just one lambda expression. Notice that the + is still on top.<\/p><\/div>\n<p>Now we do our final one to get a good old fashioned AST that is ready to be reduced to a single value.<\/p>\n<div id=\"attachment_472\" style=\"width: 316px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-472\" class=\"wp-image-472 \" src=\"https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-content\/uploads\/sites\/9\/2018\/11\/Free-15.png\" alt=\"\" width=\"306\" height=\"274\" srcset=\"https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-content\/uploads\/sites\/9\/2018\/11\/Free-15.png 390w, https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-content\/uploads\/sites\/9\/2018\/11\/Free-15-300x268.png 300w\" sizes=\"auto, (max-width: 306px) 100vw, 306px\" \/><p id=\"caption-attachment-472\" class=\"wp-caption-text\">The final AST, ready for reduction to a single number.<\/p><\/div>\n<p>Now, that was way more interesting!<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the previous section we applied a test to see if a variable was bound in the body of a function. We haven&#8217;t seen many examples so far where this would be true, but consider function\u00a0(\u03bbx. + (\u03bby. + 3 <span class=\"readmore\"><a href=\"https:\/\/books.compclassnotes.com\/elementarycomputing\/nested-functions\/\">Continue Reading<\/a><\/span><\/p>\n","protected":false},"author":4,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-455","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-json\/wp\/v2\/pages\/455","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-json\/wp\/v2\/comments?post=455"}],"version-history":[{"count":9,"href":"https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-json\/wp\/v2\/pages\/455\/revisions"}],"predecessor-version":[{"id":481,"href":"https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-json\/wp\/v2\/pages\/455\/revisions\/481"}],"wp:attachment":[{"href":"https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-json\/wp\/v2\/media?parent=455"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}