I have a strange <<loop>> error with the function below. I present it as is since any attempt to simplify eliminates the error as well.
bellmanFord :: (DistanceMap m) => m -> [Edge] -> Maybe m
bellmanFord m edges = loop m 0 (length edges)
where
loop dists step size =
if step > size then
Nothing
else
let newDists = foldl' proc dists edges
in
if identical newDists dists then
Just dists
else
loop newDists (step + 1) size
proc dists (v1, v2, w) =
let d = get dists v2
d' = get dists v1 + Only w
in
if d' < d then
assoc dists v2 d'
else
dists
The whole program is available here.
size? Why not count down to0fromsizeand use pattern matching to clean up the definition ofloop? Something likeloop dists 0 = Nothing; loop dists n = let newDists = ... in if ... then Just dists else loop newDists $ n - 1? You'd at least remove the parameter for what step you're on.<<loop>>means that you have infinite recursion in one of your functions.