Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure Haskell / ghci module search path?

Tags:

haskell

ghci

I have a first Application file, Myapp.hs

I have created a module for parsing csv file , called Csvparser which is defined in a file Csvparser.hs.

Both files are in the same directory.

I don't understand how to import in Myapp.hs the Csvparser module

Prelude Data.Maybe Data.List Data.Time Data.Either> :load C:\Test\Haskell\MyApp.hs
[1 of 1] Compiling Main             ( C:\Test\Haskell\MyApp.hs, interpreted )

C:\Test\Haskell\MyApp.hs:5:1: error:
    Could not find module `Csvparser'
    Use -v to see a list of the files searched for.
  |
5 | import Csvparser
  | ^^^^^^^^^^^^^^^^
Failed, no modules loaded.
Prelude Data.Maybe Data.List Data.Time Data.Either>

The module can be loaded in standalone and works

Prelude Data.Maybe Data.List Data.Time Data.Either> :load C:\Test\Haskell\Csvparser.hs
[1 of 1] Compiling Csvparser        ( C:\Test\Haskell\Csvparser.hs, interpreted )
Ok, one module loaded.
*Csvparser Data.Maybe Data.List Data.Time Data.Either> import Csvparser
*Csvparser Data.Maybe Data.List Data.Time Data.Either Csvparser> :t Csvparser.parseCSV
Csvparser.parseCSV :: String -> Either ParseError [[String]]
*Csvparser Data.Maybe Data.List Data.Time Data.Either Csvparser>

Here is the failing syntax at line 5 of MyApp.hs

{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE ScopedTypeVariables #-}


import Csvparser
import Database.HDBC
import Database.HDBC.ODBC

What can I do for making ghci/haskell understand that Csvparser module is to be found in the same directory of MyApp.hs ?

like image 745
sandwood Avatar asked Apr 05 '18 14:04

sandwood


1 Answers

If you want to run ghci from a different folder that the one containing the file then do:

ghci -iC:\Test\Haskell\

-i sets the import search paths and then do:

:load C:\Test\Haskell\MyApp.hs

Or from inside the ghci console do:

:set -iC:\Test\Haskell\

If you want to see in which folder haskell is looking for the modules just call:

:show paths

It should print a list of paths under module import search paths:

All this isn't necessary if you run ghci from the folder that has your files, since by default Haskell will always look for the imports in the current folder.

Don't leave a space between -i and the path

like image 155
moondaisy Avatar answered Oct 22 '22 12:10

moondaisy