1{-# LANGUAGE ExistentialQuantification,DeriveGeneric,DeriveAnyClass,FlexibleInstances,OverloadedStrings #-}
2{-# OPTIONS_GHC -fno-warn-orphans #-}
4module ProjectM36.Base where
5import ProjectM36.DatabaseContextFunctionError
6import ProjectM36.AtomFunctionError
8import qualified Data.Map as M
9import qualified Data.HashSet as HS
10import Data.Hashable (Hashable, hashWithSalt)
11import qualified Data.Set as S
12import Data.UUID (UUID)
13import Control.DeepSeq (NFData, rnf)
14import Control.DeepSeq.Generics (genericRnf)
15import GHC.Generics (Generic)
16import qualified Data.Vector as V
17import qualified Data.List as L
18import Data.Text (Text,unpack)
20import Data.Vector.Binary()
22import Data.Time.Clock.POSIX
23import Data.Time.Calendar (Day,toGregorian,fromGregorian)
24import Data.Hashable.Time ()
26import Data.ByteString (ByteString)
27import qualified Data.List.NonEmpty as NE
30-- | Database atoms are the smallest, undecomposable units of a tuple. Common examples are integers, text, or unique identity keys.
31data Atom = IntegerAtom Integer |
36 DateTimeAtom UTCTime |
37 ByteStringAtom ByteString |
39 RelationAtom Relation |
40 ConstructedAtom DataConstructorName AtomType [Atom]
41 deriving (Eq, Show, Binary, Typeable, NFData, Generic)
43instance Hashable Atom where
44 hashWithSalt salt (ConstructedAtom dConsName _ atoms) = salt `hashWithSalt` atoms
45 `hashWithSalt` dConsName --AtomType is not hashable
46 hashWithSalt salt (IntAtom i) = salt `hashWithSalt` i
47 hashWithSalt salt (IntegerAtom i) = salt `hashWithSalt` i
48 hashWithSalt salt (DoubleAtom d) = salt `hashWithSalt` d
49 hashWithSalt salt (TextAtom t) = salt `hashWithSalt` t
50 hashWithSalt salt (DayAtom d) = salt `hashWithSalt` d
51 hashWithSalt salt (DateTimeAtom dt) = salt `hashWithSalt` dt
52 hashWithSalt salt (ByteStringAtom bs) = salt `hashWithSalt` bs
53 hashWithSalt salt (BoolAtom b) = salt `hashWithSalt` b
54 hashWithSalt salt (RelationAtom r) = salt `hashWithSalt` r
56instance Binary UTCTime where
57 put utc = put $ toRational (utcTimeToPOSIXSeconds utc)
58 get = posixSecondsToUTCTime . fromRational <$> (get :: Get Rational)
60instance Binary Day where
61 put day = put $ toGregorian day
63 (y,m,d) <- get :: Get (Integer, Int, Int)
64 return (fromGregorian y m d)
66-- I suspect the definition of ConstructedAtomType with its name alone is insufficient to disambiguate the cases; for example, one could create a type named X, remove a type named X, and re-add it using different constructors. However, as long as requests are served from only one DatabaseContext at-a-time, the type name is unambiguous. This will become a problem for time-travel, however.
67-- | The AtomType uniquely identifies the type of a atom.
68data AtomType = IntAtomType |
76 RelationAtomType Attributes |
77 ConstructedAtomType TypeConstructorName TypeVarMap |
78 TypeVariableType TypeVarName
79 --wildcard used in Atom Functions and tuples for data constructors which don't provide all arguments to the type constructor
80 deriving (Eq, NFData, Generic, Binary, Show)
82instance Ord AtomType where
84type TypeVarMap = M.Map TypeVarName AtomType
86instance Hashable TypeVarMap where
87 hashWithSalt salt tvmap = hashWithSalt salt (M.keys tvmap)
89-- | Return True iff the atom type argument is relation-valued. If True, this indicates that the Atom contains a relation.
90isRelationAtomType :: AtomType -> Bool
91isRelationAtomType (RelationAtomType _) = True
92isRelationAtomType _ = False
94-- | The AttributeName is the name of an attribute in a relation.
95type AttributeName = StringType
97-- | A relation's type is composed of attribute names and types.
98data Attribute = Attribute AttributeName AtomType deriving (Eq, Show, Generic, NFData, Binary)
100instance Hashable Attribute where
101 hashWithSalt salt (Attribute attrName _) = hashWithSalt salt attrName
103-- | 'Attributes' represent the head of a relation.
104type Attributes = V.Vector Attribute
106-- | Equality function for a set of attributes.
107attributesEqual :: Attributes -> Attributes -> Bool
108attributesEqual attrs1 attrs2 = attrsAsSet attrs1 == attrsAsSet attrs2
110 attrsAsSet = HS.fromList . V.toList
112sortedAttributesIndices :: Attributes -> [(Int, Attribute)]
113sortedAttributesIndices attrs = L.sortBy (\(_, Attribute name1 _) (_,Attribute name2 _) -> compare name1 name2) $ V.toList (V.indexed attrs)
115-- | The relation's tuple set is the body of the relation.
116newtype RelationTupleSet = RelationTupleSet { asList :: [RelationTuple] } deriving (Hashable, Show, Generic, Binary)
118instance Read Relation where
119 readsPrec = error "relation read not supported"
121instance Eq RelationTupleSet where
122 set1 == set2 = hset set1 == hset set2
124 hset = HS.fromList . asList
126instance NFData RelationTupleSet where rnf = genericRnf
128--the same hash must be generated for equal tuples so that the hashset equality works
129instance Hashable RelationTuple where
130 --sanity check the tuple for attribute and tuple counts
131 --this bit me when tuples were being hashed before being verified
132 hashWithSalt salt (RelationTuple attrs tupVec) = if V.length attrs /= V.length tupVec then
133 error "invalid tuple: attributes and tuple count mismatch"
136 sortedAttrs `hashWithSalt`
137 V.toList sortedTupVec
139 sortedAttrsIndices = sortedAttributesIndices attrs
140 sortedAttrs = map snd sortedAttrsIndices
141 sortedTupVec = V.map (\(index, _) -> tupVec V.! index) $ V.fromList sortedAttrsIndices
143-- | A tuple is a set of attributes mapped to their 'Atom' values.
144data RelationTuple = RelationTuple Attributes (V.Vector Atom) deriving (Show, Generic)
146instance Binary RelationTuple
148instance Eq RelationTuple where
149 (==) tuple1@(RelationTuple attrs1 _) tuple2@(RelationTuple attrs2 _) = attributesEqual attrs1 attrs2 && atomsEqual
151 atomForAttribute attr (RelationTuple attrs tupVec) = case V.findIndex (== attr) attrs of
153 Just index -> tupVec V.!? index
154 atomsEqual = V.all (== True) $ V.map (\attr -> atomForAttribute attr tuple1 == atomForAttribute attr tuple2) attrs1
156instance NFData RelationTuple where rnf = genericRnf
159data Relation = Relation Attributes RelationTupleSet deriving (Show, Generic,Typeable)
161instance Eq Relation where
162 Relation attrs1 tupSet1 == Relation attrs2 tupSet2 = attributesEqual attrs1 attrs2 && tupSet1 == tupSet2
164instance NFData Relation where rnf = genericRnf
166instance Hashable Relation where
167 hashWithSalt salt (Relation attrs tupSet) = salt `hashWithSalt`
168 sortedAttrs `hashWithSalt`
171 sortedAttrs = map snd (sortedAttributesIndices attrs)
173instance Binary Relation
175-- | Used to represent the number of tuples in a relation.
176data RelationCardinality = Countable | Finite Int deriving (Eq, Show, Generic, Ord)
178-- | Relation variables are identified by their names.
179type RelVarName = StringType
181type RelationalExpr = RelationalExprBase ()
183-- | A relational expression represents query (read) operations on a database.
184data RelationalExprBase a =
185 --- | Create a relation from tuple expressions.
186 MakeRelationFromExprs (Maybe [AttributeExprBase a]) [TupleExprBase a] |
187 --- | Create and reference a relation from attributes and a tuple set.
188 MakeStaticRelation Attributes RelationTupleSet |
189 --- | Reference an existing relation in Haskell-space.
190 ExistingRelation Relation |
191 --MakeFunctionalRelation (creates a relation from a tuple-generating function, potentially infinite)
192 --in Tutorial D, relational variables pick up the type of the first relation assigned to them
193 --relational variables should also be able to be explicitly-typed like in Haskell
194 --- | Reference a relation variable by its name.
195 RelationVariable RelVarName a |
196 --- | Create a projection over attribute names. (Note that the 'AttributeNames' structure allows for the names to be inverted.)
197 Project (AttributeNamesBase a) (RelationalExprBase a) |
198 --- | Create a union of two relational expressions. The expressions should have identical attributes.
199 Union (RelationalExprBase a) (RelationalExprBase a) |
200 --- | Create a join of two relational expressions. The join occurs on attributes which are identical. If the expressions have no overlapping attributes, the join becomes a cross-product of both tuple sets.
201 Join (RelationalExprBase a) (RelationalExprBase a) |
202 --- | Rename an attribute (first argument) to another (second argument).
203 Rename AttributeName AttributeName (RelationalExprBase a) |
204 --- | Return a relation containing all tuples of the first argument which do not appear in the second argument (minus).
205 Difference (RelationalExprBase a) (RelationalExprBase a) |
206 --- | Create a sub-relation composed of the first argument's attributes which will become an attribute of the result expression. The unreferenced attributes are not altered in the result but duplicate tuples in the projection of the expression minus the attribute names are compressed into one. For more information, <https://github.com/agentm/project-m36/blob/master/docs/introduction_to_the_relational_algebra.markdown#group read the relational algebra tutorial.>
207 Group (AttributeNamesBase a) AttributeName (RelationalExprBase a) |
208 --- | Create an expression to unwrap a sub-relation contained within at an attribute's name. Note that this is not always an inverse of a group operation.
209 Ungroup AttributeName (RelationalExprBase a) |
210 --- | Filter the tuples of the relational expression to only retain the tuples which evaluate against the restriction predicate to true.
211 Restrict (RestrictionPredicateExprBase a) (RelationalExprBase a) |
212 --- | Returns the true relation iff
213 Equals (RelationalExprBase a) (RelationalExprBase a) |
214 NotEquals (RelationalExprBase a) (RelationalExprBase a) |
215 Extend (ExtendTupleExprBase a) (RelationalExprBase a) |
216 --Summarize :: AtomExpr -> AttributeName -> RelationalExpr -> RelationalExpr -> RelationalExpr -- a special case of Extend
217 --Evaluate relationalExpr with scoped views
218 With [(RelVarName,RelationalExprBase a)] (RelationalExprBase a)
219 deriving (Show, Eq, Generic, NFData)
221instance Binary RelationalExpr
223type NotificationName = StringType
224type Notifications = M.Map NotificationName Notification
226-- | When the changeExpr returns a different result in the database context, then the reportExpr is triggered and sent asynchronously to all clients.
227data Notification = Notification {
228 changeExpr :: RelationalExpr,
229 reportOldExpr :: RelationalExpr, --run the expression in the pre-change context
230 reportNewExpr :: RelationalExpr --run the expression in the post-change context
232 deriving (Show, Eq, Binary, Generic, NFData)
234type TypeVarName = StringType
236-- | Metadata definition for type constructors such as @data Either a b@.
237data TypeConstructorDef = ADTypeConstructorDef TypeConstructorName [TypeVarName] |
238 PrimitiveTypeConstructorDef TypeConstructorName AtomType
239 deriving (Show, Generic, Binary, Eq, NFData)
241-- | Found in data constructors and type declarations: Left (Either Int Text) | Right Int
242type TypeConstructor = TypeConstructorBase ()
243data TypeConstructorBase a = ADTypeConstructor TypeConstructorName [TypeConstructor] |
244 PrimitiveTypeConstructor TypeConstructorName AtomType |
245 RelationAtomTypeConstructor [AttributeExprBase a] |
246 TypeVariable TypeVarName
247 deriving (Show, Generic, Binary, Eq, NFData)
249type TypeConstructorMapping = [(TypeConstructorDef, DataConstructorDefs)]
251type TypeConstructorName = StringType
252type TypeConstructorArgName = StringType
253type DataConstructorName = StringType
254type AtomTypeName = StringType
256-- | Used to define a data constructor in a type constructor context such as @Left a | Right b@
257data DataConstructorDef = DataConstructorDef DataConstructorName [DataConstructorDefArg] deriving (Eq, Show, Binary, Generic, NFData)
259type DataConstructorDefs = [DataConstructorDef]
261data DataConstructorDefArg = DataConstructorDefTypeConstructorArg TypeConstructor |
262 DataConstructorDefTypeVarNameArg TypeVarName
263 deriving (Show, Generic, Binary, Eq, NFData)
265type InclusionDependencies = M.Map IncDepName InclusionDependency
266type RelationVariables = M.Map RelVarName GraphRefRelationalExpr
268-- a fundamental relational expr to which other relational expressions compile
269type GraphRefRelationalExpr = RelationalExprBase TransactionId
271type SchemaName = StringType
273type Subschemas = M.Map SchemaName Schema
275-- | Every transaction has one concrete database context and any number of isomorphic subschemas.
276data Schemas = Schemas DatabaseContext Subschemas
278-- | The DatabaseContext is a snapshot of a database's evolving state and contains everything a database client can change over time.
279-- I spent some time thinking about whether the VirtualDatabaseContext/Schema and DatabaseContext data constructors should be the same constructor, but that would allow relation variables to be created in a "virtual" context which would appear to defeat the isomorphisms of the contexts. It should be possible to switch to an alternative schema to view the same equivalent information without information loss. However, allowing all contexts to reference another context while maintaining its own relation variables, new types, etc. could be interesting from a security perspective. For example, if a user creates a new relvar in a virtual context, then does it necessarily appear in all linked contexts? After deliberation, I think the relvar should appear in *all* linked contexts to retain the isomorphic properties, even when the isomorphism is for a subset of the context. This hints that the IsoMorphs should allow for "fall-through"; that is, when a relvar is not defined in the virtual context (for morphing), then the lookup should fall through to the underlying context.
280newtype Schema = Schema SchemaIsomorphs
281 deriving (Generic, Binary)
283data SchemaIsomorph = IsoRestrict RelVarName RestrictionPredicateExpr (RelVarName, RelVarName) |
284 IsoRename RelVarName RelVarName |
285 IsoUnion (RelVarName, RelVarName) RestrictionPredicateExpr RelVarName --maps two relvars to one relvar
286 -- IsoTypeConstructor in morphAttrExpr
287 deriving (Generic, Binary, Show)
289type SchemaIsomorphs = [SchemaIsomorph]
291data DatabaseContext = DatabaseContext {
292 inclusionDependencies :: InclusionDependencies,
293 relationVariables :: RelationVariables,
294 atomFunctions :: AtomFunctions,
295 dbcFunctions :: DatabaseContextFunctions,
296 notifications :: Notifications,
297 typeConstructorMapping :: TypeConstructorMapping
298 } deriving (NFData, Generic)
300type IncDepName = StringType
302-- | Inclusion dependencies represent every possible database constraint. Constraints enforce specific, arbitrarily-complex rules to which the database context's relation variables must adhere unconditionally.
303data InclusionDependency = InclusionDependency RelationalExpr RelationalExpr deriving (Show, Eq, Generic, NFData)
305instance Binary InclusionDependency
307type AttributeNameAtomExprMap = M.Map AttributeName AtomExpr
309--used for returning information about individual expressions
310type DatabaseContextExprName = StringType
312type DatabaseContextExpr = DatabaseContextExprBase ()
314-- | Database context expressions modify the database context.
315data DatabaseContextExprBase a =
317 Define RelVarName [AttributeExpr] |
318 Undefine RelVarName | --forget existence of relvar X
319 Assign RelVarName (RelationalExprBase a) |
320 Insert RelVarName (RelationalExprBase a) |
321 Delete RelVarName RestrictionPredicateExpr |
322 Update RelVarName AttributeNameAtomExprMap RestrictionPredicateExpr |
324 AddInclusionDependency IncDepName InclusionDependency |
325 RemoveInclusionDependency IncDepName |
327 AddNotification NotificationName (RelationalExprBase a) (RelationalExprBase a) (RelationalExprBase a) |
328 RemoveNotification NotificationName |
330 AddTypeConstructor TypeConstructorDef [DataConstructorDef] |
331 RemoveTypeConstructor TypeConstructorName |
333 --adding an AtomFunction is not a pure operation (required loading GHC modules)
334 RemoveAtomFunction AtomFunctionName |
336 RemoveDatabaseContextFunction DatabaseContextFunctionName |
338 ExecuteDatabaseContextFunction DatabaseContextFunctionName [AtomExpr] |
340 MultipleExpr [DatabaseContextExprBase a]
341 deriving (Show, Eq, Generic)
343instance Binary DatabaseContextExpr
345type ObjModuleName = StringType
346type ObjFunctionName = StringType
347type Range = (Int,Int)
348-- | Adding an atom function should be nominally a DatabaseExpr except for the fact that it cannot be performed purely. Thus, we create the DatabaseContextIOExpr.
349data DatabaseContextIOExpr = AddAtomFunction AtomFunctionName [TypeConstructor] AtomFunctionBodyScript |
350 LoadAtomFunctions ObjModuleName ObjFunctionName FilePath |
351 AddDatabaseContextFunction DatabaseContextFunctionName [TypeConstructor] DatabaseContextFunctionBodyScript |
352 LoadDatabaseContextFunctions ObjModuleName ObjFunctionName FilePath |
353 CreateArbitraryRelation RelVarName [AttributeExpr] Range
354 deriving (Show, Eq, Generic, Binary)
356type RestrictionPredicateExpr = RestrictionPredicateExprBase ()
358type GraphRefRestrictionPredicateExpr = RestrictionPredicateExprBase TransactionId
360-- | Restriction predicates are boolean algebra components which, when composed, indicate whether or not a tuple should be retained during a restriction (filtering) operation.
361data RestrictionPredicateExprBase a =
363 AndPredicate (RestrictionPredicateExprBase a) (RestrictionPredicateExprBase a) |
364 OrPredicate (RestrictionPredicateExprBase a) (RestrictionPredicateExprBase a) |
365 NotPredicate (RestrictionPredicateExprBase a) |
366 RelationalExprPredicate (RelationalExprBase a) | --type must be same as true and false relations (no attributes)
367 AtomExprPredicate (AtomExprBase a) | --atom must evaluate to boolean
368 AttributeEqualityPredicate AttributeName (AtomExprBase a) -- relationalexpr must result in relation with single tuple
369 deriving (Show, Eq, Generic, NFData)
371instance Binary RestrictionPredicateExpr
373-- child + parent links
374-- | A transaction graph's head name references the leaves of the transaction graph and can be used during session creation to indicate at which point in the graph commits should persist.
375type HeadName = StringType
377type TransactionHeads = M.Map HeadName Transaction
379-- | The transaction graph is the global database's state which references every committed transaction.
380data TransactionGraph = TransactionGraph TransactionHeads (S.Set Transaction)
382-- | Every transaction has context-specific information attached to it.
383-- The `TransactionDiff`s represent child/edge relationships to previous transactions (branches or continuations of the same branch).
384data TransactionInfo = TransactionInfo {
385 parents :: TransactionParents,
387 } deriving (Show, Generic)
389type TransactionParents = NE.NonEmpty TransactionId
391data TransactionInfo = TransactionInfo TransactionId TransactionDiffs UTCTime | -- 1 parent + n children
392 MergeTransactionInfo TransactionId TransactionId TransactionDiffs UTCTime -- 2 parents, n children
393 deriving (Show, Generic)
396instance Binary TransactionInfo
398-- | Every set of modifications made to the database are atomically committed to the transaction graph as a transaction.
399type TransactionId = UUID
401data Transaction = Transaction TransactionId TransactionInfo Schemas
403-- | The disconnected transaction represents an in-progress workspace used by sessions before changes are committed. This is similar to git's "index". After a transaction is committed, it is "connected" in the transaction graph and can no longer be modified.
404data DisconnectedTransaction = DisconnectedTransaction TransactionId Schemas DatabaseContextExpr
405--the database context expression represents a difference between the disconnected transaction and its immutable parent transaction
409type TransactionDiffExpr = DatabaseContextExpr
411transactionId :: Transaction -> TransactionId
412transactionId (Transaction tid _ _) = tid
414transactionInfo :: Transaction -> TransactionInfo
415transactionInfo (Transaction _ info _) = info
417instance Eq Transaction where
418 (Transaction uuidA _ _) == (Transaction uuidB _ _) = uuidA == uuidB
420instance Ord Transaction where
421 compare (Transaction uuidA _ _) (Transaction uuidB _ _) = compare uuidA uuidB
423type AtomExpr = AtomExprBase ()
425type GraphRefAtomExpr = AtomExprBase TransactionId
427-- | An atom expression represents an action to take when extending a relation or when statically defining a relation or a new tuple.
428data AtomExprBase a = AttributeAtomExpr AttributeName |
430 FunctionAtomExpr AtomFunctionName [AtomExprBase a] a |
431 RelationAtomExpr (RelationalExprBase a) |
432 ConstructedAtomExpr DataConstructorName [AtomExprBase a] a
433 deriving (Eq,Show,Generic, NFData)
435instance Binary AtomExpr
437-- | Used in tuple creation when creating a relation.
438data ExtendTupleExprBase a = AttributeExtendTupleExpr AttributeName (AtomExprBase a)
439 deriving (Show, Eq, Generic, NFData)
441type ExtendTupleExpr = ExtendTupleExprBase ()
443instance Binary ExtendTupleExpr
445--enumerates the list of functions available to be run as part of tuple expressions
446type AtomFunctions = HS.HashSet AtomFunction
448type AtomFunctionName = StringType
450type AtomFunctionBodyScript = StringType
452type AtomFunctionBodyType = [Atom] -> Either AtomFunctionError Atom
454data AtomFunctionBody = AtomFunctionBody (Maybe AtomFunctionBodyScript) AtomFunctionBodyType
456instance NFData AtomFunctionBody where
457 rnf (AtomFunctionBody mScript _) = rnf mScript
459instance Show AtomFunctionBody where
460 show (AtomFunctionBody mScript _) = case mScript of
461 Just script -> show (unpack script)
462 Nothing -> "<compiled>"
464-- | An AtomFunction has a name, a type, and a function body to execute when called.
465data AtomFunction = AtomFunction {
466 atomFuncName :: AtomFunctionName,
467 atomFuncType :: [AtomType],
468 atomFuncBody :: AtomFunctionBody
469 } deriving (Generic, NFData)
471instance Hashable AtomFunction where
472 hashWithSalt salt func = salt `hashWithSalt` atomFuncName func
474instance Eq AtomFunction where
475 f1 == f2 = atomFuncName f1 == atomFuncName f2
477instance Show AtomFunction where
478 show aFunc = unpack (atomFuncName aFunc) ++ "::" ++ showArgTypes ++ "; " ++ body
480 body = show (atomFuncBody aFunc)
481 showArgTypes = L.intercalate "->" (map show (atomFuncType aFunc))
483-- | The 'AttributeNames' structure represents a set of attribute names or the same set of names but inverted in the context of a relational expression. For example, if a relational expression has attributes named "a", "b", and "c", the 'InvertedAttributeNames' of ("a","c") is ("b").
484data AttributeNamesBase a = AttributeNames (S.Set AttributeName) |
485 InvertedAttributeNames (S.Set AttributeName) |
486 UnionAttributeNames (AttributeNamesBase a) (AttributeNamesBase a) |
487 IntersectAttributeNames (AttributeNamesBase a) (AttributeNamesBase a) |
488 RelationalExprAttributeNames (RelationalExprBase a) -- use attribute names from the relational expression's type
489 deriving (Eq, Show, Generic, NFData)
491type AttributeNames = AttributeNamesBase ()
493type GraphRefAttributeNames = AttributeNamesBase TransactionId
495instance Binary AttributeNames
497-- | The persistence strategy is a global database option which represents how to persist the database in the filesystem, if at all.
498data PersistenceStrategy = NoPersistence | -- ^ no filesystem persistence/memory-only database
499 MinimalPersistence FilePath | -- ^ fsync off, not crash-safe
500 CrashSafePersistence FilePath -- ^ full fsync to disk (flushes kernel and physical drive buffers to ensure that the transaction is on non-volatile storage)
501 deriving (Show, Read)
503type AttributeExpr = AttributeExprBase ()
504type GraphRefAttributeExpr = AttributeExprBase TransactionId
506-- | Create attributes dynamically.
507data AttributeExprBase a = AttributeAndTypeNameExpr AttributeName TypeConstructor a |
508 NakedAttributeExpr Attribute
509 deriving (Eq, Show, Generic, Binary, NFData)
511-- | Dynamically create a tuple from attribute names and 'AtomExpr's.
512newtype TupleExprBase a = TupleExpr (M.Map AttributeName (AtomExprBase a))
513 deriving (Eq, Show, Generic, NFData)
515instance Binary TupleExpr
517type TupleExpr = TupleExprBase ()
519type GraphRefTupleExpr = TupleExprBase TransactionId
522 -- | After a union merge, the merge transaction is a result of union'ing relvars of the same name, introducing all uniquely-named relvars, union of constraints, union of atom functions, notifications, and types (unless the names and definitions collide, e.g. two types of the same name with different definitions)
524 -- | Similar to a union merge, but, on conflict, prefer the unmerged section (relvar, function, etc.) from the branch named as the argument.
525 UnionPreferMergeStrategy HeadName |
526 -- | Similar to the our/theirs merge strategy in git, the merge transaction's context is identical to that of the last transaction in the selected branch.
527 SelectedBranchMergeStrategy HeadName
528 deriving (Eq, Show, Binary, Generic, NFData)
530type DatabaseContextFunctionName = StringType
532type DatabaseContextFunctionBodyScript = StringType
534type DatabaseContextFunctionBodyType = [Atom] -> DatabaseContext -> Either DatabaseContextFunctionError DatabaseContext
536data DatabaseContextFunctionBody = DatabaseContextFunctionBody (Maybe DatabaseContextFunctionBodyScript) DatabaseContextFunctionBodyType
538instance NFData DatabaseContextFunctionBody where
539 rnf (DatabaseContextFunctionBody mScript _) = rnf mScript
541data DatabaseContextFunction = DatabaseContextFunction {
542 dbcFuncName :: DatabaseContextFunctionName,
543 dbcFuncType :: [AtomType],
544 dbcFuncBody :: DatabaseContextFunctionBody
545 } deriving (Generic, NFData)
547type DatabaseContextFunctions = HS.HashSet DatabaseContextFunction
549instance Hashable DatabaseContextFunction where
550 hashWithSalt salt func = salt `hashWithSalt` dbcFuncName func
552instance Eq DatabaseContextFunction where
553 f1 == f2 = dbcFuncName f1 == dbcFuncName f2
555attrTypeVars :: Attribute -> S.Set TypeVarName
556attrTypeVars (Attribute _ aType) = case aType of
557 IntAtomType -> S.empty
558 IntegerAtomType -> S.empty
559 DoubleAtomType -> S.empty
560 TextAtomType -> S.empty
561 DayAtomType -> S.empty
562 DateTimeAtomType -> S.empty
563 ByteStringAtomType -> S.empty
564 BoolAtomType -> S.empty
565 (RelationAtomType attrs) -> S.unions (map attrTypeVars (V.toList attrs))
566 (ConstructedAtomType _ tvMap) -> M.keysSet tvMap
567 (TypeVariableType nam) -> S.singleton nam
569typeVars :: TypeConstructor -> S.Set TypeVarName
570typeVars (PrimitiveTypeConstructor _ _) = S.empty
571typeVars (ADTypeConstructor _ args) = S.unions (map typeVars args)
572typeVars (TypeVariable v) = S.singleton v
573typeVars (RelationAtomTypeConstructor attrExprs) = S.unions (map attrExprTypeVars attrExprs)
575attrExprTypeVars :: AttributeExprBase a -> S.Set TypeVarName
576attrExprTypeVars (AttributeAndTypeNameExpr _ tCons _) = typeVars tCons
577attrExprTypeVars (NakedAttributeExpr attr) = attrTypeVars attr
579atomTypeVars :: AtomType -> S.Set TypeVarName
580atomTypeVars IntAtomType = S.empty
581atomTypeVars IntegerAtomType = S.empty
582atomTypeVars DoubleAtomType = S.empty
583atomTypeVars TextAtomType = S.empty
584atomTypeVars DayAtomType = S.empty
585atomTypeVars DateTimeAtomType = S.empty
586atomTypeVars ByteStringAtomType = S.empty
587atomTypeVars BoolAtomType = S.empty
588atomTypeVars (RelationAtomType attrs) = S.unions (map attrTypeVars (V.toList attrs))
589atomTypeVars (ConstructedAtomType _ tvMap) = M.keysSet tvMap
590atomTypeVars (TypeVariableType nam) = S.singleton nam
593unimplemented = undefined
595--for serializing GraphRefRelationalExpr as part of transaction serialization
596instance Binary (TupleExprBase TransactionId)
598instance Binary GraphRefRelationalExpr
600instance Binary (AtomExprBase TransactionId)
602instance Binary (AttributeNamesBase TransactionId)
604instance Binary (RestrictionPredicateExprBase TransactionId)
606instance Binary (ExtendTupleExprBase TransactionId)
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list