diff --git a/checks/ArrayUsageCheck.hs b/checks/ArrayUsageCheck.hs index c513445..6360fea 100644 --- a/checks/ArrayUsageCheck.hs +++ b/checks/ArrayUsageCheck.hs @@ -232,7 +232,7 @@ formatProblem varToIndex (eq, ineq) ++ " " ++ op ++ " " ++ show (negate $ item ! 0) showEq :: Array CoeffIndex Integer -> m String - showEq = liftM (concat . intersperse " + ") . mapM showItem . filter ((/= 0) . snd) . tail . assocs + showEq = liftM (joinWith " + ") . mapM showItem . filter ((/= 0) . snd) . tail . assocs showItem :: (CoeffIndex, Integer) -> m String showItem (n, a) = case find ((== n) . snd) $ Map.assocs varToIndex of @@ -255,7 +255,7 @@ solve (ls,vm,(eq,ineq)) = case solveProblem eq ineq of formatSolution :: (CSMR m, Monad m) => VarMap -> VariableMapping -> m String formatSolution varToIndex vm = do names <- mapM valOfVar $ Map.assocs varToIndex - return $ concat $ intersperse " , " $ catMaybes names + return $ joinWith " , " $ catMaybes names where indexToVar = flip lookup $ map revPair $ Map.assocs varToIndex @@ -275,7 +275,7 @@ formatSolution varToIndex vm -1 -> "-" n -> show n ++ "*" - showWithCoeff xs = liftM (concat . intersperse " + ") $ mapM showWithCoeff' xs + showWithCoeff xs = liftM (joinWith " + ") $ mapM showWithCoeff' xs valOfVar (varExp,k) = case Map.lookup k indexToConst of Nothing -> return Nothing @@ -290,7 +290,7 @@ formatSolution varToIndex vm formatBounds _ [] = "" formatBounds f [b] = f b - formatBounds f bs = f $ "(" ++ concat (intersperse "," bs) ++ ")" + formatBounds f bs = f $ "(" ++ joinWith "," bs ++ ")" showFlattenedExpSet :: Monad m => (A.Expression -> m String) -> Set.Set FlattenedExp -> m String showFlattenedExpSet showExp s = liftM concat $ sequence $ intersperse (return " + ") $ map (showFlattenedExp showExp) $ Set.toList s diff --git a/checks/Check.hs b/checks/Check.hs index a40a3db..79733e4 100644 --- a/checks/Check.hs +++ b/checks/Check.hs @@ -446,7 +446,7 @@ showCodeExSet :: (CSMR m, Ord a, ShowOccam a, ShowRain a) => ExSet a -> m String showCodeExSet Everything = return "" showCodeExSet (NormalSet s) = do ss <- mapM showCode (Set.toList s) - return $ "{" ++ concat (intersperse ", " ss) ++ "}" + return $ "{" ++ joinWith ", " ss ++ "}" checkInitVarPass :: Pass checkInitVarPass = pass "checkInitVar" [] [] diff --git a/common/Utils.hs b/common/Utils.hs index fcfbe20..46d3378 100644 --- a/common/Utils.hs +++ b/common/Utils.hs @@ -328,7 +328,7 @@ showMaybe showFunc (Just x) = "Just " ++ showFunc x showMaybe _ Nothing = "Nothing" showListCustom :: (a -> String) -> [a] -> String -showListCustom showFunc list = "[" ++ concat (intersperse "," (map showFunc list)) ++ "]" +showListCustom showFunc list = "[" ++ joinWith "," (map showFunc list) ++ "]" showPairCustom :: (a -> String) -> (b -> String) -> (a,b) -> String showPairCustom showA showB (a,b) = "(" ++ showA a ++ "," ++ showB b ++ ")" @@ -398,3 +398,9 @@ liftWrapStateT wrap m foldM1 :: Monad m => (a -> a -> m a) -> [a] -> m a foldM1 f (x:xs) = foldM f x xs foldM1 _ [] = fail "Empty list in foldM1" + +-- | A shortcut for concat and intersperse. +-- For example, @joinWith " " names@ is the same as @concat (intersperse " " +-- names)@ +joinWith :: [a] -> [[a]] -> [a] +joinWith x = concat . intersperse x diff --git a/frontends/ParseOccam.hs b/frontends/ParseOccam.hs index dd88bed..1ea4fe2 100644 --- a/frontends/ParseOccam.hs +++ b/frontends/ParseOccam.hs @@ -2004,7 +2004,7 @@ defaultDecl = concat [Token emptyMeta $ Pragma $ "TOCKEXTERNAL \"" ++ showOccam rt ++ " FUNCTION \"" ++ concatMap doubleStar op ++ "\"(" - ++ concat (intersperse "," params) + ++ joinWith "," params ++ ") = " ++ occamDefaultOperator op ts ++ "\"" diff --git a/pregen/GenNavAST.hs b/pregen/GenNavAST.hs index 5abd6c1..a162471 100644 --- a/pregen/GenNavAST.hs +++ b/pregen/GenNavAST.hs @@ -74,7 +74,7 @@ instancesFrom w -- left to apply. baseInst :: [String] baseInst - = [ "instance (" ++ concat (intersperse ", " context) ++ ") =>" + = [ "instance (" ++ joinWith ", " context ++ ") =>" , " Polyplate m () o0 (" ++ wName ++ ") where" ] ++ (if isAlgType wDType @@ -116,7 +116,7 @@ instancesFrom w ctrName = modPrefix ++ ctrS makeCtr vs = if isTuple - then "(" ++ (concat $ intersperse ", " vs) ++ ")" + then "(" ++ joinWith ", " vs ++ ")" else ctrName ++ concatMap (" " ++) vs ctrInput = makeCtr ["a" ++ show i | i <- argNums] ctrResult = makeCtr ["r" ++ show i | i <- argNums] diff --git a/pregen/GenOrdAST.hs b/pregen/GenOrdAST.hs index 326ed90..773a43c 100644 --- a/pregen/GenOrdAST.hs +++ b/pregen/GenOrdAST.hs @@ -58,7 +58,7 @@ ordFor' typeName x = process $ map processConstr $ dataTypeConstrs $ dataTypeOf ++ concat [ [ " compare (" ++ name ++ headL ++ ") (" ++ name ++ headR ++ ") = " ++ --Shortcut: if null comparisons then "EQ" else - "combineCompare [" ++ concat (intersperse "," comparisons) ++ "]" + "combineCompare [" ++ joinWith "," comparisons) ++ "]" ] ++ if isLast then [] else [ " compare (" ++ name ++ " {}) _ = LT" , " compare _ (" ++ name ++ " {}) = GT"] diff --git a/pregen/GenTagAST.hs b/pregen/GenTagAST.hs index b8ff8cd..eac1b3d 100644 --- a/pregen/GenTagAST.hs +++ b/pregen/GenTagAST.hs @@ -80,7 +80,7 @@ genItem' suffix typeName (num, name, paramTypes) where typeSuffix = "" - params = concat $ intersperse " -> " $ paramTypes ++ [typeName] + params = joinWith " -> " $ paramTypes ++ [typeName] n = show num mname = "m" ++ name ++ suffix