       s1 = matchType t1.types[0], t2.types[0]
       # We imply functional dependency of the form A a b c | a -> b c
       # s2 = mostGeneralUnifier (new Types (substituteList s1, t1.types[1...])), (new Types t2.types[1...])
-      s2 = unifyImpliedParams t1, t2
-      subUnion s1, s2
+      if isFailed s1
+        s1
+      else
+        unifyImpliedParams t1, t2
     else
       emptySubstitution()
   else
     unifyFail t1, t2
 
+# We need to check that the unification succeeds first before performing
+# it on the actual types
 unifyImpliedParams = (t1, t2) ->
-  mostGeneralUnifier (new Types t1.types[1...]), (new Types t2.types[1...])
+  types1 = new Types t1.types[1...]
+  types2 = new Types t2.types[1...]
+  makeSub = (types) ->
+    mapMap ((kind, name) -> new TypeVariable name, kind, {}), findFree types
+  subbed1 = substitute (makeSub types1), types1
+  subbed2 = substitute (makeSub types2), types2
+  if isFailed (sub = mostGeneralUnifier subbed1, subbed2)
+    sub
+  else
+    mostGeneralUnifier types1, types2
+
 
 # Used to convert between quantified and not quantified
 substitute = (substitution, type) ->

    "(: #{(map printType, join [type.type], type.constraints).join ' '})"
  else if type.TempType
    "(. #{printType type.type})"
  else if Array.isArray type
    "\"#{listOf type}\""
  else if type is undefined

  """
  '3', 3

  'defer in subdefinition'
  """
  f (fn [x]
    ""
    g (fn [y]
      ((fn [x] x) h))
    h ((fn [x] x) x))
  """
  '3', 3

  # TODO: support matching with the same name
  #       to implement this we need the iife to take as arguments all variables
  #       with the same names, since JavaScript shadows it too strongly and
