Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Represent Flowchart-specified Algorithms in Haskell

Tags:

haskell

I'm confronted with the task of implementing algorithms (mostly business logic style) expressed as flowcharts. I'm aware that flowcharts are not the best algorithm representation due to its spaghetti-code property (would this be a use-case for CPS?), but I'm stuck with the specification expressed as flowcharts.

Although I could transform the flowcharts into more appropriate equivalent representations before implementing them, that could make it harder to "recognize" the orginal flow-chart in the resulting implementation, so I was hoping there is some way to directly represent flowchart-algorithms as (maybe monadic) EDSLs in Haskell, so that the semblance to the original flowchart-specification would be (more) obvious.

like image 708
hvr Avatar asked Dec 14 '10 10:12

hvr


2 Answers

One possible representation of flowcharts is by using a group of mutually tail-recursive functions, by translating "go to step X" into "evaluate function X with state S". For improved readability, you can combine into a single function both the action (an external function that changes the state) and the chain of if/else or pattern matching that helps determine what step to take next.

This is assuming, of course, that your flowcharts are to be hardcoded (as opposed to loaded at runtime from an external source).

like image 86
Victor Nicollet Avatar answered Oct 23 '22 05:10

Victor Nicollet


Sounds like Arrows would fit exactly what you describe. Either do a visualization of arrows (should be quite simple) or generate/transform arrow code from flow-graphs if you must.

like image 36
snk_kid Avatar answered Oct 23 '22 04:10

snk_kid