functor TOPSORT( structure G: GraphSig )=
struct
type Graph = G.Graph
type Vertex = G.Vertex
exception Cycle
local
fun member [] _ = false
| member (h :: t) x = x = h orelse member t x
in
fun topSort g =
let fun sort [] path sorted = sorted
| sort (x :: xs) path sorted =
if member path x
then raise Cycle
else if member sorted x
then sort xs path sorted
else let val afterx = sort
(G.adj g x)
(x :: path)
sorted
in
sort xs path (x :: afterx)
end
in
sort (G.vertices g) [] []
end
end
end ;