     toInject = collectRequiresFor moduleName
     ctx = injectedContext toInject
     {js, ast} = compileCtxAstToJs topLevel, ctx, (astFromSource "(#{source})", -1, -1)
+    (finalizeTypes ctx, ast)
     replaceOrAddToMap compiledModules, moduleName,
       declared: (subtractContexts ctx, (injectedContext toInject)) # must recompute because ctx is mutated
       js: js
-    (finalizeTypes ctx, ast)
     js: js
     ast: ast
     types: typeEnumaration ctx

  all (map isNormalizedConstraintArgument, constraint.types.types)

isNormalizedConstraintArgument = (type) ->
  if type instanceof TypeVariable
    yes
  else if type instanceof TypeConstr
    no
  else if type instanceof TypeApp
    isNormalizedConstraintArgument type.op.type


typeEq = (a, b) ->
  if a instanceof TypeVariable and b instanceof TypeVariable or
      a instanceof TypeConstr and b instanceof TypeConstr
    a.name is b.name
  else if a instanceof QuantifiedVar and b instanceof QuantifiedVar
    a.var is b.var
  else if a instanceof TypeApp and b instanceof TypeApp
    (typeEq a.op, b.op) and (typeEq a.arg, b.arg)
  else if a instanceof ForAll and b instanceof ForAll
    typeEq a.type, b.type
  else if a instanceof Constrained and b instanceof Constrained
    (all zipWith typeEq, a.constraints, b.constraints) and
      (typeEq a.type, b.type)
  else if a instanceof ClassConstraint and b instanceof ClassConstraint
    a.className is b.className and typeEq a.types, b.types
  else if a instanceof Types and b instanceof Types
    all zipWith typeEq, a.types, b.types
  else
    no


findMatchingDefinitionsOnType = (type, definitions) ->
  ctx = new Context
  validDefinitions = filterMap ((name, def) -> def.type?), definitions # TODO: filter before
  typesUnify = (def) ->
    not isFailed mostGeneralUnifier (freshInstance ctx, def.type).type, type.type
  [typed, notTyped] = partitionMap typesUnify, validDefinitions
  # allDefs = join (setToArray typed), (setToArray notTyped)
  allDefs = concatMaps typed, notTyped # TODO: don't use object key ordering for ordering
  values mapMap (__ prettyPrint, _type), allDefs
