{"id":8214,"date":"2020-06-12T21:52:05","date_gmt":"2020-06-12T21:52:05","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=8214"},"modified":"2020-06-12T21:52:05","modified_gmt":"2020-06-12T21:52:05","slug":"the-haskell-repl-basic-commands","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/the-haskell-repl-basic-commands\/","title":{"rendered":"The Haskell repl &#8211; basic commands"},"content":{"rendered":"<p>Haskell&#8217;s repl can be run use <em>ghci<\/em> or <em>cabal repl<\/em>. The repl using : to prefix commands, so for example :? will list the help.<\/p>\n<p>I&#8217;m not going to go through all commands, but just the one&#8217;s I&#8217;ve been using regularly, check out <a href=\"https:\/\/www.haskell.org\/cabal\/users-guide\/intro.html\" rel=\"noopener noreferrer\" target=\"_blank\">Cabal<\/a> for the full list of commands and features.<\/p>\n<p><strong>Quitting the repl<\/strong><\/p>\n<p>Let&#8217;s start by looking at how to quitting the repl (gracefully)<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nPrelude&gt; :q\r\n<\/pre>\n<p><strong>Writing code<\/strong><\/p>\n<p>Obviously we want to be able to execute code in the repl, in such cases we write code such as<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nx = 1 + 2\r\n-- press enter\r\nx\r\n-- press enter\r\n<\/pre>\n<p>The output from this will obviously be 3. <\/p>\n<p>To handle multi line input we create a block using :{ and :}, for example<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n:{\r\ndata Expr = Lit Integer |\r\n            Div Expr Expr\r\n:}\r\n<\/pre>\n<p>Don&#8217;t forget indention is required!<\/p>\n<p><strong>Loading existing code<\/strong><\/p>\n<p>In some cases we might want to load existing code into the repl, such a data definitions etc. Let&#8217;s say we have a Main.hs then run<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n:load Main\r\n<\/pre>\n<p><strong>Display information<\/strong><\/p>\n<p>Assuming we added the Expr data type, to the repl, we might want to show the definition at some point, just use<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n:i Expr\r\n<\/pre>\n<p><strong>Showing the type of an expression<\/strong><\/p>\n<p>Let&#8217;s assume we have something like the following in a module<\/p>\n<pre class=\"brush: haskell; title: ; notranslate\" title=\"\">\r\neval :: Expr -&gt; Integer\r\n<\/pre>\n<p>We can use the following to return the type (obviously in this instance we should see the above, but this command can ofcourse be executed against types where we want to find the type annotations\/decalaractions<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n:t Expr\r\n<\/pre>\n<p>Now try <\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n:t print\r\n<\/pre>\n<p>and you&#8217;ll see the following<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nprint :: Show a =&gt; a -&gt; IO ()\r\n<\/pre>\n<p><strong>References<\/strong><\/p>\n<p><a href=\"https:\/\/downloads.haskell.org\/~ghc\/7.8.4\/docs\/html\/users_guide\/ghci.html\" rel=\"noopener noreferrer\" target=\"_blank\">Using GHCi<\/a><br \/>\n<a href=\"https:\/\/www.haskell.org\/cabal\/release\/cabal-1.10.1.0\/doc\/users-guide\/\" rel=\"noopener noreferrer\" target=\"_blank\">Cabal User Guide<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Haskell&#8217;s repl can be run use ghci or cabal repl. The repl using : to prefix commands, so for example :? will list the help. I&#8217;m not going to go through all commands, but just the one&#8217;s I&#8217;ve been using regularly, check out Cabal for the full list of commands and features. Quitting the repl [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[296],"tags":[],"class_list":["post-8214","post","type-post","status-publish","format-standard","hentry","category-cabal"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/8214","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=8214"}],"version-history":[{"count":5,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/8214\/revisions"}],"predecessor-version":[{"id":8239,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/8214\/revisions\/8239"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=8214"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=8214"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=8214"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}