mangleCType :: CType -> DebugOr String
mangleCType t = case t of
  CTBool -> mkSuccess "b"
  CTI32 -> mkSuccess "i32"
  CTArrow argTs resT -> do
    argStrs <- traverse mangleCType argTs
    let argsStr = intercalate "." argStrs
    resStr <- mangleCType resT
    return $ "arrow" ++ argsStr ++ "." ++ resStr ++ ".."
  CTVar (TypeName s) -> fail $ "found type variable `" ++ s ++ "` while mangling"

-- | Transform a CType into an LLVM.AST.Type.
mangleCType :: CType -> DebugOr String
mangleCType t = case t of
  CTBool -> mkSuccess "b"
  CTI32 -> mkSuccess "i32"
  CTArrow argTs resT -> do
    argStrs <- traverse mangleCType argTs
    let argsStr = intercalate "." argStrs
    resStr <- mangleCType resT
    return $ "arrow" ++ argsStr ++ "." ++ resStr ++ ".."
  CTVar (TypeName s) -> fail $ "found type variable `" ++ s ++ "` while mangling"

-- | Transform a CType into an LLVM.AST.Type.
mangleCType t = case t of
  CTBool -> mkSuccess "b"
  CTI32 -> mkSuccess "i32"
  CTArrow argTs resT -> do
    argStrs <- traverse mangleCType argTs
    let argsStr = intercalate "." argStrs
    resStr <- mangleCType resT
    return $ "arrow" ++ argsStr ++ "." ++ resStr ++ ".."
  CTVar (TypeName s) -> fail $ "found type variable `" ++ s ++ "` while mangling"

-- | Transform a CType into an LLVM.AST.Type.