Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Feb 13th, 2013  |  syntax: Haskell  |  size: 1.11 KB  |  views: 21  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
This paste has a previous version, view the difference. Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. -- Solver.hs
  2. -- cleaned up with stylish-haskell (hackage)
  3. -- and https://github.com/tibbe/haskell-style-guide
  4. -- also, flatten is 'concat', I think
  5.  
  6. {-# LANGUAGE FunctionalDependencies #-}
  7. {-# LANGUAGE MultiParamTypeClasses  #-}
  8. {-# LANGUAGE ScopedTypeVariables    #-}
  9.  
  10. module Solver
  11.     (Solver,State,Transition)
  12.   where
  13.  
  14. class (Show t,Eq t) => Transition t where
  15.     transition :: State s => s -> t -> s
  16.  
  17. class (Show s,Eq s) => State s where
  18.     getPossibleTransitions :: Transition t => s -> [t]
  19.     isStateValid :: s -> Bool
  20.     isGoalState  :: s -> Bool
  21.  
  22. class (State s, Transition t) => Solver s t | s -> t where
  23.     getPossibleNextStates :: s -> [s]
  24.     getPossibleNextStates state =
  25.        filter isStateValid (map transitionFunction possibleTransitions)
  26.       where
  27.        transitionFunction  = transition state :: t -> s
  28.        possibleTransitions = getPossibleTransitions state
  29.  
  30.     isStateVisited        :: [s] -> s -> Bool
  31.     isStateVisited visitedStates state =
  32.        any (== state) visitedStates
  33.  
  34.     getNextFringeStates :: [s] -> [[s]]
  35.     getNextFringeStates = map getPossibleNextStates