I am currently writing an interpreter in haskell for a primitive programming language.
I am trying to see if the current program being analysed is correctly Typed before interpreting. Basically what I end up with is a list called maybeList
that holds maps of the initialised variables if they are correct, or Nothing
s if they don't, like this:
[Maybe [(k,v)]]
I am trying to fold over this list with this function
foldr (\x y -> (isJust x) && (isJust y)) True maybeList
From what I understand about Haskell and the foldr
function, this should work. However, it gives me the error:
Couldn't match expected type ‘Bool’ with actual type ‘Maybe a0’ In the expression: foldr (\ x y -> (isJust x) && (isJust y)) True maybeList
What I'm asking is why doesn't the compiler know that the isJust
function returns a boolean, it keeps treating it as a Maybe a0
type?
P.S. I understand that a simple elem Nothing maybeList
will work in place of this. But I would like to understand why this doesn't work
The reducing function for foldr
takes as second argument something of the same type as the initial element:
foldr (\x y -> (isJust x) && (isJust y)) True maybeList
-- \_______________________________/
-- |
-- these two must be the same type
So y
is of type Bool
but you are considering it of type Maybe a
.
Therefore, foldr (\x y -> (isJust x) && y) True maybeList
is the right solution.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With