Home > Back-end >  How to correctly return nested types in haskell?
How to correctly return nested types in haskell?

Time:01-18

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 [] []

CodePudding user response:

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.

  • Related