{"id":3161,"date":"2015-05-13T20:52:34","date_gmt":"2015-05-14T00:52:34","guid":{"rendered":"http:\/\/www.goodmath.org\/blog\/?p=3161"},"modified":"2015-05-21T09:55:50","modified_gmt":"2015-05-21T13:55:50","slug":"expressions-and-arity-part-1","status":"publish","type":"post","link":"http:\/\/www.goodmath.org\/blog\/2015\/05\/13\/expressions-and-arity-part-1\/","title":{"rendered":"Expressions and Arity (part 1)"},"content":{"rendered":"<p> In the last post, we started looking at expressions. In this post, we&#8217;re going to continue doing that, and start looking at a simple form of expression typing called <em>arity<\/em>.<\/p>\n<p> Before we can do that, we need to introduce a couple of new formalisms to complete our understanding of the elements that form expressions. The reason we need another formalism is because so far, we&#8217;ve defined the meaning of expressions in terms of function calls. But we&#8217;ve still got some ambiguity about our function calls &#8211; specifically, how do parameters work?<\/p>\n<p> Suppose I&#8217;ve got a function, <img src='http:\/\/l.wordpress.com\/latex.php?latex=f&#038;bg=FFFFFF&#038;fg=000000&#038;s=0' title='f' style='vertical-align:1%' class='tex' alt='f' \/>. Can I call it as <img src='http:\/\/l.wordpress.com\/latex.php?latex=f%28x%29&#038;bg=FFFFFF&#038;fg=000000&#038;s=0' title='f(x)' style='vertical-align:1%' class='tex' alt='f(x)' \/>? Or <img src='http:\/\/l.wordpress.com\/latex.php?latex=f%28x%2Cy%29&#038;bg=FFFFFF&#038;fg=000000&#038;s=0' title='f(x,y)' style='vertical-align:1%' class='tex' alt='f(x,y)' \/>? Both? Neither? It depends on exactly what function calls mean, and what it means to be a valid parameter to a function.<\/p>\n<p> There are several approaches that you can take:<\/p>\n<ol>\n<li> You can say that a function application expression takes an arbitrary number of arguments. This is, roughly, what we do in dynamically typed programming languages like Python. In Python, you can write <code>f(x) + f(x,y) + f(x, y, z, a, b, c)<\/code>, and the language parser will accept it. (It&#8217;ll complain when you try to execute it, but at the level of expressions, it&#8217;s a perfectly valid expression.)<\/li>\n<li> You can say that every function takes exactly one argument, and that a multi-argument function like <img src='http:\/\/l.wordpress.com\/latex.php?latex=f%28x%2C%20y%2C%20z%29&#038;bg=FFFFFF&#038;fg=000000&#038;s=0' title='f(x, y, z)' style='vertical-align:1%' class='tex' alt='f(x, y, z)' \/> is really a shorthand for a curried call sequence <img src='http:\/\/l.wordpress.com\/latex.php?latex=f%28x%29%28y%29%28z%29&#038;bg=FFFFFF&#038;fg=000000&#038;s=0' title='f(x)(y)(z)' style='vertical-align:1%' class='tex' alt='f(x)(y)(z)' \/> &#8211; and thus, the &#8220;application&#8221; operation really takes exactly 2 parameters &#8211; a function, and a single value to apply that function to. This is the approach that we usually take in lambda calculus. <\/li>\n<li> You can say that application takes two arguments, but the second one can be  a <em>combination<\/em> of multiple objects &#8211; effectively a tuple. That&#8217;s what we do in a lot of versions of recursive function theory, and in programming languages like SML.<\/li>\n<\/ol>\n<p> In the theory of expressions, Martin-L&ouml;f chose the third approach. A function takes a single parameter, which is a <em>combination<\/em> of a collection of other objects. If <img src='http:\/\/l.wordpress.com\/latex.php?latex=a&#038;bg=FFFFFF&#038;fg=000000&#038;s=0' title='a' style='vertical-align:1%' class='tex' alt='a' \/>, <img src='http:\/\/l.wordpress.com\/latex.php?latex=b&#038;bg=FFFFFF&#038;fg=000000&#038;s=0' title='b' style='vertical-align:1%' class='tex' alt='b' \/>, <img src='http:\/\/l.wordpress.com\/latex.php?latex=c&#038;bg=FFFFFF&#038;fg=000000&#038;s=0' title='c' style='vertical-align:1%' class='tex' alt='c' \/>, and <img src='http:\/\/l.wordpress.com\/latex.php?latex=d&#038;bg=FFFFFF&#038;fg=000000&#038;s=0' title='d' style='vertical-align:1%' class='tex' alt='d' \/> are expressions, then <img src='http:\/\/l.wordpress.com\/latex.php?latex=a%2C%20b%2C%20c%2C%20d&#038;bg=FFFFFF&#038;fg=000000&#038;s=0' title='a, b, c, d' style='vertical-align:1%' class='tex' alt='a, b, c, d' \/> is an expression called the <em>combination<\/em> of its four elements. This is very closely related to the idea of <em>cartesian products<\/em> in set theory, but it&#8217;s important to realize that it&#8217;s not the same thing. We&#8217;re not defining elements of a set here: we&#8217;re defining a syntactic construct of a pseudo-programming language, where one possible interpretation of it is cartesian products.<\/p>\n<p> In addition to just multi-parameter functions, we&#8217;ll use combinations for other things. In type theory, we want to be able to talk about certain mathematical constructs as first-class objects. For example, we&#8217;d like to be able to talk about <em>orderings<\/em>, where an ordering is a collection of objects <img src='http:\/\/l.wordpress.com\/latex.php?latex=A&#038;bg=FFFFFF&#038;fg=000000&#038;s=0' title='A' style='vertical-align:1%' class='tex' alt='A' \/> combined with an operation <img src='http:\/\/l.wordpress.com\/latex.php?latex=%5Cle&#038;bg=FFFFFF&#038;fg=000000&#038;s=0' title='\\le' style='vertical-align:1%' class='tex' alt='\\le' \/>. Using combinations, we can write that very naturally as <img src='http:\/\/l.wordpress.com\/latex.php?latex=A%2C%5Cle&#038;bg=FFFFFF&#038;fg=000000&#038;s=0' title='A,\\le' style='vertical-align:1%' class='tex' alt='A,\\le' \/>.<\/p>\n<p> For combinations to be useful, we need to be able to extract the component values from them. In set theory, we&#8217;d do that by having projection functions associated with the cross-product. In our theory of expressions, we have <em>selection expressions<\/em>. If <img src='http:\/\/l.wordpress.com\/latex.php?latex=x%3D%28a%2C%20b%2C%20c%2C%20d%29&#038;bg=FFFFFF&#038;fg=000000&#038;s=0' title='x=(a, b, c, d)' style='vertical-align:1%' class='tex' alt='x=(a, b, c, d)' \/> is a combination with four elements, then <img src='http:\/\/l.wordpress.com\/latex.php?latex=x.1&#038;bg=FFFFFF&#038;fg=000000&#038;s=0' title='x.1' style='vertical-align:1%' class='tex' alt='x.1' \/> is a selection expression which extracts the first element from <img src='http:\/\/l.wordpress.com\/latex.php?latex=x&#038;bg=FFFFFF&#038;fg=000000&#038;s=0' title='x' style='vertical-align:1%' class='tex' alt='x' \/>.<\/p>\n<p> In programming language terms, combinations give us a way of writing tuple values. Guess what&#8217;s next? Record types! Or rather, combinations with named elements. We can write a combination with names: <img src='http:\/\/l.wordpress.com\/latex.php?latex=x%20%3D%20%28a%3A%201%2C%20b%3A2%29&#038;bg=FFFFFF&#038;fg=000000&#038;s=0' title='x = (a: 1, b:2)' style='vertical-align:1%' class='tex' alt='x = (a: 1, b:2)' \/>, and then write selection expressions using the names, like <img src='http:\/\/l.wordpress.com\/latex.php?latex=x.a&#038;bg=FFFFFF&#038;fg=000000&#038;s=0' title='x.a' style='vertical-align:1%' class='tex' alt='x.a' \/>.<\/p>\n<p> Now we can start getting to the meat of things. <\/p>\n<p> In combinatory logic, we&#8217;d just start with a collection of primitive constant values, and then build whatever we wanted with them using abstractions, applications, combinations, and selections. Combinatory logic is the parent of computation theory: why can&#8217;t we just stick with that foundation?<\/p>\n<p> There are two answers to that. The first is familiar to programming language people: if you don&#8217;t put any restrictions on things, then you lose type safety. You&#8217;ll be able to write &#8220;valid&#8221; expressions that don&#8217;t mean anything &#8211; things like <img src='http:\/\/l.wordpress.com\/latex.php?latex=1.x&#038;bg=FFFFFF&#038;fg=000000&#038;s=0' title='1.x' style='vertical-align:1%' class='tex' alt='1.x' \/>, even though &#8220;1&#8221; isn&#8217;t a combination, and so calling a selector on it makes no sense. Or you&#8217;d be able to call a function like <code>factorial(27, 13)<\/code>, even though the function only takes one parameter.<\/p>\n<p> The other answer is equality. One thing that we&#8217;d really like to be able to do in our theory of expressions is determine when two expressions are the same. For example, we&#8217;d really like to be able to  do things like say that <img src='http:\/\/l.wordpress.com\/latex.php?latex=%28%28x%29e%29%28x%29%20%3D%3D%20e&#038;bg=FFFFFF&#038;fg=000000&#038;s=0' title='((x)e)(x) == e' style='vertical-align:1%' class='tex' alt='((x)e)(x) == e' \/>. But without some form of control, we can&#8217;t really do that: the problem of determining whether or not two expressions are equal can become non-decidable. (The canonical example of this problem involves y combinator: <img src='http:\/\/l.wordpress.com\/latex.php?latex=%28%28x%29x%28x%29%29%28%28x%29x%28x%29%29&#038;bg=FFFFFF&#038;fg=000000&#038;s=0' title='((x)x(x))((x)x(x))' style='vertical-align:1%' class='tex' alt='((x)x(x))((x)x(x))' \/>. If we wanted to try to work out whether an expression involving this was equivilant to another expression, we could wind up in an infinite loop of applications.)<\/p>\n<p> The way that Martin-L&ouml;f worked around this is to associate an <em>arity<\/em> with an expression. Each <em>arity<\/em> of expressions is distinct, and there are rules about what operations can be applied to an expression depending on its arity. You can&#8217;t call .2 on &#8220;1+3&#8221;, because &#8220;1+3&#8221; is a <em>single<\/em> expression, and selectors can only be applied to <em>combined<\/em> expressions.<\/p>\n<p> To most of us, arity sounds like it should be a numeric value. When we we talk about the arity of a function in a program, what we mean is how many parameters it takes. In Martin-L&ouml;f expressions, arity is more expressive than that: it&#8217;s almost the same thing as types in the simply typed lambda calculus. <\/p>\n<p> There are two dimensions to arity: single\/combined and saturated\/unsaturated. <\/p>\n<p> <em>Single<\/em> expressions are atomic values, where you can&#8217;t extract other values from them by selection; <em>multiple<\/em> expressions are combinations of multiple other expressions.<\/p>\n<p> Saturated expressions are expressions that have no holes in them that can be filled by other expressions &#8211; that is, expressions with no free variables. Unsaturated expressions have open holes &#8211; which means that they can be <em>applied<\/em> to other expressions.<\/p>\n<p> Saturated single expressions have arity 0. An expression of arity 0 can&#8217;t be applied, and you can&#8217;t be the target of selection expressions.<\/p>\n<p> An unsaturated expression has an arity <img src='http:\/\/l.wordpress.com\/latex.php?latex=%28%5Calpha%20%5Ctwoheadrightarrow%20%5Cbeta%29&#038;bg=FFFFFF&#038;fg=000000&#038;s=0' title='(\\alpha \\twoheadrightarrow \\beta)' style='vertical-align:1%' class='tex' alt='(\\alpha \\twoheadrightarrow \\beta)' \/>, where both <img src='http:\/\/l.wordpress.com\/latex.php?latex=%5Calpha&#038;bg=FFFFFF&#038;fg=000000&#038;s=0' title='\\alpha' style='vertical-align:1%' class='tex' alt='\\alpha' \/> and <img src='http:\/\/l.wordpress.com\/latex.php?latex=%5Cbeta&#038;bg=FFFFFF&#038;fg=000000&#038;s=0' title='\\beta' style='vertical-align:1%' class='tex' alt='\\beta' \/> are arities. For example, the integer addition function has arity <img src='http:\/\/l.wordpress.com\/latex.php?latex=%280%20%5Cotimes%200%20%5Ctwoheadrightarrow%200%29&#038;bg=FFFFFF&#038;fg=000000&#038;s=0' title='(0 \\otimes 0 \\twoheadrightarrow 0)' style='vertical-align:1%' class='tex' alt='(0 \\otimes 0 \\twoheadrightarrow 0)' \/>.   <em>(Erik Eidt pointed out that I made an error here. I originally wrote addition as <img src='http:\/\/l.wordpress.com\/latex.php?latex=0%20%5Ctwoheadrightarrow%200&#038;bg=FFFFFF&#038;fg=000000&#038;s=0' title='0 \\twoheadrightarrow 0' style='vertical-align:1%' class='tex' alt='0 \\twoheadrightarrow 0' \/>, where it should have been <img src='http:\/\/l.wordpress.com\/latex.php?latex=0%20%5Cotimes%200%20%5Ctwoheadrightarrow%200&#038;bg=FFFFFF&#038;fg=000000&#038;s=0' title='0 \\otimes 0 \\twoheadrightarrow 0' style='vertical-align:1%' class='tex' alt='0 \\otimes 0 \\twoheadrightarrow 0' \/>.)<\/em><\/p>\n<p>A combined expression <img src='http:\/\/l.wordpress.com\/latex.php?latex=%28e_0%2C%20e_1%2C%20...%2C%20e_n%29&#038;bg=FFFFFF&#038;fg=000000&#038;s=0' title='(e_0, e_1, ..., e_n)' style='vertical-align:1%' class='tex' alt='(e_0, e_1, ..., e_n)' \/> has arity <img src='http:\/\/l.wordpress.com\/latex.php?latex=%28%5Calpha_1%20%5Cotimes%20%5Calpha_2%20%5Cotimes%20...%20%5Cotimes%20%5Calpha_n%29&#038;bg=FFFFFF&#038;fg=000000&#038;s=0' title='(\\alpha_1 \\otimes \\alpha_2 \\otimes ... \\otimes \\alpha_n)' style='vertical-align:1%' class='tex' alt='(\\alpha_1 \\otimes \\alpha_2 \\otimes ... \\otimes \\alpha_n)' \/>, where each of the <img src='http:\/\/l.wordpress.com\/latex.php?latex=%5Calpha_i&#038;bg=FFFFFF&#038;fg=000000&#038;s=0' title='\\alpha_i' style='vertical-align:1%' class='tex' alt='\\alpha_i' \/>s are the arities of the expression <img src='http:\/\/l.wordpress.com\/latex.php?latex=e_i&#038;bg=FFFFFF&#038;fg=000000&#038;s=0' title='e_i' style='vertical-align:1%' class='tex' alt='e_i' \/>.<\/p>\n<p> Sadly, I&#8217;m out of time to work on this post, so we&#8217;ll have to stop here. Next time, we&#8217;ll look at the formal rules for arities, and how to use them to define equality of expressions.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the last post, we started looking at expressions. In this post, we&#8217;re going to continue doing that, and start looking at a simple form of expression typing called arity. Before we can do that, we need to introduce a couple of new formalisms to complete our understanding of the elements that form expressions. The [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[292],"tags":[],"class_list":["post-3161","post","type-post","status-publish","format-standard","hentry","category-type-theory"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p4lzZS-OZ","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/posts\/3161","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/comments?post=3161"}],"version-history":[{"count":3,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/posts\/3161\/revisions"}],"predecessor-version":[{"id":3182,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/posts\/3161\/revisions\/3182"}],"wp:attachment":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/media?parent=3161"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/categories?post=3161"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/tags?post=3161"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}