: SourcePos -> RTAlias RTyVar SpecType -> [BareType] -> RReft -> BareM SpecType
expandRTAliasApp l rta args r
  | length args == length αs + length εs
    = do ts <- mapM (ofBareType l)                   $ take (length αs) args
         es <- mapM (resolve l . exprArg (show err)) $ drop (length αs) args
         let tsu = zipWith (\α t -> (α, toRSort t, t)) αs ts
         let esu = mkSubst $ zip (symbol <$> εs) es
         return $ subst esu . (`strengthen` r) . subsTyVars_meet tsu $ rtBody rta
  | otherwise
    = Ex.throw err
  where
    αs        = rtTArgs rta
    εs        = rtVArgs rta
    err       :: Error
    err       = ErrAliasApp (sourcePosSrcSpan l) (length args) (pprint $ rtName rta) (sourcePosSrcSpan $ rtPos rta) (length αs + length εs)

-- | exprArg converts a tyVar to an exprVar because parser cannot tell
-- HORRIBLE HACK To allow treating upperCase X as value variables X
-- e.g. type Matrix a Row Col = List (List a Row) Col

exprArg _   (RExprArg e)
  =
expandRTAliasApp :: SourcePos -> RTAlias RTyVar SpecType -> [BareType] -> RReft -> BareM SpecType
expandRTAliasApp l rta args r
  | length args == length αs + length εs
    = do ts <- mapM (ofBareType l)                   $ take (length αs) args
         es <- mapM (resolve l . exprArg (show err)) $ drop (length αs) args
         let tsu = zipWith (\α t -> (α, toRSort t, t)) αs ts
         let esu = mkSubst $ zip (symbol <$> εs) es
         return $ subst esu . (`strengthen` r) . subsTyVars_meet tsu $ rtBody rta
  | otherwise
    = Ex.throw err
  where
    αs        = rtTArgs rta
    εs        = rtVArgs rta
    err       :: Error
    err       = ErrAliasApp (sourcePosSrcSpan l) (length args) (pprint $ rtName rta) (sourcePosSrcSpan $ rtPos rta) (length αs + length εs)

-- | exprArg converts a tyVar to an exprVar because parser cannot tell
-- HORRIBLE HACK To allow treating upperCase X as value variables X
-- e.g. type Matrix a Row Col = List (List a Row) Col
expandRTAliasApp l rta args r
  | length args == length αs + length εs
    = do ts <- mapM (ofBareType l)                   $ take (length αs) args
         es <- mapM (resolve l . exprArg (show err)) $ drop (length αs) args
         let tsu = zipWith (\α t -> (α, toRSort t, t)) αs ts
         let esu = mkSubst $ zip (symbol <$> εs) es
         return $ subst esu . (`strengthen` r) . subsTyVars_meet tsu $ rtBody rta
  | otherwise
    = Ex.throw err
  where
    αs        = rtTArgs rta
    εs        = rtVArgs rta
    err       :: Error
    err       = ErrAliasApp (sourcePosSrcSpan l) (length args) (pprint $ rtName rta) (sourcePosSrcSpan $ rtPos rta) (length αs + length εs)

-- | exprArg converts a tyVar to an exprVar because parser cannot tell
-- HORRIBLE HACK To allow treating upperCase X as value variables X
-- e.g. type Matrix a Row Col = List (List a Row) Col