-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
205 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
module ProjectM36.DataTypes.SQL.Null where | ||
import ProjectM36.Base | ||
import ProjectM36.AtomFunctionError | ||
import qualified Data.Map as M | ||
import qualified Data.HashSet as HS | ||
|
||
-- analogous but not equivalent to a Maybe type due to how NULLs interact with every other value | ||
|
||
nullAtomType :: AtomType -> AtomType | ||
nullAtomType arg = ConstructedAtomType "SQLNullable" (M.singleton "a" arg) | ||
|
||
nullTypeConstructorMapping :: TypeConstructorMapping | ||
nullTypeConstructorMapping = [(ADTypeConstructorDef "SQLNullable" ["a"], | ||
[DataConstructorDef "SQLNull" [], | ||
DataConstructorDef "SQLJust" [DataConstructorDefTypeVarNameArg "a"]]) | ||
] | ||
|
||
nullAtomFunctions :: AtomFunctions | ||
nullAtomFunctions = HS.fromList [ | ||
Function { | ||
funcName = "sql_isnull", --this function works on any type variable, not just SQLNullable because removing the isnull function in cases where the type is clearly not SQLNullable is more difficult | ||
funcType = [TypeVariableType "a", BoolAtomType], | ||
funcBody = FunctionBuiltInBody $ | ||
\case | ||
a:[] -> pure $ BoolAtom (isNull a) | ||
_ -> Left AtomFunctionTypeMismatchError | ||
}, | ||
Function { | ||
funcName = "sql_equals", | ||
funcType = [nullAtomType (TypeVariableType "a"), | ||
nullAtomType (TypeVariableType "a"), | ||
nullAtomType BoolAtomType], | ||
funcBody = FunctionBuiltInBody nullEq | ||
}, | ||
Function { | ||
funcName = "sql_and", | ||
funcType = [TypeVariableType "a", TypeVariableType "b", BoolAtomType], -- for a more advanced typechecker, this should be BoolAtomType or SQLNullable BoolAtomType | ||
funcBody = FunctionBuiltInBody nullAnd | ||
} | ||
] | ||
where | ||
sqlNull typ = ConstructedAtom "SQLNull" typ [] | ||
sqlNullable val typ = ConstructedAtom "SQLJust" typ [val] | ||
isNull (ConstructedAtom dConsName _ _) | dConsName == "SQLNull" = True | ||
isNull _ = False | ||
nullEq :: AtomFunctionBodyType | ||
nullEq (a@(ConstructedAtom _ typA argsA) : b@(ConstructedAtom _ _ argsB) : []) | ||
| isNull a || isNull b = pure $ sqlNull typA | ||
| otherwise = pure $ sqlNullable (BoolAtom $ argsA == argsB) BoolAtomType | ||
nullEq _ = Left AtomFunctionTypeMismatchError | ||
|
||
isSQLBool :: Atom -> Bool | ||
isSQLBool (ConstructedAtom dConsName BoolAtomType [_]) | dConsName == "SQLNullable" = True | ||
isSQLBool (BoolAtom _) = True | ||
isSQLBool _ = False | ||
|
||
sqlBool :: Atom -> Maybe Bool | ||
sqlBool (ConstructedAtom dConsName BoolAtomType [BoolAtom tf]) | dConsName == "SQLJust" = Just tf | ||
sqlBool (ConstructedAtom dConsName BoolAtomType []) | dConsName == "SQLNull" = Nothing | ||
sqlBool (BoolAtom tf) = Just tf | ||
sqlBool x | isSQLBool x = error "internal sqlBool type error" -- should be caught above | ||
sqlBool _ = error "sqlBool type mismatch" | ||
|
||
|
||
nullAnd :: [Atom] -> Either AtomFunctionError Atom | ||
nullAnd [a,b] | isSQLBool a && isSQLBool b = do | ||
let bNull = nullAtom BoolAtomType Nothing | ||
boolF = nullAtom BoolAtomType (Just (BoolAtom False)) | ||
pure $ case (sqlBool a, sqlBool b) of | ||
(Nothing, Nothing) -> bNull | ||
(Nothing, Just True) -> bNull | ||
(Nothing, Just False) -> boolF | ||
(Just True, Nothing) -> bNull | ||
(Just False, Nothing) -> boolF | ||
(Just a', Just b') -> | ||
nullAtom BoolAtomType (Just (BoolAtom (a' && b'))) | ||
nullAnd _ = Left AtomFunctionTypeMismatchError | ||
|
||
nullAtom :: AtomType -> Maybe Atom -> Atom | ||
nullAtom aType mAtom = | ||
case mAtom of | ||
Nothing -> ConstructedAtom "SQLNull" (nullAtomType aType) [] | ||
Just atom -> ConstructedAtom "SQLJust" (nullAtomType aType) [atom] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
-- | Enables SQL-equivalent features such as NULL types in the database in addition to Project:M36 basic functions. | ||
module ProjectM36.SQLDatabaseContext where | ||
import ProjectM36.Base | ||
import ProjectM36.DatabaseContext | ||
import ProjectM36.DataTypes.SQL.Null | ||
|
||
sqlDatabaseContext :: DatabaseContext | ||
sqlDatabaseContext = basicDatabaseContext { atomFunctions = | ||
atomFunctions basicDatabaseContext <> nullAtomFunctions, | ||
typeConstructorMapping = | ||
typeConstructorMapping basicDatabaseContext <> nullTypeConstructorMapping | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.