haskell / fgl

A Functional Graph Library for Haskell
http://hackage.haskell.org/package/fgl
Other
184 stars 54 forks source link

ufold causes unexpected results #24

Closed mpickering closed 8 years ago

mpickering commented 8 years ago

I expected the following two functions to be equivalent but ufold gives strange answers. Specifically, the context was missing self-loops and incoming edges, am I misusing ufold here?

consistency :: DTMC -> Maybe Node
consistency dtmc =
  foldr (\n c ->
    if sum (map edgeLabel (out dtmc n)) == 1
      then c
      else (Just n)) Nothing
consistency :: DTMC -> Maybe (Context [String] Rational)
consistency =
  ufold (\context c ->
    if sum (map edgeLabel (out' context)) == 1)
      then c
      else (Just context)) Nothing
ivan-m commented 8 years ago

What is DTMC? It makes it difficult for me to tell what you're doing here.

Note that ufold removes the node each time it recurses, whereas I'm guessing that your first function considers the entire graph.

mpickering commented 8 years ago

Why does ufold remove the node when it recurses? I think that is my problem.

type DTMC = Gr [String] Rational

ivan-m commented 8 years ago

Because it's a fold: just like foldr removes that list element as it recurses.

What are you trying to find? The last node that has an out-going weight of 1?

If so, I would use a variant of your first one:

consistency :: DTMC -> Maybe Node
consistency dtmc = listToMaybe . filter ((==1) . sum . map edgeLabel . out dtmc) 
                               $ nodes dtmc

Note that performance-wise, if all you want is the Node then this would be as performant as a Context-based approach, possibly even a tad better (no need to get the in-coming edges, delete the node from the graph, etc.).

ivan-m commented 8 years ago

Is this issue still relevant?

mpickering commented 8 years ago

No.