Home > Software design >  How to refer to data declarations in different modules with haskell
How to refer to data declarations in different modules with haskell

Time:12-20

Is it possible from Main.hs to reference my Graph.hs -functions, edgs and vers in data Graphs a?

I want to create a Graph from my Graphs.hs file, fill it with vertices, and connect them with edges. Then from my Main.hs file I want to create a function getEdges that can list all edges for me. Is this possible or am I thinking too much in an OOP-manner? :-(

Below I've added parts of my code to try and illustrate my problems.

**File Graph.hs:**
module Graph(Graph, addVertex, addEdge, connectedVertex) where

type Vertex a = a
type Edge a = (Vertex a, Vertex a)

data Graph a = Graph {
  vers :: [Vertex a],
  edgs :: [Edge a]
} deriving (Show, Eq, Read)

**File Main.hs:**
-- func1 to func3 is just to illustrate that there is more than one function in main.
module Main(func1, func2, getEdges, paths) where
import Graph

-- getEdges shall return a list of all edges
getEdges :: Eq a => Grap a -> [a]
getEdges g = edgs $ g

-- paths will return true if there is a connection (edges) between vertecies a to b.
paths :: Eq a => Graph a -> a -> a-> Bool
paths = undefined  

I would greatly appreciate any help :-)

CodePudding user response:

You are missing a (..) in module Graph(Graph(..), addVertex, addEdge, connectedVertex) where

An smaller equally illustrative example:

Module.hs:

module Module(Record(..)) where

data Record a = Record {
  string :: String,
  int :: Int
} deriving (Show, Eq, Read)

And Main.hs:

module Main where

import Module

record = Record "Hello" 42

main =
  print (string record) >>
  print (int record)

The Type(..) exports a type constructor and its associated data constructors and field accessors. You can also do in the example above module Module(Record(Record), int, string) where to have a more fine grained control of what needs to be exported.

  • Related