scheme reverse = class type Int_set = Int-list, Pair_set = Int_set-list, Pair = Int_set, Versh = Int_set, Dug = Pair_set, Graph = Versh >< Dug value /* These functions are 'extern', but I defined them to gain better undestanding. Not all functions are defined... */ IsIn: Int_set >< Int -> Bool IsIn(set, elem) is elem isin elems(set), Union: Int_set >< Int -> Int_set Union(set, elem) is set ^ <.elem.>, SetMinus: Int_set >< Int_set -> Int_set SetMinus(set, set2) is <. elem | elem in set :- elem ~isin elems(set2) .>, PIsIn: Pair_set >< Pair -> Bool PIsIn(set, elem) is elem isin elems(set), PUnion: Pair_set >< Pair -> Pair_set PUnion(set, pair) is set ^ <.pair.>, PSetMinus: Pair_set >< Pair_set -> Pair_set PSetMinus(set, set2) is <. elem | elem in set :- elem ~isin elems(set2) .>, /* All 'extern' functions in C program not listed above are either mapped to internal RSL operations or not needed at all. For example, 'Card' is mapped to 'len', 'Copy' is mapped to '=' etc. 'IsEQ' and some other functions are not used anywhere. All other functions are listed in order of apperance in the source file. */ emptyGraph : Graph = (<..>, <.<..>.>), /* GDone - not needed, used as C garbage collection for sets */ GDone: Graph -> Graph GDone(g) is emptyGraph, /* GCopy - not needed, used for C to produce a copy of a graph */ GCopy: Graph -> Graph GCopy(g) is g, /* isCorrectEdge*/ IsCorrectEdge: Versh >< Pair -> Bool IsCorrectEdge(v, p) is len(p) ~= 2 /\ IsIn(v, p(0)) /\ IsIn(v, p(1)), /* GCreate */ GCreate: Versh >< Dug -> Graph GCreate(v, d) is local variable g: Graph in g := (v, <. duga | duga in d :- IsCorrectEdge(v, duga) .>); if (Validation(g)) then g else emptyGraph end end, /* GAddNode & GAddNodeP */ GAddNode: Graph >< Int -> Graph GAddNode((v, d), i) is (Union(v, i), d), /* GAddEdge & GAddEdgeP */ GAddEdge: Graph >< Pair -> Graph GAddEdge((v, d), p) is local variable g: Graph := emptyGraph in if (IsCorrectEdge(v, p)) then g := (v, PUnion(d, p)) end; if (Validation(g)) then g else emptyGraph end end, /* GRemoveEdge & GRemoveEdgeP */ GRemoveEdge: Graph >< Pair -> Graph GRemoveEdge((v, d), p) is if (IsCorrectEdge(v, p)) then (v, PSetMinus(d, <.p.>)) else (v, d) end, /* GRemoveNode & GRemoveNodeP */ GRemoveNode: Graph >< Int -> Graph GRemoveNode((v, d), i) is (SetMinus(v, <.i.>), <. duga | duga in d :- ~IsIn(duga, i).>), /* Way */ Way: Int >< Int >< Graph -> Bool Way(n1, n2, (v, d)) is if ( PIsIn(d, <.n1, n2.>) \/ PIsIn(d, <.n2, n1.>) ) then true else len <. duga | duga in d :- if IsIn(duga, n1) then local variable node: Int, gr: Graph in node := duga(0); if (node = n1) then node := duga(1) end; if (n1 = n2) then gr := GRemoveEdge((v, d), duga) else gr := GRemoveNode((v, d), n1) end; Way(node, n2, gr) end else false end .> ~=0 end, /* Validation */ Validation: Graph -> Bool Validation((v, d)) is len <. a | a in v :- Way(a, a, (v, d)) .> = 0, /* GConnect & GConnectP */ GConnect: Graph >< Graph -> Graph GConnect((v1, d1), (v2, d2)) is local variable g: Graph in g := (v1 ^ v2, d1 ^ d2); if (~Validation(g)) then g := (v1, d1) end; g end, /* GSelect & GSelectP */ GSelect: Graph >< Versh -> Graph GSelect((v, d), ver) is GCreate(ver, d), /* GRemoveSubGraph & GRemoveSubGraphP */ GRemoveSubGraph: Graph >< Graph -> Graph GRemoveSubGraph((gv, gd), (subv, subd)) is (SetMinus(gv, subv), <..>), /* dummy variable to get rid of commas*/ dummy: Bool = false end