tvar
    :: R.Region
    -> Env.Environment
    -> String
    -> Result.ResultErr
          (Either
              Var.Canonical
              (Var.Canonical, [String], Type.Canonical)
          )
tvar region env var =
  case adts ++ aliases of
    []  -> notFound region "type" (Map.keys (Env._adts env) ++ Map.keys (Env._aliases env)) var
    [v] -> Result.var' extract v
    vs  -> preferLocals' region env extract "type" vs var
  where
    adts =
        map Left (maybe [] Set.toList (Map.lookup var (Env._adts env)))

    aliases =
        map Right (maybe [] Set.toList (Map.lookup var (Env._aliases env)))

    extract value =
        case value of
          Left v -> v
          Right (v,_,_) -> v
tvar
    :: R.Region
    -> Env.Environment
    -> String
    -> Result.ResultErr
          (Either
              Var.Canonical
              (Var.Canonical, [String], Type.Canonical)
          )
tvar region env var =
  case adts ++ aliases of
    []  -> notFound region "type" (Map.keys (Env._adts env) ++ Map.keys (Env._aliases env)) var
    [v] -> Result.var' extract v
    vs  -> preferLocals' region env extract "type" vs var
  where
    adts =
        map Left (maybe [] Set.toList (Map.lookup var (Env._adts env)))

    aliases =
        map Right (maybe [] Set.toList (Map.lookup var (Env._aliases env)))

    extract value =
        case value of
          Left v -> v
          Right (v,_,_) -> v
tvar region env var =
  case adts ++ aliases of
    []  -> notFound region "type" (Map.keys (Env._adts env) ++ Map.keys (Env._aliases env)) var
    [v] -> Result.var' extract v
    vs  -> preferLocals' region env extract "type" vs var
  where
    adts =
        map Left (maybe [] Set.toList (Map.lookup var (Env._adts env)))

    aliases =
        map Right (maybe [] Set.toList (Map.lookup var (Env._aliases env)))

    extract value =
        case value of
          Left v -> v
          Right (v,_,_) -> v