@@ -66,7 +66,6 @@ mkDatabaseContextEvalState context = DatabaseContextEvalState {
66data RelationalExprEnv = RelationalExprEnv {
66data RelationalExprEnv = RelationalExprEnv {
67 re_context :: DatabaseContext,
67 re_context :: DatabaseContext,
68 re_graph :: TransactionGraph,
68 re_graph :: TransactionGraph,
69− re_currentTransId :: TransactionId,
70 re_extra :: Maybe (Either RelationTuple Attributes)
69 re_extra :: Maybe (Either RelationTuple Attributes)
@@ -88,21 +87,14 @@ runRelationalExprM env m = runIdentity (runExceptT (runReaderT m env))
88reGraph :: RelationalExprM TransactionGraph
87reGraph :: RelationalExprM TransactionGraph
89reGraph = re_graph <$> R.ask
88reGraph = re_graph <$> R.ask
91−currentTrans :: RelationalExprM Transaction
90+reContext :: RelationalExprM DatabaseContext
92−currentTrans = do
91+reContext = re_context <$> R.ask
93− tid <- re_currentTransId <$> R.ask
94− graph <- re_graph <$> R.ask
95− lift $ except $ transactionForId tid graph
97−reTransId :: RelationalExprM TransactionId
98−reTransId = re_currentTransId <$> R.ask
100−mkRelationalExprEnv :: DatabaseContext -> TransactionId -> TransactionGraph -> RelationalExprEnv
93+mkRelationalExprEnv :: DatabaseContext -> TransactionGraph -> RelationalExprEnv
101−mkRelationalExprEnv ctx currentTransId graph =
94+mkRelationalExprEnv ctx graph =
102 RelationalExprEnv
95 RelationalExprEnv
103 { re_context = ctx,
96 { re_context = ctx,
104 re_graph = graph,
97 re_graph = graph,
105− re_currentTransId = currentTransId,
106 re_extra = Nothing }
98 re_extra = Nothing }
108askEnv :: GraphRefRelationalExprM GraphRefRelationalExprEnv
100askEnv :: GraphRefRelationalExprM GraphRefRelationalExprEnv
@@ -159,7 +151,7 @@ dbcGraph = dce_graph <$> RWS.ask
160dbcRelationalExprEnv :: DatabaseContextEvalMonad RelationalExprEnv
152dbcRelationalExprEnv :: DatabaseContextEvalMonad RelationalExprEnv
161dbcRelationalExprEnv =
153dbcRelationalExprEnv =
162− mkRelationalExprEnv <$> getStateContext <*> dbcTransId <*> dbcGraph
154+ mkRelationalExprEnv <$> getStateContext <*> dbcGraph
164getStateContext :: DatabaseContextEvalMonad DatabaseContext
156getStateContext :: DatabaseContextEvalMonad DatabaseContext
165getStateContext = dbc_context <$> get
157getStateContext = dbc_context <$> get
@@ -167,10 +159,12 @@ getStateContext = dbc_context <$> get
167putStateContext :: DatabaseContext -> DatabaseContextEvalMonad ()
159putStateContext :: DatabaseContext -> DatabaseContextEvalMonad ()
168putStateContext ctx' = do
160putStateContext ctx' = do
170− put (s {dbc_context = ctx'})
162+ put (s {dbc_context = ctx', dbc_dirty = True})
164+-- | The context is optionally passed down along in cases where the current context is uncommitted.
172data GraphRefRelationalExprEnv =
165data GraphRefRelationalExprEnv =
173 GraphRefRelationalExprEnv {
166 GraphRefRelationalExprEnv {
167+ gre_context :: Maybe DatabaseContext,
174 gre_graph :: TransactionGraph,
168 gre_graph :: TransactionGraph,
175 gre_extra :: Maybe (Either RelationTuple Attributes)
169 gre_extra :: Maybe (Either RelationTuple Attributes)
@@ -182,11 +176,20 @@ gfTransForId tid = do
182 graph <- gfGraph
176 graph <- gfGraph
183 lift $ except $ transactionForId tid graph
177 lift $ except $ transactionForId tid graph
179+gfDatabaseContextForMarker :: GraphRefTransactionMarker -> GraphRefRelationalExprM DatabaseContext
180+gfDatabaseContextForMarker (TransactionMarker transId) = concreteDatabaseContext <$> gfTransForId transId
181+gfDatabaseContextForMarker UncommittedContextMarker = do
182+ mctx <- gre_context <$> askEnv
184+ Nothing -> throwError NoUncommittedContextInEvalError
185+ Just ctx -> pure ctx
185runGraphRefRelationalExprM :: GraphRefRelationalExprEnv -> GraphRefRelationalExprM a -> Either RelationalError a
187runGraphRefRelationalExprM :: GraphRefRelationalExprEnv -> GraphRefRelationalExprM a -> Either RelationalError a
186runGraphRefRelationalExprM env m = runIdentity (runExceptT (runReaderT m env))
188runGraphRefRelationalExprM env m = runIdentity (runExceptT (runReaderT m env))
188−freshGraphRefRelationalExprEnv :: TransactionGraph -> GraphRefRelationalExprEnv
190+freshGraphRefRelationalExprEnv :: Maybe DatabaseContext -> TransactionGraph -> GraphRefRelationalExprEnv
189−freshGraphRefRelationalExprEnv graph = GraphRefRelationalExprEnv {
191+freshGraphRefRelationalExprEnv mctx graph = GraphRefRelationalExprEnv {
190 gre_graph = graph,
193 gre_graph = graph,
191 gre_extra = Nothing
194 gre_extra = Nothing
@@ -385,11 +388,12 @@ evalGraphRefDatabaseContextExpr :: GraphRefDatabaseContextExpr -> DatabaseContex
385evalGraphRefDatabaseContextExpr NoOperation = pure ()
388evalGraphRefDatabaseContextExpr NoOperation = pure ()
387evalGraphRefDatabaseContextExpr (Define relVarName attrExprs) = do
390evalGraphRefDatabaseContextExpr (Define relVarName attrExprs) = do
391+ context <- getStateContext
388 relvars <- fmap relationVariables getStateContext
392 relvars <- fmap relationVariables getStateContext
389 tConss <- fmap typeConstructorMapping getStateContext
393 tConss <- fmap typeConstructorMapping getStateContext
390 graph <- dbcGraph
394 graph <- dbcGraph
391 let eAttrs = runGraphRefRelationalExprM gfEnv (mapM evalGraphRefAttrExpr attrExprs)
395 let eAttrs = runGraphRefRelationalExprM gfEnv (mapM evalGraphRefAttrExpr attrExprs)
392− gfEnv = freshGraphRefRelationalExprEnv graph
396+ gfEnv = freshGraphRefRelationalExprEnv (Just context) graph
393 case eAttrs of
397 case eAttrs of
394 Left err -> dbErr err
398 Left err -> dbErr err
395 Right attrsList -> do
399 Right attrsList -> do
@@ -416,7 +420,7 @@ evalGraphRefDatabaseContextExpr (Assign relVarName expr) = do
416 Just existingRel -> do
420 Just existingRel -> do
417 let eExpectedType = runGraphRefRelationalExprM reEnv (typeForGraphRefRelationalExpr existingRel)
421 let eExpectedType = runGraphRefRelationalExprM reEnv (typeForGraphRefRelationalExpr existingRel)
418 eNewExprType = runGraphRefRelationalExprM reEnv (typeForGraphRefRelationalExpr expr)
422 eNewExprType = runGraphRefRelationalExprM reEnv (typeForGraphRefRelationalExpr expr)
419− reEnv = freshGraphRefRelationalExprEnv graph
423+ reEnv = freshGraphRefRelationalExprEnv (Just context) graph
420 case eExpectedType of
424 case eExpectedType of
421 Left err -> dbErr err
425 Left err -> dbErr err
422 Right expectedType ->
426 Right expectedType ->
@@ -429,34 +433,32 @@ evalGraphRefDatabaseContextExpr (Assign relVarName expr) = do
429 dbErr (RelationTypeMismatchError (attributes expectedType) (attributes newExprType))
433 dbErr (RelationTypeMismatchError (attributes expectedType) (attributes newExprType))
431evalGraphRefDatabaseContextExpr (Insert relVarName relExpr) = do
435evalGraphRefDatabaseContextExpr (Insert relVarName relExpr) = do
432− transId <- dbcTransId
433 evalGraphRefDatabaseContextExpr (Assign relVarName
436 evalGraphRefDatabaseContextExpr (Assign relVarName
435− (RelationVariable relVarName transId)
438+ (RelationVariable relVarName UncommittedContextMarker)
436 relExpr))
439 relExpr))
438evalGraphRefDatabaseContextExpr (Delete relVarName predicate) = do
441evalGraphRefDatabaseContextExpr (Delete relVarName predicate) = do
439 transId <- dbcTransId
442 transId <- dbcTransId
440− let rv = RelationVariable relVarName transId
443+ let rv = RelationVariable relVarName (TransactionMarker transId)
441 setRelVar relVarName (Restrict (NotPredicate predicate) rv)
444 setRelVar relVarName (Restrict (NotPredicate predicate) rv)
443--union of restricted+updated portion and the unrestricted+unupdated portion
446--union of restricted+updated portion and the unrestricted+unupdated portion
444evalGraphRefDatabaseContextExpr (Update relVarName atomExprMap pred') = do
447evalGraphRefDatabaseContextExpr (Update relVarName atomExprMap pred') = do
445 context <- getStateContext
448 context <- getStateContext
446 transId <- dbcTransId
449 transId <- dbcTransId
448 let relVarMap = relationVariables context
450 let relVarMap = relationVariables context
449 case M.lookup relVarName relVarMap of
451 case M.lookup relVarName relVarMap of
450 Nothing -> dbErr (RelVarNotDefinedError relVarName)
452 Nothing -> dbErr (RelVarNotDefinedError relVarName)
451 Just _ -> do
453 Just _ -> do
452 let unrestrictedPortion = Restrict (NotPredicate pred') rv
454 let unrestrictedPortion = Restrict (NotPredicate pred') rv
453− rv = RelationVariable relVarName transId
455+ rv = RelationVariable relVarName (TransactionMarker transId)
454 tmpAttr attr = "_tmp_" <> attr --this could certainly be improved to verify that there is no attribute name conflict
456 tmpAttr attr = "_tmp_" <> attr --this could certainly be improved to verify that there is no attribute name conflict
455 updateAttr nam atomExpr = Extend (AttributeExtendTupleExpr (tmpAttr nam) atomExpr)
457 updateAttr nam atomExpr = Extend (AttributeExtendTupleExpr (tmpAttr nam) atomExpr)
456 projectAndRename attr expr = Rename (tmpAttr attr) attr (Project (InvertedAttributeNames (S.singleton attr)) expr)
458 projectAndRename attr expr = Rename (tmpAttr attr) attr (Project (InvertedAttributeNames (S.singleton attr)) expr)
457 --TODO restrictedPortion = Restrict pred'
459 --TODO restrictedPortion = Restrict pred'
458 updated = foldr (\(oldname, atomExpr) accum ->
460 updated = foldr (\(oldname, atomExpr) accum ->
459− let procAtomExpr = runProcessExprM transId graph (processAtomExpr atomExpr) in
461+ let procAtomExpr = runProcessExprM UncommittedContextMarker (processAtomExpr atomExpr) in
460 updateAttr oldname procAtomExpr accum
462 updateAttr oldname procAtomExpr accum
461 ) rv (M.toList atomExprMap)
463 ) rv (M.toList atomExprMap)
462 -- the atomExprMap could reference other attributes, so we must perform multi-pass folds
464 -- the atomExprMap could reference other attributes, so we must perform multi-pass folds
@@ -535,15 +537,10 @@ evalGraphRefDatabaseContextExpr (RemoveTypeConstructor tConsName) = do
535 let newTypes = filter (\(tCons, _) -> TCD.name tCons /= tConsName) oldTypes
537 let newTypes = filter (\(tCons, _) -> TCD.name tCons /= tConsName) oldTypes
536 putStateContext $ currentContext { typeConstructorMapping = newTypes }
538 putStateContext $ currentContext { typeConstructorMapping = newTypes }
538−evalGraphRefDatabaseContextExpr (MultipleExpr _) = do
540+evalGraphRefDatabaseContextExpr (MultipleExpr exprs) = do
539 --the multiple expressions must pass the same context around- not the old unmodified context
541 --the multiple expressions must pass the same context around- not the old unmodified context
541−{- evald <- forM exprs (\expr -> evalGraphRefDatabaseContextExpr expr)
542+ mapM_ (\expr -> evalGraphRefDatabaseContextExpr expr) exprs
542− --some lifting magic needed here
543+
545− errs -> dbErr (someErrors errs)
547evalGraphRefDatabaseContextExpr (RemoveAtomFunction funcName) = do
544evalGraphRefDatabaseContextExpr (RemoveAtomFunction funcName) = do
548 currentContext <- getStateContext
545 currentContext <- getStateContext
549 let atomFuncs = atomFunctions currentContext
546 let atomFuncs = atomFunctions currentContext
@@ -574,7 +571,7 @@ evalGraphRefDatabaseContextExpr (ExecuteDatabaseContextFunction funcName atomArg
574 --resolve atom arguments
571 --resolve atom arguments
575 let eAtomTypes = mapM (\atomExpr -> runGraphRefRelationalExprM gfEnv (typeForGraphRefAtomExpr emptyAttributes atomExpr)) atomArgExprs
572 let eAtomTypes = mapM (\atomExpr -> runGraphRefRelationalExprM gfEnv (typeForGraphRefAtomExpr emptyAttributes atomExpr)) atomArgExprs
576 eFunc = databaseContextFunctionForName funcName (dbcFunctions context)
573 eFunc = databaseContextFunctionForName funcName (dbcFunctions context)
577− gfEnv = freshGraphRefRelationalExprEnv graph
574+ gfEnv = freshGraphRefRelationalExprEnv (Just context) graph
578 case eFunc of
575 case eFunc of
579 Left err -> dbErr err
576 Left err -> dbErr err
580 Right func -> do
577 Right func -> do
@@ -591,8 +588,7 @@ evalGraphRefDatabaseContextExpr (ExecuteDatabaseContextFunction funcName atomArg
591 Left err -> Just err
588 Left err -> Just err
592 Right _ -> Nothing) (zip (dbcFuncType func) atomTypes)
589 Right _ -> Nothing) (zip (dbcFuncType func) atomTypes)
593 typeErrors = catMaybes mValidTypes
590 typeErrors = catMaybes mValidTypes
594− eAtomArgs = unimplemented
591+ eAtomArgs = map (\arg -> runGraphRefRelationalExprM gfEnv (evalGraphRefAtomExpr emptyTuple arg)) atomArgExprs
595− --eAtomArgs = map (\arg -> runReader (evalAtomExpr emptyTuple arg) relExprState) atomArgExprs
596 if length (lefts eAtomArgs) > 1 then
592 if length (lefts eAtomArgs) > 1 then
597 dbErr (someErrors (lefts eAtomArgs))
593 dbErr (someErrors (lefts eAtomArgs))
598 else if not (null typeErrors) then
594 else if not (null typeErrors) then
@@ -638,7 +634,7 @@ getDBCIORelationalExprEnv :: DatabaseContextIOEvalMonad RelationalExprEnv
638getDBCIORelationalExprEnv = do
634getDBCIORelationalExprEnv = do
639 context <- getDBCIOContext
635 context <- getDBCIOContext
640 env <- RWS.ask
636 env <- RWS.ask
641− pure $ mkRelationalExprEnv context (dbcio_transId env) (dbcio_graph env)
637+ pure $ mkRelationalExprEnv context (dbcio_graph env)
643evalGraphRefDatabaseContextIOExpr :: GraphRefDatabaseContextIOExpr -> DatabaseContextIOEvalMonad (Either RelationalError ())
639evalGraphRefDatabaseContextIOExpr :: GraphRefDatabaseContextIOExpr -> DatabaseContextIOEvalMonad (Either RelationalError ())
644evalGraphRefDatabaseContextIOExpr (AddAtomFunction funcName funcType script) = do
640evalGraphRefDatabaseContextIOExpr (AddAtomFunction funcName funcType script) = do
@@ -751,7 +747,7 @@ evalGraphRefDatabaseContextIOExpr (CreateArbitraryRelation relVarName attrExprs
751 case existingRelVar of
747 case existingRelVar of
752 Nothing -> pure $ Left (RelVarNotDefinedError relVarName)
748 Nothing -> pure $ Left (RelVarNotDefinedError relVarName)
753 Just existingRel -> do
749 Just existingRel -> do
754− let gfEnv = freshGraphRefRelationalExprEnv graph
750+ let gfEnv = freshGraphRefRelationalExprEnv (Just currentContext) graph
755 case runGraphRefRelationalExprM gfEnv (typeForGraphRefRelationalExpr existingRel) of
751 case runGraphRefRelationalExprM gfEnv (typeForGraphRefRelationalExpr existingRel) of
756 Left err -> pure (Left err)
752 Left err -> pure (Left err)
757 Right relType -> do
753 Right relType -> do
@@ -791,18 +787,18 @@ checkConstraints context transId graph@(TransactionGraph graphHeads transSet) =
791 deps = inclusionDependencies context
787 deps = inclusionDependencies context
792 -- no optimization available here, really? perhaps the optimizer should be passed down to here or the eval function should be passed through the environment
788 -- no optimization available here, really? perhaps the optimizer should be passed down to here or the eval function should be passed through the environment
793 checkIncDep depName (InclusionDependency subsetExpr supersetExpr) = do
789 checkIncDep depName (InclusionDependency subsetExpr supersetExpr) = do
794− let process = runProcessExprM transId graph
790+ let process = runProcessExprM UncommittedContextMarker
795 gfSubsetExpr = process (processRelationalExpr subsetExpr)
791 gfSubsetExpr = process (processRelationalExpr subsetExpr)
796 gfSupersetExpr = process (processRelationalExpr supersetExpr)
792 gfSupersetExpr = process (processRelationalExpr supersetExpr)
797 --if both expressions are of a single-attribute (such as with a simple foreign key), the names of the attributes are irrelevant (they need not match) because the expression is unambiguous, but special-casing this to rename the attribute automatically would not be orthogonal behavior and probably cause confusion. Instead, special case the error to make it clear.
793 --if both expressions are of a single-attribute (such as with a simple foreign key), the names of the attributes are irrelevant (they need not match) because the expression is unambiguous, but special-casing this to rename the attribute automatically would not be orthogonal behavior and probably cause confusion. Instead, special case the error to make it clear.
798− let gfEnv = freshGraphRefRelationalExprEnv graph
794+ let gfEnv = freshGraphRefRelationalExprEnv (Just context) graph
799 runGfRel = runGraphRefRelationalExprM gfEnv
795 runGfRel = runGraphRefRelationalExprM gfEnv
800 typeSub <- runGfRel (typeForGraphRefRelationalExpr gfSubsetExpr)
796 typeSub <- runGfRel (typeForGraphRefRelationalExpr gfSubsetExpr)
801 typeSuper <- runGfRel (typeForGraphRefRelationalExpr gfSupersetExpr)
797 typeSuper <- runGfRel (typeForGraphRefRelationalExpr gfSupersetExpr)
802 when (typeSub /= typeSuper) (Left (RelationTypeMismatchError (attributes typeSub) (attributes typeSuper)))
798 when (typeSub /= typeSuper) (Left (RelationTypeMismatchError (attributes typeSub) (attributes typeSuper)))
803 let checkExpr = Equals gfSupersetExpr (Union gfSubsetExpr gfSupersetExpr)
799 let checkExpr = Equals gfSupersetExpr (Union gfSubsetExpr gfSupersetExpr)
804 gfEvald = runGraphRefRelationalExprM gfEnv' (evalGraphRefRelationalExpr checkExpr)
800 gfEvald = runGraphRefRelationalExprM gfEnv' (evalGraphRefRelationalExpr checkExpr)
805− gfEnv' = freshGraphRefRelationalExprEnv potentialGraph
801+ gfEnv' = freshGraphRefRelationalExprEnv (Just context) potentialGraph
806 case gfEvald of
802 case gfEvald of
807 Left err -> Left err
803 Left err -> Left err
808 Right resultRel -> if resultRel == relationTrue then
804 Right resultRel -> if resultRel == relationTrue then
@@ -818,9 +814,9 @@ typeForRelationalExpr expr = do
818 --replace the relationVariables context element with a cloned set of relation devoid of tuples
814 --replace the relationVariables context element with a cloned set of relation devoid of tuples
819 --evalRelationalExpr could still return an existing relation with tuples, so strip them
815 --evalRelationalExpr could still return an existing relation with tuples, so strip them
820 graph <- reGraph
816 graph <- reGraph
821− transId <- reTransId
817+ context <- reContext
822− let gfExpr = runProcessExprM transId graph (processRelationalExpr expr)
818+ let gfExpr = runProcessExprM UncommittedContextMarker (processRelationalExpr expr)
823− gfEnv = freshGraphRefRelationalExprEnv graph
819+ gfEnv = freshGraphRefRelationalExprEnv (Just context) graph
824 runGf = runGraphRefRelationalExprM gfEnv (typeForGraphRefRelationalExpr gfExpr)
820 runGf = runGraphRefRelationalExprM gfEnv (typeForGraphRefRelationalExpr gfExpr)
825 lift $ except $ runGf
821 lift $ except $ runGf
@@ -934,18 +930,64 @@ extendGraphRefTupleExpressionProcessor relIn (AttributeExtendTupleExpr newAttrNa
934 atomExprType' <- verifyGraphRefAtomExprTypes relIn atomExpr atomExprType
930 atomExprType' <- verifyGraphRefAtomExprTypes relIn atomExpr atomExprType
935 let newAttrs = A.attributesFromList [Attribute newAttrName atomExprType']
931 let newAttrs = A.attributesFromList [Attribute newAttrName atomExprType']
936 newAndOldAttrs = A.addAttributes (attributes relIn) newAttrs
932 newAndOldAttrs = A.addAttributes (attributes relIn) newAttrs
937− pure (newAndOldAttrs, \tup ->
934+ pure (newAndOldAttrs, \tup -> do
938− --let substate = mergeTuplesIntoGraphRefRelationalExprEnv tup renv in
935+ let gfEnv = mergeTuplesIntoGraphRefRelationalExprEnv tup env
939− case unimplemented {-runReader (evalAtomExpr tup atomExpr) substate-} of
936+ atom <- runGraphRefRelationalExprM gfEnv (evalGraphRefAtomExpr tup atomExpr)
940− Left err -> Left err
941− Right atom -> Right (tupleAtomExtend newAttrName atom tup)
937+ Right (tupleAtomExtend newAttrName atom tup)
944evalGraphRefAtomExpr :: RelationTuple -> GraphRefAtomExpr -> GraphRefRelationalExprM Atom
940evalGraphRefAtomExpr :: RelationTuple -> GraphRefAtomExpr -> GraphRefRelationalExprM Atom
945−evalGraphRefAtomExpr = unimplemented
941+evalGraphRefAtomExpr tupIn (AttributeAtomExpr attrName) =
942+ case atomForAttributeName attrName tupIn of
943+ Right atom -> pure atom
944+ Left err@(NoSuchAttributeNamesError _) -> do
946+ case gre_extra env of
947+ Nothing -> throwError err
948+ Just (Left ctxtup) -> lift $ except $ atomForAttributeName attrName ctxtup
949+ Just (Right _) -> throwError err
950+ Left err -> throwError err
951+evalGraphRefAtomExpr _ (NakedAtomExpr atom) = pure atom
952+evalGraphRefAtomExpr tupIn (FunctionAtomExpr funcName arguments tid) = do
953+ argTypes <- mapM (typeForGraphRefAtomExpr (tupleAttributes tupIn)) arguments
954+ context <- gfDatabaseContextForMarker tid
955+ let functions = atomFunctions context
956+ func <- lift $ except (atomFunctionForName funcName functions)
957+ let expectedArgCount = length (atomFuncType func) - 1
958+ actualArgCount = length argTypes
959+ safeInit [] = [] -- different behavior from normal init
960+ safeInit xs = init xs
961+ if expectedArgCount /= actualArgCount then
962+ throwError (FunctionArgumentCountMismatchError expectedArgCount actualArgCount)
964+ let zippedArgs = zip (safeInit (atomFuncType func)) argTypes
965+ mapM_ (\(expType, actType) -> do
966+ lift $ except (atomTypeVerify expType actType)) zippedArgs
967+ evaldArgs <- mapM (evalGraphRefAtomExpr tupIn) arguments
968+ case evalAtomFunction func evaldArgs of
969+ Left err -> throwError (AtomFunctionUserError err)
971+ --validate that the result matches the expected type
972+ _ <- lift $ except (atomTypeVerify (last (atomFuncType func)) (atomTypeForAtom result))
974+evalGraphRefAtomExpr tupIn (RelationAtomExpr relExpr) = do
975+ --merge existing state tuple context into new state tuple context to support an arbitrary number of levels, but new attributes trounce old attributes
977+ let gfEnv = mergeTuplesIntoGraphRefRelationalExprEnv tupIn env
978+ relAtom <- lift $ except $ runGraphRefRelationalExprM gfEnv (evalGraphRefRelationalExpr relExpr)
979+ pure (RelationAtom relAtom)
980+evalGraphRefAtomExpr tupIn cons@(ConstructedAtomExpr dConsName dConsArgs _) = do --why is the tid unused here? suspicious
981+ let mergeEnv = mergeTuplesIntoGraphRefRelationalExprEnv tupIn
982+ aType <- local mergeEnv (typeForGraphRefAtomExpr (tupleAttributes tupIn) cons)
983+ argAtoms <- local mergeEnv $
984+ mapM (evalGraphRefAtomExpr tupIn) dConsArgs
985+ pure (ConstructedAtom dConsName aType argAtoms)
947evalAtomExpr :: RelationTuple -> AtomExpr -> RelationalExprM Atom
988evalAtomExpr :: RelationTuple -> AtomExpr -> RelationalExprM Atom
948−evalAtomExpr = unimplemented
989+evalAtomExpr tupIn expr =
951evalAtomExpr tupIn (AttributeAtomExpr attrName) = case atomForAttributeName attrName tupIn of
993evalAtomExpr tupIn (AttributeAtomExpr attrName) = case atomForAttributeName attrName tupIn of
@@ -1011,7 +1053,7 @@ typeForGraphRefAtomExpr attrs (AttributeAtomExpr attrName) = do
1012typeForGraphRefAtomExpr _ (NakedAtomExpr atom) = pure (atomTypeForAtom atom)
1054typeForGraphRefAtomExpr _ (NakedAtomExpr atom) = pure (atomTypeForAtom atom)
1013typeForGraphRefAtomExpr attrs (FunctionAtomExpr funcName atomArgs transId) = do
1055typeForGraphRefAtomExpr attrs (FunctionAtomExpr funcName atomArgs transId) = do
1014− funcs <- atomFunctions . concreteDatabaseContext <$> gfTransForId transId
1056+ funcs <- atomFunctions <$> gfDatabaseContextForMarker transId
1015 case atomFunctionForName funcName funcs of
1057 case atomFunctionForName funcName funcs of
1016 Left err -> throwError err
1058 Left err -> throwError err
1017 Right func -> do
1059 Right func -> do
@@ -1029,7 +1071,7 @@ typeForGraphRefAtomExpr attrs (RelationAtomExpr relExpr) = do
1029typeForGraphRefAtomExpr attrs (ConstructedAtomExpr dConsName dConsArgs tid) =
1071typeForGraphRefAtomExpr attrs (ConstructedAtomExpr dConsName dConsArgs tid) =
1031 argsTypes <- mapM (typeForGraphRefAtomExpr attrs) dConsArgs
1073 argsTypes <- mapM (typeForGraphRefAtomExpr attrs) dConsArgs
1032− tConsMap <- typeConstructorMapping . concreteDatabaseContext <$> gfTransForId tid
1074+ tConsMap <- typeConstructorMapping <$> gfDatabaseContextForMarker tid
1033 lift $ except $ atomTypeForDataConstructor tConsMap dConsName argsTypes
1075 lift $ except $ atomTypeForDataConstructor tConsMap dConsName argsTypes
1035-- | Validate that the type of the AtomExpr matches the expected type.
1077-- | Validate that the type of the AtomExpr matches the expected type.
@@ -1051,7 +1093,7 @@ verifyGraphRefAtomExprTypes relIn (AttributeAtomExpr attrName) expectedType = do
1051verifyGraphRefAtomExprTypes _ (NakedAtomExpr atom) expectedType =
1093verifyGraphRefAtomExprTypes _ (NakedAtomExpr atom) expectedType =
1052 lift $ except $ atomTypeVerify expectedType (atomTypeForAtom atom)
1094 lift $ except $ atomTypeVerify expectedType (atomTypeForAtom atom)
1053verifyGraphRefAtomExprTypes relIn (FunctionAtomExpr funcName funcArgExprs tid) expectedType = do
1095verifyGraphRefAtomExprTypes relIn (FunctionAtomExpr funcName funcArgExprs tid) expectedType = do
1054− context <- concreteDatabaseContext <$> gfTransForId tid
1096+ context <- gfDatabaseContextForMarker tid
1055 let functions = atomFunctions context
1097 let functions = atomFunctions context
1056 func <- lift $ except $ atomFunctionForName funcName functions
1098 func <- lift $ except $ atomFunctionForName funcName functions
1057 let expectedArgTypes = atomFuncType func
1099 let expectedArgTypes = atomFuncType func
@@ -1077,7 +1119,7 @@ verifyGraphRefAtomExprTypes rel cons@ConstructedAtomExpr{} expectedType = do
1077-- | Look up the type's name and create a new attribute.
1119-- | Look up the type's name and create a new attribute.
1078evalGraphRefAttrExpr :: GraphRefAttributeExpr -> GraphRefRelationalExprM Attribute
1120evalGraphRefAttrExpr :: GraphRefAttributeExpr -> GraphRefRelationalExprM Attribute
1079evalGraphRefAttrExpr (AttributeAndTypeNameExpr attrName tCons transId) = do
1121evalGraphRefAttrExpr (AttributeAndTypeNameExpr attrName tCons transId) = do
1080− tConsMap <- typeConstructorMapping . concreteDatabaseContext <$> gfTransForId transId
1122+ tConsMap <- typeConstructorMapping <$> gfDatabaseContextForMarker transId
1081 aType <- lift $ except $ atomTypeForTypeConstructorValidate True tCons tConsMap M.empty
1123 aType <- lift $ except $ atomTypeForTypeConstructorValidate True tCons tConsMap M.empty
1082 lift $ except $ validateAtomType aType tConsMap
1124 lift $ except $ validateAtomType aType tConsMap
1083 pure $ Attribute attrName aType
1125 pure $ Attribute attrName aType
@@ -1179,7 +1221,7 @@ evalGraphRefRelationalExpr (MakeStaticRelation attributeSet tupleSet) =
1179 lift $ except $ mkRelation attributeSet tupleSet
1221 lift $ except $ mkRelation attributeSet tupleSet
1180evalGraphRefRelationalExpr (ExistingRelation rel) = pure rel
1222evalGraphRefRelationalExpr (ExistingRelation rel) = pure rel
1181evalGraphRefRelationalExpr (RelationVariable name tid) = do
1223evalGraphRefRelationalExpr (RelationVariable name tid) = do
1182− ctx <- concreteDatabaseContext <$> gfTransForId tid
1224+ ctx <- gfDatabaseContextForMarker tid
1183 case M.lookup name (relationVariables ctx) of
1225 case M.lookup name (relationVariables ctx) of
1184 Nothing -> throwError (RelVarNotDefinedError name)
1226 Nothing -> throwError (RelVarNotDefinedError name)
1185 Just rv -> evalGraphRefRelationalExpr rv
1227 Just rv -> evalGraphRefRelationalExpr rv
@@ -1263,7 +1305,7 @@ typeForGraphRefRelationalExpr (MakeRelationFromExprs mAttrExprs tupleExprs) = do
1263 pure $ emptyRelationWithAttrs retAttrs
1305 pure $ emptyRelationWithAttrs retAttrs
1265typeForGraphRefRelationalExpr (RelationVariable rvName tid) = do
1307typeForGraphRefRelationalExpr (RelationVariable rvName tid) = do
1266− relVars <- relationVariables . concreteDatabaseContext <$> gfTransForId tid
1308+ relVars <- relationVariables <$> gfDatabaseContextForMarker tid
1267 case M.lookup rvName relVars of
1309 case M.lookup rvName relVars of
1268 Nothing -> throwError (RelVarNotDefinedError rvName)
1310 Nothing -> throwError (RelVarNotDefinedError rvName)
1269 Just rvExpr -> typeForGraphRefRelationalExpr rvExpr
1311 Just rvExpr -> typeForGraphRefRelationalExpr rvExpr
@@ -1341,7 +1383,7 @@ evalGraphRefAttributeNames attrNames expr = do
1342evalGraphRefAttributeExpr :: GraphRefAttributeExpr -> GraphRefRelationalExprM Attribute
1384evalGraphRefAttributeExpr :: GraphRefAttributeExpr -> GraphRefRelationalExprM Attribute
1343evalGraphRefAttributeExpr (AttributeAndTypeNameExpr attrName tCons tid) = do
1385evalGraphRefAttributeExpr (AttributeAndTypeNameExpr attrName tCons tid) = do
1344− tConsMap <- typeConstructorMapping . concreteDatabaseContext <$> gfTransForId tid
1386+ tConsMap <- typeConstructorMapping <$> gfDatabaseContextForMarker tid
1345 case atomTypeForTypeConstructorValidate True tCons tConsMap M.empty of
1387 case atomTypeForTypeConstructorValidate True tCons tConsMap M.empty of
1346 Left err -> throwError err
1388 Left err -> throwError err
1347 Right aType -> do
1389 Right aType -> do
@@ -1381,7 +1423,7 @@ relationVariablesAsRelation relVarMap graph = do
1381 attrs = A.attributesFromList [Attribute "name" TextAtomType,
1423 attrs = A.attributesFromList [Attribute "name" TextAtomType,
1382 Attribute "attributes" (RelationAtomType subrelAttrs)]
1424 Attribute "attributes" (RelationAtomType subrelAttrs)]
1383 mkRvDesc (rvName, gfExpr) = do
1425 mkRvDesc (rvName, gfExpr) = do
1384− let gfEnv = freshGraphRefRelationalExprEnv graph
1426+ let gfEnv = freshGraphRefRelationalExprEnv Nothing graph
1385 gfType <- runGraphRefRelationalExprM gfEnv (typeForGraphRefRelationalExpr gfExpr)
1427 gfType <- runGraphRefRelationalExprM gfEnv (typeForGraphRefRelationalExpr gfExpr)
1386 pure (rvName, gfType)
1428 pure (rvName, gfType)
1387 relVarToAtomList (rvName, rel) = [TextAtom rvName, attributesToRel (attributes rel)]
1429 relVarToAtomList (rvName, rel) = [TextAtom rvName, attributesToRel (attributes rel)]
@@ -1393,3 +1435,4 @@ relationVariablesAsRelation relVarMap graph = do
1393 let tups = map relVarToAtomList rvs
1435 let tups = map relVarToAtomList rvs
1394 mkRelationFromList attrs tups
1436 mkRelationFromList attrs tups