bkerby

Untitled

Jul 11th, 2012
521
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. {-# LANGUAGE UnicodeSyntax #-}
  2. module Graph
  3.   ( Distance, Graph, Vertex
  4.   , fromList, findShortestPath, findShortestPaths, findShortestPaths_
  5.   ) where
  6.  
  7. import Control.Monad (when)
  8. import Control.Monad.Reader (Reader, ask, runReader)
  9. import Control.Monad.State (StateT, execStateT, get, put)
  10. import Data.Function (on)
  11. import Data.IntMap (fromList)
  12. import Data.List (delete, intersect, (\\), nub, sortBy)
  13. import Data.Maybe (catMaybes, listToMaybe)
  14. import qualified Data.IntMap as IM
  15.  
  16. import Debug.Trace (trace)
  17.  
  18. type Distance = Int
  19. type Graph = IM.IntMap (IM.IntMap Vertex)
  20. type Vertex = Int
  21.  
  22. findShortestPaths ∷ Graph → Vertex → IM.IntMap Distance
  23. findShortestPaths γ v = runReader (execStateT allSteps (fromList [(v,0)])) γ
  24.  
  25. findShortestPaths_ ∷ Graph → Vertex → [(Vertex,Distance)]
  26. findShortestPaths_ γ v = IM.toList $ findShortestPaths γ v
  27.  
  28. findShortestPath ∷ Graph → Vertex → Vertex → Maybe Distance
  29. findShortestPath γ s t = IM.lookup t $ findShortestPaths γ s
  30.  
  31. step ∷ StateT (IM.IntMap Distance) (Reader Graph) Bool
  32. step = do
  33.   γ ← ask
  34.   distance ← get
  35.   let candidates ∷ [Vertex]
  36.       candidates = concatMap nub . map (\v → (IM.keys $ γ IM.! v) \\ (IM.keys distance)) $ IM.keys distance ∷ [Vertex]
  37.       nearestWay ∷ Vertex → Distance
  38.       nearestWay c = minimum . catMaybes . map (\v → (+ distance IM.! v) `fmap` IM.lookup c $ γ IM.! v) $ IM.keys distance
  39.       newVertex ∷ Maybe (Vertex, Distance)
  40.       newVertex = listToMaybe . sortBy (compare `on` snd) . map (\c → (c, nearestWay c)) $ candidates
  41.   case newVertex of
  42.     Just (v, d)do
  43.       put $ IM.insert v d distance
  44.       return True
  45.     Nothing → return False
  46.  
  47. allSteps ∷ StateT (IM.IntMap Vertex) (Reader Graph) ()
  48. allSteps = do
  49.   r ← step
  50.   when r allSteps
  51.  
  52. ---------------------------
  53.  
  54. Graph.hs:34:14:
  55.     Couldn't match type `Int' with `Maybe Int'
  56.    When using functional dependencies to combine
  57.      Control.Monad.State.Class.MonadState s (StateT s m),
  58.        arising from the dependency `m -> s'
  59.         in the instance declaration in `Control.Monad.State.Class'
  60.      Control.Monad.State.Class.MonadState
  61.        (IM.IntMap (Maybe Int))
  62.        (StateT
  63.           (IM.IntMap Int)
  64.           (Control.Monad.Trans.Reader.ReaderT
  65.              (IM.IntMap (IM.IntMap Int)) Data.Functor.Identity.Identity)),
  66.        arising from a use of `get' at Graph.hs:34:14-16
  67.     In a stmt of a 'do' block: distance <- get
  68.     In the expression:
  69.       do { γ <- ask;
  70.            distance <- get;
  71.            let candidates :: [Vertex]
  72.                candidates = ...
  73.                ....;
  74.            case newVertex of {
  75.              Just (v, d) -> do { ... }
  76.              Nothing -> return False } }
  77.  
  78. Graph.hs:43:27:
  79.     Couldn't match expected type `Distance'
  80.                 with actual type `Maybe Distance'
  81.    Expected type: IM.IntMap Distance
  82.      Actual type: IM.IntMap (Maybe Distance)
  83.    In the third argument of `IM.insert', namely `distance'
  84.    In the second argument of `($)', namely `IM.insert v d distance'
Advertisement
Add Comment
Please, Sign In to add comment