{"id":422,"date":"2022-08-30T15:01:05","date_gmt":"2022-08-30T15:01:05","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/why-does-ghci-desugar-type-lists-and-type-families-can-this-be-selectively-disabled-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:01:05","modified_gmt":"2022-08-30T15:01:05","slug":"why-does-ghci-desugar-type-lists-and-type-families-can-this-be-selectively-disabled-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/why-does-ghci-desugar-type-lists-and-type-families-can-this-be-selectively-disabled-collection-of-common-programming-errors\/","title":{"rendered":"Why does ghci desugar type lists and type families? Can this be selectively disabled?-Collection of common programming errors"},"content":{"rendered":"<p>I&#8217;m trying to make the types ghci displays for my libraries as intuitive as possible, but I&#8217;m running into a lot of difficulties when using more advanced type features.<\/p>\n<p>Let&#8217;s say I have this code in a file:<\/p>\n<pre><code>{-# LANGUAGE TypeFamilies #-}\n{-# LANGUAGE DataKinds #-}\n{-# LANGUAGE TypeOperators #-}\n\nimport GHC.TypeLits\n\ndata Container (xs::[*]) = Container\n<\/code><\/pre>\n<p>I load it up in ghci, then I type the following command:<\/p>\n<pre><code>ghci&gt; :t undefined :: Container '[String,String,String,String,String]\n<\/code><\/pre>\n<p>Unfortunately, ghci gives me the rather ugly looking:<\/p>\n<pre><code>:: Container\n       ((':)\n          *\n          String\n          ((':)\n             * String ((':) * String ((':) * String ((':) * String ('[] *))))))\n<\/code><\/pre>\n<p>ghci has removed the sugar for type level strings. Is there any way to prevent ghci from doing this and giving me just the pretty version?<\/p>\n<p>On a related note, lets say I create a type level <code>Replicate<\/code> function<\/p>\n<pre><code>data Nat1 = Zero | Succ Nat1\n\ntype family Replicate (n::Nat1) x :: [*]\ntype instance Replicate Zero x = '[]\ntype instance Replicate (Succ n) x = x ': (Replicate n x)\n\ntype LotsOfStrings = Replicate (Succ (Succ (Succ (Succ (Succ Zero))))) String\n<\/code><\/pre>\n<p>Now, when I ask ghci for a type using <code>LotsOfStrings<\/code>:<\/p>\n<pre><code>ghci&gt; :t undefined :: Container LotsOfStrings\n<\/code><\/pre>\n<p>ghci is nice and gives me the pretty result:<\/p>\n<pre><code>undefined :: Container LotsOfStrings\n<\/code><\/pre>\n<p>But if I ask for the <code>Replicate<\/code>d version,<\/p>\n<pre><code>ghci&gt; :t undefined :: Container (Replicate (Succ (Succ (Succ (Succ (Succ Zero))))) String)\n<\/code><\/pre>\n<p>ghci substitutes in for the type family when it didn&#8217;t do that for the type synonym:<\/p>\n<pre><code>:: Container\n       ((':)\n          *\n          [Char]\n          ((':)\n             * [Char] ((':) * [Char] ((':) * [Char] ((':) * [Char] ('[] *))))))\n<\/code><\/pre>\n<p>Why is ghci doing the substitution for the type family, but not the type synonym? Is there a way to control when ghci will do the substitution?<\/p>\n<ol>\n<li>\n<p>The workaround that I know of is using :kind. For instance,<\/p>\n<blockquote>\n<p>ghci&gt; :kind (Container &#8216;[String,String,String,String,String])<\/p>\n<\/blockquote>\n<p>Gives:<\/p>\n<blockquote>\n<p>( Container &#8216;[String,String,String,String,String]) :: *<\/p>\n<\/blockquote>\n<p>While<\/p>\n<blockquote>\n<p>ghci&gt; :kind! (Container &#8216;[String,String,String,String,String])<\/p>\n<\/blockquote>\n<p>Will print something like this:<\/p>\n<blockquote>\n<p>Container<\/p>\n<p>((&#8216;:)<\/p>\n<pre><code>  *\n  [Char]\n  ((':)\n     * [Char] ((':) * [Char] ((':) * [Char] ((':) * [Char] ('[] *))))))\n<\/code><\/pre>\n<\/blockquote>\n<p>Officially, of course, you&#8217;re asking ghci a different question with <code>kind<\/code>, but it works. Using <code>undefined ::<\/code> is sort of a workaround anyhow, so I thought this might suffice.<\/p>\n<\/li>\n<li>\n<pre><code>import GHC.TypeLits\n\ndata Container (xs::[*]) = Container\n<\/code><\/pre>\n<p>I load it up in ghci, then I type the following command:<\/p>\n<pre><code>:t undefined :: Container '[String,String,String,String,String]\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-09 19:25:56. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I&#8217;m trying to make the types ghci displays for my libraries as intuitive as possible, but I&#8217;m running into a lot of difficulties when using more advanced type features. Let&#8217;s say I have this code in a file: {-# LANGUAGE TypeFamilies #-} {-# LANGUAGE DataKinds #-} {-# LANGUAGE TypeOperators #-} import GHC.TypeLits data Container (xs::[*]) [&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-422","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/422","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=422"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/422\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=422"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=422"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=422"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}