{"id":3113,"date":"2015-02-04T16:33:17","date_gmt":"2015-02-04T21:33:17","guid":{"rendered":"http:\/\/www.goodmath.org\/blog\/?p=3113"},"modified":"2015-02-04T16:33:17","modified_gmt":"2015-02-04T21:33:17","slug":"the-program-is-the-proof-propositions-in-type-theory","status":"publish","type":"post","link":"http:\/\/www.goodmath.org\/blog\/2015\/02\/04\/the-program-is-the-proof-propositions-in-type-theory\/","title":{"rendered":"The Program is the Proof: Propositions in Type Theory"},"content":{"rendered":"<p> As usual, I&#8217;m going in several different directions. I&#8217;m going to continue doing data structure posts, but at the same time I also want to get back to the type theory stuff that I was writing about before I put the blog on hiatus.<\/p>\n<p> So let&#8217;s get back to a bit of Martin-Loff type theory! (The stuff I&#8217;m writing about today corresponds, roughly, to chapter 2 of the Nordstrom\/Petersson\/Smith text.)<\/p>\n<p> One of the key ideas of Martin-Loff&#8217;s type theory is that a logical statement is exactly the same thing as a specification of a computation. When you define a predicate like &#8220;Even&#8221;, the definition specifies both the set of numbers that satisfy the predicate, and the computation that tests a number for membership in the set of even numbers. If you haven&#8217;t provided enough information to fully specify the computation, then in Martin-Loff type theory, you haven&#8217;t defined a predicate or a set.<\/p>\n<p> When you say &#8220;2 is even&#8221;, what you&#8217;re really saying in terms of the type theory is that &#8220;The computation for &#8216;even(2)&#8217; will succeed&#8221;. The computation and the logical statement are <em>the same thing<\/em>.<\/p>\n<p> In functional programming, we like to say that the program is the proof. Martin-Loff type theory is where that came from &#8211; and today we&#8217;re going to take a first look in detail at exactly what it means. In the world of type theory, the program is the proof, and the proof doesn&#8217;t exist without the program.<\/p>\n<p> This creates an interesting set of equivalent interpretations. When you see a statement like &#8220;x : T&#8221; (or <img src='http:\/\/l.wordpress.com\/latex.php?latex=x%20%5Cin%20T&#038;bg=FFFFFF&#038;fg=000000&#038;s=0' title='x \\in T' style='vertical-align:1%' class='tex' alt='x \\in T' \/>), that could be interpreted in the following ways, all of which are really equivalent in type theory.<\/p>\n<ol>\n<li> <em>Set theoretic<\/em>: <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' \/> is a <em>member<\/em> of the <em>set<\/em> <img src='http:\/\/l.wordpress.com\/latex.php?latex=T&#038;bg=FFFFFF&#038;fg=000000&#038;s=0' title='T' style='vertical-align:1%' class='tex' alt='T' \/>.<\/li>\n<li> <em>Intuitionistic<\/em>: <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' \/> is a <em>proof object<\/em> for the <em>proposition<\/em> <img src='http:\/\/l.wordpress.com\/latex.php?latex=T&#038;bg=FFFFFF&#038;fg=000000&#038;s=0' title='T' style='vertical-align:1%' class='tex' alt='T' \/>.<\/li>\n<li> <em>Computational<\/em>: <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' \/> is a <em>program<\/em> that satisfies the <em>specification<\/em> <img src='http:\/\/l.wordpress.com\/latex.php?latex=T&#038;bg=FFFFFF&#038;fg=000000&#038;s=0' title='T' style='vertical-align:1%' class='tex' alt='T' \/>.<\/li>\n<li> <em>Abstract<\/em>: <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' \/> is a <em>solution<\/em> for the <em>problem<\/em> <img src='http:\/\/l.wordpress.com\/latex.php?latex=T&#038;bg=FFFFFF&#038;fg=000000&#038;s=0' title='T' style='vertical-align:1%' class='tex' alt='T' \/>.<\/li>\n<\/ol>\n<p> In the rest of this post, I&#8217;m going to focus on those four interpretations,  and explain how each of them makes sense in this version of type theory.<\/p>\n<p> The set theoretic interpretation is obvious &#8211; as the name suggests, it&#8217;s nothing but what we all learned from basic set theory. An object is a member of a set &#8211; which means, from set theory,  that the object satisfies some predicate in first order predicate logic &#8211; because that&#8217;s what it means to be a member of a set.<\/p>\n<p>  The intuitionistic interpretation is almost the same as the set theoretic, but rewritten for intuitionistic logic. In intuitionistic logic, the predicate over the set is written as a proposition <img src='http:\/\/l.wordpress.com\/latex.php?latex=T&#038;bg=FFFFFF&#038;fg=000000&#038;s=0' title='T' style='vertical-align:1%' class='tex' alt='T' \/>, and if we know that <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' \/> is a member of the set <img src='http:\/\/l.wordpress.com\/latex.php?latex=T&#038;bg=FFFFFF&#038;fg=000000&#038;s=0' title='T' style='vertical-align:1%' class='tex' alt='T' \/>, then that means that we have a proof that <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' \/> demonstrates that <img src='http:\/\/l.wordpress.com\/latex.php?latex=T&#038;bg=FFFFFF&#038;fg=000000&#038;s=0' title='T' style='vertical-align:1%' class='tex' alt='T' \/> is true.<\/p>\n<p> The computational interpretation takes the intuitionistic one, and rephrases  it in computational terms. A logical proposition, rendered into a computational setting, is just a specification of a program; and a proof of the proposition is a program that satisfies the specification.<\/p>\n<p> Finally, the abstract interpretation just rephrases the computational one into terms that aren&#8217;t tied to a computing device. A predicate is a problem that needs to be solved; anything that provides a solution to the problem is demonstrating a member of the set.<\/p>\n<p> The key takeaway though is the basic idea here of what a <em>type<\/em> is. What we&#8217;re talking about as a type here is something that goes far beyond any programming language&#8217;s idea of what a type is. In intuitionistic type theory, a type is a <em>specification of a computation<\/em>. If we had this in a real language, that would mean that any program that typechecked would be <em>guaranteed<\/em> to work: asserting that <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' \/> has type <img src='http:\/\/l.wordpress.com\/latex.php?latex=T&#038;bg=FFFFFF&#038;fg=000000&#038;s=0' title='T' style='vertical-align:1%' class='tex' alt='T' \/> means, precisely, that <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' \/> is a computation that matches the specification!<\/p>\n<p> (Of course, that&#8217;s not the panacea that you might think the first time you hear that. The catch is simple: the type is a specification of the computation. That means that just writing a type is a form of programming! And that means that your type descriptions are going to have bugs. But we&#8217;ll come back to that in a much later post.)<\/p>\n<p> What type theory is doing is taking something like set theory, and re-rendering it entirely in a computational world. It still has a mapping from the computations to the abstract concepts that we use when we&#8217;re thinking, but if we can talk about those abstract concepts in type theory, we&#8217;ll always do it by mapping them into some kind of computation. <\/p>\n<p> In type theory, we&#8217;re not dealing in a world of pure mathematical objects that exist if we can describe them; instead, we&#8217;re building a world where everything is at least theoretically computable. That might seem constraining, but every proof already corresponds to a computation of some sort; the only additional constraint here is that we can&#8217;t play tricks like the axiom of choice, where we can &#8220;prove&#8221; the existence of some unattainable, intangible, nonsensical object.<\/p>\n<p> To make that work, we&#8217;re going to take all of the construct that we&#8217;re used to seeing in intuitionistic logic, and give them a meaning in terms of computations.<\/p>\n<p> For example, in set theory, we can have a statement <img src='http:\/\/l.wordpress.com\/latex.php?latex=A%20%5Csupset%20B&#038;bg=FFFFFF&#038;fg=000000&#038;s=0' title='A \\supset B' style='vertical-align:1%' class='tex' alt='A \\supset B' \/> &#8211; meaning that <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' \/> is a superset of <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' \/>, that every element of <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' \/> is also necessarity an element of <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' \/>. In type theory, since <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' \/> and <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' \/> are specifications of computations, that means that a member (or proof) of <img src='http:\/\/l.wordpress.com\/latex.php?latex=A%20%5Csupset%20B&#038;bg=FFFFFF&#038;fg=000000&#038;s=0' title='A \\supset B' style='vertical-align:1%' class='tex' alt='A \\supset B' \/> is a computation that given a proof of <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' \/>, generates a proof of <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' \/> &#8211; in short, that <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' \/> implies <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' \/>.<\/p>\n<p> Now, suppose that we want to prove <img src='http:\/\/l.wordpress.com\/latex.php?latex=A%20%5Csupset%20A&#038;bg=FFFFFF&#038;fg=000000&#038;s=0' title='A \\supset A' style='vertical-align:1%' class='tex' alt='A \\supset A' \/>. How could we do that? We need a program that given a proof of <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' \/> generates a proof of <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' \/>. That is, we need an implementation of the identity function: <img src='http:\/\/l.wordpress.com\/latex.php?latex=%5Clambda%20a%20.%20a&#038;bg=FFFFFF&#038;fg=000000&#038;s=0' title='\\lambda a . a' style='vertical-align:1%' class='tex' alt='\\lambda a . a' \/>. <\/p>\n<p> In fact, using the computation interpretation of things, we can interpret <img src='http:\/\/l.wordpress.com\/latex.php?latex=A%20%5Csupset%20B&#038;bg=FFFFFF&#038;fg=000000&#038;s=0' title='A \\supset B' style='vertical-align:1%' class='tex' alt='A \\supset B' \/> as being the type of a function that takes an instance of <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' \/>, and generates an instance of <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' \/> &#8211; that is, that if <img src='http:\/\/l.wordpress.com\/latex.php?latex=f%20%3A%20%28A%20%5Csupset%20B%29&#038;bg=FFFFFF&#038;fg=000000&#038;s=0' title='f : (A \\supset B)' style='vertical-align:1%' class='tex' alt='f : (A \\supset B)' \/>, then <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' \/> is a function from an instance of <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' \/> to an instance of <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' \/>!<\/p>\n<p> The only trick to that is understanding that in type theory, saying that <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' \/> is an element of <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' \/> means that <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' \/> is a proof of <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' \/>. Using the same interpretation, that means that <img src='http:\/\/l.wordpress.com\/latex.php?latex=f%3A%20A%20%5Csupset%20B&#038;bg=FFFFFF&#038;fg=000000&#038;s=0' title='f: A \\supset B' style='vertical-align:1%' class='tex' alt='f: A \\supset B' \/> means that <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' \/> is a proof of <img src='http:\/\/l.wordpress.com\/latex.php?latex=A%20%5Csupset%20B&#038;bg=FFFFFF&#038;fg=000000&#038;s=0' title='A \\supset B' style='vertical-align:1%' class='tex' alt='A \\supset B' \/> &#8211; which means the same thing as saying that given an example of an element of <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' \/> (a proof of <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=f&#038;bg=FFFFFF&#038;fg=000000&#038;s=0' title='f' style='vertical-align:1%' class='tex' alt='f' \/> will produce an element of <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' \/> (a proof of <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' \/>). The statement <img src='http:\/\/l.wordpress.com\/latex.php?latex=A%20%5Csupset%20B&#038;bg=FFFFFF&#038;fg=000000&#038;s=0' title='A \\supset B' style='vertical-align:1%' class='tex' alt='A \\supset B' \/> is exactly the same thing as the logical implication <img src='http:\/\/l.wordpress.com\/latex.php?latex=A%20%5Crightarrow%20B&#038;bg=FFFFFF&#038;fg=000000&#038;s=0' title='A \\rightarrow B' style='vertical-align:1%' class='tex' alt='A \\rightarrow B' \/>, which is exactly the same thing as the <em>type<\/em> of a function from <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' \/> to <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' \/>.<\/p>\n<hr\/>\n<p><em><\/p>\n<p>Notes on sources: I&#8217;m working from two main references in this series of posts, both of which are available in PDF form online.<\/p>\n<ol>\n<li> &#8220;Programming in Martin-Lof&#8217;s Type Theory&#8221;, by Nordstrom, Petersson, and Smith, which you can download <a \"href= http:\/\/www.cse.chalmers.se\/research\/group\/logic\/book\/book.pdf\">here<\/a>.<\/li>\n<li> &#8220;Type Theory and Functional Programming&#8221; by Simon Thompson (available <a href=\"https:\/\/www.cs.kent.ac.uk\/people\/staff\/sjt\/TTFP\/\">here<\/a>).<\/li>\n<\/ol>\n<p> In addition, I first learned a lot of this from reading some papers by Phil Wadler and Simon Peyton Jones. The exactly references but the exact references to those are long-lost in the shadows of my memory. But any of their papers are well-worth reading, so just read them all!)<\/p>\n<p><\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>As usual, I&#8217;m going in several different directions. I&#8217;m going to continue doing data structure posts, but at the same time I also want to get back to the type theory stuff that I was writing about before I put the blog on hiatus. So let&#8217;s get back to a bit of Martin-Loff type theory! [&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-3113","post","type-post","status-publish","format-standard","hentry","category-type-theory"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p4lzZS-Od","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/posts\/3113","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=3113"}],"version-history":[{"count":2,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/posts\/3113\/revisions"}],"predecessor-version":[{"id":3115,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/posts\/3113\/revisions\/3115"}],"wp:attachment":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/media?parent=3113"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/categories?post=3113"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/tags?post=3113"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}