@@ -22,6 +22,7 @@ import qualified Data.Map as M
2222import qualified Data.HashSet as HS
2323import qualified Data.Set as S
2424import Control.Monad.State hiding (join)
25+import Data.Bifunctor (second)
2526import Data.Maybe
2627import Data.Either
2728import Data.Char (isUpper)
@@ -90,10 +91,10 @@ runRelationalExprM :: RelationalExprEnv -> RelationalExprM a -> Either Relationa
9091runRelationalExprM env m = runIdentity (runExceptT (runReaderT m env))
9192
9293reGraph :: RelationalExprM TransactionGraph
93−reGraph = re_graph <$> R.ask
94+reGraph = asks re_graph
9495
9596reContext :: RelationalExprM DatabaseContext
96−reContext = re_context <$> R.ask
97+reContext = asks re_context
9798
9899mkRelationalExprEnv :: DatabaseContext -> TransactionGraph -> RelationalExprEnv
99100mkRelationalExprEnv ctx graph =
@@ -138,12 +139,12 @@ data DatabaseContextEvalEnv = DatabaseContextEvalEnv
138139 }
139140
140141mkDatabaseContextEvalEnv :: TransactionId -> TransactionGraph -> DatabaseContextEvalEnv
141−mkDatabaseContextEvalEnv tid graph = DatabaseContextEvalEnv tid graph
142+mkDatabaseContextEvalEnv = DatabaseContextEvalEnv
142143
143144type DatabaseContextEvalMonad a = RWST DatabaseContextEvalEnv () DatabaseContextEvalState (ExceptT RelationalError Identity) a
144145
145146runDatabaseContextEvalMonad :: DatabaseContext -> DatabaseContextEvalEnv -> DatabaseContextEvalMonad () -> Either RelationalError DatabaseContextEvalState
146−runDatabaseContextEvalMonad ctx env m = runIdentity (runExceptT (fst <$> (execRWST m env freshEnv)))
147+runDatabaseContextEvalMonad ctx env m = runIdentity (runExceptT (fst <$> execRWST m env freshEnv))
147148 where
148149 freshEnv = mkDatabaseContextEvalState ctx
149150
@@ -159,7 +160,7 @@ dbcRelationalExprEnv =
159160 mkRelationalExprEnv <$> getStateContext <*> dbcGraph
160161
161162getStateContext :: DatabaseContextEvalMonad DatabaseContext
162−getStateContext = dbc_context <$> get
163+getStateContext = gets dbc_context
163164
164165putStateContext :: DatabaseContext -> DatabaseContextEvalMonad ()
165166putStateContext ctx' = do
@@ -200,7 +201,7 @@ freshGraphRefRelationalExprEnv mctx graph = GraphRefRelationalExprEnv {
200201 }
201202
202203gfGraph :: GraphRefRelationalExprM TransactionGraph
203−gfGraph = gre_graph <$> R.ask
204+gfGraph = asks gre_graph
204205
205206envContext :: RelationalExprEnv -> DatabaseContext
206207envContext = re_context
@@ -544,9 +545,9 @@ evalGraphRefDatabaseContextExpr (RemoveTypeConstructor tConsName) = do
544545 let newTypes = filter (\(tCons, _) -> TCD.name tCons /= tConsName) oldTypes
545546 putStateContext $ currentContext { typeConstructorMapping = newTypes }
546547
547−evalGraphRefDatabaseContextExpr (MultipleExpr exprs) = do
548+evalGraphRefDatabaseContextExpr (MultipleExpr exprs) =
548549 --the multiple expressions must pass the same context around- not the old unmodified context
549− mapM_ (\expr -> evalGraphRefDatabaseContextExpr expr) exprs
550+ mapM_ evalGraphRefDatabaseContextExpr exprs
550551
551552evalGraphRefDatabaseContextExpr (RemoveAtomFunction funcName) = do
552553 currentContext <- getStateContext
@@ -576,7 +577,7 @@ evalGraphRefDatabaseContextExpr (ExecuteDatabaseContextFunction funcName atomArg
576577 context <- getStateContext
577578 graph <- dbcGraph
578579 --resolve atom arguments
579− let eAtomTypes = mapM (\atomExpr -> runGraphRefRelationalExprM gfEnv (typeForGraphRefAtomExpr emptyAttributes atomExpr)) atomArgExprs
580+ let eAtomTypes = mapM (runGraphRefRelationalExprM gfEnv . typeForGraphRefAtomExpr emptyAttributes) atomArgExprs
580581 eFunc = databaseContextFunctionForName funcName (dbcFunctions context)
581582 gfEnv = freshGraphRefRelationalExprEnv (Just context) graph
582583 case eFunc of
@@ -591,11 +592,13 @@ evalGraphRefDatabaseContextExpr (ExecuteDatabaseContextFunction funcName atomArg
591592 case eAtomTypes of
592593 Left err -> dbErr err
593594 Right atomTypes -> do
594− let mValidTypes = map (\(expType, actType) -> case atomTypeVerify expType actType of
595− Left err -> Just err
596− Right _ -> Nothing) (zip (dbcFuncType func) atomTypes)
595+ let mValidTypes = zipWith (\ expType actType
596+ -> case atomTypeVerify expType actType of
597+ Left err -> Just err
598+ Right _ -> Nothing)
599+ (dbcFuncType func) atomTypes
597600 typeErrors = catMaybes mValidTypes
598− eAtomArgs = map (\arg -> runGraphRefRelationalExprM gfEnv (evalGraphRefAtomExpr emptyTuple arg)) atomArgExprs
601+ eAtomArgs = map (runGraphRefRelationalExprM gfEnv . evalGraphRefAtomExpr emptyTuple) atomArgExprs
599602 if length (lefts eAtomArgs) > 1 then
600603 dbErr (someErrors (lefts eAtomArgs))
601604 else if not (null typeErrors) then
@@ -639,8 +642,7 @@ getDBCIOContext = dbc_context <$> RWS.get
639642getDBCIORelationalExprEnv :: DatabaseContextIOEvalMonad RelationalExprEnv
640643getDBCIORelationalExprEnv = do
641644 context <- getDBCIOContext
642− env <- RWS.ask
643− pure $ mkRelationalExprEnv context (dbcio_graph env)
645+ mkRelationalExprEnv context . dbcio_graph <$> RWS.ask
644646
645647evalGraphRefDatabaseContextIOExpr :: GraphRefDatabaseContextIOExpr -> DatabaseContextIOEvalMonad (Either RelationalError ())
646648#if !defined(PM36_HASKELL_SCRIPTING)
@@ -827,7 +829,7 @@ typeForRelationalExpr expr = do
827829 let gfExpr = runProcessExprM UncommittedContextMarker (processRelationalExpr expr)
828830 gfEnv = freshGraphRefRelationalExprEnv (Just context) graph
829831 runGf = runGraphRefRelationalExprM gfEnv (typeForGraphRefRelationalExpr gfExpr)
830− lift $ except $ runGf
832+ lift $ except runGf
831833
832834--returns a database context with all tuples removed
833835--this is useful for type checking and optimization
@@ -929,7 +931,7 @@ tupleExprCheckNewAttrName attrName rel = if isRight (attributeForName attrName r
929931 Right rel
930932
931933extendGraphRefTupleExpressionProcessor :: Relation -> GraphRefExtendTupleExpr -> GraphRefRelationalExprM (Attributes, RelationTuple -> Either RelationalError RelationTuple)
932−extendGraphRefTupleExpressionProcessor relIn (AttributeExtendTupleExpr newAttrName atomExpr) = do
934+extendGraphRefTupleExpressionProcessor relIn (AttributeExtendTupleExpr newAttrName atomExpr) =
933935-- renv <- askEnv
934936 -- check that the attribute name is not in use
935937 case tupleExprCheckNewAttrName newAttrName relIn of
@@ -971,7 +973,7 @@ evalGraphRefAtomExpr tupIn (FunctionAtomExpr funcName arguments tid) = do
971973 throwError (FunctionArgumentCountMismatchError expectedArgCount actualArgCount)
972974 else do
973975 let zippedArgs = zip (safeInit (atomFuncType func)) argTypes
974− mapM_ (\(expType, actType) -> do
976+ mapM_ (\(expType, actType) ->
975977 lift $ except (atomTypeVerify expType actType)) zippedArgs
976978 evaldArgs <- mapM (evalGraphRefAtomExpr tupIn) arguments
977979 case evalAtomFunction func evaldArgs of
@@ -1160,14 +1162,14 @@ evalGraphRefTupleExprs mAttrs (TupleExprs fixedMarker tupleExprL) = do
11601162 pure (A.attributesFromList mostResolvedTypes)
11611163 --strategy: if all the tuple expr transaction markers refer to one location, then we can pass the type constructor mapping from that location, otherwise, we cannot assume that the types are the same
11621164 tConsMap <- case singularTransactions tupleExprL of
1163− SingularTransactionRef commonTransId -> do
1165+ SingularTransactionRef commonTransId ->
11641166 typeConstructorMapping <$> gfDatabaseContextForMarker commonTransId
1165− NoTransactionsRef -> do
1167+ NoTransactionsRef ->
11661168 typeConstructorMapping <$> gfDatabaseContextForMarker fixedMarker
11671169 -- if there are multiple transaction markers in the TupleExprs, then we can't assume a single type constructor mapping- this could be improved in the future, but if all the tuples are fully resolved, then we don't need further resolution
11681170 _ -> throwError TupleExprsReferenceMultipleMarkersError
11691171 lift $ except $ validateAttributes tConsMap finalAttrs
1170− mapM (\tup -> lift $ except $ resolveTypesInTuple finalAttrs tConsMap tup) tuples
1172+ mapM (lift . except . resolveTypesInTuple finalAttrs tConsMap) tuples
11711173
11721174
11731175--resolveAttributes (Attribute "gonk" (ConstructedAtomType "Either" (fromList [("a",IntegerAtomType),("b",TypeVariableType "b")]))) (Attribute "gonk" (ConstructedAtomType "Either" (fromList [("a",TypeVariableType "a"),("b",TextAtomType)])))
@@ -1313,7 +1315,7 @@ evalGraphRefRelationalExpr (Extend extendTupleExpr expr) = do
13131315 rel <- evalGraphRefRelationalExpr expr
13141316 (newAttrs, tupProc) <- extendGraphRefTupleExpressionProcessor rel extendTupleExpr
13151317 lift $ except $ relMogrify tupProc newAttrs rel
1316−evalGraphRefRelationalExpr expr@With{} = do
1318+evalGraphRefRelationalExpr expr@With{} =
13171319 --strategy A: add relation variables to the contexts in the graph
13181320 --strategy B: drop in macros in place (easier programmatically)
13191321 --strategy B implementation
@@ -1341,7 +1343,7 @@ typeForGraphRefRelationalExpr (ExistingRelation rel) = pure (emptyRelationWithAt
13411343typeForGraphRefRelationalExpr (MakeRelationFromExprs mAttrExprs tupleExprs) = do
13421344 mAttrs <- case mAttrExprs of
13431345 Just attrExprs -> do
1344− attrs <- mapM (\e -> evalGraphRefAttributeExpr e) attrExprs
1346+ attrs <- mapM evalGraphRefAttributeExpr attrExprs
13451347 pure (Just (attributesFromList attrs))
13461348 Nothing -> pure Nothing
13471349 tuples <- evalGraphRefTupleExprs mAttrs tupleExprs
@@ -1386,9 +1388,9 @@ typeForGraphRefRelationalExpr (Restrict pred' expr) = do
13861388 expr' <- typeForGraphRefRelationalExpr expr
13871389 filt <- predicateRestrictionFilter (attributes expr') pred'
13881390 lift $ except $ restrict filt expr'
1389−typeForGraphRefRelationalExpr Equals{} = do
1391+typeForGraphRefRelationalExpr Equals{} =
13901392 pure relationFalse
1391−typeForGraphRefRelationalExpr NotEquals{} = do
1393+typeForGraphRefRelationalExpr NotEquals{} =
13921394 pure relationFalse
13931395typeForGraphRefRelationalExpr (Extend extendTupleExpr expr) = do
13941396 rel <- typeForGraphRefRelationalExpr expr
@@ -1400,7 +1402,7 @@ typeForGraphRefRelationalExpr expr@(With withs _) = do
14001402 case M.lookup macroName rvs of
14011403 Just _ -> lift $ except $ Left (RelVarAlreadyDefinedError macroName) --this error does not include the transaction marker, but should be good enough to identify the cause
14021404 Nothing -> pure ()
1403− mapM_ checkMacroName (map fst withs)
1405+ mapM_ (checkMacroName . fst) withs
14041406 typeForGraphRefRelationalExpr expr'
14051407
14061408evalGraphRefAttributeNames :: GraphRefAttributeNames -> GraphRefRelationalExpr -> GraphRefRelationalExprM (S.Set AttributeName)
@@ -1446,7 +1448,7 @@ evalGraphRefAttributeExpr (AttributeAndTypeNameExpr attrName tCons tid) = do
14461448evalGraphRefAttributeExpr (NakedAttributeExpr attr) = pure attr
14471449
14481450mkEmptyRelVars :: RelationVariables -> RelationVariables
1449−mkEmptyRelVars rvMap = M.map mkEmptyRelVar rvMap
1451+mkEmptyRelVars = M.map mkEmptyRelVar
14501452 where
14511453 mkEmptyRelVar expr@MakeRelationFromExprs{} = expr --do not truncate here because we might lose essential type information in emptying the tuples
14521454 mkEmptyRelVar (MakeStaticRelation attrs _) = MakeStaticRelation attrs emptyTupleSet
@@ -1463,7 +1465,7 @@ mkEmptyRelVars rvMap = M.map mkEmptyRelVar rvMap
14631465 mkEmptyRelVar (Equals exprA exprB) = Equals (mkEmptyRelVar exprA) (mkEmptyRelVar exprB)
14641466 mkEmptyRelVar (NotEquals exprA exprB) = NotEquals (mkEmptyRelVar exprA) (mkEmptyRelVar exprB)
14651467 mkEmptyRelVar (Extend extTuple expr) = Extend extTuple (mkEmptyRelVar expr)
1466− mkEmptyRelVar (With macros expr) = With (map (\(rv, expr') -> (rv, mkEmptyRelVar expr')) macros) (mkEmptyRelVar expr)
1468+ mkEmptyRelVar (With macros expr) = With (map (second mkEmptyRelVar) macros) (mkEmptyRelVar expr)
14671469
14681470
14691471dbErr :: RelationalError -> DatabaseContextEvalMonad ()