There is much said about 'nondeterminism' in Haskell: The list monad and the Amb
monad call themselves nondeterministic.
But these functions just model nondeterminism - they (deterministically) generate all possible outcomes.
How would one write a truly nondeterministic choice in Haskell?
By this I mean a function of type
nondeterministicChoice :: a -> a -> IO a
where the compiler is unconstrained in whether to return the first or second argument (using the same logic it uses, for example, to choose whether to recalculate or memoize a thunk). Especially interesting would be if the resulting a
could be lazy.
I think the answer is - the question as asked is pointless. There's no point simply freeing the compiler do things like this, because it doesn't know what to do with it. There is (currently) no logic in the compiler that will decide which of the arguments is better to use.
However, you could code up this function to, i.e. choose an argument that has already been evaluated, using the evaluated
function from https://stackoverflow.com/a/28701687/12153248:
nondeterministicChoice :: a -> a -> IO a
nondeterministicChoice a b = do
a' <-evaluated a
if a'
then return a
else return b