Name resolution and the environment

During typechecking, Unison substitutes free variables in an expression by looking them up in an environment populated from acodebaseof available definitions. A Unison codebase is a database of term and type definitions, indexed byhashesand names.

A name in the environment can refer to either terms or types, or both (a type name can never be confused with a term name).

Suffix-based name resolution

If the list ofsegmentsof a name (''base.List.map'' has the segments[base,List,map])is a suffix of exactly one fully qualified name in the environment, Unison substitutes that name in the expression with a reference to the definition. For example, the fully qualified name.base.List.mapcould be referenced viabase.List.map,List.mapas long as no other definitions end inbase.List.maporList.map.This reduces the number ofimportsneeded and cuts down on needing to remember the fully qualified names for definitions. ("Was itbase.List.maporutil.List.map?")

Hash literalsin the program are substituted with references to the definitions in the environment whose hashes they match.

If a free term variable in the program cannot be found in the environment and is not the name of another term in scope in the program itself, or if an free variable matches more than one name (it's ambiguous), Unison triestype-directed name resolution.

Type-directed name resolution

During typechecking, if Unison encounters a free term variable that is not a term name in the environment, Unison attemptstype-directed name resolution,which:

  1. Finds term definitions in the environment whoseunqualifiedname is the same as the free variable.
  2. If exactly one of those terms has a type that conforms to the expected type of the variable (the type system has always inferred this type already at this point), perform that substitution and resume typechecking.

If name resolution is unable to find the definition of a name, or is unable to disambiguate an ambiguous name, Unison reports an error.