diff --git a/src/Model/Type.hs b/src/Model/Type.hs index 5ead1e6..d475b4e 100644 --- a/src/Model/Type.hs +++ b/src/Model/Type.hs @@ -115,6 +115,7 @@ data CardinalityCoercion = | MakeObject2MaybeCoercion {fromCardinality :: Cardinality, toCardinality :: Cardinality} | MakeObject2ListCoercion {fromCardinality :: Cardinality, toCardinality :: Cardinality} | MakeOneOfCoercion {toCardinality :: Cardinality} + | MakeMaybe2ObjectCoercion {toCardinality :: Cardinality} deriving (Eq, Show) -- |Used to handle polymorphism in Rosetta diff --git a/src/PrettyPrinter/Expression.hs b/src/PrettyPrinter/Expression.hs index 3fbeac9..1582f2b 100644 --- a/src/PrettyPrinter/Expression.hs +++ b/src/PrettyPrinter/Expression.hs @@ -49,9 +49,9 @@ printExpression (ExplicitFunction "all <>" args returnCoerce) = printCoercion (s printExpression (ExplicitFunction "all =" args returnCoerce) = "all (Eq)" <+> printCoercion (snd $ head $ tail args) (printExpression (fst $ head $ tail args)) <+> printCoercion (snd $ head args) (printExpression (fst $ head args)) printExpression (ExplicitFunction "and" args returnCoerce) = printCoercion (snd $ head args) (printExpression (fst $ head args)) <+> "&&" <+> printCoercion (snd $ head $ tail args) (printExpression (fst $ head $ tail args)) printExpression (ExplicitFunction "or" args returnCoerce) = printCoercion (snd $ head args) (printExpression (fst $ head args)) <+> "||" <+> printCoercion (snd $ head $ tail args) (printExpression (fst $ head $ tail args)) -printExpression (ExplicitFunction name args returnCoerce) = +printExpression (ExplicitFunction name args returnCoerce) = if null printedArgs then pretty (uncapitalize name) - else pretty (uncapitalize name) <+> printCoercion returnCoerce (hsep (map (enclose "(" ")") printedArgs)) + else pretty (uncapitalize name) <+> (hsep (map (enclose "(" ")") printedArgs)) where printedArgs = zipWith printCoercion [c | (e,c) <- args] [printExpression e | (e, c) <- args] printExpression (ExplicitIfSimple cond thenBlock returnCoercion) = printIf @@ -101,7 +101,8 @@ printCardinalityCoercion (MakeListCardinalityCoercion _ _) d = d printCardinalityCoercion (MakeNothing2MaybeCoercion _ _) d = "Nothing" printCardinalityCoercion (MakeNothing2ListCoercion _ _) d = "[]" printCardinalityCoercion (MakeMaybe2ListCoercion _ _) d = "maybeToList" <+> enclose "(" ")" d -printCardinalityCoercion (MakeObject2MaybeCoercion _ _) d = "fromJust" <+> enclose "(" ")" d +printCardinalityCoercion (MakeObject2MaybeCoercion _ _) d = "Just" <+> enclose "(" ")" d +printCardinalityCoercion (MakeMaybe2ObjectCoercion _) d = "fromJust" <+> enclose "(" ")" d printCardinalityCoercion (MakeObject2ListCoercion _ _) d = "[" <> d <> "]" printCardinalityCoercion (MakeOneOfCoercion _) d = d diff --git a/src/Semantic/ExpressionChecker.hs b/src/Semantic/ExpressionChecker.hs index 8341b04..ed5abb8 100644 --- a/src/Semantic/ExpressionChecker.hs +++ b/src/Semantic/ExpressionChecker.hs @@ -166,13 +166,18 @@ checkExpression defT symbolMap (PathExpression ex1 (Variable b)) = Left err -> Left err Right exp1 -> case findAttributeTypeRec defT b type1 of Left err -> Left $ UndefinedVariable $ show (typeName type1) ++ " -> " ++ b - Right exp2 -> case Bounds(1, 1) `cardinalityIncluded` crd1 of + Right exp2 -> case cardC of Left err -> Left $ PathExpressionOnList (show ex1) Right c -> if MakeCondition Nothing (Keyword "one-of") `elem` conditions (typeFromExpression exp1) then Right $ ExplicitPath (changeCoercion exp1 ((returnCoercion exp1){cardinalityCoercion = c})) (changeCoercion exp2 exp2C) exp2C else Right $ ExplicitPath (changeCoercion exp1 ((returnCoercion exp1){cardinalityCoercion = c})) exp2 (returnCoercion exp2) - where - exp2C = MakeCoercion (typeCoercion $ returnCoercion exp2) (MakeOneOfCoercion (Bounds (1,1))) + where + exp2C = MakeCoercion (typeCoercion $ returnCoercion exp2) (MakeOneOfCoercion (Bounds (1,1))) + cardC = case crd1 of + Bounds (1, 1) -> Right $ MakeCardinalityIdCoercion crd1 + Bounds (0, 1) -> Right $ MakeMaybe2ObjectCoercion (Bounds (1, 1)) + _ -> Left $ PathExpressionOnList (show ex1) + where type1 = typeFromExpression exp1 crd1 = toCardinality $ cardinalityCoercion $ returnCoercion exp1