on master help sign in
add fromMaybe function for Maybe types
79f2d42AgentM
src/lib/ProjectM36/DataTypes/Maybe.hs+9−2
@@ -1,8 +1,10 @@
1module ProjectM36.DataTypes.Maybe where
1module ProjectM36.DataTypes.Maybe where
2import ProjectM36.Base
2import ProjectM36.Base
3+import ProjectM36.DataTypes.Primitive
3import qualified Data.HashSet as HS
4import qualified Data.HashSet as HS
4import qualified Data.Map as M
5import qualified Data.Map as M
5
6
7+
6maybeAtomType :: AtomType -> AtomType
8maybeAtomType :: AtomType -> AtomType
7maybeAtomType arg = ConstructedAtomType "Maybe" (M.singleton "a" arg)
9maybeAtomType arg = ConstructedAtomType "Maybe" (M.singleton "a" arg)
8
10
@@ -17,6 +19,11 @@ maybeAtomFunctions = HS.fromList [
17 AtomFunction {
19 AtomFunction {
18 atomFuncName ="isJust",
20 atomFuncName ="isJust",
19 atomFuncType = [maybeAtomType AnyAtomType, BoolAtomType],
21 atomFuncType = [maybeAtomType AnyAtomType, BoolAtomType],
20− atomFuncBody = AtomFunctionBody Nothing $ \((ConstructedAtom dConsName _ _):_) -> BoolAtom (dConsName == "Nothing")
22+ atomFuncBody = AtomFunctionBody Nothing $ \((ConstructedAtom dConsName _ _):_) -> BoolAtom (dConsName /= "Nothing")
23+ },
24+ AtomFunction {
25+ atomFuncName = "fromMaybe",
26+ atomFuncType = [AnyAtomType, maybeAtomType AnyAtomType, AnyAtomType], -- this really should be more specific, but we don't have type checker handling for this yet
27+ atomFuncBody = AtomFunctionBody Nothing $ \(defaultAtom:(ConstructedAtom dConsName _ (atomVal:_)):_) -> if atomTypeForAtom defaultAtom /= atomTypeForAtom atomVal then error "poop" else if dConsName == "Nothing" then defaultAtom else atomVal
21− }
28+ }
22 ]
29 ]