{"id":435,"date":"2018-11-13T15:15:06","date_gmt":"2018-11-13T15:15:06","guid":{"rendered":"https:\/\/books.compclassnotes.com\/elementarycomputing\/?page_id=435"},"modified":"2018-11-13T15:15:06","modified_gmt":"2018-11-13T15:15:06","slug":"free-and-bound-variables-in-%ce%bb-calculus","status":"publish","type":"page","link":"https:\/\/books.compclassnotes.com\/elementarycomputing\/free-and-bound-variables-in-%ce%bb-calculus\/","title":{"rendered":"Free\u00a0and\u00a0Bound variables in\u00a0\u03bb calculus"},"content":{"rendered":"<p>We&#8217;ve already seen local and global variables in Racket.<\/p>\n<pre>&gt; (define x 2) \r\n&gt; (define addx (lambda (y) (+ y x))) \r\n<\/pre>\n<p>In the function\u00a0<strong>addx<\/strong>\u00a0the variable<strong> y<\/strong><strong>\u00a0<\/strong>is local and\u00a0<strong>x<\/strong> is global.<\/p>\n<p>Let&#8217;s try applying the function to the value\u00a0<strong>4.\u00a0<\/strong>As\u00a0<strong>y<\/strong> is the parameter, it gets the value 4, which is added to the value of\u00a0<strong>x<\/strong>, which is 2.<\/p>\n<pre>&gt; (addx 4) \r\n6\r\n<\/pre>\n<p>Here&#8217;s what it looks like using syntax trees. Remember, a \\(\\beta\\) conversion is when we bring the value in for a parameter.<\/p>\n<div id=\"attachment_439\" style=\"width: 607px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-439\" class=\"wp-image-439\" src=\"https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-content\/uploads\/sites\/9\/2018\/11\/Free-01.png\" alt=\"\" width=\"597\" height=\"323\" srcset=\"https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-content\/uploads\/sites\/9\/2018\/11\/Free-01.png 907w, https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-content\/uploads\/sites\/9\/2018\/11\/Free-01-300x162.png 300w, https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-content\/uploads\/sites\/9\/2018\/11\/Free-01-768x416.png 768w\" sizes=\"auto, (max-width: 597px) 100vw, 597px\" \/><p id=\"caption-attachment-439\" class=\"wp-caption-text\">Applying the addx function to an argument. Notice that x is a global variable so we don&#8217;t explicitly pass a value to it (because it has already been defined).<\/p><\/div>\n<p>This is fairly straightforward because there&#8217;s just one function, and it is a fairly simple one, but this is about to get very complex, so let&#8217;s build ourselves some machinery to analyse these functions. We&#8217;ll start off with some shorthand:<\/p>\n<p><strong>\u03bbx. E<\/strong><\/p>\n<p>is a function that takes\u00a0<strong>exactly\u00a0<\/strong>one argument. Note that we don&#8217;t care what the function body actually does, as all we&#8217;re interested in is keeping track of variables<\/p>\n<p>Similarly:<\/p>\n<p><strong>\u03bbx. E F<\/strong><\/p>\n<p>is also a function that takes\u00a0<strong>exactly\u00a0<\/strong>one argument, but this time there are two distinct parts to the function body, for example, if we had something like this\u00a0<strong>(\u03bbx. + 1 2) <\/strong>we could say that<\/p>\n\\(\\lambda x. + x\\; y \\equiv \\lambda x. E \\)\n<p>because\u00a0<strong>E = + x y<\/strong><\/p>\n<p>We can also say that<\/p>\n\\(\\lambda x. + x\\; y \\equiv \\lambda x. E\\; F \\)\n<p>because\u00a0<strong>E = + x\u00a0<\/strong>and\u00a0<strong>F = y<\/strong><strong>.\u00a0<\/strong>However, we could equally say that<\/p>\n\\(\\lambda x. + x\\; y \\equiv \\lambda x. E\\; F \\)\n<p>because\u00a0<strong>E = +\u00a0\u00a0<\/strong>and\u00a0<strong>F = x<\/strong> <strong>y<\/strong>, because there are still two parts. What we could\u00a0<strong>not<\/strong> say is that\u00a0<strong>F = + x y<\/strong>, because then there would be nothing for E.<\/p>\n<p>Here&#8217;s one last example:<\/p>\n\\(\\lambda x. x \\equiv \\lambda x. E \\)\n<p>because\u00a0<strong>E =\u00a0 x<\/strong>, but<\/p>\n\\(\\lambda x. x \\not\\equiv \\lambda x. E\\; F \\)\n<p>because there&#8217;s only one item in the function body.<\/p>\n<p>In\u00a0\u03bb calculus, a local variable is referred to as a\u00a0<strong>bound\u00a0<\/strong>variable, so for example,\u00a0<strong>x\u00a0<\/strong>is bound in\u00a0<strong>(\u03bbx. + x y)<\/strong> because it is in the body of the function\u00a0<em>and<\/em> in the parameter list. What about\u00a0<strong>y<\/strong>? It&#8217;s clearly not bound because it&#8217;s value is coming from somewhere else, however, for reasons that we&#8217;ll see shortly it isn&#8217;t <em>necessarily<\/em>\u00a0a global variable, so we refer to it as a <strong>free<\/strong> variable.<\/p>\n<p>The next section will look how formal steps to identify free and bound variables which will make our lives easier when dealing with complex functions and expressions.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We&#8217;ve already seen local and global variables in Racket. &gt; (define x 2) &gt; (define addx (lambda (y) (+ y x))) In the function\u00a0addx\u00a0the variable y\u00a0is local and\u00a0x is global. Let&#8217;s try applying the function to the value\u00a04.\u00a0As\u00a0y is the <span class=\"readmore\"><a href=\"https:\/\/books.compclassnotes.com\/elementarycomputing\/free-and-bound-variables-in-%ce%bb-calculus\/\">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-435","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-json\/wp\/v2\/pages\/435","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=435"}],"version-history":[{"count":10,"href":"https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-json\/wp\/v2\/pages\/435\/revisions"}],"predecessor-version":[{"id":447,"href":"https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-json\/wp\/v2\/pages\/435\/revisions\/447"}],"wp:attachment":[{"href":"https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-json\/wp\/v2\/media?parent=435"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}