I just finished hacking the SML/NJ parser to allow monad comprehension sugar. Here is an example of using it when using the list monad.
- - infix 5 >>=;
- infix 5 >>=
- - [| x | x <~ [1,2,3] |];
- val it = [1,2,3] : int list
- - [| (x, y) | x <~ [1,2], y <~ ["a", "b"] |];
- val it = [(1,"a"),(1,"b"),(2,"a"),(2,"b")] : (int * string) list
I first looked at adding »do« notation, but that would have been kind of ugly without the offside rule. The only disadvantage to using comprehension syntax is that you must always »return« something. However, I expect that will be reasonable enough for my work on InforML. Additionally I had original chosen to use <- instead of <~, but it was already taken in some user defined code somewhere in the SML basis. I also determined that the source of my mysterious parsing behavior in InforML was being caused by a few small instances of backtracking in my grammar. However, I still can't see how backtracking was causing the problem. In any event, I was lucky and discovered that the backtracking was no longer necessary in the grammar and all is well.