Why can ghci see non-exported types and constructors? How can I fix it?

南楼画角 提交于 2020-01-02 04:04:14

问题


I am a novice in Haskell. Here's some simple code:

module Src
(   -- The 'Answer' type isn't exported
    Shape(Circle), -- i.e '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 x y) r) dx dy  = Circle (Point(x + dx) (y + dy)) r

I've hidden the Answer type and the Rectangle constructor. But when I load 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> :t a
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> :t r
r :: Shape
ghci>

Why did this happen and how can I fix it?


回答1:


Yes, when you load a file in GHCi like that, you can access anything defined in that file regardless of whether or not it is exported. This way expressions you write into GHCi behave exactly like they would inside the loaded file. So you can use GHCi to quickly test an expression that you mean to use inside the file or to quickly test a function defined in the file even if it is private.

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




回答2:


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 you instead import Export, you get only the exported bindings:

Prelude> import Export
Prelude Export> a
6
Prelude Export> b

<interactive>:28:1: Not in scope: ‘b’


来源:https://stackoverflow.com/questions/27548049/why-can-ghci-see-non-exported-types-and-constructors-how-can-i-fix-it

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!