@@ -6,6 +6,7 @@ import qualified ProjectM36.DataConstructorDef as DCD
6import ProjectM36.MiscUtils
6import ProjectM36.MiscUtils
7import ProjectM36.Error
7import ProjectM36.Error
8import ProjectM36.DataTypes.Primitive
8import ProjectM36.DataTypes.Primitive
9+import ProjectM36.AttributeExpr as AE
9import qualified ProjectM36.Attribute as A
10import qualified ProjectM36.Attribute as A
10import qualified Data.Vector as V
11import qualified Data.Vector as V
11import qualified Data.Set as S
12import qualified Data.Set as S
@@ -55,6 +56,7 @@ isValidAtomTypeForTypeConstructor aType (PrimitiveTypeConstructor _ expectedATyp
55--lookup constructor name and check if the incoming atom types are valid
56--lookup constructor name and check if the incoming atom types are valid
56isValidAtomTypeForTypeConstructor (ConstructedAtomType tConsName _) (ADTypeConstructor expectedTConsName _) _ = if tConsName /= expectedTConsName then Just (TypeConstructorNameMismatch expectedTConsName tConsName) else Nothing
57isValidAtomTypeForTypeConstructor (ConstructedAtomType tConsName _) (ADTypeConstructor expectedTConsName _) _ = if tConsName /= expectedTConsName then Just (TypeConstructorNameMismatch expectedTConsName tConsName) else Nothing
59+isValidAtomTypeForTypeConstructor (RelationAtomType attrs) (RelationAtomTypeConstructor attrExprs) _ = error "poopla"
58isValidAtomTypeForTypeConstructor aType tCons _ = Just (AtomTypeTypeConstructorReconciliationError aType (TC.name tCons))
60isValidAtomTypeForTypeConstructor aType tCons _ = Just (AtomTypeTypeConstructorReconciliationError aType (TC.name tCons))
60-- | Used to determine if the atom arguments can be used with the data constructor.
62-- | Used to determine if the atom arguments can be used with the data constructor.
@@ -116,17 +118,16 @@ resolveTypeConstructorTypeVars (ADTypeConstructor tConsName _) (ConstructedAtomT
116 Left (TypeConstructorTypeVarsMismatch expectedPVarNames (M.keysSet pVarMap'))
118 Left (TypeConstructorTypeVarsMismatch expectedPVarNames (M.keysSet pVarMap'))
117resolveTypeConstructorTypeVars (TypeVariable tvName) typ _ = Right (M.singleton tvName typ)
119resolveTypeConstructorTypeVars (TypeVariable tvName) typ _ = Right (M.singleton tvName typ)
118resolveTypeConstructorTypeVars (ADTypeConstructor tConsName _) typ _ = Left (TypeConstructorAtomTypeMismatch tConsName typ)
120resolveTypeConstructorTypeVars (ADTypeConstructor tConsName _) typ _ = Left (TypeConstructorAtomTypeMismatch tConsName typ)
119−resolveTypeConstructorTypeVars (RelationAtomTypeConstructor attrExpr) typ tConsMap =
121+resolveTypeConstructorTypeVars (RelationAtomTypeConstructor attrExprs) typ tConsMap =
120 case typ of
122 case typ of
121 RelationAtomType attrs -> do
123 RelationAtomType attrs -> do
122− resolvedAttrs <- mapM (\atomtype' -> resolveAttributeExprTypeVars attrExpr atomtype' tConsMap) (map A.atomType (V.toList attrs))
124+ --resolve any typevars in the attrExprs
123− error "poop"
125+ error "poop4"
126+ otherType -> error "poop5"
125--used when recursing down sub-relation type definition
128--used when recursing down sub-relation type definition
126resolveAttributeExprTypeVars :: AttributeExprBase a -> AtomType -> TypeConstructorMapping -> Either RelationalError TypeVarMap
129resolveAttributeExprTypeVars :: AttributeExprBase a -> AtomType -> TypeConstructorMapping -> Either RelationalError TypeVarMap
127−resolveAttributeExprTypeVars (NakedAttributeExpr attr) aType _ = do
130+resolveAttributeExprTypeVars (NakedAttributeExpr attr) aType _ = error "poop2"
128− resolvedType <- resolveAtomType (A.atomType attr) aType
130resolveAttributeExprTypeVars (AttributeAndTypeNameExpr _ tCons _) aType tConsMap = resolveTypeConstructorTypeVars tCons aType tConsMap
131resolveAttributeExprTypeVars (AttributeAndTypeNameExpr _ tCons _) aType tConsMap = resolveTypeConstructorTypeVars tCons aType tConsMap
132-- check that type vars on the right also appear on the left
133-- check that type vars on the right also appear on the left
@@ -149,6 +150,10 @@ atomTypeForTypeConstructor (PrimitiveTypeConstructor _ aType) _ _ = Right aType
149atomTypeForTypeConstructor (TypeVariable tvname) _ tvMap = case M.lookup tvname tvMap of
150atomTypeForTypeConstructor (TypeVariable tvname) _ tvMap = case M.lookup tvname tvMap of
150 Nothing -> Right (TypeVariableType tvname)
151 Nothing -> Right (TypeVariableType tvname)
151 Just typ -> Right typ
152 Just typ -> Right typ
153+atomTypeForTypeConstructor (RelationAtomTypeConstructor attrExprs) tConsMap tvMap = do
154+ resolvedAtomTypes <- mapM (\expr -> atomTypeForAttributeExpr expr tConsMap tvMap) attrExprs
155+ let attrs = map (uncurry Attribute) (zip (map AE.attributeName attrExprs) resolvedAtomTypes)
156+ pure (RelationAtomType (A.attributesFromList attrs))
152atomTypeForTypeConstructor tCons tConss tvMap = case findTypeConstructor (TC.name tCons) tConss of
157atomTypeForTypeConstructor tCons tConss tvMap = case findTypeConstructor (TC.name tCons) tConss of
153 Nothing -> Left (NoSuchTypeConstructorError (TC.name tCons))
158 Nothing -> Left (NoSuchTypeConstructorError (TC.name tCons))
154 Just (tConsDef, _) -> do
159 Just (tConsDef, _) -> do
@@ -156,6 +161,10 @@ atomTypeForTypeConstructor tCons tConss tvMap = case findTypeConstructor (TC.nam
156 let pVarNames = TCD.typeVars tConsDef
161 let pVarNames = TCD.typeVars tConsDef
157 tConsArgs = M.fromList (zip pVarNames tConsArgTypes)
162 tConsArgs = M.fromList (zip pVarNames tConsArgTypes)
158 Right (ConstructedAtomType (TC.name tCons) tConsArgs)
163 Right (ConstructedAtomType (TC.name tCons) tConsArgs)
165+atomTypeForAttributeExpr :: AttributeExprBase a -> TypeConstructorMapping -> TypeVarMap -> Either RelationalError AtomType
166+atomTypeForAttributeExpr (NakedAttributeExpr attr) _ _ = pure (A.atomType attr)
167+atomTypeForAttributeExpr (AttributeAndTypeNameExpr _ tCons _) tConsMap tvMap = atomTypeForTypeConstructor tCons tConsMap tvMap
160findTypeConstructor :: TypeConstructorName -> TypeConstructorMapping -> Maybe (TypeConstructorDef, [DataConstructorDef])
169findTypeConstructor :: TypeConstructorName -> TypeConstructorMapping -> Maybe (TypeConstructorDef, [DataConstructorDef])
161findTypeConstructor name = foldr tConsFolder Nothing
170findTypeConstructor name = foldr tConsFolder Nothing
@@ -228,7 +237,7 @@ validateAtomType typ@(ConstructedAtomType tConsName tVarMap) tConss =
230 _ -> Right ()
239 _ -> Right ()
231−validateAtomType (RelationAtomType attrs) = error "poop"
240+validateAtomType (RelationAtomType attrs) _ = error "poop"
232validateAtomType _ _ = Right ()
241validateAtomType _ _ = Right ()
234validateTuple :: RelationTuple -> TypeConstructorMapping -> Either RelationalError ()
243validateTuple :: RelationTuple -> TypeConstructorMapping -> Either RelationalError ()