 resolveDeferredTypes = (ctx) ->
   if _notEmpty ctx.deferredBindings()
     groups = topologicallySortedGroups ctx.deferredBindings()
+    allDeferredConstraints = newMap()
+    deferredToParent = newSet()
     for group in groups
       bindings = newMap()
       shouldBeDeferred = no
       for binding in ctx.deferredBindings() when inSet group, binding.name
         added = (lookupOrAdd bindings, binding.name, (types: []))
+        added.id = binding.id
         added.scopeIndex = binding.scopeIndex
         added.types.push binding.type
         added.constraints = binding.constraints
         added.polymorphic = binding.polymorphic
         added.deps = binding.deps
         for dep in binding.deps
-          added = (lookupOrAdd bindings, dep.name, (types: []))
-          added.scopeIndex ?= dep.scopeIndex
-          added.types.push dep.type
+          addedDep = (lookupOrAdd bindings, dep.name, (types: []))
+          addedDep.id ?= dep.id
+          addedDep.scopeIndex ?= dep.scopeIndex
+          addedDep.types.push dep.type
+          if dep.defining
+            depDeferredConstraints = lookupInMap allDeferredConstraints, dep.scopeIndex
+            # If depDeferredConstraints is null the dependency must be mutually dependant
+            added.constraints = join added.constraints, depDeferredConstraints or []
 
       # names = concatConcatMaps map (({name, type}) -> newMapWith name, type), groupBindings
       # First get rid of instances of already resolved types
       unresolvedNames = newMap()
       for name, binding of values bindings
-        if canonicalType = ctx.finalType name, binding.scopeIndex
+        if canonicalType = ctx.finalType binding.id, binding.scopeIndex
           for type in binding.types
             unify ctx, type.type, (mapOrigin (freshInstance ctx, canonicalType).type, type.type.origin)
         else
-          if (ctx.isDeclared name) and (not ctx.isCurrentlyDeclared name) and not binding.deps
+          if (ctx.isDeclared binding.id) and (not ctx.isCurrentlyDeclared binding.id) and not binding.deps or
+              inSet deferredToParent, name
             shouldBeDeferred = yes
           else
             addToMap unresolvedNames, name, binding

    @savedScopes[closedScope.index] =
      parent: @currentScopeIndex()
      definitions: cloneMap closedScope
      boundTypeVariables: closedScope.boundTypeVariables

  currentScopeIndex: ->

  currentDeclarations: ->
    cloneMap @_scope()

  scopeIndexOfDeclaration: (name) ->
    (@_scopeOfDeclared @scopes.length - 1, name).index

  assignType: (name, type) ->
    @assignTypeTo name, (@_declarationInCurrentScope name), type

