@@ -1,11 +1,9 @@
11{-# LANGUAGE OverloadedStrings #-}
22module ProjectM36.AtomType where
33import ProjectM36.Base
4−import ProjectM36.DataTypes.Primitive
54import qualified ProjectM36.TypeConstructorDef as TCD
65import qualified ProjectM36.TypeConstructor as TC
76import qualified ProjectM36.DataConstructorDef as DCD
8−import qualified Data.Text as T
97import ProjectM36.Error
108import ProjectM36.Attribute
119import qualified Data.Vector as V
@@ -79,11 +77,18 @@ atomTypeForDataConstructor tConss dConsName atomArgTypes = do
7977resolveDataConstructorTypeVars :: DataConstructorDef -> [AtomType] -> TypeConstructorMapping -> Either RelationalError TypeVarMap
8078resolveDataConstructorTypeVars dCons aTypeArgs tConss = do
8179 maps <- mapM (\(dCons',aTypeArg) -> resolveDataConstructorArgTypeVars dCons' aTypeArg tConss) (zip (DCD.fields dCons) aTypeArgs)
82− --if any two maps have the same key and different values- bail!
83− let typeVarMapFolder
84− foldr typeVarMapFolder (Right M.empty) maps
80+ --if any two maps have the same key and different values, this indicates a type arg mismatch
81+ let typeVarMapFolder valMap acc = case acc of
82+ Left err -> Left err
83+ Right accMap -> if accMap `M.isSubmapOf` valMap then
84+ Right (M.union accMap valMap)
85+ else
86+ Left (DataConstructorTypeVarsMismatch (DCD.name dCons) accMap valMap)
87+ case foldr typeVarMapFolder (Right M.empty) maps of
88+ Left err -> Left err
89+ Right typeVarMaps -> pure typeVarMaps
8590 --if the data constructor cannot complete a type constructor variables (ex. "Nothing" could be Maybe Int or Maybe Text, etc.), then fill that space with AnyAtomType which is resolved when the relation is constructed- the relation must contain all resolved atom types.
86− pure (M.unions maps)
91+
8792
8893-- | Attempt to match the data constructor argument to a type constructor type variable.
8994resolveDataConstructorArgTypeVars :: DataConstructorDefArg -> AtomType -> TypeConstructorMapping -> Either RelationalError TypeVarMap
@@ -92,13 +97,13 @@ resolveDataConstructorArgTypeVars (DataConstructorDefTypeConstructorArg tCons) a
9297resolveDataConstructorArgTypeVars (DataConstructorDefTypeVarNameArg pVarName) aType _ = Right (M.singleton pVarName aType)
9398
9499resolveTypeConstructorTypeVars :: TypeConstructor -> AtomType -> TypeConstructorMapping -> Either RelationalError TypeVarMap
95−resolveTypeConstructorTypeVars (PrimitiveTypeConstructor _ pType) aType tConss =
100+resolveTypeConstructorTypeVars (PrimitiveTypeConstructor _ pType) aType _ =
96101 if aType /= pType then
97102 Left (AtomTypeMismatchError pType aType)
98103 else
99104 Right M.empty
100105
101−resolveTypeConstructorTypeVars (ADTypeConstructor tConsName tConsArgs) (ConstructedAtomType tConsName' pVarMap') tConss =
106+resolveTypeConstructorTypeVars (ADTypeConstructor tConsName _) (ConstructedAtomType tConsName' pVarMap') tConss =
102107 if tConsName /= tConsName' then
103108 Left (TypeConstructorNameMismatch tConsName tConsName')
104109 else
@@ -156,7 +161,7 @@ findTypeConstructor name tConsList = foldr tConsFolder Nothing tConsList
156161 accum
157162
158163resolveAtomType :: AtomType -> AtomType -> Either RelationalError AtomType
159−resolveAtomType orig@(ConstructedAtomType dConsName resolvedTypeVarMap) (ConstructedAtomType _ unresolvedTypeVarMap) = do
164+resolveAtomType orig@(ConstructedAtomType _ resolvedTypeVarMap) (ConstructedAtomType _ unresolvedTypeVarMap) = do
160165 _ <- resolveAtomTypesInTypeVarMap resolvedTypeVarMap unresolvedTypeVarMap
161166 pure orig
162167resolveAtomType typeFromRelation unresolvedType = if typeFromRelation == unresolvedType then
@@ -167,14 +172,14 @@ resolveAtomType typeFromRelation unresolvedType = if typeFromRelation == unresol
167172-- this could be optimized to reduce new tuple creation- if anyatomtype does not appear, just return the original typevarmap
168173resolveAtomTypesInTypeVarMap :: TypeVarMap -> TypeVarMap -> Either RelationalError TypeVarMap
169174resolveAtomTypesInTypeVarMap resolvedTypeMap unresolvedTypeMap = do
170− let lookup key tMap = case M.lookup key tMap of
175+ let lookup' key tMap = case M.lookup key tMap of
171176 Nothing -> Left (TypeConstructorTypeVarMissing key)
172177 Just val -> Right val
173178 let resolveTypePair unresKey AnyAtomType = do
174− resType <- lookup unresKey resolvedTypeMap
179+ resType <- lookup' unresKey resolvedTypeMap
175180 pure (unresKey, resType)
176181 resolveTypePair unresKey subType@(ConstructedAtomType _ _) = do --recurse
177− resType <- lookup unresKey resolvedTypeMap
182+ resType <- lookup' unresKey resolvedTypeMap
178183 resAType <- resolveAtomType resType subType
179184 pure (unresKey, resAType)
180185 resolveTypePair unresKey unresV = pure (unresKey, unresV)