I want to build a nondeterministic state monad in Haskell. This will allow me to generate all the elements in my search space using the built up state to prune bad locations. Suppose I have the following (pseudo-)code:
primitives :: [State Int Element]
primitives = [... list of primitive stateful elements ...]
combine :: Element -> Element -> State Int Element
expand :: Depth -> [State Int Element]
expand 0 = primitives
expand d = do
... do something to the state ...
left <- expand (d-1)
right <- expand (d-1)
let out = combine left right
guard ( ... some check on out ... )
return out
There are several things here that don't work: the most basic thing that I need to understand is how to do something to state and then pipe it in to each of the expand
branches. I've tried a bunch of ways with functions of type State Int [ State Int Element]
but, ultimately, once I wrap the branches of the list monad in a state wrapper I can't remove it, right? So is there a way to do this?
Thanks.