           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


          for n in dep.names
            hoistableNames[n] = yes
  for where in graph
    {names} = where
    hoisted = no
    for n in names
      # if one of the names needs hoisting
      if hoistableNames[n]
        hoistable.push where
        hoisted = yes
        break
    if not hoisted
      valid.push where
  [
    [hoistable, lookupTableForGraph hoistable]
    [valid, lookupTableForGraph valid]
  ]


#compileWhereImpl = (wheres) ->
#  sorted = checkEvenness sortWheres wheres
#  "#{sorted.map(compileDef).join '\n'}"


sortWheres = (wheres) ->
  xs = (sortTopologically constructDependencyGraph wheres).map ({def}) -> def
