Why can ghci see non-exportable types and constructors? How can i fix this?

I am new to Haskell. Here is a simple code:

module Src ( -- The 'Answer' type isn't exported Shape(Circle), -- ie 'Rectangle' data constructor isn't exported Point(..), area, nudge ) where data Answer = Yes | No deriving (Show) data Point = Point Float Float deriving (Show) data Shape = Circle Point Float | Rectangle Point Point deriving (Show) area :: Shape -> Float area (Circle _ r) = pi * r ^ 2 area (Rectangle (Point x1 y1) (Point x2 y2)) = (abs $ x2 - x1) * (abs $ y2 - y1) nudge::Shape->Float->Float->Shape nudge (Rectangle(Point x1 y1)(Point x2 y2)) dx dy = Rectangle (Point (x1 + dx) (y1 + dy)) (Point (x2 + dx) (y2 + dy)) nudge (Circle (Point xy) r) dx dy = Circle (Point(x + dx) (y + dy)) r 

I hid the Answer type and the Rectangle constructor. But when I download the Src.hs file, ghci still sees them:

 ghci> :l src [1 of 1] Compiling Src ( src.hs, interpreted ) Ok, modules loaded: Src. ghci> let a = Yes ghci> a Yes ghci> :ta a :: Answer ghci> let r = Rectangle (Point 0 0) (Point 100 100) ghci> r Rectangle (Point 0.0 0.0) (Point 100.0 100.0) ghci> :tr r :: Shape ghci> 

Why did this happen and how can I fix it?

+5
haskell ghci
source share
2 answers

Yes, when you upload a file to GHCi, you can access anything defined in this file, regardless of whether it is exported. Thus, the expressions you write to GHCi behave exactly the same as in the downloaded file. That way, you can use GHCi to quickly test the expression you want to use inside the file, or to quickly test the function defined in the file, even if it is closed.

If you want the code to work as imported from another file, you can use import instead :l . Then it will only allow access to advanced definitions.

+5
source share

If you import a module with :l , you get access to everything, ignoring the export clause:

 Prelude Export> :l Export [1 of 1] Compiling Export ( Export.hs, interpreted ) Ok, modules loaded: Export. *Export> a 6 *Export> b 5 

If instead of import Export you get only exported bindings:

 Prelude> import Export Prelude Export> a 6 Prelude Export> b <interactive>:28:1: Not in scope: 'b' 
+1
source share

All Articles