// Souffle - A Datalog Compiler
// Copyright (c) 2016, The Souffle Developers. All rights reserved
// Licensed under the Universal Permissive License v 1.0 as shown at:
// - https://opensource.org/licenses/UPL
// - <souffle root>/licenses/SOUFFLE-UPL.txt

.type Node <: symbol

.decl links(A:Node, B:Node)
.input links()

.decl nodes(A:Node)
nodes(A) :-
  links(A,_) ; links(_,A).

.decl reaches(A:Node, B:Node)
reaches(A, B) :-
  links(A, B).
reaches(A, B) :-
  links(A, C), reaches(C, B).

.decl mutually_reaching(A:Node, B:Node)
mutually_reaching(A, B) :-
  reaches(A, B), reaches(B, A).
mutually_reaching(A,A) :-
  nodes(A).

.decl chain(A:Node, B:Node)
chain(A,B) :-
  mutually_reaching(A,B), ord(A) < ord(B).

.decl leader(A:Node)
leader(A) :-
  nodes(A), !chain(_,A).

.decl count_scc(A:Node, n:number)
.output count_scc()
count_scc(A, S) :-
  leader(A), S = count : {mutually_reaching(A,_)}.
