Using exported symbols

Viewed 51

I have a Haskell file called maxflow.hs that exports few symbols

module MaxFlow 
(solveMaxFlow,MaxFlowNet,Vertex,Graph) where
import Data.List 


data Vertex = Vertex {
                          vertexLabel :: String
                        , vertexNeighbors :: [(String,Int)]
                        , vertexDistance :: Int
                        , vertexPredecessor :: String
                      } deriving (Show)

....

In the same directory I have another file called elimination.hs that tries to use one of those symbols

import MaxFlow

g =  [
                Vertex "0" [("1",16), ("2",13)         ] (maxBound::Int)  "",
                Vertex "1" [("2",10), ("3",12)  ] (maxBound::Int) "",
                Vertex "2" [("4",14) ,("1",4)        ] (maxBound::Int) ""    ,
                Vertex "3" [ ("5",20), ("2",9)] (maxBound::Int) ""      ,
                Vertex "4" [("5",4), ("3",7) ] (maxBound::Int) ""      ,
                Vertex "5" [ ] (maxBound::Int) ""    
      ]

But for some reason I can't load this file. Running :l elimination.hs

I get

elimination.hs:4:17: error:
    Data constructor not in scope:
      Vertex :: [Char] -> [([Char], Integer)] -> Int -> [Char] -> a
  |
4 |                 Vertex "0" [("1",16), ("2",13)         ] (maxBound::Int)  "",
  |                 ^^^^^^

I'm probably missing something very basic. Any idea ? Thanks!

2 Answers

You only exported the type constructor, not its data constructor(s). If you want to export the data constructor(s), you can write the can write this between parenthesis in the export:

module MaxFlow (solveMaxFlow, MaxFlowNet, Vertex(Vertex), Graph) where

-- …

If you want to export the getters/setters, you should include these in the parenthesis as well:

module MaxFlow (solveMaxFlow, MaxFlowNet, Vertex(Vertex, vertexLabel, vertexNeighbors, vertexDistance, vertexPredecessor), Graph) where

-- …

You can write two consecutive dots (..) if you want to export all:

module MaxFlow (solveMaxFlow, MaxFlowNet, Vertex(..), Graph) where

-- …
module MaxFlow 
   (...,Vertex,...) where

This says you want to export the type named Vertex and not the data constructor or the fields. What you probably want is either to export both the data type and the data constructor:

module MaxFlow (Vertex(Vertex)) where

Or to export the type, all data constructors, and all fields:

module MaxFlow (Vertex(..)) where

Those dots are literal not a short hand, you can type Vertex(..) in the export list to mean the type, data constructors, and all fields.

Related