From 1b92395bcb40f110fc3dd6e261692e28e6bbe328 Mon Sep 17 00:00:00 2001 From: Pepe Iborra Date: Thu, 26 Apr 2007 10:30:14 +0000 Subject: [PATCH] Update an example on the ghci debugger section --- docs/users_guide/ghci.xml | 77 +++++++++++++++++++++++---------------------- 1 file changed, 39 insertions(+), 38 deletions(-) diff --git a/docs/users_guide/ghci.xml b/docs/users_guide/ghci.xml index 9775c34..62a9c3b 100644 --- a/docs/users_guide/ghci.xml +++ b/docs/users_guide/ghci.xml @@ -1657,17 +1657,19 @@ main = do { Simply use ghci to evaluate your Haskell expressions and whenever a breakpoint is hit, the debugger will enter the stage: -*main:Main> :break add Main 2 -Breakpoint set at (2,15) - -*main:Main> qsort [10,9..1] -Local bindings in scope: - x :: a, xs :: [a], left :: [a], right :: [a] - -qsort2.hs:2:15-46> +*main:Main> :break qsort +Breakpoint 0 activated at ../QSort.hs:(4,0)-(6,54) +*QSort> qsort [10,9,1] +Stopped at ../QSort.hs:(4,0)-(6,54) +_result :: [a] +xs :: [a] +x :: a +left :: [a] +right :: [a] +[../QSort.hs:(4,0)-(6,54)] *QSort> 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, 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 @@ -1677,66 +1679,65 @@ qsort2.hs:2:15-46> GHCi has left its types parameterised by a variable! 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. - In general, polymorphic programs deal with polymorphic values, + tell us really what the types of x + and xs can be. + In general, polymorphic functions deal with polymorphic values, and this means that some of the bindings available in a breakpoint site will be parametrically typed. So, what can we do with a value without concrete type? Very few interesting - things. The :print command in ghci allows you to + things, not even using show on it. + The :print command in ghci allows you to explore its contents and see if it is evaluated or not. - 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 - type to do its work. + :print works here because it does not need the + type information to do its work. In fact, as we will see later, + :print can even recover the missing type information. + Let's go on with the debugging session of the qsort example: A short debugging session qsort2.hs:2:15-46> x -This is an untyped, unevaluated computation. You can use seq to -force its evaluation and then :print to recover its type +<interactive>:1:0: + Ambiguous type variable `a' in the constraint: + `Show a' arising from a use of `print' at <interactive>:1:0 qsort2.hs:2:15-46> seq x () () qsort2.hs:2:15-46> x -This is an untyped, unevaluated computation. You can use seq to -force its evaluation and then :print to recover its type - +<interactive>:1:0: + Ambiguous type variable `a' in the constraint: + `Show a' arising from a use of `print' at <interactive>:1:0 qsort2.hs:2:15-46> :t x -x :: GHC.Base.Unknown -qsort2.hs:2:15-46> :p x -x - 10 +x :: a +qsort2.hs:2:15-46> :print x +x = 10 qsort2.hs:2:15-46> :t x -x :: Int +x :: Integer - GHCi reminds us that this value is untyped, and instructs us to force its evaluation + GHCi reminds us that x is untyped 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. + Even though x has been evaluated, + we have not updated its type yet. We can explore x using the :print - command, which does find out that x is of type Int and prints - its value accordingly. + command, which does find out that x is of type Int and + prints its value. - :print also updates the type of x with - the most concrete type information available. + In addition, :print also updates + its type information. - The example shows the standard way to proceeed with polymorphic values in a breakpoint. + + This example shows the standard way to proceeed with polymorphic values in a breakpoint. Commands -- 1.7.10.4