           child.missing.push node.token
 
   [graph, lookupByName]
+
+lookupTableForGraph = (graph) ->
+  table = {}
+  for where in graph
+    {names} = where
+    for name in names
+      table[name] = where
+  table
+
+findNames = (pattern) ->
+  names = []
+  crawl pattern, (node) ->
+    if node.label is 'name'
+      names.push node.token
+  names
+
 
 dependencySet = ->
   numDependencies: 0

  lookupByName = {}
  deps = dependencySet()
  # find all defined names
  graph = []
  for [pattern, def], i in wheres
    graph[i] = whereDef =
      def: [pattern, def]
      set: dependencySet()
      names: []
      missing: []
    crawl pattern, (node) ->
      if node.label is 'name'
        whereDef.names.push node.token
        lookupByName[node.token] = whereDef
  # then construct local graph
  for [pattern, def], i in wheres



compileWheres = (ast) ->
  ast.scope = topScopeDefines()
  (compileWhereImpl inside ast).map(compileDef).join '\n'

compileWheresInModule = (ast) ->
  ast.scope = topScopeDefines()
  (compileWhereImpl inside ast).map(compileExportedDef).join '\n'

compileWhereImpl = (tokens) ->
  if tokens.length % 2 != 0
    throw new Error "missing definition in top level assignment"
  [readyWheres, hoistableWheres] = wheresWithHoisting whereList tokens
  if hoistableWheres.length > 0
    undefinedNames = []
    for {names} in hoistableWheres
      undefinedNames.push names...
    throw new Error "#{undefinedNames.join ', '} used but not defined"
  addToScopeAllNames readyWheres
  graphToWheres readyWheres

compileExportedDef = ([name, def]) ->
  if name.token
    identifier = validIdentifier name.token
    "var #{identifier} = exports['#{identifier}'] = #{compileImpl def};"
  else
    compileDef [name, def]
