       ctx.addToDeferred {name, type, deps: (map (({name}) -> name), deps)}
       for dep in deps
         ctx.addToDeferred {name: dep.name, type: dep.type, deps: [name]}
-      ctx.addType name, new TempType type
-    else
-      #log "adding type for lhs #{name}", currentType
-      ctx.addArity name, [] unless ctx.arity name
-      ctx.addType name, if ctx._nameExpression()
-        quantifyAll currentType
-      else
-        toForAll currentType
+      ctx.declare name, type: new TempType type
+    else
+      # TODO: this is because functions might declare arity before being declared
+      if not ctx.isDeclared name
+        ctx.declare name
+      ctx.assignType name,
+        type:
+          if ctx.isAtDefinition()
+            quantifyAll currentType
+          else
+            toForAll currentType
   # here I will create type schemes for all definitions
   # The problem is I don't know which are impricise, because the names are done inside the
   # pattern. I can use the context to know which types where added in the current assignment.


# end of Utils



exports.compileTopLevel = compileTopLevel
exports.compileTopLevelAndExpression = compileTopLevelAndExpression
exports.astizeList = astizeList
exports.astizeExpression = astizeExpression
exports.astizeExpressionWithWrapper = astizeExpressionWithWrapper
exports.syntaxedExpHtml = syntaxedExpHtml

# exports.compileModule = (source) ->
#   """
#   #{library}
#   var exports = {};
#   #{compileDefinitionsInModule source}
#   exports"""

exports.library = library

exports.isForm = isForm
exports.isAtom = isAtom


exports.join = join
exports.concatMap = concatMap
exports.concat = concat
exports.id = id
exports.map = map
exports.allMap = allMap
exports.all = all
exports.filter = filter
exports.partition = partition
exports._notEmpty = _notEmpty
exports._is = _is
exports.__ = __

exports._operator = _operator
exports._arguments = _arguments
exports._terms = _terms
exports._snd = _snd
exports._fst = _fst
exports._labelName = _labelName
exports._symbol = _symbol

  listOfLines compiledPairs

# This function resolves the types of mutually recursive functions
resolveDeferredTypes = (ctx) ->
  if _notEmpty ctx.deferredBindings()
    # TODO: proper dependency analysis to get the smallest circular deps
