# Flow/graph-based programming in Mathematica?

Is it possible in Mathematica to write a program inside a graph? For example if you have a bunch of different If[] statements that you need to embed within one another, instead of writing them all within one another and getting very confused as to where in the control flow you are up to, is it possible to somehow embed these within a graph, so that you can more clearly keep track of precisely where you are?

Thank you

• You can certainly use a Graph object in such a way, e.g., putting the "logic" in vertex/edge custom properties. But, you'd need to handle what MMA does with it (a simple parser), since on their own they have no meaning to MMA (they're custom, after all). I'd also suggest if nesting of If is such it's confusing, a different control-of-flow construct, or a refactoring of code, might be in order. – ciao Mar 1 '14 at 19:48
• Do you mean that instead of writing code you'd like to use a flowchart to describe control flow within the program? – Szabolcs Mar 1 '14 at 20:47
• Yes, @Szabolcs, exactly. – Aron Mar 1 '14 at 23:31

Let me know if this doesn't cut as an answer.

If your goal is a visual programming language with flow-chart paradigm - it doesn't have to be a Graph that Wolfram Language (WL) uses, but could be another dedicated flow-language that can drive WL computations.

Mohamed Zaghloul is developing Mantis add-on tools for Grasshopper to link it with the Mathematica kernel. I like the idea - so this is why I give detailed information. I am sure some folks could use it.

You already can use it - just get all components.

Screen shots of the environment - yellow cells feature actual WL code:

Here's a completely contrived example that gets to the gist of my comment. I store the "logic" of the nested If as custom vertex properties, and simply follow the results, highlighting the appropriate edges. Clearly, code can use the results for control of flow, and that flow can be visualized with the graph. If you really wanted to do this kind of thing, packaging up some functions to parse your logic and create the graph/properties/query API would make sense.

ClearAll[gr, x, y, z]

Manipulate[
With[{x = x, y = y, z = z},
HighlightGraph[
gr, {a \[DirectedEdge] (res = ReleaseHold@PropertyValue[{gr, a}, "Logic"]),
res \[DirectedEdge]
ReleaseHold@PropertyValue[{gr, res}, "Logic"]}]], {x, {1, 2}}, {y, {1, 2}}, {z, {1, 2}},
Initialization :> {gr =
Graph[{Property[a, "Logic" -> Hold[If[x == 1, b, c]]],
Property[b, "Logic" -> Hold[If[y == 1, d, e, d]]],
Property[c, "Logic" -> Hold[If[z == 1, f, g, f]]], d, e, f,
g}, {a \[DirectedEdge] b, a \[DirectedEdge] c,
b \[DirectedEdge] d, b \[DirectedEdge] e, c \[DirectedEdge] f,
c \[DirectedEdge] g}, VertexLabels -> "Name"]}]


• Thank you @rasher, but is there a way to then compile this graph into a usable function? or will this only work for illustrative purposes? – Aron Mar 2 '14 at 13:08
• @Aron: this was, as I said, to illustrate the idea in the comment, and as said there, you'd have to write the framework yourself to do such things. MMA is not a data-flow language, nor a graph-based language, so if you want/need that you need to do it yourself (I'm not aware of any packages in those areas either). – ciao Mar 2 '14 at 22:41
• Ok, thank you, @rasher – Aron Mar 2 '14 at 23:57