{- ----------------------------------------------------------------------------- Copyright 2019-2020 Kevin P. Barry Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ----------------------------------------------------------------------------- -} -- Author: Kevin P. Barry [ta0kira@gmail.com] module Types.Positional ( Positional(..), alwaysPair, processPairs, processPairsT, ) where import Control.Monad (Monad(..)) import Control.Monad.Trans (MonadTrans(..)) import Base.CompileError newtype Positional a = Positional { pValues :: [a] } deriving (Eq,Ord,Show) instance Functor Positional where fmap f = Positional . fmap f . pValues alwaysPair :: Monad m => a -> b -> m (a,b) alwaysPair x y = return (x,y) processPairs :: (Show a, Show b, CompileErrorM m) => (a -> b -> m c) -> Positional a -> Positional b -> m [c] processPairs f (Positional ps1) (Positional ps2) | length ps1 == length ps2 = collectAllOrErrorM $ map (uncurry f) (zip ps1 ps2) | otherwise = compileError $ "Parameter count mismatch: " ++ show ps1 ++ " vs. " ++ show ps2 processPairsT :: (MonadTrans t, Monad (t m), Show a, Show b, CompileErrorM m) => (a -> b -> t m c) -> Positional a -> Positional b -> t m [c] processPairsT f (Positional ps1) (Positional ps2) | length ps1 == length ps2 = sequence $ map (uncurry f) (zip ps1 ps2) | otherwise = lift $ compileError $ "Parameter count mismatch: " ++ show ps1 ++ " vs. " ++ show ps2