agentm / project-m36

Project: M36 Relational Algebra Engine
The Unlicense
876 stars 47 forks source link

resolveFunctionReturnValue #342

Closed YuMingLiao closed 2 years ago

YuMingLiao commented 2 years ago
resolveFunctionReturnValue :: FunctionName -> TypeVarMap -> AtomType -> Either RelationalError AtomType
  resolveFunctionReturnValue funcName' tvMap (ConstructedAtomType tCons retMap) = do
    let diff = M.difference (traceShow ("ret" ++ show retMap) $ retMap) (traceShow ("tvMap" ++ show tvMap) $ tvMap)
    if M.null diff then
      pure (ConstructedAtomType tCons (M.intersection tvMap retMap))
      else
      Left (AtomFunctionTypeVariableResolutionError funcName' (fst (head (M.toList diff))))
TutorialD (master/main): loadatomfunctions "ConversionFunctions" "haskellFunctions" "ConversionFunctions.so"
TutorialD (master/main): :showexpr nutrition{`樣本數`}:{sample := text2MaybeInt(@`樣本數`)}
"funcArgTypes: [TextAtomType]"
"argTypes: [TextAtomType]"
"ret: fromList [(\"a\",IntAtomType)]"
"tvMap: fromList []"
ERR: AtomFunctionTypeVariableResolutionError "text2MaybeInt" "a"
Prelude ProjectM36.Atomable Data.Proxy> toAtomType (Proxy :: Proxy (Maybe Int))
ConstructedAtomType "Maybe" (fromList [("a",IntAtomType)])

It seems the return type Maybe Int is not considered as resolved.

Should I go further to check there is no TypeVariable in retMap to determine if it is resolved?

YuMingLiao commented 2 years ago
TutorialD (master/main): :showexpr nutrition{`樣本數`}:{sample := text2MaybeInt(@`樣本數`)}
"extTupProc:FunctionAtomExpr \"text2MaybeInt\" [AttributeAtomExpr \"\\27171\\26412\\25976\"] UncommittedContextMarker
ConstructedAtomType \"Maybe\" (fromList [])"
"expectedType:ConstructedAtomType \"Maybe\" (fromList [])"
ERR: TypeConstructorTypeVarsTypesMismatch "Maybe" (fromList []) (fromList [("a",IntAtomType)])

It seems that parsing FunctionAtomExpr couldn't get Maybe Int.

agentm commented 2 years ago

Ugh- I've had problems with this function in the past. With hindsight, we should have two different data types for concrete (saturated) data types and those which are not yet resolved. I'll take a look to see what the best solution is here.

agentm commented 2 years ago

I am able to reproduce this with this script:

TutorialD (master/main): addatomfunction "text2MaybeInt" Text -> Either AtomFunctionError (Maybe Int) """(const $ Right $ ConstructedAtom "Just" (ConstructedAtomType "Maybe" (Data.Map.singleton "a" IntAtomType)) [IntegerAtom 4]) :: [Atom] -> Either AtomFunctionError Atom"""
TutorialD (master/main): :showexpr relation{tuple{spam text2MaybeInt("123")}}
ERR: AtomFunctionTypeVariableResolutionError "text2MaybeInt" "a"
agentm commented 2 years ago

I've pushed a fix for the immediate issue, but I would still like to separate saturated and unsaturated types- that would prevent a lot of these types of issues.