Srinivas
Srinivas

Reputation: 2098

Error when importing a Haskell module

I am trying to import a Haskell module named Shapes from a file called surface (which compiles fine)

import qualified surface.Shapes as Shapes

surfaceImport :: Shape -> Float
surfaceImport (Circle _ r) = pi * r ^ 2
surfaceImport (Rectangle (Point x1 x2) (Point y1 y2)) = (abs $ x1 - x2) * (abs $ y1 - y2)

I am getting the following error when I try to compile this program

surfaceImport.hs:1:18: error: parse error on input `surface'
Failed, modules loaded: none.

The module I am trying to import is this

module Shapes
(
Point(..),
Shape(..),
surface,
nudge,
baseCircle,
baseRectangle
)

Thanks in advance where

Upvotes: 0

Views: 1317

Answers (1)

Christoph W.
Christoph W.

Reputation: 1024

so first of all I think your code is from: http://learnyouahaskell.com/making-our-own-types-and-typeclasses

and there is also a part in the introduction concerning the definition of modules: http://learnyouahaskell.com/modules

It is recommended that the file and the module have the same name as stated in the link above. This will resolve your problem with the parse error on "surface". The next point that you should not do is to name your file like a function in your module.

You use a qualified import in your example. Qualified imports are explained here: https://www.haskell.org/tutorial/modules.html

In general, one uses qualified imports if there are two modules containing different entities but with the same name. The qualified import allows you to prefix the imported names with the imported module. Consequently, I am not sure if you need a qualified import in your example at all.

In summary you should make the following changes:

  • Rename the file containing the Shapes module to "Shapes.hs"
  • Rename your second file from "surfaceImport.hs" to something like "ShapesUsageExample.hs"
  • Change your import to import Shapes

Upvotes: 1

Related Questions