@@ -34,9 +34,7 @@ databaseContextExprDetailsFunc :: DatabaseContextExprDetails -> ResultAccumFunc
3434databaseContextExprDetailsFunc CountUpdatedTuples _ relIn = Relation attrs newTups
3535 where
3636 attrs = A.attributesFromList [Attribute "count" IntAtomType]
37− existingTuple = case singletonTuple relIn of
38− Just t -> t
39− Nothing -> error "impossible counting error in singletonTuple"
37+ existingTuple = fromMaybe (error "impossible counting error in singletonTuple") (singletonTuple relIn)
4038 existingCount = case V.head (tupleAtoms existingTuple) of
4139 IntAtom v -> v
4240 _ -> error "impossible counting error in tupleAtoms"
@@ -59,7 +57,7 @@ instance Show RelationalExprStateElems where
5957 show (RelationalExprStateElems _) = "RelationalExprStateElems"
6058
6159mkRelationalExprState :: DatabaseContext -> RelationalExprStateElems
62−mkRelationalExprState ctx = RelationalExprStateElems ctx
60+mkRelationalExprState = RelationalExprStateElems
6361
6462mergeTuplesIntoRelationalExprState :: RelationTuple -> RelationalExprStateElems -> RelationalExprStateElems
6563mergeTuplesIntoRelationalExprState tupIn (RelationalExprStateElems ctx) = RelationalExprStateTupleElems ctx tupIn
@@ -84,7 +82,7 @@ type DatabaseStateElems = (DatabaseContext, M.Map ResultAccumName ResultAccum, D
8482
8583type DatabaseState a = State DatabaseStateElems a
8684
87−getStateContext :: DatabaseState (DatabaseContext)
85+getStateContext :: DatabaseState DatabaseContext
8886getStateContext = do
8987 (ctx,_, _) <- get
9088 pure ctx
@@ -109,7 +107,7 @@ setStateElemsContext (RelationalExprStateAttrsElems _ attrs) ctx = RelationalExp
109107--relvar state is needed in evaluation of relational expression but only as read-only in order to extract current relvar values
110108evalRelationalExpr :: RelationalExpr -> RelationalExprState (Either RelationalError Relation)
111109evalRelationalExpr (RelationVariable name _) = do
112− relvarTable <- liftM (relationVariables . stateElemsContext) ask
110+ relvarTable <- fmap (relationVariables . stateElemsContext) ask
113111 return $ case M.lookup name relvarTable of
114112 Just res -> Right res
115113 Nothing -> Left $ RelVarNotDefinedError name
@@ -147,24 +145,24 @@ evalRelationalExpr (Difference exprA exprB) = do
147145 Left err -> return $ Left err
148146 Right relB2 -> return $ difference relA2 relB2
149147
150−evalRelationalExpr (MakeStaticRelation attributeSet tupleSet) = do
148+evalRelationalExpr (MakeStaticRelation attributeSet tupleSet) =
151149 case mkRelation attributeSet tupleSet of
152150 Right rel -> return $ Right rel
153151 Left err -> return $ Left err
154152
155153evalRelationalExpr (MakeRelationFromExprs mAttrExprs tupleExprs) = do
156− currentContext <- liftM stateElemsContext ask
154+ currentContext <- fmap stateElemsContext ask
157155 let tConss = typeConstructorMapping currentContext
158156 -- if the mAttrExprs is Nothing, then we should attempt to infer the tuple attributes from the first tuple itself- note that this is not always possible
159157 runExceptT $ do
160158 mAttrs <- case mAttrExprs of
161159 Just _ -> do
162− attrs <- mapM (\expr -> either throwE pure (evalAttrExpr tConss expr)) (fromMaybe [] mAttrExprs)
160+ attrs <- mapM (either throwE pure . evalAttrExpr tConss) (fromMaybe [] mAttrExprs)
163161 pure (Just (A.attributesFromList attrs))
164162 Nothing -> pure Nothing
165− tuples <- mapM (\expr -> liftE (evalTupleExpr mAttrs expr)) tupleExprs
163+ tuples <- mapM (liftE . evalTupleExpr mAttrs) tupleExprs
166164 let attrs = fromMaybe firstTupleAttrs mAttrs
167− firstTupleAttrs = if length tuples == 0 then A.emptyAttributes else tupleAttributes (head tuples)
165+ firstTupleAttrs = if null tuples then A.emptyAttributes else tupleAttributes (head tuples)
168166 either throwE pure (mkRelation attrs (RelationTupleSet tuples))
169167
170168evalRelationalExpr (ExistingRelation rel) = pure (Right rel)
@@ -195,7 +193,7 @@ evalRelationalExpr (Restrict predicateExpr relExpr) = do
195193 eFilterFunc <- predicateRestrictionFilter (attributes rel) predicateExpr
196194 case eFilterFunc of
197195 Left err -> return $ Left err
198− Right filterfunc -> do
196+ Right filterfunc ->
199197 pure (restrict filterfunc rel)
200198
201199evalRelationalExpr (Equals relExprA relExprB) = do
@@ -259,8 +257,8 @@ evalDatabaseContextExpr :: DatabaseContextExpr -> DatabaseState (Maybe Relationa
259257evalDatabaseContextExpr NoOperation = pure Nothing
260258
261259evalDatabaseContextExpr (Define relVarName attrExprs) = do
262− relvars <- liftM relationVariables getStateContext
263− tConss <- liftM typeConstructorMapping getStateContext
260+ relvars <- fmap relationVariables getStateContext
261+ tConss <- fmap typeConstructorMapping getStateContext
264262 let eAttrs = map (evalAttrExpr tConss) attrExprs
265263 case lefts eAttrs of
266264 err:_ -> pure (Just err)
@@ -271,8 +269,7 @@ evalDatabaseContextExpr (Define relVarName attrExprs) = do
271269 attrs = A.attributesFromList (rights eAttrs)
272270 emptyRelation = Relation attrs emptyTupleSet
273271
274−evalDatabaseContextExpr (Undefine relVarName) = do
275− deleteRelVar relVarName
272+evalDatabaseContextExpr (Undefine relVarName) = deleteRelVar relVarName
276273
277274evalDatabaseContextExpr (Assign relVarName expr) = do
278275 -- in the future, it would be nice to get types from the RelationalExpr instead of needing to evaluate it
@@ -326,7 +323,7 @@ evalDatabaseContextExpr (Update relVarName atomExprMap restrictionPredicateExpr)
326323 let relVarTable = relationVariables context
327324 case M.lookup relVarName relVarTable of
328325 Nothing -> return $ Just (RelVarNotDefinedError relVarName)
329− Just rel -> do
326+ Just rel ->
330327 case runReader (predicateRestrictionFilter (attributes rel) restrictionPredicateExpr) (RelationalExprStateElems context) of
331328 Left err -> return $ Just err
332329 Right predicateFunc -> do
@@ -335,9 +332,9 @@ evalDatabaseContextExpr (Update relVarName atomExprMap restrictionPredicateExpr)
335332 if cardinality restrictedPortion == Finite 0 then
336333 pure Nothing
337334 else do
338− unrestrictedPortion <- restrict (\tup -> predicateFunc tup >>= pure . not) rel
335+ unrestrictedPortion <- restrict (predicateFunc >=> (pure . not)) rel
339336 updatedPortion <- relMap (updateTupleWithAtomExprs atomExprMap context) restrictedPortion
340− updatedRel <- union updatedPortion unrestrictedPortion
337+ updatedRel <- updatedPortion `union` unrestrictedPortion
341338 pure (Just updatedRel)
342339 case ret of
343340 Left err -> pure (Just err)
@@ -400,21 +397,18 @@ evalDatabaseContextExpr (AddTypeConstructor tConsDef dConsDefList) = do
400397 -- validate that the constructor's types exist
401398 case validateTypeConstructorDef tConsDef dConsDefList of
402399 errs@(_:_) -> pure $ Just (someErrors errs)
403− [] -> do
404− if T.length tConsName < 1 || not (isUpper (T.head tConsName)) then
405− pure $ Just (InvalidAtomTypeName tConsName)
406− else if isJust (findTypeConstructor tConsName oldTypes) then
407− pure $ Just (AtomTypeNameInUseError tConsName)
408− else do
409− let newTypes = oldTypes ++ [(tConsDef, dConsDefList)]
410− putStateContext $ currentContext { typeConstructorMapping = newTypes }
411− pure Nothing
400+ [] | T.null tConsName || not (isUpper (T.head tConsName)) -> pure $ Just (InvalidAtomTypeName tConsName)
401+ | isJust (findTypeConstructor tConsName oldTypes) -> pure $ Just (AtomTypeNameInUseError tConsName)
402+ | otherwise -> do
403+ let newTypes = oldTypes ++ [(tConsDef, dConsDefList)]
404+ putStateContext $ currentContext { typeConstructorMapping = newTypes }
405+ pure Nothing
412406
413407-- | Removing the atom constructor prevents new atoms of the type from being created. Existing atoms of the type remain. Thus, the atomTypes list in the DatabaseContext need not be all-inclusive.
414408evalDatabaseContextExpr (RemoveTypeConstructor tConsName) = do
415409 currentContext <- getStateContext
416410 let oldTypes = typeConstructorMapping currentContext
417− if findTypeConstructor tConsName oldTypes == Nothing then
411+ if isNothing (findTypeConstructor tConsName oldTypes) then
418412 pure $ Just (AtomTypeNameNotInUseError tConsName)
419413 else do
420414 let newTypes = filter (\(tCons, _) -> TCD.name tCons /= tConsName) oldTypes
@@ -426,8 +420,8 @@ evalDatabaseContextExpr (MultipleExpr exprs) = do
426420 evald <- forM exprs evalDatabaseContextExpr
427421 --some lifting magic needed here
428422 case catMaybes evald of
429− [] -> return $ Nothing
430− err:_ -> return $ Just err
423+ [] -> pure Nothing
424+ err:_ -> pure (Just err)
431425
432426evalDatabaseContextExpr (RemoveAtomFunction funcName) = do
433427 currentContext <- getStateContext
@@ -467,7 +461,7 @@ evalDatabaseContextExpr (ExecuteDatabaseContextFunction funcName atomArgExprs) =
467461 actualArgCount = length atomArgExprs
468462 if expectedArgCount /= actualArgCount then
469463 pure (Just (FunctionArgumentCountMismatch expectedArgCount actualArgCount))
470− else do
464+ else
471465 --check that the atom types are valid
472466 case lefts eAtomTypes of
473467 _:_ -> pure (Just (someErrors (lefts eAtomTypes)))
@@ -480,15 +474,15 @@ evalDatabaseContextExpr (ExecuteDatabaseContextFunction funcName atomArgExprs) =
480474 eAtomArgs = map (\arg -> runReader (evalAtomExpr emptyTuple arg) relExprState) atomArgExprs
481475 if length (lefts eAtomArgs) > 1 then
482476 pure (Just (someErrors (lefts eAtomArgs)))
483− else if length typeErrors > 0 then
477+ else if not (null typeErrors) then
484478 pure (Just (someErrors typeErrors))
485− else do
479+ else
486480 case evalDatabaseContextFunction func (rights eAtomArgs) context of
487481 Left err -> pure (Just err)
488482 Right newContext -> putStateContext newContext >> pure Nothing
489483
490484evalDatabaseContextIOExpr :: Maybe ScriptSession -> DatabaseContext -> DatabaseContextIOExpr -> IO (Either RelationalError DatabaseContext)
491−evalDatabaseContextIOExpr mScriptSession currentContext (AddAtomFunction funcName funcType script) = do
485+evalDatabaseContextIOExpr mScriptSession currentContext (AddAtomFunction funcName funcType script) =
492486 case mScriptSession of
493487 Nothing -> pure (Left (ScriptError ScriptCompilationDisabledError))
494488 Just scriptSession -> do
@@ -512,7 +506,7 @@ evalDatabaseContextIOExpr mScriptSession currentContext (AddAtomFunction funcNam
512506 -- check if the name is already in use
513507 if HS.member funcName (HS.map atomFuncName atomFuncs) then
514508 Left (FunctionNameInUseError funcName)
515− else do
509+ else
516510 Right newContext
517511 case res of
518512 Left (exc :: SomeException) -> pure $ Left (ScriptError (OtherScriptCompilationError (show exc)))
@@ -520,7 +514,7 @@ evalDatabaseContextIOExpr mScriptSession currentContext (AddAtomFunction funcNam
520514 Left err -> pure (Left err)
521515 Right context' -> pure (Right context')
522516
523−evalDatabaseContextIOExpr mScriptSession currentContext (AddDatabaseContextFunction funcName funcType script) = do
517+evalDatabaseContextIOExpr mScriptSession currentContext (AddDatabaseContextFunction funcName funcType script) =
524518 case mScriptSession of
525519 Nothing -> pure (Left (ScriptError ScriptCompilationDisabledError))
526520 Just scriptSession -> do
@@ -552,7 +546,7 @@ evalDatabaseContextIOExpr mScriptSession currentContext (AddDatabaseContextFunct
552546 -- check if the name is already in use
553547 if HS.member funcName (HS.map dbcFuncName dbcFuncs) then
554548 Left (FunctionNameInUseError funcName)
555− else do
549+ else
556550 Right newContext
557551 case res of
558552 Left (exc :: SomeException) -> pure $ Left (ScriptError (OtherScriptCompilationError (show exc)))
@@ -561,7 +555,7 @@ evalDatabaseContextIOExpr mScriptSession currentContext (AddDatabaseContextFunct
561555 Right context' -> pure (Right context')
562556
563557
564−updateTupleWithAtomExprs :: (M.Map AttributeName AtomExpr) -> DatabaseContext -> RelationTuple -> Either RelationalError RelationTuple
558+updateTupleWithAtomExprs :: M.Map AttributeName AtomExpr -> DatabaseContext -> RelationTuple -> Either RelationalError RelationTuple
565559updateTupleWithAtomExprs exprMap context tupIn = do
566560 --resolve all atom exprs
567561 atomsAssoc <- mapM (\(attrName, atomExpr) -> do
@@ -616,7 +610,7 @@ liftE v = do
616610
617611{- used for restrictions- take the restrictionpredicate and return the corresponding filter function -}
618612predicateRestrictionFilter :: Attributes -> RestrictionPredicateExpr -> RelationalExprState (Either RelationalError RestrictionFilter)
619−predicateRestrictionFilter attrs (AndPredicate expr1 expr2) = do
613+predicateRestrictionFilter attrs (AndPredicate expr1 expr2) =
620614 runExceptT $ do
621615 expr1v <- liftE (predicateRestrictionFilter attrs expr1)
622616 expr2v <- liftE (predicateRestrictionFilter attrs expr2)
@@ -625,7 +619,7 @@ predicateRestrictionFilter attrs (AndPredicate expr1 expr2) = do
625619 ev2 <- expr2v x
626620 pure (ev1 && ev2))
627621
628−predicateRestrictionFilter attrs (OrPredicate expr1 expr2) = do
622+predicateRestrictionFilter attrs (OrPredicate expr1 expr2) =
629623 runExceptT $ do
630624 expr1v <- liftE (predicateRestrictionFilter attrs expr1)
631625 expr2v <- liftE (predicateRestrictionFilter attrs expr2)
@@ -636,7 +630,7 @@ predicateRestrictionFilter attrs (OrPredicate expr1 expr2) = do
636630
637631predicateRestrictionFilter _ TruePredicate = pure (Right (\_ -> pure True))
638632
639−predicateRestrictionFilter attrs (NotPredicate expr) = do
633+predicateRestrictionFilter attrs (NotPredicate expr) =
640634 runExceptT $ do
641635 exprv <- liftE (predicateRestrictionFilter attrs expr)
642636 pure (\x -> do
@@ -691,7 +685,7 @@ predicateRestrictionFilter attrs (AtomExprPredicate atomExpr) = do
691685 if aType /= BoolAtomType then
692686 throwE (AtomTypeMismatchError aType BoolAtomType)
693687 else
694− pure (\tupleIn -> do
688+ pure (\tupleIn ->
695689 pure $ case runReader (evalAtomExpr tupleIn atomExpr) rstate of
696690 Left _ -> False
697691 Right boolAtomValue -> boolAtomValue == BoolAtom True)
@@ -713,7 +707,7 @@ extendTupleExpressionProcessor relIn (AttributeExtendTupleExpr newAttrName atomE
713707 atomExprType' <- liftE (verifyAtomExprTypes relIn atomExpr atomExprType)
714708 let newAttrs = A.attributesFromList [Attribute newAttrName atomExprType']
715709 newAndOldAttrs = A.addAttributes (attributes relIn) newAttrs
716− pure $ (newAndOldAttrs, \tup -> let substate = mergeTuplesIntoRelationalExprState tup rstate in case runReader (evalAtomExpr tup atomExpr) substate of
710+ pure (newAndOldAttrs, \tup -> let substate = mergeTuplesIntoRelationalExprState tup rstate in case runReader (evalAtomExpr tup atomExpr) substate of
717711 Left err -> Left err
718712 Right atom -> Right (tupleAtomExtend newAttrName atom tup)
719713 )
@@ -731,7 +725,7 @@ evalAtomExpr tupIn (AttributeAtomExpr attrName) = case atomForAttributeName attr
731725evalAtomExpr _ (NakedAtomExpr atom) = pure (Right atom)
732726evalAtomExpr tupIn (FunctionAtomExpr funcName arguments ()) = do
733727 argTypes <- mapM (typeFromAtomExpr (tupleAttributes tupIn)) arguments
734− context <- liftM stateElemsContext ask
728+ context <- fmap stateElemsContext ask
735729 runExceptT $ do
736730 let functions = atomFunctions context
737731 func <- either throwE pure (atomFunctionForName funcName functions)
@@ -744,7 +738,7 @@ evalAtomExpr tupIn (FunctionAtomExpr funcName arguments ()) = do
744738 throwE (FunctionArgumentCountMismatch expectedArgCount actualArgCount)
745739 else do
746740 _ <- mapM (\(expType, actType) -> either throwE pure (atomTypeVerify expType actType)) (safeInit (zip (atomFuncType func) argTypes))
747− evaldArgs <- mapM (\arg -> liftE (evalAtomExpr tupIn arg)) arguments
741+ evaldArgs <- mapM (liftE . evalAtomExpr tupIn) arguments
748742 case evalAtomFunction func evaldArgs of
749743 Left err -> throwE (AtomFunctionUserError err)
750744 Right result -> pure result
@@ -778,13 +772,13 @@ typeFromAtomExpr attrs (AttributeAtomExpr attrName) = do
778772 Left err -> pure (error "GONK" $ Left err)
779773typeFromAtomExpr _ (NakedAtomExpr atom) = pure (Right (atomTypeForAtom atom))
780774typeFromAtomExpr attrs (FunctionAtomExpr funcName atomArgs _) = do
781− context <- liftM stateElemsContext ask
775+ context <- fmap stateElemsContext ask
782776 let funcs = atomFunctions context
783777 case atomFunctionForName funcName funcs of
784778 Left err -> pure (Left err)
785779 Right func -> do
786780 let funcRetType = last (atomFuncType func)
787− funcArgTypes = reverse (tail (reverse (atomFuncType func)))
781+ funcArgTypes = init (atomFuncType func)
788782 eArgTypes <- mapM (typeFromAtomExpr attrs) atomArgs
789783 case lefts eArgTypes of
790784 errs@(_:_) -> pure (Left (someErrors errs))
@@ -801,8 +795,8 @@ typeFromAtomExpr attrs (RelationAtomExpr relExpr) = runExceptT $ do
801795-- grab the type of the data constructor, then validate that the args match the expected types
802796typeFromAtomExpr attrs (ConstructedAtomExpr dConsName dConsArgs _) =
803797 runExceptT $ do
804− argsTypes <- mapM (\arg -> liftE (typeFromAtomExpr attrs arg)) dConsArgs
805− context <- liftM stateElemsContext (lift ask)
798+ argsTypes <- mapM (liftE . typeFromAtomExpr attrs) dConsArgs
799+ context <- fmap stateElemsContext (lift ask)
806800 aType <- either throwE pure (atomTypeForDataConstructor (typeConstructorMapping context) dConsName argsTypes)
807801 pure aType
808802
@@ -829,18 +823,18 @@ verifyAtomExprTypes relIn (FunctionAtomExpr funcName funcArgExprs _) expectedTyp
829823 let expectedArgTypes = atomFuncType func
830824 funcArgTypes <- mapM (\(atomExpr,expectedType2,argCount) -> case runReader (verifyAtomExprTypes relIn atomExpr expectedType2) rstate of
831825 Left (AtomTypeMismatchError expSubType actSubType) -> throwE (AtomFunctionTypeError funcName argCount expSubType actSubType)
832− Left err -> throwE (err)
826+ Left err -> throwE err
833827 Right x -> pure x
834828 ) $ zip3 funcArgExprs expectedArgTypes [1..]
835829 if length funcArgTypes /= length expectedArgTypes - 1 then
836830 throwE (AtomTypeCountError funcArgTypes expectedArgTypes)
837− else do
831+ else
838832 either throwE pure (atomTypeVerify expectedType (last expectedArgTypes))
839833verifyAtomExprTypes relIn (RelationAtomExpr relationExpr) expectedType = runExceptT $ do
840834 rstate <- lift ask
841835 relType <- either throwE pure (runReader (typeForRelationalExpr relationExpr) (mergeAttributesIntoRelationalExprState (attributes relIn) rstate))
842836 either throwE pure (atomTypeVerify expectedType (RelationAtomType (attributes relType)))
843−verifyAtomExprTypes rel cons@(ConstructedAtomExpr _ _ _) expectedType = runExceptT $ do
837+verifyAtomExprTypes rel cons@ConstructedAtomExpr{} expectedType = runExceptT $ do
844838 cType <- liftE (typeFromAtomExpr (attributes rel) cons)
845839 either throwE pure (atomTypeVerify expectedType cType)
846840
@@ -854,7 +848,7 @@ evalAttrExpr _ (NakedAttributeExpr attr) = Right attr
854848
855849evalTupleExpr :: Maybe Attributes -> TupleExpr -> RelationalExprState (Either RelationalError RelationTuple)
856850evalTupleExpr attrs (TupleExpr tupMap) = do
857− context <- liftM stateElemsContext ask
851+ context <- fmap stateElemsContext ask
858852 runExceptT $ do
859853 -- it's not possible for AtomExprs in tuple constructors to reference other Attributes' atoms due to the necessary order-of-operations (need a tuple to pass to evalAtomExpr)- it may be possible with some refactoring of type usage or delayed evaluation- needs more thought, but not a priority
860854 -- I could adjust this logic so that when the attributes are not specified (Nothing), then I can attempt to extract the attributes from the tuple- the type resolution will blow up if an ambiguous data constructor is used (Left 4) and this should allow simple cases to "relation{tuple{a 4}}" to be processed