2
votes

I'm writing a Haskell function that takes 2 substitutions as arguments. I've handled the case where if either of the arguments is Nothing, then the function will return Nothing. If neither one is Nothing, it should combine them into a single substitution. I've done the following:

args :: Maybe (Subst a) -> Maybe (Subst a) -> Maybe (Subst a)
args (Just v) (Just v') = Just (v ++ v')
args _        _         = Nothing

However, I'm receiving errors that the expected type doesn't match the actual type. I'm confused as to why. Any ideas?

2

2 Answers

3
votes

The type of ++ is

(++) :: [a] -> [a] -> [a]

but you're trying to apply it to two values of type Subst a.

You could write a helper function combineSubs like this:

combineSubs :: Subst a -> Subst a -> Subst a
combineSubs (S xs) (S ys) = S (xs ++ ys)

You can also pattern match on S in the arguments of your function args.

2
votes

Just a remark to your architecture:

Is there really a difference between no substitution (which would be Nothing here) and an "empty" substituion S []? If not, you probably should drop the Maybe level and code missing substitutions simply as S []. If there is a difference, you might consider to add this case to Subst, e.g. data Subst a = S [(String,a)] | None.