{"id":4421,"date":"2014-03-30T10:50:41","date_gmt":"2014-03-30T10:50:41","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/using-haskells-types-to-replace-assert-statements-or-if-checks-in-other-languages-collection-of-common-programming-errors\/"},"modified":"2014-03-30T10:50:41","modified_gmt":"2014-03-30T10:50:41","slug":"using-haskells-types-to-replace-assert-statements-or-if-checks-in-other-languages-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/using-haskells-types-to-replace-assert-statements-or-if-checks-in-other-languages-collection-of-common-programming-errors\/","title":{"rendered":"Using Haskell&#39;s types to replace assert statements or if checks in other languages-Collection of common programming errors"},"content":{"rendered":"<p>You might want an ADT that can only be constructed with golden ratio numbers then write myfun to accept that data type.<\/p>\n<p>I&#8217;ve assumed Integer as a base type, but you could use others (ex: Double or Float) or even be polymorphic.<\/p>\n<p>1) Make the ADT<\/p>\n<pre><code>module Golden (Gold, getGold, buildGold) where\n\ndata Gold = G Integer Integer\n\ngetGold :: Gold -&gt; (Integer, Integer)\ngetGold (G x y) = (x, y)\n\nbuildGold :: Integer -&gt; Integer -&gt; Maybe Gold\nbuildGold x y\n    | isGolden x y = Just (G x y)\n    | otherwise    = Nothing\n<\/code><\/pre>\n<p>Notice this module exports the <code>Gold<\/code> type but not the constructor (namely, not <code>G<\/code>). So the only way to get a value of type <code>Gold<\/code> is with <code>buildGold<\/code> which performs a run-time check &#8211; but only one &#8211; so the values of Gold can be used and assumed to be a golden ratio by all the consumers without checking.<\/p>\n<p>2) Use the ADT to build <code>myfun<\/code><\/p>\n<pre><code>myfun :: Gold -&gt; ???\nmyfun g = expr\n  where (x, y) = getGold g\n<\/code><\/pre>\n<p>Now if you try to call <code>myfun<\/code> with a non-golden number (a value not of type <code>Gold<\/code>) then you will get a compile time error.<\/p>\n<p>Recap To build golden numbers <code>buildGold<\/code> function must be used, which forces the number to be checked.<\/p>\n<p>Notice what gets checked when! You have a compile time guarantee that <code>myfun<\/code>, and all other functions you want to use with <code>Gold<\/code>, are always provided golden ratios. The program input (from user, network, or where ever) still needs runtime checks and that&#8217;s what <code>buildGold<\/code> provides; obviously there&#8217;s never going to be a program that can promise the human won&#8217;t type something undesirable.<\/p>\n<p>The alternatives given in the comments to your question are also worthy of consideration. An ADT is slightly heavy weight if all you need is a single function, <code>myfun<\/code>, that can fail then just have <code>myfun :: (Integer, Integer) -&gt; Maybe ???<\/code>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You might want an ADT that can only be constructed with golden ratio numbers then write myfun to accept that data type. I&#8217;ve assumed Integer as a base type, but you could use others (ex: Double or Float) or even be polymorphic. 1) Make the ADT module Golden (Gold, getGold, buildGold) where data Gold = [&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-4421","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4421","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=4421"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4421\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4421"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4421"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4421"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}