 # This function resolves the types of mutually recursive functions
 resolveDeferredTypes = (ctx) ->
   if _notEmpty ctx.deferredBindings()
-    groups = toNameSets sortedStronglyConnectedComponents deferredToGraph ctx.deferredBindings()
+    groups = topologicallySortedGroups ctx.deferredBindings()
     for group in groups
       bindings = newMap()
       for binding in ctx.deferredBindings() when inSet group, binding.name

syntaxType = (expression) ->
  # ignore type classes for now
  if isName expression
    expression.label = if isNotCapital expression then 'typevar' else 'typename'
  else if isTuple expression
    map syntaxType, (_terms expression)
  else if isCall expression

    .replace(/\|/g, 'or_')
    .replace(/\?/g, 'p_')

topologicallySortedGroups = (dependent) ->
  toNameSets sortedStronglyConnectedComponents dependentToGraph dependent

dependentToGraph = (dependent) ->
  nodes = newMap()
  findOrAdd = (name) ->
    lookupOrAdd nodes, name, {name: name, edges: []}
  for {name, deps} in dependent
    node = findOrAdd name
    for dep in deps
      node.edges.push findOrAdd dep.name
