I came up with some code when trying to understand applicatives:<div><br></div><div><div>import Control.Applicative</div><div>import qualified Data.Map as M</div><div><br></div><div>instance Applicative (M.Map String) where</div>
<div> pure x = M.fromList [("",x)]</div><div> fs <*> xs = M.fromList [(k1 ++ " " ++ k2,v1 v2) | k1 <- M.keys fs, k2 <- M.keys xs, v1 <- M.elems fs, v2 <- M.elems xs]</div></div><div>
<br></div><div>1. When I :load this in ghci it gives me some error about using (M.Map String) here, and tells me it'll work if I use the -XFlexibleInstances flag. Why is this type of behavior disabled by default? Is it potentially dangerous in some way?</div>
<div><br></div><div>2. When running the following:</div><div><br></div><div> fromList [("double",(*2))] <*> fromList[("two",2),("seven",7)]</div><div><br></div><div>I get:</div><div>
<br></div><div> fromList [("double seven",4),("double two",4)]</div><div><br></div><div>instead of what I'd expect:</div><div><br></div><div> fromList [("double seven",14),("double two",4)]</div>
<div><br></div><div>Although this:</div><div><br></div><div><div> (*2) <$> fromList[("two",2),("seven",7)]</div><div><br></div><div>gives what I'd expect:</div><div><br></div><div> fromList [("seven",14),("two",4)]</div>
</div><div><br></div><div>Why is this happening? I can't seem to figure it out.</div><div><br></div><div><br></div>