@@ -72,18 +72,23 @@ atomTypeForDataConstructor tConss dConsName atomArgTypes =
7272
7373-- | Walks the data and type constructors to extract the type variable map.
7474resolveDataConstructorTypeVars :: DataConstructorDef -> [AtomType] -> TypeConstructorMapping -> Either RelationalError TypeVarMap
75−resolveDataConstructorTypeVars dCons aTypeArgs tConss = do
76− maps <- mapM (\(dCons',aTypeArg) -> resolveDataConstructorArgTypeVars dCons' aTypeArg tConss) (zip (DCD.fields dCons) aTypeArgs)
75+resolveDataConstructorTypeVars dCons@(DataConstructorDef _ defArgs) aTypeArgs tConss = do
76+ let defCount = length defArgs
77+ argCount = length aTypeArgs
78+ if defCount /= argCount then
79+ Left (ConstructedAtomArgumentCountMismatchError defCount argCount)
80+ else do
81+ maps <- mapM (\(dCons',aTypeArg) -> resolveDataConstructorArgTypeVars dCons' aTypeArg tConss) (zip (DCD.fields dCons) aTypeArgs)
7782 --if any two maps have the same key and different values, this indicates a type arg mismatch
78− let typeVarMapFolder valMap acc = case acc of
79− Left err -> Left err
80− Right accMap -> if accMap `M.isSubmapOf` valMap then
81− Right (M.union accMap valMap)
82− else
83− Left (DataConstructorTypeVarsMismatch (DCD.name dCons) accMap valMap)
84− case foldr typeVarMapFolder (Right M.empty) maps of
85− Left err -> Left err
86− Right typeVarMaps -> pure typeVarMaps
83+ let typeVarMapFolder valMap acc = case acc of
84+ Left err -> Left err
85+ Right accMap -> if accMap `M.isSubmapOf` valMap then
86+ Right (M.union accMap valMap)
87+ else
88+ Left (DataConstructorTypeVarsMismatch (DCD.name dCons) accMap valMap)
89+ case foldr typeVarMapFolder (Right M.empty) maps of
90+ Left err -> Left err
91+ Right typeVarMaps -> pure typeVarMaps
8792 --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 TypeVar which is resolved when the relation is constructed- the relation must contain all resolved atom types.
8893
8994
@@ -111,8 +116,8 @@ resolveTypeConstructorTypeVars (ADTypeConstructor tConsName _) (ConstructedAtomT
111116 Right pVarMap'
112117 else
113118 Left (TypeConstructorTypeVarsMismatch expectedPVarNames (M.keysSet pVarMap'))
114−resolveTypeConstructorTypeVars (TypeVariable tvName) typ _ = Right (M.singleton tvName typ)
115−resolveTypeConstructorTypeVars x y _ = error $ "Unhandled type vars:" ++ show x ++ show y
119+resolveTypeConstructorTypeVars (TypeVariable tvName) typ _ = Right (M.singleton tvName typ)
120+resolveTypeConstructorTypeVars (ADTypeConstructor tConsName _) typ _ = Left (TypeConstructorAtomTypeMismatch tConsName typ)
116121
117122-- check that type vars on the right also appear on the left
118123-- check that the data constructor names are unique