module ProjectM36.DataTypes.Maybe where
import ProjectM36.Base
import ProjectM36.DataTypes.Primitive
import ProjectM36.AtomFunctionError
import qualified Data.HashSet as HS
import qualified Data.Map as M

maybeAtomType :: AtomType -> AtomType
maybeAtomType :: AtomType -> AtomType
maybeAtomType AtomType
arg = TypeConstructorName -> TypeVarMap -> AtomType
ConstructedAtomType TypeConstructorName
"Maybe" (forall k a. k -> a -> Map k a
M.singleton TypeConstructorName
"a" AtomType
arg)
  
maybeTypeConstructorMapping :: TypeConstructorMapping
maybeTypeConstructorMapping :: TypeConstructorMapping
maybeTypeConstructorMapping = [(TypeConstructorName -> [TypeConstructorName] -> TypeConstructorDef
ADTypeConstructorDef TypeConstructorName
"Maybe" [TypeConstructorName
"a"],
                                [TypeConstructorName
-> [DataConstructorDefArg] -> DataConstructorDef
DataConstructorDef TypeConstructorName
"Nothing" [],
                                 TypeConstructorName
-> [DataConstructorDefArg] -> DataConstructorDef
DataConstructorDef TypeConstructorName
"Just" [TypeConstructorName -> DataConstructorDefArg
DataConstructorDefTypeVarNameArg TypeConstructorName
"a"]])
                              ]

maybeAtomFunctions :: AtomFunctions
maybeAtomFunctions :: AtomFunctions
maybeAtomFunctions = forall a. (Eq a, Hashable a) => [a] -> HashSet a
HS.fromList [
  Function {
     funcName :: TypeConstructorName
funcName =TypeConstructorName
"isJust",
     funcType :: [AtomType]
funcType = [AtomType -> AtomType
maybeAtomType (TypeConstructorName -> AtomType
TypeVariableType TypeConstructorName
"a"), AtomType
BoolAtomType],
     funcBody :: FunctionBody ([Atom] -> Either AtomFunctionError Atom)
funcBody = forall a. a -> FunctionBody a
FunctionBuiltInBody forall a b. (a -> b) -> a -> b
$
       \case
               ConstructedAtom TypeConstructorName
dConsName AtomType
_ [Atom]
_:[Atom]
_ -> forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ Bool -> Atom
BoolAtom (TypeConstructorName
dConsName forall a. Eq a => a -> a -> Bool
/= TypeConstructorName
"Nothing")
               [Atom]
_ -> forall a b. a -> Either a b
Left AtomFunctionError
AtomFunctionTypeMismatchError
     },
  Function {
     funcName :: TypeConstructorName
funcName = TypeConstructorName
"fromMaybe",
     funcType :: [AtomType]
funcType = [TypeConstructorName -> AtomType
TypeVariableType TypeConstructorName
"a", AtomType -> AtomType
maybeAtomType (TypeConstructorName -> AtomType
TypeVariableType TypeConstructorName
"a"), TypeConstructorName -> AtomType
TypeVariableType TypeConstructorName
"a"],
     funcBody :: FunctionBody ([Atom] -> Either AtomFunctionError Atom)
funcBody = forall a. a -> FunctionBody a
FunctionBuiltInBody forall a b. (a -> b) -> a -> b
$
       \case
         (Atom
defaultAtom:ConstructedAtom TypeConstructorName
dConsName AtomType
_ (Atom
atomVal:[Atom]
_):[Atom]
_) -> if Atom -> AtomType
atomTypeForAtom Atom
defaultAtom forall a. Eq a => a -> a -> Bool
/= Atom -> AtomType
atomTypeForAtom Atom
atomVal then forall a b. a -> Either a b
Left AtomFunctionError
AtomFunctionTypeMismatchError else if TypeConstructorName
dConsName forall a. Eq a => a -> a -> Bool
== TypeConstructorName
"Nothing" then forall (f :: * -> *) a. Applicative f => a -> f a
pure Atom
defaultAtom else forall (f :: * -> *) a. Applicative f => a -> f a
pure Atom
atomVal
         [Atom]
_ ->forall a b. a -> Either a b
Left AtomFunctionError
AtomFunctionTypeMismatchError         
     }
  ]

{- To create an inclusion dependency for uniqueness for "Just a" values only 
person := relation{name Text, boss Maybe Text}{tuple{name "Steve",boss Nothing}, tuple{name "Bob", boss Just "Steve"}}
:showexpr ((relation{tuple{}}:{a:=person where ^isJust(@boss)}):{b:=count(@a)}){b}
:showexpr ((relation{tuple{}}:{a:=person{boss} where ^isJust(@boss)}):{b:=count(@a)}){b}
constraint uniqueJust ((relation{tuple{}}:{a:=person where ^isJust(@boss)}):{b:=count(@a)}){b} in ((relation{tuple{}}:{a:=person{boss} where ^isJust(@boss)}):{b:=count(@a)}){b}
person := relation{name Text, boss Maybe Text}{tuple{name "Steve",boss Nothing}, tuple{name "Bob", boss Just "Steve"}, tuple{name "Jim", boss Just "Steve"}} 
ERR: InclusionDependencyCheckError "uniqueJust"
-}