Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell : loading ALL files in current directory path

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
like image 519
artella Avatar asked Apr 22 '12 14:04

artella


2 Answers

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.

like image 166
rotskoff Avatar answered Sep 17 '22 22:09

rotskoff


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> 
like image 38
JJJ Avatar answered Sep 20 '22 22:09

JJJ