{"id":2270,"date":"2013-12-20T11:00:38","date_gmt":"2013-12-20T16:00:38","guid":{"rendered":"http:\/\/scientopia.org\/blogs\/goodmath\/?p=2270"},"modified":"2013-12-20T11:00:38","modified_gmt":"2013-12-20T16:00:38","slug":"more-basics-compilers-programs-and-languages","status":"publish","type":"post","link":"http:\/\/www.goodmath.org\/blog\/2013\/12\/20\/more-basics-compilers-programs-and-languages\/","title":{"rendered":"More Basics: Compilers, Programs, and Languages"},"content":{"rendered":"<p> After my &#8220;what is an OS?&#8221; post, a couple of readers asked me to write a similar post about compilers.<\/p>\n<p> Before I can answer what a compiler is, it&#8217;s helpful to first answer a different question: what is a <em>program<\/em>?<\/p>\n<p> And here we get to one of my pet peeves. The most common answer to that question is &#8220;a detailed step-by-step sequence of instructions&#8221;. For example, here&#8217;s what wikipedia says:<\/p>\n<blockquote><p>\nA computer program, or just a program, is a sequence of instructions, written to perform a specified task with a computer.\n<\/p><\/blockquote>\n<p> This is <em>wrong<\/em>.<\/p>\n<p> Back when people first started to study the idea of computing devices, they talked about computing machines as devices that performed a single, specific task. If you think about a <a href=\"http:\/\/scientopia.org\/blogs\/goodmath\/2012\/06\/24\/turing-machines-what-they-are-what-they-arent\/\">basic Turing machine<\/a>, you normally define Turing machines that perform a single computation. They&#8217;ve got a built-in sequence of states, and a built in transition table &#8211; the machine can only perform one computation. It took one kind of input, and performed its computation on that input, producing its output.<\/p>\n<p> Building up from these specific machines, they came up with the idea of a <em>universal<\/em> computing device. A universal computer was a computing machine whose input was a description of a <em>different<\/em> computing machine. By giving the universal machine different inputs, it could perform different computations.<\/p>\n<p> The point of this diversion is that looking at this history tells us what a program really is: it&#8217;s a description of a computing machine. Our computers are universal computing machines; they take programs as input to describe the computing machines we want them to emulate. What we&#8217;re doing when we program is describing a computing machine that we&#8217;d like to create. Then we feed it into our universal computing machine, and it behaves as if we&#8217;d built a custom piece of hardware to do our computation!<\/p>\n<p> The problem is, our computers are simultaneously very primitive and overwhelming complex. They can only work with data expressed in fixed-length sequences of on\/off values; to do anything else, we need to find a way of expressing in terms of extremely simple operations on those on\/off values. To make them operate efficiently, they&#8217;ve got a complex structure: many different kinds of storage (registers, l1 and l2 caches, addressable memory), complicated instruction sets, and a whole lot of tricky perfomance tweaks. It&#8217;s <em>really<\/em> hard to program a computer in terms of its native instructions!<\/p>\n<p> In fact, it&#8217;s so hard to program in terms of native instructions that we just don&#8217;t do it. What we do is write programs in terms of different machines. That&#8217;s the point of a programming language.<\/p>\n<p> Looked at this way, a program language is a way of describing computing machines. The difference between different programming languages is how they describe computing machines. A language like C describes von Neumann machines. Haskell describes machines that work via lambda calculus computations using something like a <a href=\"http:\/\/stgcompiler.googlecode.com\/svn\/trunk\/Docs\/Artigos\/p244-burn.pdf\">spineless G-machine. <\/a>. Prolog describes machines that perform computations in terms of intuitionistic logical inference like a <a href=\"http:\/\/en.wikipedia.org\/wiki\/Warren_Abstract_Machine\">Warren Abstract Machine<\/a>.<\/p>\n<p> So finally, we can get to the point: what is a compiler? A compiler is a program that takes a description of a computing device defined in one way, and translates into the kind of machine description that can be used by our hardware. A programming language ets us ignore all of the complexities of how our actual hardware is built, and describe our computations in terms of a simple abstraction. A compiler takes that description, and turns it into the form that computer hardware can actually use.<\/p>\n<p><p> For anyone who&#8217;s read this far: I&#8217;ve gotten a few requests to talk about assembly language. I haven&#8217;t programmed in assembly since the days of the Motorola 68000. This means that to do it, I&#8217;ll need to learn something more up-to-date. Would you be more interested in seeing Intel, or ARM?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>After my &#8220;what is an OS?&#8221; post, a couple of readers asked me to write a similar post about compilers. Before I can answer what a compiler is, it&#8217;s helpful to first answer a different question: what is a program? And here we get to one of my pet peeves. The most common answer to [&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":[74,54],"tags":[],"class_list":["post-2270","post","type-post","status-publish","format-standard","hentry","category-basics","category-programming"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p4lzZS-AC","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/posts\/2270","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=2270"}],"version-history":[{"count":0,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/posts\/2270\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/media?parent=2270"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/categories?post=2270"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/tags?post=2270"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}