<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Talk Like A Duck: Perspective on the Static vs. Dynamic Type Debate.</title>
    <link>http://talklikeaduck.denhaven2.com/articles/2007/06/29/perspective-on-the-static-vs-dynamic-type-debate</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>In Ruby, it's not the dog, it's the tricks!</description>
    <item>
      <title>Perspective on the Static vs. Dynamic Type Debate.</title>
      <description>My &lt;span class="caps"&gt;RSS&lt;/span&gt; reader uncovered a thought provoking paper by Chris Smith entitled &lt;a href="http://cdsmith.twu.net/types.html"&gt;&amp;#8220;What To Know Before Debating Type Systems&amp;#8221;&lt;/a&gt;. It clears up a lot of the confusion around the differences between static and dynamic type systems.
&lt;/p&gt;Much of the debate comes from people with limited exposure to both static and dynamic typing.  He observes that:
&lt;blockquote&gt;
&lt;ol&gt;
&lt;li&gt;Many programmers have used very poor statically typed languages.&lt;/li&gt;
&lt;li&gt;Many programmers have used dynamically typed languages very poorly.&lt;/li&gt;
&lt;/ol&gt;
&lt;/blockquote&gt;
&lt;blockquote class = "pullquote"&gt;
Many programmers have used very poor statically typed languages.
Many programmers have used dynamically typed languages very poorly.
&lt;/ol&gt;
&lt;/blockquote&gt;
&lt;p&gt;He then points out that although there is a good deal of overlap in the problems which can be solved by the two approaches, they are fundamentally different techniques. According to the paper:
&lt;blockquote&gt;
A static type system is a mechanism by which a compiler examines source code and assigns labels (called &amp;#8220;types&amp;#8221;) to pieces of the syntax, and then uses them to infer something about the program&amp;#8217;s behavior.  A dynamic type system is a mechanism by which a compiler generates code to keep track of the sort of data (called its &amp;#8220;type&amp;#8221;) used by the program.  The use of the same word &amp;#8220;type&amp;#8221; in each of these two systems is, of course, not entirely coincidental; yet it is best understood as having a sort of historical or generalized significance.  Great confusion results from trying to find a world view in which &amp;#8220;type&amp;#8221; really means the same thing in both systems.  It doesn&amp;#8217;t.
&lt;/blockquote&gt;
&lt;p&gt;Most of the popular languages with static type systems use static types to mechanically prove that a program is type-safe.  Programs for which this can be proven are accepted, and others are rejected.  The paper shows that for any such type-system there will be correct programs which cannot be proven correct and will be rejected.&lt;p&gt;
&lt;p&gt;Languages with dynamic type systems allow execution of programs without prior proof of &amp;#8216;innocence.&amp;#8217; However, the presence of meta-information at run-time allows for fast-failure and better context for correction when errors do occur.&lt;/p&gt;
&lt;p&gt;The type systems of most popular statically typed languages are rather limited in the properties which can be proven.  Not all program bugs can be statically determined. Seemingly easy checks such as arrray index checking are actually quite difficult to express statically, most languages don&amp;#8217;t check this statically.  As Smith says, &amp;#8220;this is an unusually hard problem to solve without making the languages that solve it unusable, but it&amp;#8217;s a popular one to work on&amp;#8221; as a research topic. Reading this paper has motivated me to learn more about Haskell to explore how easy or hard it is to push the notions of what can be statically checked.&lt;/p&gt;
&lt;p&gt;It occurs to me that a language like C++ has a Napoleonic-code where you are &amp;#8216;guilty until proven innocent&amp;#8217;, but once you&amp;#8217;ve gotten through the trial, you are free to go where you want and do what you want to do. However this means that if you are guilty of something which the type-system doesn&amp;#8217;t discover, like an array bounds violation, your malfeasance might be undetected for some time, and then require sophisticated forensics to debug.  In Ruby or Smalltalk, you are &amp;#8216;innocent until proven guilty,&amp;#8217; but your actions are under a higher level of surveillance by the system.&lt;/p&gt;
&lt;p&gt;Smith wraps up by re-framing the static vs. dynamic typing debate:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Static type systems prove properties of code, but it almost appears that Rice&amp;#8217;s Theorem means we can&amp;#8217;t prove anything of interest with a computer.  If true, that would be an ironclad argument against static typing systems.  Of course, it&amp;#8217;s not true.  However, it is very nearly true.  What Rice&amp;#8217;s Theorem says is that we can&amp;#8217;t determine anything.  (Often the word &amp;#8220;decide&amp;#8221; is used; they mean the same thing here.)  It didn&amp;#8217;t say we can&amp;#8217;t prove anything.  It&amp;#8217;s an important distinction!&lt;/p&gt;
&lt;p&gt;What this distinction means is that a static type system is a conservative estimate.  If it accepts a program, then we know the program has the properties proven by that type checker.  If it fails&amp;#8230; then we don&amp;#8217;t know anything.  Possibly the program doesn&amp;#8217;t have that property, or possibly the type checker just doesn&amp;#8217;t know how to prove.  Furthermore, there is an ironclad mathematical proof that a type checker of any interest at all is always conservative.  Building a type checker that doesn&amp;#8217;t reject any correct programs isn&amp;#8217;t a challenger that may be solved some day; it&amp;#8217;s impossible.&lt;/p&gt;
&lt;p&gt;That, then, is the trade-off.  We get assurance that the program is correct (in the properties checked by this type checker), but in turn we must reject some interesting programs.  To continue the pattern, this is the diametric opposite of testing.  With testing, we are assured that we&amp;#8217;ll never fail a correct program.  The trade-off is that for any program with an infinite number of possible inputs (in other words, any interesting program), a test suite may still accept programs that are not correct &amp;#8211; even in just those properties that are tested.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;He then poses eight questions to ponder when considering where and how to use static or dynamic typing in our programming tasks:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;For what interesting properties of programs can we build static type systems?&lt;/li&gt;
&lt;li&gt;How close can we bring those type systems to the unattainable ideal of never rejecting a correct program?&lt;/li&gt;
&lt;li&gt;How easy can it be made to program in a language with such a static type system?&lt;/li&gt;
&lt;li&gt;What is the cost associated with accidentally rejecting a correct computer program?&lt;/li&gt;
&lt;li&gt;For what interesting properties of programs can we build test suites via dynamic typing?&lt;/li&gt;
&lt;li&gt;How close can we bring those test suites to the unattainable ideal of never accepting a broken program?&lt;/li&gt;
&lt;li&gt;How easy can it be made to write and execute test suites for programs?&lt;/li&gt;
&lt;li&gt;What is the cost associated with accidentally accepting an incorrect computer program?&lt;/li&gt;
&lt;/ol&gt;</description>
      <pubDate>Fri, 29 Jun 2007 08:49:00 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:dae611e6-76e9-45bd-907a-518303b125ee</guid>
      <author>Rick DeNatale</author>
      <link>http://talklikeaduck.denhaven2.com/articles/2007/06/29/perspective-on-the-static-vs-dynamic-type-debate</link>
      <category>best_practices</category>
      <category>types</category>
      <category>dynamictypes</category>
      <category>statictypes</category>
      <trackback:ping>http://talklikeaduck.denhaven2.com/articles/trackback/437</trackback:ping>
    </item>
  </channel>
</rss>
