# will produce following topological order and lifetimes:
    ---------------
    rel_order
    ===============
    0       i
    1       b
    1       a
    2       c
    2       d
    3       o
    ===============
    ---------------
    rel_lt
    ===============
    1       i
    2       b
    2       a
    3       o
    3       c
    3       d
    ===============

struct i (x : s32)
struct a (x : s32)
struct b (x : s32)
struct c (x : s32)
struct d (x : s32)
struct o (x : s32)

a x if (i x)

a x if (b x)
b x if (a x)

c x if (b x)
d x if (c x)
c x if (d x)

o x if (d x)
# will produce following topological order and lifetimes: --------------- rel_order =============== 0 i 1 b 1 a 2 c 2 d 3 o =============== --------------- rel_lt =============== 1 i 2 b 2 a 3 o 3 c 3 d =============== struct i (x : s32) struct a (x : s32) struct b (x : s32) struct c (x : s32) struct d (x : s32) struct o (x : s32) a x if (i x) a x if (b x) b x if (a x) c x if (b x) d x if (c x) c x if (d x) o x if (d x)
// topological order of relations
.decl rel_order(n:number, rel:symbol)
rel_order(i,rel) :- Rel(rel), scc(scc_id, rel),
    scc_order(i, scc_id).

// relation lifetime (after which order can we free this relation)
.decl rel_lt(n:number, rel:symbol)
rel_lt(i,rel) :- Rel(rel), scc(scc_id, rel),
    !scc_dep(_, scc_id),
    scc_order(i, scc_id).
rel_lt(mxi,rel) :- Rel(rel), scc(scc_id, rel),
    scc_dep(scc_user, scc_id),
    mxi = max i : {
        scc_order(i, scc_user)
    }.
// topological order of relations .decl rel_order(n:number, rel:symbol) rel_order(i,rel) :- Rel(rel), scc(scc_id, rel), scc_order(i, scc_id). // relation lifetime (after which order can we free this relation) .decl rel_lt(n:number, rel:symbol) rel_lt(i,rel) :- Rel(rel), scc(scc_id, rel), !scc_dep(_, scc_id), scc_order(i, scc_id). rel_lt(mxi,rel) :- Rel(rel), scc(scc_id, rel), scc_dep(scc_user, scc_id), mxi = max i : { scc_order(i, scc_user) }.