{"id":940,"date":"2022-08-30T15:09:43","date_gmt":"2022-08-30T15:09:43","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/f-can-someone-explain-my-compiler-error-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:09:43","modified_gmt":"2022-08-30T15:09:43","slug":"f-can-someone-explain-my-compiler-error-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/f-can-someone-explain-my-compiler-error-collection-of-common-programming-errors\/","title":{"rendered":"F#: Can someone explain my compiler error?-Collection of common programming errors"},"content":{"rendered":"<p>Let&#8217;s try to reproduce how the compiler tries to infer types here.<\/p>\n<pre><code>let rec Foo(a,b) =\n    match a () with\n    | None -&gt; Some(b)\n    | Some(c) -&gt; Some(Foo(c,b))\n<\/code><\/pre>\n<p>&#8220;Ok, so I see <code>a ()<\/code>. <code>a<\/code> must be a function from <code>unit<\/code> to some type, I don&#8217;t know which one yet. I&#8217;ll call it <code>'a<\/code>.&#8221;<\/p>\n<pre><code>a : unit -&gt; 'a\n<\/code><\/pre>\n<p>&#8220;The result of <code>a ()<\/code> is matched with <code>None<\/code>\/<code>Some<\/code> patterns. So <code>'a<\/code> must be a <code>'b option<\/code> and <code>c<\/code> has the type <code>'b<\/code>.&#8221; (Again, <code>'b<\/code> stands for an unknown, as of yet, type).<\/p>\n<pre><code>a : unit -&gt; 'b option\n\u0441 : 'b\n<\/code><\/pre>\n<p>&#8220;No functions or methods are called on <code>b<\/code> (except <code>Some<\/code>, which doesn&#8217;t narrow the type down, and <code>Foo<\/code>, the type of which we don&#8217;t know so far). I&#8217;ll denote its type by <code>'c<\/code>.&#8221;<\/p>\n<pre><code>a : unit -&gt; 'b option\nb : 'c\nc : 'b\n<\/code><\/pre>\n<p>&#8220;<code>Foo<\/code> returns <code>Some(b)<\/code> in one of the branches, so the return type must be <code>'c option<\/code>.&#8221;<\/p>\n<pre><code>Foo : (unit -&gt; 'b option) * 'c -&gt; 'c option\n<\/code><\/pre>\n<p>&#8220;Am I done yet? No, I need to check that all types in the expression make sense. Let&#8217;s see, in the <code>Some(c)<\/code> case, <code>Some(Foo(c,b))<\/code> is returned. So <code>Foo(c,b) : 'c<\/code>. Since <code>Foo<\/code> returns an <code>option<\/code>, I know <code>'c<\/code> must be <code>'d option<\/code> for some <code>'d<\/code>, and <code>b : 'd<\/code>. Wait, I already have <code>b : 'c<\/code>, that is, <code>b : 'd option<\/code>. <code>'d<\/code> and <code>'d option<\/code> have to be the same type, but this is impossible! There must be an error in the definition. I need to report it.&#8221; So it does.<\/p>\n<p id=\"rop\"><small>Originally posted 2013-11-09 23:04:41. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>Let&#8217;s try to reproduce how the compiler tries to infer types here. let rec Foo(a,b) = match a () with | None -&gt; Some(b) | Some(c) -&gt; Some(Foo(c,b)) &#8220;Ok, so I see a (). a must be a function from unit to some type, I don&#8217;t know which one yet. I&#8217;ll call it &#8216;a.&#8221; a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-940","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/940","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=940"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/940\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=940"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=940"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=940"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}