<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<title>Technocoder - type-systems</title>
	<link href="https://blog.techno.fish/tags/type-systems/atom.xml" rel="self" type="application/atom+xml"/>
  <link href="https://blog.techno.fish"/>
	<generator uri="https://www.getzola.org/">Zola</generator>
	<updated>2021-06-05T00:00:00+00:00</updated>
	<id>https://blog.techno.fish/tags/type-systems/atom.xml</id>
	<entry xml:lang="en">
		<title>Introduction</title>
		<published>2021-06-05T00:00:00+00:00</published>
		<updated>2021-06-05T00:00:00+00:00</updated>
		<link href="https://blog.techno.fish/procedural-type-inference/introduction/" type="text/html"/>
		<id>https://blog.techno.fish/procedural-type-inference/introduction/</id>
		<content type="html">&lt;p&gt;So you&#x27;ve just finished generating the abstract syntax tree for your language, and you want to implement some kind of type inference. Or maybe you&#x27;re fascinated by the scarce amount of type annotations you need in a language like Haskell or Rust and you want to know how it works. You search around but all you can find are tutorials using functional languages that you struggle to understand or notation heavy research papers that you feel you need a master’s degree to even read.&lt;&#x2F;p&gt;
&lt;p&gt;This series is for you. &lt;&#x2F;p&gt;
&lt;h2 id=&quot;but-first-what-is-a-type-system&quot;&gt;But first, what is a type system?&lt;&#x2F;h2&gt;
&lt;p&gt;Let&#x27;s clear up some misconceptions you may have. There are many type systems used across a multitude of programming languages. At its core, a type system is, by definition, a set of formal mathematical rules that specify what type a variable or expression is given some information we have about the program. Consider this code you might find in a C program:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;&quot;&gt;
&lt;code&gt;&lt;span style=&quot;color:#cd74e8;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span style=&quot;color:#abb2bf;&quot;&gt; index;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If we were to write down C&#x27;s type system on paper, we might have a rule that informally states: &lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;If we see a variable declaration with the word &lt;code&gt;int&lt;&#x2F;code&gt; written in front of it, the type of that variable is &lt;code&gt;int&lt;&#x2F;code&gt;. &lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;At first glance, this looks like an absolutely useless rule. But note carefully the two different meanings of &lt;code&gt;int&lt;&#x2F;code&gt;. The first time it&#x27;s used, it&#x27;s referring to the syntax &lt;code&gt;int&lt;&#x2F;code&gt; (what we literally see in our code). The second time it&#x27;s used, it&#x27;s referring to the &lt;em&gt;type&lt;&#x2F;em&gt; &lt;code&gt;int&lt;&#x2F;code&gt; (something a type checker can understand).&lt;&#x2F;p&gt;
&lt;p&gt;Here&#x27;s another example written in C++:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;&quot;&gt;
&lt;code&gt;&lt;span style=&quot;color:#cd74e8;&quot;&gt;auto&lt;&#x2F;span&gt;&lt;span style=&quot;color:#abb2bf;&quot;&gt; flag &lt;&#x2F;span&gt;&lt;span style=&quot;color:#adb7c9;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#db9d63;&quot;&gt;true&lt;&#x2F;span&gt;&lt;span style=&quot;color:#abb2bf;&quot;&gt;;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Here we might see a rule stating: &lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;If we see a variable initialization with the word &lt;code&gt;auto&lt;&#x2F;code&gt; written in front of it, the type of the variable is the type of the expression it&#x27;s assigned to.&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;We can imagine how changing or adding new rules can result in wildly different and unique type systems. Of course, some rules (and by extension, type systems) make more sense than others. As a simplistic example, it wouldn&#x27;t make sense to have a rule stating: the expression &lt;code&gt;true&lt;&#x2F;code&gt; has the type &lt;code&gt;float&lt;&#x2F;code&gt; (unless of course, you&#x27;re trying to prank someone). As such, the design of useful and practical rules and type systems is an active area of research.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;type-systems-and-type-inference&quot;&gt;Type systems and type inference&lt;&#x2F;h2&gt;
&lt;p&gt;You may have read the previous section and wondered, &amp;quot;Hey, can&#x27;t we turn those rules into &lt;em&gt;a&lt;&#x2F;em&gt; type inference algorithm?&amp;quot;, and you&#x27;d be right. Most type systems are &lt;em&gt;syntax directed&lt;&#x2F;em&gt; which means that each rule matches with a piece of syntax in a language. We will leverage this property when we write a &lt;em&gt;bidirectional&lt;&#x2F;em&gt; type checker later in the series. For now however, we will make one thing clear:&lt;&#x2F;p&gt;
&lt;p&gt;A type system is not the same as a type inference algorithm itself.&lt;&#x2F;p&gt;
&lt;p&gt;Just because a type system is capable of sophisticated type inference doesn&#x27;t mean an algorithm for that system fulfills that level of sophistication. There are often many algorithms and one might opt for a less powerful one if it improves the quality of error messages or makes implementation easier. Directly translating a type system&#x27;s rules into an algorithm (with the aforementioned &lt;em&gt;syntax directed&lt;&#x2F;em&gt; approach) does not usually yield the most powerful algorithm for that type system and, more importantly, isn&#x27;t often directly applicable to your language. Take the rules for the &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Hindley%E2%80%93Milner_type_system&quot;&gt;Hindley-Milner type system&lt;&#x2F;a&gt;:&lt;&#x2F;p&gt;
&lt;details&gt;
	&lt;summary&gt;Expand image&lt;&#x2F;summary&gt;
	&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;wikimedia.org&#x2F;api&#x2F;rest_v1&#x2F;media&#x2F;math&#x2F;render&#x2F;svg&#x2F;10d6e781d61259108db5fbfcfd84cb47daac33a6&quot; alt=&quot;Hindley Milner Typing Rules&quot; &#x2F;&gt;
&lt;strong&gt;&lt;p class=&quot;text-center&quot;&gt;Note: You will not need to understand this&lt;&#x2F;p&gt;&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;

&lt;&#x2F;details&gt;
&lt;p&gt;It&#x27;s not obvious how one might turn these rules into an algorithm for an imperative language (if you can understand them!). There aren&#x27;t any syntactic constructs specific to a language like C like &lt;code&gt;for&lt;&#x2F;code&gt; or &lt;code&gt;while&lt;&#x2F;code&gt; loops. It&#x27;s clear we&#x27;ll have to take a different approach.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;the-hindley-milner-type-system&quot;&gt;The Hindley-Milner type system&lt;&#x2F;h2&gt;
&lt;p&gt;Throughout this series we will be implementing type inference for the Hindley-Milner type system (or lesser variants thereof). Yes, the same type system as that frightful picture describes. Don&#x27;t worry, we won&#x27;t need to comprehend those rules for our approaches.&lt;&#x2F;p&gt;
&lt;p&gt;Hindley-Milner has actually been around since 1969&lt;sup class=&quot;footnote-reference&quot;&gt;&lt;a href=&quot;#hindley&quot;&gt;1&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt; and was independently discovered by both, you guessed it, Hindley and Milner. It forms the basis of type systems in many influential functional languages such as &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;ML_(programming_language)&quot;&gt;ML&lt;&#x2F;a&gt;, &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Clean_(programming_language)&quot;&gt;Clean&lt;&#x2F;a&gt;, and &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Haskell_(programming_language)&quot;&gt;Haskell&lt;&#x2F;a&gt; and is also used in some imperative languages (perhaps most notably, &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Rust_(programming_language)&quot;&gt;Rust&lt;&#x2F;a&gt;&lt;sup class=&quot;footnote-reference&quot;&gt;&lt;a href=&quot;#rust&quot;&gt;2&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;). People have written many articles on it for futher reading. Here are some neat features of Hindley-Milner:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Types are completely inferable&lt;&#x2F;li&gt;
&lt;li&gt;Has practically efficient type inference algorithms&lt;&#x2F;li&gt;
&lt;li&gt;Supports generics (also known as parametric polymorphism)&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;Here are some features that it does &lt;em&gt;not&lt;&#x2F;em&gt; support:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Traits (also known as typeclasses and similar to C++ concepts)&lt;&#x2F;li&gt;
&lt;li&gt;Subtyping (inheritance)&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;We&#x27;ll explore how we might implement extensions of Hindley-Milner in future chapters. &lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;div class=&quot;footnote-definition&quot; id=&quot;hindley&quot;&gt;&lt;sup class=&quot;footnote-definition-label&quot;&gt;1&lt;&#x2F;sup&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;doi.org&#x2F;10.2307%2F1995158&quot;&gt;The Principal Type-Scheme of an Object in Combinatory Logic&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;div class=&quot;footnote-definition&quot; id=&quot;rust&quot;&gt;&lt;sup class=&quot;footnote-definition-label&quot;&gt;2&lt;&#x2F;sup&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;rustc-dev-guide.rust-lang.org&#x2F;type-inference.html&quot;&gt;https:&#x2F;&#x2F;rustc-dev-guide.rust-lang.org&#x2F;type-inference.html&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;div class=&quot;footnote-definition&quot; id=&quot;thanks&quot;&gt;&lt;sup class=&quot;footnote-definition-label&quot;&gt;3&lt;&#x2F;sup&gt;
&lt;p&gt;Thanks to &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;brendanzab&quot;&gt;@brendanzab&lt;&#x2F;a&gt; for reviewing and suggesting edits!&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
</content>
	</entry>
</feed>
