I'm programming an AI General Problem Solver in Haskell for the AI Planning course at Coursera and ghci complains about an ambiguous type variable. Here is the Haskell code and the error I get:
-- Solver.hs
{-# LANGUAGE GADTs,FlexibleInstances,UndecidableInstances,ScopedTypeVariables,TypeFamilies,MultiParamTypeClasses #-}
module Solver
(Solver,State,Transition)
where
class (Show t,Eq t) => Transition t where
transition :: State s => s -> t -> s
class (Show s,Eq s) => State s where
getPossibleTransitions :: Transition t => s -> [t]
isStateValid :: s -> Bool
isGoalState :: s -> Bool
class Solver s t where
getPossibleNextStates :: s -> [s]
isStateVisited :: [s] -> s -> Bool
getNextFringeStates :: [s] -> [[s]]
--getNextGeneration :: [s] -> [s] -> [s]
flatten :: [[a]] -> [a]
flatten [] = []
flatten listOfLists = (head listOfLists) ++ (flatten (tail listOfLists))
instance (State s,Transition t) => Solver s t where
getPossibleNextStates (state::s) =
filter isStateValid (map transitionFunction possibleTransitions)
where
transitionFunction = (transition state)::(t -> s)
possibleTransitions = (getPossibleTransitions state)::([t])
isStateVisited visitedStates state =
any (== state) visitedStates
getNextFringeStates (states::[s]) =
map (getPossibleNextStates :: (s -> [s])) (states::[s])
-- COMPILATION:
{-
Prelude> :l Solver.hs
[1 of 1] Compiling Solver ( Solver.hs, interpreted )
Solver.hs:38:8:
Ambiguous type variable `t0' in the constraint:
(Transition t0) arising from a use of `getPossibleNextStates'
Probable fix: add a type signature that fixes these type variable(s)
In the first argument of `map', namely
`(getPossibleNextStates :: s -> [s])'
In the expression:
map (getPossibleNextStates :: s -> [s]) (states :: [s])
In an equation for `getNextFringeStates':
getNextFringeStates (states :: [s])
= map (getPossibleNextStates :: s -> [s]) (states :: [s])
Failed, modules loaded: none.
-}
flatten
function is actually equivalent toconcat
from Prelude) – Peter Hall