Haskell : loading ALL files in current directory path
Asked Answered
C

2

5

The command (in GHCi)

:load abc

Loads the functions in the file abc (which must exist in the current directory path). How would I load all the files in the current directory path? Thanks

----------------------------------------------------------------------------------

[RESPONSE TO POST BELOW]

Hi Rotskoff, thanks I tried your suggestion but I could not get it to work, so I think I must have misunderstood something.

I created 3 files test.hs, test1.hs, and test2.hs as follows :

->

--test.hs
import NecessaryModule

->

--test1.hs
module NecessaryModule where

addNumber1 :: Int -> Int -> Int
addNumber1 a b = a + b

->

--test2.hs
module NecessaryModule where

addNumber2 :: Int -> Int -> Int
addNumber2 a b = a + b

Then when I did :

:load test

I got the error message :

test.hs:1:8:
    Could not find module `NecessaryModule':
      Use -v to see a list of the files searched for.

Thanks

---------------------------------------------------------------------------------

Thanks. This is what I did to get it to work (following Rotskoff's suggestion) :

->

--test.hs
import NecessaryModule1
import NecessaryModule2

->

--NecessaryModule1.hs
addNumber1 :: Int -> Int -> Int
addNumber1 a b = a + b

->

--NecessaryModule2.hs
addNumber2 :: Int -> Int -> Int 
addNumber2 a b = a + b
Crimson answered 22/4, 2012 at 14:16 Comment(1)
You'll need to name your modules accordingly. (You'd see that GHC looked for things like necessarymodule.hs if you had compiled with the -v option outside of interactive mode.) Because you've saved the files as test2.hs, etc, the compiler doesn't know how to locate them. If you rename test2.hs to necessarymodule.hs it should work. P.S., it's also good practice to name the source files by their module names.Tarpon
T
5

Presumably you mean Haskell source files, because you can't use the :load command in GHCi for anything else.

At the top of the source file that you load, include the line:

import NecessaryModule

For each of the source files, make sure to name the modules, e.g.,

module NecessaryModule where

should appear. GHCi will automatically link all the files.

If you're trying to import data, take a look at System.Directory in the documentation.

Tarpon answered 22/4, 2012 at 16:3 Comment(2)
n.b. in practice it is quite rare for a single module to span multiple files, with the exception of a master module that exposes several (differently named) sub-modules.Bolometer
Hi I tried your suggestion but I could not get it to work. I put it above as an edit (because of lack of space here). ThanksCrimson
P
2

It's better if the filenames and the names of the modules will be the same:

➤ mv test1.hs NecessaryModule.hs
➤ ghci
GHCi, version 7.0.4: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Loading package ffi-1.0 ... linking ... done.
Prelude> :load test
[1 of 2] Compiling NecessaryModule  ( NecessaryModule.hs, interpreted )
[2 of 2] Compiling Main             ( test.hs, interpreted )
Ok, modules loaded: NecessaryModule, Main.

since the :load command load module(s) (by filenames) and their dependents (as you can read by typing :help or :? in the GHCi prompt).

Also the :load command erase all previous declarations which was defined in the current GHCi session, so for loading all files in the current directory you can do something like this:

Prelude> :q
Leaving GHCi.
➤ ghci *.hs
GHCi, version 7.0.4: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Loading package ffi-1.0 ... linking ... done.

<no location info>:
    module `main:NecessaryModule' is defined in multiple files: NecessaryModule.hs
                                                            test2.hs
Failed, modules loaded: none.
Prelude> :q
Leaving GHCi.
➤ rm test2.hs
➤ ghci *.hs  
GHCi, version 7.0.4: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Loading package ffi-1.0 ... linking ... done.
[1 of 2] Compiling NecessaryModule  ( NecessaryModule.hs, interpreted )
[2 of 2] Compiling Main             ( test.hs, interpreted )
Ok, modules loaded: NecessaryModule, Main.
*NecessaryModule> 
Prohibitive answered 23/4, 2012 at 21:19 Comment(2)
I am using WinGHCi so I don't think I can do that, but it is nice to know. Thank you.Crimson
@Crimson you can use unix shells like bash or zsh on Windows (using MinGW or Cygwin), it's usefull for invoking GHC / GHCi and similar stuff like working with files, git (and any other (D)CVS) or makefiles (any other building system, e.g. cabal). Also I guess something like this is possible in cmd.exe or PowerShell (not sure about wildcards).Prohibitive

© 2022 - 2024 — McMap. All rights reserved.