How to correctly return nested types in haskell?


I want to return empty lists if one of the parameters is Nothing, but I can’t

data UtilsDiff = UtilsDiff {syntaxBlockOptions :: [String], optionsBlockOptions :: [String]}
  deriving Show
data UtilsOrDiff = UtilsOrDiff Utils | Diff UtilsDiff
  deriving Show

useBlockExpr :: Parser UtilsOrDiff
useBlockExpr = do
  u1 <- syntaxExpr
  opts <- optionsBlockExpr
  _ <- absorbToStopWord "Description:"
  utilDesc <- many anyChar
  case options u1 of
    Nothing -> Diff [] [] -- <- this line
    Just val-> do

Apparently I haven’t figured out exactly how to return the Diff type

warning: [-Wdeferred-type-errors]     
• Couldn't match expected type ‘[a0] -> Text.Parsec.Prim.ParsecT String () Data.Functor.Identity.Identity UtilsOrDiff’ with actual type ‘UtilsOrDiff’
 • The function ‘Diff’ is applied to two value arguments,but its type ‘UtilsDiff -> UtilsOrDiff’ has only one 
In the expression: Diff [] [] In a case alternative: Nothing -> Diff [] []

>Solution :

Diff has type UtilsDiff -> UtilsOrDiff, not [a] -> [a] -> UtilsDiff. You need to create the UtilsDiff value first, then use that to create the Diff value.

Nothing -> return $ Diff (UtilsDiff [] [])

As the Just branch presumably evaluates to a Parser UtilsOrDiff value, so must the Nothing branch, hence the call to return.

Leave a Reply Cancel reply