{"id":2004,"date":"2013-01-21T14:19:59","date_gmt":"2013-01-21T19:19:59","guid":{"rendered":"http:\/\/scientopia.org\/blogs\/goodmath\/?p=2004"},"modified":"2013-01-21T14:19:59","modified_gmt":"2013-01-21T19:19:59","slug":"types-gone-wild-ski-at-compile-time","status":"publish","type":"post","link":"http:\/\/www.goodmath.org\/blog\/2013\/01\/21\/types-gone-wild-ski-at-compile-time\/","title":{"rendered":"Types Gone Wild! SKI at Compile-Time"},"content":{"rendered":"<p> Over the weekend, a couple of my Foursquare coworkers and I were chatting on twitter, and one of my smartest coworkers, a great guy named Jorge Ortiz, pointed out that type inference in Scala (the language we use at Foursquare, and also pretty much my favorite language) is Turing complete.<\/p>\n<p> Somehow, I hadn&#8217;t seen this before, and it absolutely blew my mind. So I asked Jorge for a link to the proof. The <a href=\"http:\/\/michid.wordpress.com\/2010\/01\/29\/scala-type-level-encoding-of-the-ski-calculus\/\">link he sent me<\/a> is a really beautiful blog post. It doesn&#8217;t just prove that Scala type inference is Turing complete, but it does it in a remarkably beautiful way.<\/p>\n<p> Before I get to the proof, what does this mean?<\/p>\n<p> A system is Turing complete when it can perform <em>any<\/em> possible computation that could be performed on any other computing device. The Turing machine is, obviously, Turing complete. So is lambda calculus, the Minsky machine, the Brainfuck computing model, and the Scala programming language itself.<\/p>\n<p> If type inference is Turing complete, then that means that you can write a Scala program where, in order to type-check the program, the compiler has to run an arbitrary program to completion. It means that there are, at least theoretically, Scala programs where the compiler will take forever &#8211; <em>literally forever<\/em> &#8211; to determine whether or not a given program contains a type error.  Needless to say, I consider this to be a bad thing. Personally, I&#8217;d really prefer to see the type system be less flexible. In fact, I&#8217;d go so far as to say that this is a fundamental error in the design of Scala, and a big strike against it as a language.  Having a type-checking system which isn&#8217;t guaranteed to terminate is bad. <\/p>\n<p> But let&#8217;s put that aside: Scala is pretty entrenched in the community that uses it, and they&#8217;ve accepted this as a tradeoff. How did the blog author, Michael D\u00fcrig, prove that Scala type checking is Turing complete? By showing how to implement a variant of lambda calculus called SKI combinator calculus entirely with types.<\/p>\n<p> SKI calculus is seriously cool. We know that lambda calculus is Turing complete. It turns out that for any lambda calculus expression, there&#8217;s a way rewriting it without any variables, and without any lambdas at all, using three canonical master functions. If you&#8217;ve got those three, then you can write anything, <em>anything at all<\/em>. The three are called S, K, and I.<\/p>\n<ul>\n<li> The S combinator is: <img src='http:\/\/l.wordpress.com\/latex.php?latex=S%20x%20y%20z%20%3D%20x%20z%20%28y%20z%29&#038;bg=FFFFFF&#038;fg=000000&#038;s=0' title='S x y z = x z (y z)' style='vertical-align:1%' class='tex' alt='S x y z = x z (y z)' \/>.<\/li>\n<li> The K combinator is: <img src='http:\/\/l.wordpress.com\/latex.php?latex=K%20x%20y%20%3D%20x&#038;bg=FFFFFF&#038;fg=000000&#038;s=0' title='K x y = x' style='vertical-align:1%' class='tex' alt='K x y = x' \/>.<\/li>\n<li> The I combinator is: <img src='http:\/\/l.wordpress.com\/latex.php?latex=I%20x%20%3D%20x&#038;bg=FFFFFF&#038;fg=000000&#038;s=0' title='I x = x' style='vertical-align:1%' class='tex' alt='I x = x' \/>.<\/li>\n<\/ul>\n<p> They come from intuitionistic logic, where they&#8217;re fundamental axioms that describe how intuitionistic implication works. K is the rule <img src='http:\/\/l.wordpress.com\/latex.php?latex=A%20Rightarrow%20%28B%20Rightarrow%20A%29&#038;bg=FFFFFF&#038;fg=000000&#038;s=0' title='A Rightarrow (B Rightarrow A)' style='vertical-align:1%' class='tex' alt='A Rightarrow (B Rightarrow A)' \/>; S is the rule <img src='http:\/\/l.wordpress.com\/latex.php?latex=%28A%20Rightarrow%20%28B%20Rightarrow%20C%29%29%20Rightarrow%20%28%28A%20Rightarrow%20B%29%20Rightarrow%20C%29&#038;bg=FFFFFF&#038;fg=000000&#038;s=0' title='(A Rightarrow (B Rightarrow C)) Rightarrow ((A Rightarrow B) Rightarrow C)' style='vertical-align:1%' class='tex' alt='(A Rightarrow (B Rightarrow C)) Rightarrow ((A Rightarrow B) Rightarrow C)' \/>; and I is <img src='http:\/\/l.wordpress.com\/latex.php?latex=%28A%20Rightarrow%20A%29&#038;bg=FFFFFF&#038;fg=000000&#038;s=0' title='(A Rightarrow A)' style='vertical-align:1%' class='tex' alt='(A Rightarrow A)' \/>.  <\/p>\n<p> Given any lambda calculus expression, you can rewrite it as a chain of SKIs. (If you&#8217;re interested in seeing how, please just ask in the comments; if enough people are interested, I&#8217;ll write it up.) What the author of the post id is show how to implement the S, K, and I combinators in Scala types. <\/p>\n<pre>\ntrait Term {\n  type ap[x &lt;: Term] &lt;: Term\n  type eval &lt;: Term\n}\n<\/pre>\n<p> He&#8217;s created a type <code>Term<\/code>, which is the supertype of any computable fragment written in this type-SKI. Since everything is a function, all terms have to have two methods: one of them is a one-parameter &#8220;function&#8221; which applies the term to a parameter, and the second is a &#8220;function&#8221; which simplifies the term into canonical form.<\/p>\n<p> He implements the S, K, and I combinators as traits that extend <code>Term<\/code>. We&#8217;ll start with the simplest one, the <code>I<\/code> combinator.<\/p>\n<pre>\ntrait I extends Term {\n  type ap[x &lt;: Term] = I1[x]\n  type eval = I\n}\n\ntrait I1[x &lt;: Term] extends Term {\n  type ap[y &lt;: Term] = eval#ap[y]\n  type eval = x#eval\n}\n<\/pre>\n<p> <code>I<\/code> needs to take a parameter, so its apply type-function takes a parameter <code>x<\/code>, and returns a new type <code>I1[x]<\/code> which has the parameter encoded into it.  Evaluating <code>I1[x]<\/code> does exactly what you&#8217;d want from the <code>I<\/code> combinator with its parameter &#8211; it returns it.<\/p>\n<p> The apply &#8220;method&#8221; of <code>I1<\/code> looks strange. What you have to remember is that in lambda calculus (and in the SKI combinator calculus), everything is a function &#8211; so even after evaluating <im>I.ap[x]<\/im> to some other type, it&#8217;s <em>still<\/em> a type function. So it still needs to be applicable.  Applying it is exactly the same thing as applying its parameter.<\/p>\n<p> So if have any type A, if you write something like <code>var a : I.ap[A].eval<\/code>, the type of <code>a<\/code> will evaluate to <code>A<\/code>. If you apply <code>I.ap[A].ap[Z]<\/code>, it&#8217;s equivalent to taking the result of evaluating <code>I.ap[A]<\/code>, giving you <code>A<\/code>, and then applying that to <code>Z<\/code>.<\/p>\n<p> The <code>K<\/code> combinator is much more interesting:<\/p>\n<pre>\n\/\/ The K combinator\ntrait K extends Term {\n  type ap[x &lt;: Term] = K1[x]\n  type eval = K\n}\n\ntrait K1[x &lt;: Term] extends Term {\n  type ap[y &lt;: Term] = K2[x, y]\n  type eval = K1[x]\n}\n\ntrait K2[x &lt;: Term, y <: Term] extends Term {\n  type ap[z &lt;: Term] = eval#ap[z]\n  type eval = x#eval\n}\n<\/pre>\n<p> It's written in curried form, so it's a type trait <code>K<\/code>, which returns a type trait <code>K1<\/code>, which takes a parameter and returns a type trait <code>K2<\/code>.<\/p>\n<p> The implementation is a whole lot trickier, but it's the same basic mechanics. Applying <code>K.ap[X]<\/code> gives you <code>K1[X]<\/code>.  Applying that to <code>Y<\/code> with <code>K1[X].ap[Y]<\/code> gives you <code>K2[K, Y]<\/code>. Evaluating that gives you <im>X<\/im>.\n<\/p>\n<p> The <code>S<\/code> combinator is more of the same.<\/p>\n<pre>\n\/\/ The S combinator\ntrait S extends Term {\n  type ap[x &lt;: Term] = S1[x]\n  type eval = S\n}\n\ntrait S1[x &lt;: Term] extends Term {\n  type ap[y &lt;: Term] = S2[x, y]\n  type eval = S1[x]\n}\n\ntrait S2[x &lt;: Term, y &lt;: Term] extends Term {\n  type ap[z &lt;: Term] = S3[x, y, z]\n  type eval = S2[x, y]\n}\n\ntrait S3[x &lt;: Term, y <: Term, z &lt;: Term] extends Term {\n  type ap[v &lt;: Term] = eval#ap[v]\n  type eval = x#ap[z]#ap[y#ap[z]]#eval\n}\n\n\n<\/pre>\n<p> Michid then goes on to show examples of how to use these beasts. He implements equality testing, and then shows how to test if different type-expressions evaluate to the same thing. And all of this happens <em>at compile time<\/em>. If the equality test fails, then it's a type error at compile time!<\/p>\n<p> It's a brilliant little proof. Even if you can't read Scala syntax, and you don't really understand Scala type inference, as long as you know SKI, you can look at the equality comparisons, and see how it works in SKI. It's really beautiful.  <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Over the weekend, a couple of my Foursquare coworkers and I were chatting on twitter, and one of my smartest coworkers, a great guy named Jorge Ortiz, pointed out that type inference in Scala (the language we use at Foursquare, and also pretty much my favorite language) is Turing complete. Somehow, I hadn&#8217;t seen this [&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":[7,32],"tags":[],"class_list":["post-2004","post","type-post","status-publish","format-standard","hentry","category-bad-software","category-lambda-calculus"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p4lzZS-wk","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/posts\/2004","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=2004"}],"version-history":[{"count":0,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/posts\/2004\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/media?parent=2004"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/categories?post=2004"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/tags?post=2004"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}