           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

    throw new Error "Missing definition of a function"
  # Find invalid (hoistable) wheres and let the function body
  # define them

  [validWheres, hoistableWheres] = findHoistableWheres wheres
  wheresDef = compileWhereImpl validWheres
  bodyDef = compileImpl body, hoistableWheres
  [wheresDef, bodyDef]

compileWhereWithHoisting = (wheres) ->
  graph = constructDependencyGraph wheres


# Returns two lists, first the wheres that can be defined, second
# the wheres which use an identifier which is not in scope (possibly
# defined inside match)

  # get list of names from wheres
  # for each where, get list of used names
  #   if some used name is not in newly defined or any parent scope
  #     save it for later, maybe it's defined inside body
findHoistableWheres = ([graph, lookupTable]) ->
  reversedDependencies = reverseGraph graph
  hoistableNames = {}

          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
