Vanson Samuel
Vanson Samuel

Reputation: 2089

GHCi environment dump

Is there is way in GHCi to basically get a state dump? By this I mean a list of:

Assuming that this is possible, is it also possible to do this at runtime, say during an exception?

Upvotes: 6

Views: 359

Answers (2)

hammar
hammar

Reputation: 139840

:browse will give you most of this information. It shows

  • Type signatures for functions and operators.
  • Classes and their methods.
  • Data types, newtypes and type synonyms, with constructors if they are in scope.

Without any arguments, it shows this information for the currently loaded module. You can also specify a different module.

Prelude> :browse Control.Applicative
class (Functor f) => Applicative f where
  pure :: a -> f a
  (<*>) :: f (a -> b) -> f a -> f b
  (*>) :: f a -> f b -> f b
  (<*) :: f a -> f b -> f a
...

To see more detail, including precedence and associativity for operators, as well as instances for a data type, use :info.

Prelude> :info (^)
(^) :: (Num a, Integral b) => a -> b -> a   -- Defined in GHC.Real
infixr 8 ^
Prelude> :info Bool
data Bool = False | True    -- Defined in GHC.Bool
instance Bounded Bool -- Defined in GHC.Enum
instance Enum Bool -- Defined in GHC.Enum
instance Eq Bool -- Defined in GHC.Base
instance Ord Bool -- Defined in GHC.Base
instance Read Bool -- Defined in GHC.Read

These commands are also available while debugging.

For more information, type :help or see the GHCi chapter of the GHC user's guide.

Upvotes: 4

HaskellElephant
HaskellElephant

Reputation: 9891

There are ways of getting this information. The problem is that, as far as I know, there is no way of filtering it exactly the way you want, but anyways here it is:

  • If you want to see all identifiers, including classes, operators, Constructors, and Types, currently defined in ghci just press tab while you have the cursor on whitespace.

  • If you want to know the precedence and associativity of an operator * just use

    :i *
    
  • If you want to see what classes M is instance of just use

    :i M
    
  • If you want to see the signature of a function f just use

    :i f
    

    If you write :set -fbreak-on-exception then ghci will break rather then fail when an exception is thrown and then you can use all of the above commands during an exception.

Upvotes: 1

Related Questions