code: func NewGraph(modules map[string]*Module, list func(Ref) []*Rule) *Graph {
  
  	graph := &Graph{
  		adj:    map[util.T]map[util.T]struct{}{},
  		nodes:  map[util.T]struct{}{},
  		sorted: nil,
  	}
  
  	// Create visitor to walk a rule AST and add edges to the rule graph for
  	// each dependency.
  	vis := func(a *Rule) Visitor {
  		stop := false
  		return NewGenericVisitor(func(x interface{}) bool {
  			switch x := x.(type) {
  			case Ref:
  				for _, b := range list(x.GroundPrefix()) {
  					for node := b; node != nil; node = node.Else {
  						graph.addDependency(a, node)
  					}
  				}
  			case *Rule:
  				if stop {
  					// Do not recurse into else clauses (which will be handled
  					// by the outer visitor.)
  					return true
  				}
  				stop = true
  			}
  			return false
  		})
  	}
  
  	// Walk over all rules, add them to graph, and build adjencency lists.
  	for _, module := range modules {
  		WalkRules(module, func(a *Rule) bool {
  			graph.addNode(a)
  			Walk(vis(a), a)
  			return false
  		})
  	}
  
  	return graph
  }
docs: // NewGraph returns a new Graph based on modules. The list function must return
  // the rules referred to directly by the ref.