Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compile multi .hs files of Haskell - Unix

I created some files from a project in Unix, they are a lot and if I want to execute it in another pc or folder I need to copy all files to there. They are all connected in import. How can I make an executable haskell program?

I have per example:

main.hs - main where all the program executes; using,besides haskell, unix shell.
ex1.hs - basically types of data, some functions.
ex2.lhs - same as ex1.lhs but is literate with LaTeX
pic.jpg - picture to use on the pdflatex
package.sty - package needed to use some functions

How do I proceed and compile all of these? I tried using ghc but always giving errors:

>ghc -o MAIN main.hs ex1.hs ex2.lhs pic.jpg package.sty

Failed to load interface for 'ex1.hs' And is in the line which has import ex1.hs

Curious is if I trade import ex1.hs to import ex2.lhs line will give error on ex2

like image 415
MrFabio Avatar asked Jan 19 '12 23:01

MrFabio


People also ask

How do I run a compiled Haskell file?

Open a command window and navigate to the directory where you want to keep your Haskell source files. Run Haskell by typing ghci or ghci MyFile. hs. (The "i" in "GHCi" stands for "interactive", as opposed to compiling and producing an executable file.)

How do I run Haskell in GHC?

If you have installed the Haskell Platform, open a terminal and type ghci (the name of the executable of the GHC interpreter) at the command prompt. Alternatively, if you are on Windows, you may choose WinGHCi in the Start menu. And you are presented with a prompt. The Haskell system now attentively awaits your input.

What is .HI file Haskell?

hi file is the equivalent of C's header files (that is, with . h extension), only these are generated by GHC from the original Haskell source. Thus, the . hi is used when GHC compiles other modules, and the .o is used when linking all modules together to produce the executable.

How do I load a file into ghci?

From the command line, enter "ghci" (or "ghci -W") followed by an optional filename to load. Note: We recommend using "ghci -W", which tells GHC to output useful warning messages in more situations. These warnings help to avoid common programming errors.


2 Answers

  1. Haskell module names must begin with an upper case letter, so start by renaming ex1.hs and ex2.hs to Ex1.hs and Ex2.hs. They should also start with module Ex1 where, otherwise the module name will default to Main, and GHC will be very confused when the module names don't match the file names.

  2. Import statements should refer to the module name, not the file name, so change them in main.hs to correspond to the module names.

    import Ex1
    import Ex2
    
  3. Now compile with ghc --make main.hs, and it should find the other modules automatically. It will search for modules with both .hs and .lhs extensions, and correctly treat the latter like literate Haskell files.

For larger projects, you should look into using Cabal, the build system used by most Haskell libraries and programs. It will help with managing dependencies and compiler options, sort of like a Makefile.

like image 94
hammar Avatar answered Sep 20 '22 21:09

hammar


Try

ghc --make main.hs

That should make ghc try to do everything it can...

like image 45
Volker Stolz Avatar answered Sep 19 '22 21:09

Volker Stolz