{-# LANGUAGE FlexibleInstances #-} import Data.Set (Set,elems,cartesianProduct,fromList,empty,insert,singleton, union,unions) data CombinatorialProblem a = CP { bound :: Set a, close :: Set a -> Set a } enumerate :: CombinatorialProblem a -> Set a enumerate cp = iterate' f null (empty, singleton (close cp empty)) where f (old, new) = (union old new, fromList fresh) where fresh = [close cp (insert a s) | a <- elems (bound cp), s <- elems new] iterate' :: ((a,a) -> (a,a)) -> (a -> Bool) -> (a,a) -> a iterate' f p x = let (x0,x1) = iterate'' x in x0 where iterate'' x = if p (let (x0,x1) = x in x1) then x else iterate'' (f x)