I am trying to translate traverse/ sequenceAinto Javascript. Now the following Haskell implementation behavior gives me problems:
traverse (\x -> x) Nothing -- yields Nothing
sequenceA Nothing -- yields Nothing
traverse (\x -> x) (Just [7]) -- yields [Just 7]
sequenceA (Just [7]) -- yields [Just 7]
As a newbie to Haskell, I wonder why the first expression works at all:
instance Traversable Maybe where
traverse _ Nothing = pure Nothing
traverse f (Just x) = Just <$> f x
pure Nothingshould not work in this case, since there is no minimal application context in which the value can be placed. It seems that the compiler lazily checks the type of this expression and since the mapping of the id function over Nothingis noop, it simply “skips” the type error, so to speak.
Here is my Javascript translation:
( , , Javascirpt , , CH .)
const $tag = Symbol.for("ftor/tag");
const $Option = Symbol.for("ftor/Option");
const Option = {};
const Some = x => {
const Some = r => {
const Some = f => f(x);
return Some[$tag] = "Some", Some[$Option] = true, Some;
};
return Some[$tag] = "Some", Some[$Option] = true, Some;
};
const None = r => {
const None = f => r;
return None[$tag] = "None", None[$Option] = true, None;
};
None[$tag] = "None";
None[$Option] = true;
Option.traverse = (of, map) => ft => tx =>
tx[$Option] && tx(of(None)) (x => map(Some) (ft(x)));
const map = f => xs => xs.map(f);
const of = x => [x];
const I = x => x;
Option.traverse(of, map) (I) (None)
Option.traverse(of, map) (I) (Some([7]))
, Haskell, [None], None. , , , .