X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=docs%2Fusers_guide%2Fghci.xml;h=54ce6b7c74ea8d558c4abd48c21560fe49e90775;hb=45b04f650e6985c524301112a8381a1aff808fc6;hp=d82127d0faf6a451cac793c5e6c7a321bb819cd0;hpb=2f4e21c6f741995e20cc3b53b109ff9edf18eb3c;p=ghc-hetmet.git diff --git a/docs/users_guide/ghci.xml b/docs/users_guide/ghci.xml index d82127d..54ce6b7 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. @@ -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 @@ -1206,9 +1261,7 @@ li - [Just 1,(_t6::Maybe Integer),Just 3,(_t7::Maybe Integer),Just 4] The example uses :print and :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. + the list to compute its result, but without evaluating the individual elements. @@ -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 @@ -1342,9 +1395,7 @@ 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. + the list to compute its result, but without evaluating the individual elements. @@ -1780,22 +1831,90 @@ x :: Int - Limitations - - - - Implicit parameters (see ) are only available - at the scope of a breakpoint if there is a explicit type signature. - - - - - Modules compiled by GHCi under the -fdebugging - flag will perform slower: the debugging mode introduces some overhead. - Modules compiled to object code by ghc are not affected. - - - + Debugging Higher-Order functions + It is possible to use the debugger to examine lambdas. + When we are at a breakpoint and a lambda is in scope, the debugger cannot show + you the source code that constitutes it; however, it is possible to get some + information by applying it to some arguments and observing the result. + + The process is slightly complicated when the binding is polymorphic. + We will use a example to show the process. + To keep it simple, we will use the well known map function: + +import Prelude hiding (map) + +map :: (a->b) -> a -> b +map f [] = [] +map f (x:xs) = f x : map f xs + + We set a breakpoint on map, and call it. + +*Main> :break map +Breakpoint 0 activated at map.hs:(4,0)-(5,12) +*Main> map Just [1..5] +Stopped at map.hs:(4,0)-(5,12) +_result :: [b] +x :: a +f :: a -> b +xs :: [a] + + GHCi tells us that, among other bindings, f is in scope. + However, its type is not fully known yet, + and thus it is not possible to apply it yet to any + arguments. Nevertheless, observe that the type of its first argument is the + same as the type of x, and its result type is the + same as the type of _result. + The debugger has some intelligence built-in to update the type of + f whenever the types of x or + _result are reconstructed. So what we do in this scenario is + force x a bit, in order to recover both its type + and the argument part of f. + +*Main> seq x () +*Main> :print x +x = 1 + + We can check now that as expected, the type of x + has been reconstructed, and with it the + type of f has been too: + +*Main> :t x +x :: Integer +*Main> :t f +f :: Integer -> b + + From here, we can apply f to any argument of type Integer and observe the + results. + let b = f 10 +*Main> :t b +b :: b +*Main> b +:1:0: + Ambiguous type variable `b' in the constraint: + `Show b' arising from a use of `print' at :1:0 +*Main> :p b +b = (_t2::a) +*Main> seq b () +() +*Main> :t b +b :: a +*Main> :p b +b = Just 10 +*Main> :t b +b :: Maybe Integer +*Main> :t f +f :: Integer -> Maybe Integer +*Main> f 20 +Just 20 +*Main> map f [1..5] +[Just 1, Just 2, Just 3, Just 4, Just 5] + ]]> + In the first application of f, we had to do + some more type reconstruction + in order to recover the result type of f. + But after that, we are free to use + f normally. Tips @@ -1814,8 +1933,8 @@ x :: Int type MyLongType a = [Maybe [Maybe a]] -main:Main> :m +GHC.Exts -main:Main> main +*Main> :m +GHC.Exts +*Main> main Local bindings in scope: x :: a Main.hs:15> let x' = unsafeCoerce x :: MyLongType Bool @@ -1825,7 +1944,7 @@ 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, @@ -1834,7 +1953,25 @@ Main.hs:15> x' - + + Limitations + + + + Implicit parameters (see ) are only available + at the scope of a breakpoint if there is a explicit type signature. + + + + + Modules compiled by GHCi under the -fdebugging + flag will perform slower: the debugging mode introduces some overhead. + Modules compiled to object code by ghc are not affected. + + + + + The <filename>.ghci</filename> file .ghcifile @@ -1897,6 +2034,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