@@ -22,6 +22,7 @@ import qualified Data.Map as M
22import qualified Data.HashSet as HS
22import qualified Data.HashSet as HS
23import qualified Data.Set as S
23import qualified Data.Set as S
24import Control.Monad.State hiding (join)
24import Control.Monad.State hiding (join)
25+import Data.Bifunctor (second)
25import Data.Maybe
26import Data.Maybe
26import Data.Either
27import Data.Either
27import Data.Char (isUpper)
28import Data.Char (isUpper)
@@ -90,10 +91,10 @@ runRelationalExprM :: RelationalExprEnv -> RelationalExprM a -> Either Relationa
90runRelationalExprM env m = runIdentity (runExceptT (runReaderT m env))
91runRelationalExprM env m = runIdentity (runExceptT (runReaderT m env))
92reGraph :: RelationalExprM TransactionGraph
93reGraph :: RelationalExprM TransactionGraph
93−reGraph = re_graph <$> R.ask
94+reGraph = asks re_graph
95reContext :: RelationalExprM DatabaseContext
96reContext :: RelationalExprM DatabaseContext
96−reContext = re_context <$> R.ask
97+reContext = asks re_context
98mkRelationalExprEnv :: DatabaseContext -> TransactionGraph -> RelationalExprEnv
99mkRelationalExprEnv :: DatabaseContext -> TransactionGraph -> RelationalExprEnv
99mkRelationalExprEnv ctx graph =
100mkRelationalExprEnv ctx graph =
@@ -138,12 +139,12 @@ data DatabaseContextEvalEnv = DatabaseContextEvalEnv
140mkDatabaseContextEvalEnv :: TransactionId -> TransactionGraph -> DatabaseContextEvalEnv
141mkDatabaseContextEvalEnv :: TransactionId -> TransactionGraph -> DatabaseContextEvalEnv
141−mkDatabaseContextEvalEnv tid graph = DatabaseContextEvalEnv tid graph
142+mkDatabaseContextEvalEnv = DatabaseContextEvalEnv
143type DatabaseContextEvalMonad a = RWST DatabaseContextEvalEnv () DatabaseContextEvalState (ExceptT RelationalError Identity) a
144type DatabaseContextEvalMonad a = RWST DatabaseContextEvalEnv () DatabaseContextEvalState (ExceptT RelationalError Identity) a
145runDatabaseContextEvalMonad :: DatabaseContext -> DatabaseContextEvalEnv -> DatabaseContextEvalMonad () -> Either RelationalError DatabaseContextEvalState
146runDatabaseContextEvalMonad :: 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))
148 freshEnv = mkDatabaseContextEvalState ctx
149 freshEnv = mkDatabaseContextEvalState ctx
@@ -159,7 +160,7 @@ dbcRelationalExprEnv =
159 mkRelationalExprEnv <$> getStateContext <*> dbcGraph
160 mkRelationalExprEnv <$> getStateContext <*> dbcGraph
161getStateContext :: DatabaseContextEvalMonad DatabaseContext
162getStateContext :: DatabaseContextEvalMonad DatabaseContext
162−getStateContext = dbc_context <$> get
163+getStateContext = gets dbc_context
164putStateContext :: DatabaseContext -> DatabaseContextEvalMonad ()
165putStateContext :: DatabaseContext -> DatabaseContextEvalMonad ()
165putStateContext ctx' = do
166putStateContext ctx' = do
@@ -200,7 +201,7 @@ freshGraphRefRelationalExprEnv mctx graph = GraphRefRelationalExprEnv {
202gfGraph :: GraphRefRelationalExprM TransactionGraph
203gfGraph :: GraphRefRelationalExprM TransactionGraph
203−gfGraph = gre_graph <$> R.ask
204+gfGraph = asks gre_graph
205envContext :: RelationalExprEnv -> DatabaseContext
206envContext :: RelationalExprEnv -> DatabaseContext
206envContext = re_context
207envContext = re_context
@@ -544,9 +545,9 @@ evalGraphRefDatabaseContextExpr (RemoveTypeConstructor tConsName) = do
544 let newTypes = filter (\(tCons, _) -> TCD.name tCons /= tConsName) oldTypes
545 let newTypes = filter (\(tCons, _) -> TCD.name tCons /= tConsName) oldTypes
545 putStateContext $ currentContext { typeConstructorMapping = newTypes }
546 putStateContext $ currentContext { typeConstructorMapping = newTypes }
547−evalGraphRefDatabaseContextExpr (MultipleExpr exprs) = do
548+evalGraphRefDatabaseContextExpr (MultipleExpr exprs) =
548 --the multiple expressions must pass the same context around- not the old unmodified context
549 --the multiple expressions must pass the same context around- not the old unmodified context
549− mapM_ (\expr -> evalGraphRefDatabaseContextExpr expr) exprs
550+ mapM_ evalGraphRefDatabaseContextExpr exprs
551evalGraphRefDatabaseContextExpr (RemoveAtomFunction funcName) = do
552evalGraphRefDatabaseContextExpr (RemoveAtomFunction funcName) = do
552 currentContext <- getStateContext
553 currentContext <- getStateContext
@@ -576,7 +577,7 @@ evalGraphRefDatabaseContextExpr (ExecuteDatabaseContextFunction funcName atomArg
576 context <- getStateContext
577 context <- getStateContext
577 graph <- dbcGraph
578 graph <- dbcGraph
578 --resolve atom arguments
579 --resolve atom arguments
579− let eAtomTypes = mapM (\atomExpr -> runGraphRefRelationalExprM gfEnv (typeForGraphRefAtomExpr emptyAttributes atomExpr)) atomArgExprs
580+ let eAtomTypes = mapM (runGraphRefRelationalExprM gfEnv . typeForGraphRefAtomExpr emptyAttributes) atomArgExprs
580 eFunc = databaseContextFunctionForName funcName (dbcFunctions context)
581 eFunc = databaseContextFunctionForName funcName (dbcFunctions context)
581 gfEnv = freshGraphRefRelationalExprEnv (Just context) graph
582 gfEnv = freshGraphRefRelationalExprEnv (Just context) graph
582 case eFunc of
583 case eFunc of
@@ -591,11 +592,13 @@ evalGraphRefDatabaseContextExpr (ExecuteDatabaseContextFunction funcName atomArg
591 case eAtomTypes of
592 case eAtomTypes of
592 Left err -> dbErr err
593 Left err -> dbErr err
593 Right atomTypes -> do
594 Right atomTypes -> do
595+ let mValidTypes = zipWith (\ expType actType
594− let mValidTypes = map (\(expType, actType) -> case atomTypeVerify expType actType of
596+ -> case atomTypeVerify expType actType of
595− Left err -> Just err
597+ Left err -> Just err
596− Right _ -> Nothing) (zip (dbcFuncType func) atomTypes)
599+ (dbcFuncType func) atomTypes
597 typeErrors = catMaybes mValidTypes
600 typeErrors = catMaybes mValidTypes
598− eAtomArgs = map (\arg -> runGraphRefRelationalExprM gfEnv (evalGraphRefAtomExpr emptyTuple arg)) atomArgExprs
601+ eAtomArgs = map (runGraphRefRelationalExprM gfEnv . evalGraphRefAtomExpr emptyTuple) atomArgExprs
599 if length (lefts eAtomArgs) > 1 then
602 if length (lefts eAtomArgs) > 1 then
600 dbErr (someErrors (lefts eAtomArgs))
603 dbErr (someErrors (lefts eAtomArgs))
601 else if not (null typeErrors) then
604 else if not (null typeErrors) then
@@ -639,8 +642,7 @@ getDBCIOContext = dbc_context <$> RWS.get
639getDBCIORelationalExprEnv :: DatabaseContextIOEvalMonad RelationalExprEnv
642getDBCIORelationalExprEnv :: DatabaseContextIOEvalMonad RelationalExprEnv
640getDBCIORelationalExprEnv = do
643getDBCIORelationalExprEnv = do
641 context <- getDBCIOContext
644 context <- getDBCIOContext
643− pure $ mkRelationalExprEnv context (dbcio_graph env)
645+ mkRelationalExprEnv context . dbcio_graph <$> RWS.ask
645evalGraphRefDatabaseContextIOExpr :: GraphRefDatabaseContextIOExpr -> DatabaseContextIOEvalMonad (Either RelationalError ())
647evalGraphRefDatabaseContextIOExpr :: GraphRefDatabaseContextIOExpr -> DatabaseContextIOEvalMonad (Either RelationalError ())
646#if !defined(PM36_HASKELL_SCRIPTING)
648#if !defined(PM36_HASKELL_SCRIPTING)
@@ -827,7 +829,7 @@ typeForRelationalExpr expr = do
827 let gfExpr = runProcessExprM UncommittedContextMarker (processRelationalExpr expr)
829 let gfExpr = runProcessExprM UncommittedContextMarker (processRelationalExpr expr)
828 gfEnv = freshGraphRefRelationalExprEnv (Just context) graph
830 gfEnv = freshGraphRefRelationalExprEnv (Just context) graph
829 runGf = runGraphRefRelationalExprM gfEnv (typeForGraphRefRelationalExpr gfExpr)
831 runGf = runGraphRefRelationalExprM gfEnv (typeForGraphRefRelationalExpr gfExpr)
830− lift $ except $ runGf
832+ lift $ except runGf
832--returns a database context with all tuples removed
834--returns a database context with all tuples removed
833--this is useful for type checking and optimization
835--this is useful for type checking and optimization
@@ -929,7 +931,7 @@ tupleExprCheckNewAttrName attrName rel = if isRight (attributeForName attrName r
929 Right rel
931 Right rel
931extendGraphRefTupleExpressionProcessor :: Relation -> GraphRefExtendTupleExpr -> GraphRefRelationalExprM (Attributes, RelationTuple -> Either RelationalError RelationTuple)
933extendGraphRefTupleExpressionProcessor :: Relation -> GraphRefExtendTupleExpr -> GraphRefRelationalExprM (Attributes, RelationTuple -> Either RelationalError RelationTuple)
932−extendGraphRefTupleExpressionProcessor relIn (AttributeExtendTupleExpr newAttrName atomExpr) = do
934+extendGraphRefTupleExpressionProcessor relIn (AttributeExtendTupleExpr newAttrName atomExpr) =
933-- renv <- askEnv
935-- renv <- askEnv
934 -- check that the attribute name is not in use
936 -- check that the attribute name is not in use
935 case tupleExprCheckNewAttrName newAttrName relIn of
937 case tupleExprCheckNewAttrName newAttrName relIn of
@@ -971,7 +973,7 @@ evalGraphRefAtomExpr tupIn (FunctionAtomExpr funcName arguments tid) = do
971 throwError (FunctionArgumentCountMismatchError expectedArgCount actualArgCount)
973 throwError (FunctionArgumentCountMismatchError expectedArgCount actualArgCount)
973 let zippedArgs = zip (safeInit (atomFuncType func)) argTypes
975 let zippedArgs = zip (safeInit (atomFuncType func)) argTypes
974− mapM_ (\(expType, actType) -> do
976+ mapM_ (\(expType, actType) ->
975 lift $ except (atomTypeVerify expType actType)) zippedArgs
977 lift $ except (atomTypeVerify expType actType)) zippedArgs
976 evaldArgs <- mapM (evalGraphRefAtomExpr tupIn) arguments
978 evaldArgs <- mapM (evalGraphRefAtomExpr tupIn) arguments
977 case evalAtomFunction func evaldArgs of
979 case evalAtomFunction func evaldArgs of
@@ -1160,14 +1162,14 @@ evalGraphRefTupleExprs mAttrs (TupleExprs fixedMarker tupleExprL) = do
1160 pure (A.attributesFromList mostResolvedTypes)
1162 pure (A.attributesFromList mostResolvedTypes)
1161 --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
1163 --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
1162 tConsMap <- case singularTransactions tupleExprL of
1164 tConsMap <- case singularTransactions tupleExprL of
1163− SingularTransactionRef commonTransId -> do
1165+ SingularTransactionRef commonTransId ->
1164 typeConstructorMapping <$> gfDatabaseContextForMarker commonTransId
1166 typeConstructorMapping <$> gfDatabaseContextForMarker commonTransId
1165− NoTransactionsRef -> do
1167+ NoTransactionsRef ->
1166 typeConstructorMapping <$> gfDatabaseContextForMarker fixedMarker
1168 typeConstructorMapping <$> gfDatabaseContextForMarker fixedMarker
1167 -- 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
1169 -- 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
1168 _ -> throwError TupleExprsReferenceMultipleMarkersError
1170 _ -> throwError TupleExprsReferenceMultipleMarkersError
1169 lift $ except $ validateAttributes tConsMap finalAttrs
1171 lift $ except $ validateAttributes tConsMap finalAttrs
1170− mapM (\tup -> lift $ except $ resolveTypesInTuple finalAttrs tConsMap tup) tuples
1172+ mapM (lift . except . resolveTypesInTuple finalAttrs tConsMap) tuples
1173--resolveAttributes (Attribute "gonk" (ConstructedAtomType "Either" (fromList [("a",IntegerAtomType),("b",TypeVariableType "b")]))) (Attribute "gonk" (ConstructedAtomType "Either" (fromList [("a",TypeVariableType "a"),("b",TextAtomType)])))
1175--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
1313 rel <- evalGraphRefRelationalExpr expr
1315 rel <- evalGraphRefRelationalExpr expr
1314 (newAttrs, tupProc) <- extendGraphRefTupleExpressionProcessor rel extendTupleExpr
1316 (newAttrs, tupProc) <- extendGraphRefTupleExpressionProcessor rel extendTupleExpr
1315 lift $ except $ relMogrify tupProc newAttrs rel
1317 lift $ except $ relMogrify tupProc newAttrs rel
1316−evalGraphRefRelationalExpr expr@With{} = do
1318+evalGraphRefRelationalExpr expr@With{} =
1317 --strategy A: add relation variables to the contexts in the graph
1319 --strategy A: add relation variables to the contexts in the graph
1318 --strategy B: drop in macros in place (easier programmatically)
1320 --strategy B: drop in macros in place (easier programmatically)
1319 --strategy B implementation
1321 --strategy B implementation
@@ -1341,7 +1343,7 @@ typeForGraphRefRelationalExpr (ExistingRelation rel) = pure (emptyRelationWithAt
1341typeForGraphRefRelationalExpr (MakeRelationFromExprs mAttrExprs tupleExprs) = do
1343typeForGraphRefRelationalExpr (MakeRelationFromExprs mAttrExprs tupleExprs) = do
1342 mAttrs <- case mAttrExprs of
1344 mAttrs <- case mAttrExprs of
1343 Just attrExprs -> do
1345 Just attrExprs -> do
1344− attrs <- mapM (\e -> evalGraphRefAttributeExpr e) attrExprs
1346+ attrs <- mapM evalGraphRefAttributeExpr attrExprs
1345 pure (Just (attributesFromList attrs))
1347 pure (Just (attributesFromList attrs))
1346 Nothing -> pure Nothing
1348 Nothing -> pure Nothing
1347 tuples <- evalGraphRefTupleExprs mAttrs tupleExprs
1349 tuples <- evalGraphRefTupleExprs mAttrs tupleExprs
@@ -1386,9 +1388,9 @@ typeForGraphRefRelationalExpr (Restrict pred' expr) = do
1386 expr' <- typeForGraphRefRelationalExpr expr
1388 expr' <- typeForGraphRefRelationalExpr expr
1387 filt <- predicateRestrictionFilter (attributes expr') pred'
1389 filt <- predicateRestrictionFilter (attributes expr') pred'
1388 lift $ except $ restrict filt expr'
1390 lift $ except $ restrict filt expr'
1389−typeForGraphRefRelationalExpr Equals{} = do
1391+typeForGraphRefRelationalExpr Equals{} =
1390 pure relationFalse
1392 pure relationFalse
1391−typeForGraphRefRelationalExpr NotEquals{} = do
1393+typeForGraphRefRelationalExpr NotEquals{} =
1392 pure relationFalse
1394 pure relationFalse
1393typeForGraphRefRelationalExpr (Extend extendTupleExpr expr) = do
1395typeForGraphRefRelationalExpr (Extend extendTupleExpr expr) = do
1394 rel <- typeForGraphRefRelationalExpr expr
1396 rel <- typeForGraphRefRelationalExpr expr
@@ -1400,7 +1402,7 @@ typeForGraphRefRelationalExpr expr@(With withs _) = do
1400 case M.lookup macroName rvs of
1402 case M.lookup macroName rvs of
1401 Just _ -> lift $ except $ Left (RelVarAlreadyDefinedError macroName) --this error does not include the transaction marker, but should be good enough to identify the cause
1403 Just _ -> lift $ except $ Left (RelVarAlreadyDefinedError macroName) --this error does not include the transaction marker, but should be good enough to identify the cause
1402 Nothing -> pure ()
1404 Nothing -> pure ()
1403− mapM_ checkMacroName (map fst withs)
1405+ mapM_ (checkMacroName . fst) withs
1404 typeForGraphRefRelationalExpr expr'
1406 typeForGraphRefRelationalExpr expr'
1406evalGraphRefAttributeNames :: GraphRefAttributeNames -> GraphRefRelationalExpr -> GraphRefRelationalExprM (S.Set AttributeName)
1408evalGraphRefAttributeNames :: GraphRefAttributeNames -> GraphRefRelationalExpr -> GraphRefRelationalExprM (S.Set AttributeName)
@@ -1446,7 +1448,7 @@ evalGraphRefAttributeExpr (AttributeAndTypeNameExpr attrName tCons tid) = do
1446evalGraphRefAttributeExpr (NakedAttributeExpr attr) = pure attr
1448evalGraphRefAttributeExpr (NakedAttributeExpr attr) = pure attr
1448mkEmptyRelVars :: RelationVariables -> RelationVariables
1450mkEmptyRelVars :: RelationVariables -> RelationVariables
1449−mkEmptyRelVars rvMap = M.map mkEmptyRelVar rvMap
1451+mkEmptyRelVars = M.map mkEmptyRelVar
1451 mkEmptyRelVar expr@MakeRelationFromExprs{} = expr --do not truncate here because we might lose essential type information in emptying the tuples
1453 mkEmptyRelVar expr@MakeRelationFromExprs{} = expr --do not truncate here because we might lose essential type information in emptying the tuples
1452 mkEmptyRelVar (MakeStaticRelation attrs _) = MakeStaticRelation attrs emptyTupleSet
1454 mkEmptyRelVar (MakeStaticRelation attrs _) = MakeStaticRelation attrs emptyTupleSet
@@ -1463,7 +1465,7 @@ mkEmptyRelVars rvMap = M.map mkEmptyRelVar rvMap
1463 mkEmptyRelVar (Equals exprA exprB) = Equals (mkEmptyRelVar exprA) (mkEmptyRelVar exprB)
1465 mkEmptyRelVar (Equals exprA exprB) = Equals (mkEmptyRelVar exprA) (mkEmptyRelVar exprB)
1464 mkEmptyRelVar (NotEquals exprA exprB) = NotEquals (mkEmptyRelVar exprA) (mkEmptyRelVar exprB)
1466 mkEmptyRelVar (NotEquals exprA exprB) = NotEquals (mkEmptyRelVar exprA) (mkEmptyRelVar exprB)
1465 mkEmptyRelVar (Extend extTuple expr) = Extend extTuple (mkEmptyRelVar expr)
1467 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)
1469dbErr :: RelationalError -> DatabaseContextEvalMonad ()
1471dbErr :: RelationalError -> DatabaseContextEvalMonad ()