It does not use CPS ... but I am working on a planar graph library for Haskell using a similar scheme to what you described above. Edges are added by specifying which existing front comes before or after it.
The actual implementation of the graph is performed, resulting in binary serialization working and working (using PLANAR_CODE for beginners, possibly Graph6 and Sparse6) and a few additional things.
You are currently getting a double graph (which you seem to have drawn as well) with a separate function, although I am considering the possibility of double-calculating each time you add an edge (assuming a connected graph).
The code can be obtained from darcs get http://code.haskell.org/~ivanm/planar-graph/ ; A usage example (which I am developing for this library) is at http://code.haskell.org/~ivanm/dangd/ .
Taken from the Haddock documentation as an example of its use:
For example, let g refer to the following graph (where n1 , etc. are both labels and variable names):
We can add an edge between n1 and n2 (using Anywhere as EdgePos , since there are currently no edges on node):
((e1,e2),g') = addEdge n1 Anywhere n2 Anywhere "e1" "e2" g
This will result in the following graph:
e2 ==== <--------------- ==== ( n1 ) ( n2 ) ==== ---------------> ==== e1 ==== ( n3 ) ====
If we want to add edges between n2 and n3 , we have three parameters for the location on n2 :
Use Anywhere : since there is only one other edge, it does not make a difference in terms of nesting where the second edge goes.
Place the new edge BeforeEdge e2 (clockwise around n2 ).
Place the new AfterEdge e2 edge (clockwise around n2 ).
Since n2 has only one edge, all three EdgePos values โโwill lead to the same graph, so we can arbitrarily choose one:
((e3,e4),g'') = addEdge n2 (BeforeEdge e2) n3 Anywhere "e3" "e4" g'
However, with more edges, care must be taken at which EdgePos value is used. Received Count:
e2 ==== <--------------- ==== ( n1 ) ( n2 ) ==== ---------------> ==== e1 | ^ | | e3 | | e4 | | v | ==== ( n3 ) ====
The same graph (up to the actual values โโof Edge , so it will not satisfy == ) would be obtained with:
((e4,e3), g'') = addEdge n3 Anywhere n2 (BeforeEdge e2) "e4" "e3" g'