From 7c8ffd3d8c328d5ca145fe15638ec5c952782132 Mon Sep 17 00:00:00 2001 From: Thomas Miedema Date: Wed, 24 Jun 2015 12:52:16 +0200 Subject: [PATCH] GHCi docs: layout rule is respected inside :{ :} Summary: I don't know if or when this changed, but the documentation on :{ :} (multiline input) seems out of date. Layout rule works fine. I added a regression test. Reviewers: austin Subscribers: thomie, bgamari Differential Revision: https://phabricator.haskell.org/D1013 --- docs/users_guide/ghci.xml | 20 +++++------- testsuite/tests/ghci/scripts/ghci023.script | 11 +++++++ testsuite/tests/ghci/scripts/ghci023.stdout | 34 +++++++++++---------- 3 files changed, 37 insertions(+), 28 deletions(-) diff --git a/docs/users_guide/ghci.xml b/docs/users_guide/ghci.xml index d3b65ef807..a1271e1ac1 100644 --- a/docs/users_guide/ghci.xml +++ b/docs/users_guide/ghci.xml @@ -437,10 +437,10 @@ Prelude> The variable's type is not polymorphic, is not (), and is an instance of - Show + Show. - . + Of course, you can also bind normal non-IO expressions @@ -477,9 +477,9 @@ Prelude> However, this quickly gets tedious when defining functions with multiple clauses, or groups of mutually recursive functions, because the complete definition has to be given on a single line, - using explicit braces and semicolons instead of layout: + using explicit semicolons instead of layout: -Prelude> let { f op n [] = n ; f op n (h:t) = h `op` f op n t } +Prelude> let f op n [] = n ; f op n (h:t) = h `op` f op n t Prelude> f (+) 0 [1..3] 6 Prelude> @@ -489,18 +489,14 @@ Prelude> :} (each on a single line of its own): Prelude> :{ -Prelude| let { g op n [] = n -Prelude| ; g op n (h:t) = h `op` g op n t -Prelude| } +Prelude| let g op n [] = n +Prelude| g op n (h:t) = h `op` g op n t Prelude| :} Prelude> g (*) 1 [1..3] 6 Such multiline commands can be used with any GHCi command, - and the lines between :{ and - :} are simply merged into a single line for - interpretation. That implies that each such group must form a single - valid command when merged, and that no layout rule is used. + and note that the layout rule is in effect. The main purpose of multiline commands is not to replace module loading but to make definitions in .ghci-files (see ) more readable and maintainable. @@ -575,7 +571,7 @@ Prelude> Explicit braces and semicolons can be used instead of - layout, as usual: + layout: Prelude> do { diff --git a/testsuite/tests/ghci/scripts/ghci023.script b/testsuite/tests/ghci/scripts/ghci023.script index dd77433af7..a21fcb6ccc 100644 --- a/testsuite/tests/ghci/scripts/ghci023.script +++ b/testsuite/tests/ghci/scripts/ghci023.script @@ -9,6 +9,17 @@ putStrLn "-- via stdin" } :} print (f 0,f 1,y) + +putStrLn "-- layout rule instead of explicit braces and semicolons works too" +:{ + let + g 0 = 1 + g 1 = w + where w = 2 + z = 3 +:} +print (g 0,g 1,z) + :{ :browse Data.Maybe diff --git a/testsuite/tests/ghci/scripts/ghci023.stdout b/testsuite/tests/ghci/scripts/ghci023.stdout index 010fe50878..61a859a73c 100644 --- a/testsuite/tests/ghci/scripts/ghci023.stdout +++ b/testsuite/tests/ghci/scripts/ghci023.stdout @@ -1,16 +1,18 @@ --- testing ghci multiline commands :{ .. :} --- via stdin -(1,2,3) -catMaybes :: [Maybe a] -> [a] -fromJust :: Maybe a -> a -fromMaybe :: a -> Maybe a -> a -isJust :: Maybe a -> Bool -isNothing :: Maybe a -> Bool -listToMaybe :: [a] -> Maybe a -mapMaybe :: (a -> Maybe b) -> [a] -> [b] -maybe :: b -> (a -> b) -> Maybe a -> b -maybeToList :: Maybe a -> [a] -data Maybe a = Nothing | Just a --- via readFile -(True,False) -id :: a -> a +-- testing ghci multiline commands :{ .. :} +-- via stdin +(1,2,3) +-- layout rule instead of explicit braces and semicolons works too +(1,2,3) +catMaybes :: [Maybe a] -> [a] +fromJust :: Maybe a -> a +fromMaybe :: a -> Maybe a -> a +isJust :: Maybe a -> Bool +isNothing :: Maybe a -> Bool +listToMaybe :: [a] -> Maybe a +mapMaybe :: (a -> Maybe b) -> [a] -> [b] +maybe :: b -> (a -> b) -> Maybe a -> b +maybeToList :: Maybe a -> [a] +data Maybe a = Nothing | Just a +-- via readFile +(True,False) +id :: a -> a -- GitLab