Fedo
Fedo

Reputation: 349

Why I cannot import a module?


Hello everybody

I cannot understand why I get such error when I try to import a module after I have imorted a Data.Char library? Actually when I delete module Test where everything works correctly

enter image description here

import Data.Char
module Test where

sayHello = putStrLn "Hello, world withoutCorona!"

lenVec3 x y z = sqrt ( x ^ 2 + y ^ 2 + z ^ 2 )

sign x = (if x > 0 then 1 else 0) + (if x < 0 then -1 else 0) + 0


twoDigits2Int x y = if isDigit x && isDigit y then digitToInt x * 10 + digitToInt y else 100

Thanks in avance

Upvotes: 3

Views: 585

Answers (2)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476493

A Haskell program is a set of modules. A module is structured gramatically as:

module     ->   module modid [exports] where body
    |   body
body    ->   { impdecls ; topdecls }
    |   { impdecls }
    |   { topdecls }
modid   ->   conid
impdecls    ->   impdecl1 ; … ; impdecln    (n>=1)
topdecls    ->   topdecl1 ; … ; topdecln    (n>=1)

The import declarations are the importdecls in this grammar, and thus part of the body. A module can exists without a module modid … part, but if we define a module identifier, then this precedes the body.

You thus write such module as:

module Test where

import Data.Char

-- …

Upvotes: 3

lehins
lehins

Reputation: 9767

Imports go after the module, not before. module Name where should be the first thing, possibly preceded only by language extensions and compiler flags:

module Test where
import Data.Char

Upvotes: 7

Related Questions