anchor.diff:      ets:delete(type_defs),
       ets:delete(record_fields).
   
  --spec insert_typedef(aeso_syntax:id(), [aeso_syntax:tvar()], aeso_syntax:typedef()) -> ok.
  -insert_typedef(Id = {id, Attrs, Name}, Args, Typedef) ->
  -    ets:insert(type_defs, {Name, Args, Typedef}),
  +%% Key used in type_defs ets table.
  +-spec type_key(type_id()) -> [string()].
  +type_key({id, _, Name})   -> [Name];
  +type_key({qid, _, QName}) -> QName.
  +
  +-spec insert_typedef(type_id(), [aeso_syntax:tvar()], aeso_syntax:typedef()) -> ok.
  +insert_typedef(Id, Args, Typedef) ->
  +    Attrs = aeso_syntax:get_ann(Id),
  +    Key   = type_key(Id),
  +    ets:insert(type_defs, {Key, Args, Typedef}),
       case Typedef of
           {record_t, Fields} ->
               [insert_record_field(FieldName, #field_info{ kind     = record,
  
positive.code:     destroy_field_constraints(),
      ok.
  
  record_type_name({app_t, _Attrs, RecId={id, _, _}, _Args}) ->
      RecId;
  record_type_name(RecId={id, _, _}) ->
      RecId.
  
  solve_for_uvar(UVar = {uvar, Attrs, _}, Fields) ->
      %% Does this set of fields uniquely identify a record type?
      FieldNames = [ Name || {id, _, Name} <- Fields ],
      UniqueFields = lists:usort(FieldNames),
      Candidates = [RecName || {_, #field_info{record_t = {app_t, _, {id, _, RecName}, _}}} <- lookup_record_field(hd(FieldNames))],
      TypesAndFields = [case lookup_type(RecName) of
                            {_, {record_t, RecFields}} ->
                                {RecName, [Field || {field_t, _, {id, _, Field}, _} <- RecFields]};
  
negative.code:                | aeso_syntax:con() | aeso_syntax:qcon()  %% contracts
                 | aeso_syntax:tvar()
                 | {uvar, aeso_syntax:ann(), reference()}.
  
  -type type_id() :: aeso_syntax:id() | aeso_syntax:qid().
  
  -define(is_type_id(T), element(1, T) =:= id orelse element(1, T) =:= qid).
  
  -type why_record() :: aeso_syntax:field(aeso_syntax:expr())
                      | {proj, aeso_syntax:ann(), aeso_syntax:expr(), aeso_syntax:id()}.
  