Class Description for General Charts in Haskell

I am trying to write a class for graphs. Basically, the class is as follows:

class Graph g where
  adjacentNodes :: g n -> n -> [n]

in which I use nto represent the type of nodes.

Then I have the following Graph, defined as follows:

data FiniteGraph n = FiniteGraph { runFiniteGraph :: Array n [n] }

where it is Arraytaken from a standard container Data.Array, the structure must represent the final graph to map each node to their neighboring nodes.

This is where the problem arises when I try to make an FiniteGraphinstance Graph.

instance Graph FiniteGraph where
  adjacentNodes g n = (runFiniteGraph g) ! n

Unfortunately, this does not work, because the operator !requires a restriction Ix n, but I did not find where to declare it.

I expect the instance declaration to look like this:

instance (Ix n) => Graph (FiniteGraph n) where { ... }

g in class Graph g * * -> *, , n g.

? .

+4
1

Graph.

{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE FlexibleInstances #-}
import Data.Array

class Graph g n | n -> g where
  adjacentNodes :: g n -> n -> [n]

data FiniteGraph n = FiniteGraph { runFiniteGraph :: Array n [n] }

instance Ix n => Graph FiniteGraph n where
  adjacentNodes g n = (runFiniteGraph g) ! n

, : .

+5

All Articles