Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge branch 'master' of http://darcs.haskell.org/ghc

  • Loading branch information...
commit 5ac223a73611763a3b24570120755a1a9742c22f 2 parents 8838983 + 90d70e4
igfoo igfoo authored
90 compiler/coreSyn/CorePrep.lhs
@@ -5,6 +5,7 @@
5 5 Core pass to saturate constructors and PrimOps
6 6
7 7 \begin{code}
  8 +{-# LANGUAGE BangPatterns #-}
8 9 module CorePrep (
9 10 corePrepPgm, corePrepExpr
10 11 ) where
@@ -19,7 +20,6 @@ import CoreMonad ( endPass, CoreToDo(..) )
19 20 import CoreSyn
20 21 import CoreSubst
21 22 import MkCore
22   -import OccurAnal ( occurAnalyseExpr )
23 23 import Type
24 24 import Literal
25 25 import Coercion
@@ -288,23 +288,29 @@ After specialisation and SpecConstr, we would get something like this:
288 288 g$Bool_True_Just = ...
289 289 g$Unit_Unit_Just = ...
290 290
291   -Note that the g$Bool and g$Unit functions are actually dead code: they are only kept
292   -alive by the occurrence analyser because they are referred to by the rules of g,
293   -which is being kept alive by the fact that it is used (unspecialised) in the returned pair.
  291 +Note that the g$Bool and g$Unit functions are actually dead code: they
  292 +are only kept alive by the occurrence analyser because they are
  293 +referred to by the rules of g, which is being kept alive by the fact
  294 +that it is used (unspecialised) in the returned pair.
294 295
295   -However, at the CorePrep stage there is no way that the rules for g will ever fire,
296   -and it really seems like a shame to produce an output program that goes to the trouble
297   -of allocating a closure for the unreachable g$Bool and g$Unit functions.
  296 +However, at the CorePrep stage there is no way that the rules for g
  297 +will ever fire, and it really seems like a shame to produce an output
  298 +program that goes to the trouble of allocating a closure for the
  299 +unreachable g$Bool and g$Unit functions.
298 300
299 301 The way we fix this is to:
300 302 * In cloneBndr, drop all unfoldings/rules
301   - * In deFloatTop, run the occurrence analyser on each top-level RHS to drop
302   - the dead local bindings
303   -
304   -The reason we don't just OccAnal the whole output of CorePrep is that the tidier
305   -ensures that all top-level binders are GlobalIds, so they don't show up in the free
306   -variables any longer. So if you run the occurrence analyser on the output of CoreTidy
307   -(or later) you e.g. turn this program:
  303 + * In deFloatTop, run a simple dead code analyser on each top-level RHS to drop
  304 + the dead local bindings. (we used to run the occurrence analyser to do
  305 + this job, but the occurrence analyser sometimes introduces new let
  306 + bindings for case binders, which lead to the bug in #5433, hence we
  307 + now have a special-purpose dead code analyser).
  308 +
  309 +The reason we don't just OccAnal the whole output of CorePrep is that
  310 +the tidier ensures that all top-level binders are GlobalIds, so they
  311 +don't show up in the free variables any longer. So if you run the
  312 +occurrence analyser on the output of CoreTidy (or later) you e.g. turn
  313 +this program:
308 314
309 315 Rec {
310 316 f = ... f ...
@@ -1006,8 +1012,60 @@ deFloatTop (Floats _ floats)
1006 1012 get b _ = pprPanic "corePrepPgm" (ppr b)
1007 1013
1008 1014 -- See Note [Dead code in CorePrep]
1009   - occurAnalyseRHSs (NonRec x e) = NonRec x (occurAnalyseExpr e)
1010   - occurAnalyseRHSs (Rec xes) = Rec [(x, occurAnalyseExpr e) | (x, e) <- xes]
  1015 + occurAnalyseRHSs (NonRec x e) = NonRec x (fst (dropDeadCode e))
  1016 + occurAnalyseRHSs (Rec xes) = Rec [ (x, fst (dropDeadCode e))
  1017 + | (x, e) <- xes]
  1018 +
  1019 +---------------------------------------------------------------------------
  1020 +-- Simple dead-code analyser, see Note [Dead code in CorePrep]
  1021 +
  1022 +dropDeadCode :: CoreExpr -> (CoreExpr, VarSet)
  1023 +dropDeadCode (Var v)
  1024 + = (Var v, if isLocalId v then unitVarSet v else emptyVarSet)
  1025 +dropDeadCode (App fun arg)
  1026 + = (App fun' arg', fun_fvs `unionVarSet` arg_fvs)
  1027 + where !(fun', fun_fvs) = dropDeadCode fun
  1028 + !(arg', arg_fvs) = dropDeadCode arg
  1029 +dropDeadCode (Lam v e)
  1030 + = (Lam v e', delVarSet fvs v)
  1031 + where !(e', fvs) = dropDeadCode e
  1032 +dropDeadCode (Let (NonRec v rhs) body)
  1033 + | v `elemVarSet` body_fvs
  1034 + = (Let (NonRec v rhs') body', rhs_fvs `unionVarSet` (body_fvs `delVarSet` v))
  1035 + | otherwise
  1036 + = (body', body_fvs) -- drop the dead let bind!
  1037 + where !(body', body_fvs) = dropDeadCode body
  1038 + !(rhs', rhs_fvs) = dropDeadCode rhs
  1039 +dropDeadCode (Let (Rec prs) body)
  1040 + | any (`elemVarSet` all_fvs) bndrs
  1041 + -- approximation: strictly speaking we should do SCC analysis here,
  1042 + -- but for simplicity we just look to see whether any of the binders
  1043 + -- is used and drop the entire group if all are unused.
  1044 + = (Let (Rec (zip bndrs rhss')) body', all_fvs `delVarSetList` bndrs)
  1045 + | otherwise
  1046 + = (body', body_fvs) -- drop the dead let bind!
  1047 + where !(body', body_fvs) = dropDeadCode body
  1048 + !(bndrs, rhss) = unzip prs
  1049 + !(rhss', rhs_fvss) = unzip (map dropDeadCode rhss)
  1050 + all_fvs = unionVarSets (body_fvs : rhs_fvss)
  1051 +
  1052 +dropDeadCode (Case scrut bndr t alts)
  1053 + = (Case scrut' bndr t alts', scrut_fvs `unionVarSet` alts_fvs)
  1054 + where !(scrut', scrut_fvs) = dropDeadCode scrut
  1055 + !(alts', alts_fvs) = dropDeadCodeAlts alts
  1056 +dropDeadCode (Cast e c)
  1057 + = (Cast e' c, fvs)
  1058 + where !(e', fvs) = dropDeadCode e
  1059 +dropDeadCode (Note n e)
  1060 + = (Note n e', fvs)
  1061 + where !(e', fvs) = dropDeadCode e
  1062 +dropDeadCode e = (e, emptyVarSet) -- Lit, Type, Coercion
  1063 +
  1064 +dropDeadCodeAlts :: [CoreAlt] -> ([CoreAlt], VarSet)
  1065 +dropDeadCodeAlts alts = (alts', unionVarSets fvss)
  1066 + where !(alts', fvss) = unzip (map do_alt alts)
  1067 + do_alt (c, vs, e) = ((c,vs,e'), fvs `delVarSetList` vs)
  1068 + where !(e', fvs) = dropDeadCode e
1011 1069
1012 1070 -------------------------------------------
1013 1071 canFloatFromNoCaf :: Floats -> CpeRhs -> Maybe (Floats, CpeRhs)
17 compiler/main/HscMain.lhs
@@ -83,6 +83,7 @@ import VarSet
83 83 import VarEnv ( emptyTidyEnv )
84 84 import Panic
85 85 import Class
  86 +import Data.List
86 87 #endif
87 88
88 89 import Id
@@ -145,7 +146,7 @@ import Bag
145 146 import Exception
146 147
147 148 import Control.Monad
148   -import Data.Maybe ( catMaybes )
  149 +import Data.Maybe
149 150 import Data.IORef
150 151 \end{code}
151 152 #include "HsVersions.h"
@@ -1364,16 +1365,26 @@ hscDeclsWithLocation hsc_env str source linenumber = runHsc hsc_env $ do
1364 1365 let
1365 1366 tcs = filter (not . isImplicitTyCon) $ mg_tcs simpl_mg
1366 1367 clss = mg_clss simpl_mg
1367   - tythings = map ATyCon tcs ++ map (ATyCon . classTyCon) clss
1368   - sys_vars = filter (isExternalName . idName) $
  1368 +
  1369 + ext_vars = filter (isExternalName . idName) $
1369 1370 bindersOfBinds (cg_binds tidy_cg)
  1371 +
  1372 + (sys_vars, user_vars) = partition is_sys_var ext_vars
  1373 + is_sys_var id = isDFunId id
  1374 + || isRecordSelector id
  1375 + || isJust (isClassOpId_maybe id)
1370 1376 -- we only need to keep around the external bindings
1371 1377 -- (as decided by TidyPgm), since those are the only ones
1372 1378 -- that might be referenced elsewhere.
1373 1379
  1380 + tythings = map AnId user_vars
  1381 + ++ map ATyCon tcs
  1382 + ++ map (ATyCon . classTyCon) clss
  1383 +
1374 1384 -- pprTrace "new tycons" (ppr tcs) $ return ()
1375 1385 -- pprTrace "new classes" (ppr clss) $ return ()
1376 1386 -- pprTrace "new sys Ids" (ppr sys_vars) $ return ()
  1387 + -- pprTrace "new user Ids" (ppr user_vars) $ return ()
1377 1388
1378 1389 let ictxt1 = extendInteractiveContext icontext tythings
1379 1390 ictxt = ictxt1 {
176 docs/users_guide/ghci.xml
@@ -38,12 +38,11 @@ Prelude>
38 38
39 39 <para>There may be a short pause while GHCi loads the prelude and
40 40 standard libraries, after which the prompt is shown. As the banner
41   - says, you can type <literal>:?</literal> to see the list of commands
42   - available, and a half line description of each of them.</para>
43   -
44   - <para>We'll explain most of these commands as we go along. For
45   - Hugs users: many things work the same as in Hugs, so you should be
46   - able to get going straight away.</para>
  41 + says, you can type <literal>:?</literal> to see the list of
  42 + commands available, and a half line description of each of them.
  43 + We'll explain most of these commands as we go along, and there is
  44 + complete documentation for all the commands in
  45 + <xref linkend="ghci-commands" />.</para>
47 46
48 47 <para>Haskell expressions can be typed at the prompt:</para>
49 48 <indexterm><primary>prompt</primary><secondary>GHCi</secondary>
@@ -61,49 +60,12 @@ Prelude>
61 60 The expression may not span several lines - as soon as you press enter,
62 61 GHCi will attempt to evaluate it.</para>
63 62
64   - <para>GHCi also has a multiline mode,
65   - <indexterm><primary><literal>:set +m</literal></primary></indexterm>,
66   - which is terminated by an empty line:</para>
67   -
68   -<screen>
69   -Prelude> :set +m
70   -Prelude> let x = 42 in x / 9
71   -Prelude|
72   -4.666666666666667
73   -Prelude>
74   -</screen>
75   -
76 63 <para>In Haskell, a <literal>let</literal> expression is followed
77 64 by <literal>in</literal>. However, in GHCi, since the expression
78 65 can also be interpreted in the <literal>IO</literal> monad,
79 66 a <literal>let</literal> binding with no accompanying
80 67 <literal>in</literal> statement can be signalled by an empty line,
81 68 as in the above example.</para>
82   -
83   - <para>Multiline mode is useful when entering monadic
84   - <literal>do</literal> statements:</para>
85   -
86   -<screen>
87   -Control.Monad.State> flip evalStateT 0 $ do
88   -Control.Monad.State| i &lt;- get
89   -Control.Monad.State| lift $ do
90   -Control.Monad.State| putStrLn "Hello World!"
91   -Control.Monad.State| print i
92   -Control.Monad.State|
93   -"Hello World!"
94   -0
95   -Control.Monad.State>
96   -</screen>
97   -
98   - <para>During a multiline interaction, the user can interrupt and
99   - return to the top-level prompt.</para>
100   -
101   -<screen>
102   -Prelude> do
103   -Prelude| putStrLn "Hello, World!"
104   -Prelude| ^C
105   -Prelude>
106   -</screen>
107 69 </sect1>
108 70
109 71 <sect1 id="loading-source-files">
@@ -570,6 +532,132 @@ xs :: [Integer]
570 532
571 533 </sect2>
572 534
  535 + <sect2 id="ghci-multiline">
  536 + <title>Multiline input</title>
  537 +
  538 + <para>Apart from the <literal>:{ ... :}</literal> syntax for
  539 + multi-line input mentioned above, GHCi also has a multiline
  540 + mode, enabled by <literal>:set +m</literal>,
  541 + <indexterm><primary><literal>:set +m</literal></primary></indexterm>
  542 + in which GHCi detects automatically when the current statement
  543 + is unfinished and allows further lines to be added. A
  544 + multi-line input is terminated with an empty line. For example:</para>
  545 +
  546 +<screen>
  547 +Prelude> :set +m
  548 +Prelude> let x = 42
  549 +Prelude|
  550 +</screen>
  551 +
  552 + <para>Further bindings can be added to
  553 + this <literal>let</literal> statement, so GHCi indicates that
  554 + the next line continues the previous one by changing the
  555 + prompt. Note that layout is in effect, so to add more bindings
  556 + to this <literal>let</literal> we have to line them up:</para>
  557 +
  558 +<screen>
  559 +Prelude> :set +m
  560 +Prelude> let x = 42
  561 +Prelude| y = 3
  562 +Prelude|
  563 +Prelude>
  564 +</screen>
  565 +
  566 + <para>Explicit braces and semicolons can be used instead of
  567 + layout, as usual:</para>
  568 +
  569 +<screen>
  570 +Prelude> do {
  571 +Prelude| putStrLn "hello"
  572 +Prelude| ;putStrLn "world"
  573 +Prelude| }
  574 +hello
  575 +world
  576 +Prelude>
  577 +</screen>
  578 +
  579 + <para>Note that after the closing brace, GHCi knows that the
  580 + current statement is finished, so no empty line is required.</para>
  581 +
  582 + <para>Multiline mode is useful when entering monadic
  583 + <literal>do</literal> statements:</para>
  584 +
  585 +<screen>
  586 +Control.Monad.State> flip evalStateT 0 $ do
  587 +Control.Monad.State| i &lt;- get
  588 +Control.Monad.State| lift $ do
  589 +Control.Monad.State| putStrLn "Hello World!"
  590 +Control.Monad.State| print i
  591 +Control.Monad.State|
  592 +"Hello World!"
  593 +0
  594 +Control.Monad.State>
  595 +</screen>
  596 +
  597 + <para>During a multiline interaction, the user can interrupt and
  598 + return to the top-level prompt.</para>
  599 +
  600 +<screen>
  601 +Prelude> do
  602 +Prelude| putStrLn "Hello, World!"
  603 +Prelude| ^C
  604 +Prelude>
  605 +</screen>
  606 + </sect2>
  607 +
  608 + <sect2 id="ghci-decls">
  609 + <title>Type, class and other declarations</title>
  610 +
  611 + <para>[<emphasis role="bold">New in version 7.4.1</emphasis>] At the GHCi
  612 + prompt you can also enter any top-level Haskell declaration,
  613 + including <literal>data</literal>, <literal>type</literal>, <literal>newtype</literal>, <literal>class</literal>, <literal>instance</literal>, <literal>deriving</literal>,
  614 + and <literal>foreign</literal> declarations. For
  615 + example:</para>
  616 +
  617 +<screen>
  618 +Prelude> data T = A | B | C deriving (Eq, Ord, Show, Enum)
  619 +Prelude> [A ..]
  620 +[A,B,C]
  621 +Prelude> :i T
  622 +data T = A | B | C -- Defined at &lt;interactive>:2:6
  623 +instance Enum T -- Defined at &lt;interactive>:2:45
  624 +instance Eq T -- Defined at &lt;interactive>:2:30
  625 +instance Ord T -- Defined at &lt;interactive>:2:34
  626 +instance Show T -- Defined at &lt;interactive>:2:39
  627 +</screen>
  628 +
  629 + <para>As with ordinary variable bindings, later definitions shadow
  630 + earlier ones, so you can re-enter a declaration to fix a problem
  631 + with it or extend it. But there's a gotcha: when a new type
  632 + declaration shadows an older one, there might be other
  633 + declarations that refer to the old type. The thing to remember is
  634 + that the old type still exists, and these other declarations still
  635 + refer to the old type. However, while the old and the new type
  636 + have the same name, GHCi will treat them as distinct. For
  637 + example:</para>
  638 +
  639 +<screen>
  640 +Prelude> data T = A | B
  641 +Prelude> let f A = True; f B = False
  642 +Prelude> data T = A | B | C
  643 +Prelude> f A
  644 +
  645 +&lt;interactive>:2:3:
  646 + Couldn't match expected type `main::Interactive.T'
  647 + with actual type `T'
  648 + In the first argument of `f', namely `A'
  649 + In the expression: f A
  650 + In an equation for `it': it = f A
  651 +Prelude>
  652 +</screen>
  653 +
  654 + <para>The old, shadowed, version of <literal>T</literal> is
  655 + displayed as <literal>main::Interactive.T</literal> by GHCi in
  656 + an attempt to distinguish it from the new <literal>T</literal>,
  657 + which is displayed as simply <literal>T</literal>.</para>
  658 +
  659 + </sect2>
  660 +
573 661 <sect2 id="ghci-scope">
574 662 <title>What's really in scope at the prompt?</title>
575 663
@@ -2768,7 +2856,7 @@ bar
2768 2856 <listitem>
2769 2857 <para>Enable parsing of multiline commands. A multiline command
2770 2858 is prompted for when the current input line contains open layout
2771   - contexts.</para>
  2859 + contexts (see <xref linkend="ghci-multiline" />).</para>
2772 2860 </listitem>
2773 2861 </varlistentry>
2774 2862
3  ghc/InteractiveUI.hs
@@ -728,7 +728,8 @@ enqueueCommands cmds = do
728 728 -- | If we one of these strings prefixes a command, then we treat it as a decl
729 729 -- rather than a stmt.
730 730 declPrefixes :: [String]
731   -declPrefixes = ["class ","data ","newtype ","type ","instance ", "deriving "]
  731 +declPrefixes = ["class ","data ","newtype ","type ","instance ", "deriving ",
  732 + "foreign "]
732 733
733 734 runStmt :: String -> SingleStep -> GHCi Bool
734 735 runStmt stmt step

0 comments on commit 5ac223a

Please sign in to comment.
Something went wrong with that request. Please try again.