X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=docs%2Fusers_guide%2Fghci.xml;h=aaa9c460931aaebe4af10f9366fb8405e30d2b95;hb=708aa86d707f71db9430d41b4e8188c030012597;hp=5f38b376245036e51cd2109888346f327c00df2e;hpb=26680cab67bdc2cd964530970a552f1f45b49459;p=ghc-hetmet.git diff --git a/docs/users_guide/ghci.xml b/docs/users_guide/ghci.xml index 5f38b37..aaa9c46 100644 --- a/docs/users_guide/ghci.xml +++ b/docs/users_guide/ghci.xml @@ -18,7 +18,7 @@ FFIGHCi support Foreign Function InterfaceGHCi support - + Introduction to GHCi Let's start with an example GHCi session. You can fire up @@ -106,7 +106,7 @@ Prelude> enter, GHCi will attempt to evaluate it. - + Loading source files Suppose we have the following Haskell source code, which we @@ -363,7 +363,7 @@ Ok, modules loaded: A, B, C, D. - + Interactive evaluation at the prompt When you type an expression at the prompt, GHCi immediately @@ -747,24 +747,79 @@ it <- e standard rules take each group of constraints (C1 a, C2 a, ..., Cn a) for each type variable a, and defaults the type variable if - - The type variable a - appears in no other constraints - All the classes Ci are standard. - At least one of the classes Ci is - numeric. - - At the GHCi prompt, the second and third rules are relaxed as follows - (differences italicised): - - All of the classes - Ci are single-parameter type classes. - At least one of the classes Ci is - numeric, or is Show, - Eq, or Ord. - - The same type-default behaviour can be enabled in an ordinary Haskell - module, using the flag -fextended-default-rules. + + + + The type variable a appears in no + other constraints + + + + + All the classes Ci are standard. + + + + + At least one of the classes Ci is + numeric. + + + + At the GHCi prompt, or with GHC if the + -fextended-default-rules flag is given, + the following additional differences apply: + + + + Rule 2 above is relaxed thus: + All of the classes + Ci are single-parameter type classes. + + + + + Rule 3 above is relaxed this: + At least one of the classes Ci is + numeric, or is Show, + Eq, or + Ord. + + + + + The unit type () is added to the + start of the standard list of types which are tried when + doing type defaulting. + + + + The last point means that, for example, this program: + +main :: IO () +main = print def + +instance Num () + +def :: (Num a, Enum a) => a +def = toEnum 0 + + prints () rather than 0 as the + type is defaulted to () rather than + Integer. + + + The motivation for the change is that it means IO a + actions default to IO (), which in turn means that + ghci won't try to print a result when running them. This is + particularly important for printf, which has an + instance that returns IO a. + However, it is only able to return + undefined + (the reason for the instance having this type is to not require + extensions to the class system), so if the type defaults to + Integer then ghci gives an error when running a + printf. @@ -966,7 +1021,7 @@ $ ghci -lm :continue :continue - Shortcut to :breakpoint continue + Shortcut to :breakpoint continue @@ -1189,9 +1244,9 @@ li - (_t1::[Maybe Integer]) Prelude> head li Just 1 Prelude> :sp li -li - [Just 1 | _] +li - Just 1 : _ Prelude> :p li -li - [Just 1 | (_t2::[Maybe Integer])] +li - Just 1 : (_t2::[Maybe Integer]) Prelude> last li Just 5 Prelude> :sp li @@ -1207,8 +1262,6 @@ li - [Just 1,(_t6::Maybe Integer),Just 3,(_t7::Maybe Integer),Just 4] to help us observe how the li variable is evaluated progressively as we operate with it. Note for instance how last traverses all the elements of the list to compute its result, but without evaluating the individual elements. - Finally note that the Prolog convention of [head | tail] is used by - :sprint to display unevaluated lists. @@ -1334,7 +1387,7 @@ li - _ Prelude> head li Just 1 Prelude> :sp li -li - [Just 1 | _] +li - Just 1 : _ Prelude> last li Just 5 Prelude> :sp li @@ -1343,8 +1396,6 @@ li - [Just 1,_,_,_,Just 5] The example uses :sprint to help us observe how the li variable is evaluated progressively as we operate with it. Note for instance how last traverses all the elements of the list to compute its result, but without evaluating the individual elements. - Finally note that the Prolog convention of [head | tail] is used by - :sprint to display unevaluated lists. @@ -1623,7 +1674,7 @@ Local bindings in scope: qsort2.hs:2:15-46> What is happening here is that GHCi has interrupted the evaluation of - qsort at the breakpoint set in line 2, as the prompt indicates. + qsort at the breakpoint set in line 2, as the prompt indicates. At this point you can freely explore the contents of the bindings in scope, but with two catches. First, take into account that due to the lazy nature of Haskell, some of @@ -1631,9 +1682,9 @@ qsort2.hs:2:15-46> trigger a computation. Second: look at the types of the things in scope. GHCi has left its types parameterised by a variable! - Look at the type of qsort, which is + Look at the type of qsort, which is polymorphic on the type of its argument. It does not - tell us really what the types of x and xs can be. + tell us really what the types of x and xs can be. In general, polymorphic programs deal with polymorphic values, and this means that some of the bindings available in a breakpoint site will be parametrically typed. @@ -1644,11 +1695,11 @@ qsort2.hs:2:15-46> This is useful because you cannot just type x in the prompt and expect GHCi to return you its value. Perhaps you know for sure that - x is of type Int, which is an instance of - Show, but GHCi does not have this information. - :print however is fine, because it does not need to know the + x is of type Int, which is an instance of + Show, but GHCi does not have this information. + :print however is fine, because it does not need to know the type to do its work. - Let's go on with the debugging session of the qsort + Let's go on with the debugging session of the qsort example: A short debugging session @@ -1674,22 +1725,22 @@ x :: Int GHCi reminds us that this value is untyped, and instructs us to force its evaluation - This line forces the evaluation of x + This line forces the evaluation of x Even though x has been evaluated, we cannot simply use its name to see its value! This is a bit counterintuitive, but currently in GHCi the type of a binding - cannot be a type variable a. - Thus, the binding x gets assigned the concrete type Unknown. + cannot be a type variable a. + Thus, the binding x gets assigned the concrete type Unknown. - We can explore x using the :print - command, which does find out that x is of type Int and prints + We can explore x using the :print + command, which does find out that x is of type Int and prints its value accordingly. - :print also updates the type of x with - the most concrete type information available. + :print also updates the type of x with + the most concrete type information available. The example shows the standard way to proceeed with polymorphic values in a breakpoint. @@ -1784,7 +1835,7 @@ x :: Int - are only available + Implicit parameters (see ) are only available at the scope of a breakpoint if there is a explicit type signature. @@ -1804,13 +1855,13 @@ x :: Int {-# OPTIONS_GHC -fdebugging #-} - * Repeated use of seq and + * Repeated use of seq and :print may be necessary to observe unevaluated untyped bindings see - * GHC.Exts.unsafeCoerce can help if you are positive about the type of a binding + * GHC.Exts.unsafeCoerce can help if you are positive about the type of a binding type MyLongType a = [Maybe [Maybe a]] @@ -1825,11 +1876,11 @@ Main.hs:15> x' Note that a wrong coercion will likely result in your debugging session being interrupted by a segmentation fault - * The undocumented (and unsupported) :force command + * The :force command equivalent to :print with automatic - seq forcing, - may prove useful to replace sequences of seq and + seq forcing, + may prove useful to replace sequences of seq and :print in some situations. @@ -1897,6 +1948,32 @@ Main.hs:15> x' + + Compiling to object code inside GHCi + + By default, GHCi compiles Haskell source code into byte-code + that is interpreted by the runtime system. GHCi can also compile + Haskell code to object code: to turn on this feature, use the + flag either on the command line or + with :set (the option + restores byte-code compilation + again). Compiling to object code takes longer, but typically the + code will execute 10-20 times faster than byte-code. + + Compiling to object code inside GHCi is particularly useful + if you are developing a compiled application, because the + :reload command typically runs much faster than + restarting GHC with from the command-line, + because all the interface files are already cached in + memory. + + There are disadvantages to compiling to object-code: you + can't set breakpoints in object-code modules, for example. Only + the exports of an object-code module will be visible in GHCi, + rather than all top-level bindings as in interpreted + modules. + + FAQ and Things To Watch Out For @@ -1975,7 +2052,7 @@ Main.hs:15> x' I can't use Control-C to interrupt computations in GHCi on Windows. - See . + See