Haskell : display/get list of all user defined functions - haskell

Is there a command in Haskell which displays (or get as a list of) all the user defined functions which have been loaded/defined in the GHCi? Thanks

To see bindings you've made at the ghci prompt (e.g. with let or <-), try :show bindings.
If you've loaded some modules, you can use :show modules to get the names of loaded modules and then :browse ModuleName to list everything in scope from that module.

When in ghci, use :browse or just :bro after loading the file. You may also browse unloaded modules via :browse Foo.Bar.Baz.

Related

How to reload a file in ghci without modifying the source code?

I am using ghc-8.10.2. When I compile a source file (which contains many top bindings and a main function, without a module declaration) into a executable, then type ghci to use the interpreter of ghc, load the source file with the :load filename command. I found that only the main top-binding is visible in ghci environment, other top bindings can be refereed unless I have made some meaningless modification to the source file, and reload it, I have tried to reload the file without making changes in the source file, even with the :reload command, but it doesn't work, so I want to ask if there is a command to force reload a module at any time.
Up to the Haskell Report
An abbreviated form of module, consisting only of the module body, is permitted. If this is used, the header is assumed to be ‘module Main(main) where’.
Meaning that if you don't use a module declaration, by default you only export the main function from it. That's why you can only load such function. If you want to avoid this start your file with
module Main where
...
:r should be it, instead of :reload that other people seem to have used.

Is it possible join two modules as top-level in GHCI?

I discovered that changing order or paths given to :load changes visible bindings and it seems impossible to debug multiple modules at the same time. It is especially annoying as I lose bindings every time I :load.
It seems there can be only one module in *-form as suggested by wording of "the most recently successfuly loaded module". Despite that, the top level module seems to one given as first argument to :load.
When I'm developing and I want to use two or more modules at once in GHCi, here's what I do. Let's say I want to use Control.Monad and Control.Lens, and my own module which I'm programming with called Main:
> :m Control.Lens Control.Monad
Now both Control.Lens and Control.Monad are in scope.
Now at this point, it seems I forgot to add Main, so I can easily put a + which will add modules. This is so handy, I usually always use it, I was showing you the above solely so you could see how to import multiple modules at once.
> :m + Main
Or, what I'd ordinarily do, as I just described:
> :m + Main Control.Lens Control.Monad
I think Main is usually included by default, but you get the picture :)
As to reloading, I use the :r command to reload, and yeah, it can suck that bindings are lost, but usually I put them into the code I'm writing in, sometimes temporarily, or I use the readline support of the OS to "go up" through the historical backscroll to find previous definitions.
Happy Haskelling!

Use different configuration for "ghci" and "stack ghci"

I often use ghci for little calculations and stack ghci to work on my actual projects.
To make the first easier I have written a .ghci file with a lot of imported modules in it, but some of these modules aren't present in my stack project and I get nasty errors.
At the moment I use a alias stackghci="stack ghci --ghci-options -ignore-dot-ghci", but then I have the default prompt again and so on..
Is there a way of specifying two .ghci files; one for stack and one for ghci?
ghci supports the --ghci-script flag for specifying additional scripts to read at startup.
Docs for --ghci-script: (link)

GHCi load a file and redefine one of its functions in the repl

Imagine I load a file (not a module) in GHCi like this
:l file.hs
and that inside the file there are two functions f and g, and that f has some bug (e.g., bad result) and that g calls f.
How can I redefine f with a correct version at the GHCi REPL, and make g call the new version without having to change or reload the file?
Thanks
You can't.
When loading the file in the repl it's as if the definitions were in a separate module and thus g will not look for the f you define at the repl, even if from your point of view your definition hides the original f function.
Haskell uses lexical scoping, while what you suggest is a dynamic scoping feature. See lexical scoping vs dynamic scoping.
It's completely impractical to modify the repl to compile code using lexical scoping and then allow people to modify it by using dynamic scoping. Hence it's not done and what you ask is impossible.

How to use :? to find all function list and manual in ghci

everyone, i'm pretty new to haskell. i was a c++ programmer.
how to find a detailed list of functions in a particular module such like in the default "prelude" module? and how to find out how these functions work in ghci environment?
ie. is there a command to find out all functions in "Prelude"?
Thanks.
how to find a detailed list of
functions in a particular module such
like in the default "prelude" module?
Typing :browse <Module> into GHCi will produce a list of all (exported) functions in a module with their type signatures. For the Prelude and other standard modules such as Data.List or Control.Monad, the names and type signatures should provide good insight into the functionality you can squeeze out of it. Second, you can browse the standard library and source on hackage.haskell.org. Third, GHCi on Acid (an extension to GHCi which you can cabal-install) gives you commands like :source and :doc, providing a direct link to the source code and documentation for a module; and :hoogle, which performs a Hoogle search on a given argument.
and how to find out how these
functions work in ghci environment
Try them out and study the source code. Since you can evaluate functions interactively in GHCi, you can get a feel for how functions behave; since you can read their source, you can get an exact definition of their behavior.
A nice starting point for this is Hoogle. http://haskell.org/hoogle/ Just type Prelude in the search box for example, it's a quite good resource with a lot of examples on using Haskell's features.

Resources