tag:blogger.com,1999:blog-15136575.post4077169238394607913..comments2023-10-17T12:00:16.772+01:00Comments on Code rant: Monads in C#-7. Turning our parser into a MonadMike Hadlowhttp://www.blogger.com/profile/16441901713967254504noreply@blogger.comBlogger7125tag:blogger.com,1999:blog-15136575.post-60010876986291717892013-08-03T21:42:53.156+01:002013-08-03T21:42:53.156+01:00Wow! That is the coolest technique I've ever s...Wow! That is the coolest technique I've ever seen in C#. TBH I feel slightly ashamed that I didn't know you could extend the LINQ query syntax: I always use the method chains because I think it looks a bit out of place and thought it was totally superfluous - but this has totally changed my opinion :)Cozy treetophttps://www.blogger.com/profile/04984618778743798116noreply@blogger.comtag:blogger.com,1999:blog-15136575.post-58671779262430908142013-01-29T23:40:21.033+00:002013-01-29T23:40:21.033+00:00I'm late to the party, but great blog post, le...I'm late to the party, but great blog post, learned a lot, and I had no idea how powerful LINQ syntax was. Thanks for writing! <br /><br />I don't know why SelectMany takes the two funcs, seems like an interesting design decision (opposed to having two separate extension methods you'd define), but I'm glad you explained the how. As an exercise I gave the Reader Monad for Depenency Injection a shot in C# and it worked out quite well: <br /><br />https://gist.github.com/4640678<br /><br />Your parser Monad looks a lot like the State Monad interestingly enough ...Unknownhttps://www.blogger.com/profile/11423741726738312940noreply@blogger.comtag:blogger.com,1999:blog-15136575.post-23124756252334020682011-12-11T18:53:08.322+00:002011-12-11T18:53:08.322+00:00I've been reading on monads in C# for 3 days n...I've been reading on monads in C# for 3 days now and this post finally put it together for me. I have a pattern matching project (http://metasharp.codeplex.com) which as <i>almost</i> monadic but not quite and those areas where it isn't monadic it makes things a little rough, so I've been trying to see how to redo it in a monadic fashion. After trying this out in a million different ways I think I just realized that a Pattern Matching monad is actually a combination of two different monads: Maybe and IO. Instead of just using string I have to use Stream<S> and Bind to that as well. Figuring out how to use linq and select to do productions though really was the piece of the puzzle I was missing. I still have a lot to go but I think you helped me break through a barrier here! Thanks!Anonymoushttps://www.blogger.com/profile/09465842474543801600noreply@blogger.comtag:blogger.com,1999:blog-15136575.post-79832157592329262962011-11-26T05:57:00.538+00:002011-11-26T05:57:00.538+00:00ddarius in #haskell said to post the following cod...ddarius in #haskell said to post the following code:<br /><br /><br />var r = from as in "a".Find().Many()<br /> let n = as.Length<br /> from bs in "b".Find().Count(n)<br /> from cs in "c".Find().Count(n)<br /> select { as, bs, cs }<br /><br />It's possible to write Many() and Count() for the monad, and have them do backtracking. The internals of the monadic parser might need to be different though. The monad shown in Mike Hadlow's example is not the only way to do monadic parsing, just a simple one.Sgeohttps://www.blogger.com/profile/18170177145561744093noreply@blogger.comtag:blogger.com,1999:blog-15136575.post-49174223513217934522011-11-26T04:29:13.217+00:002011-11-26T04:29:13.217+00:00Keep in mind that your monad is free to implement ...Keep in mind that your monad is free to implement more than just the requirements to get LINQ syntax working. Parsec implements a thing that takes two parsers and combines them into one, where if the first fails, the second one is tried. (Well, by default, not really usable for lookahead, but there's another function that makes it usable for such)Sgeohttps://www.blogger.com/profile/18170177145561744093noreply@blogger.comtag:blogger.com,1999:blog-15136575.post-8977602332432728362011-02-13T09:43:17.533+00:002011-02-13T09:43:17.533+00:00Hi Michael,
Don't mistake me for someone who ...Hi Michael,<br /><br />Don't mistake me for someone who actually knows anything about parsers :)<br /><br />I don't really understand the difference between LL(1) and LALR, but looking at the documentation for Parsec (an industrial strength monadic parser in Haskell) suggests that it can support LALR (infinite look ahead grammars right?).<br /><br />http://legacy.cs.uu.nl/daan/download/parsec/parsec.html<br /><br />I'd agree with you though, I think this technique could be very useful where you need a simple DSL, but for anything serious you'd probably be better off with a parser toolkit like ANTLR.Mike Hadlowhttps://www.blogger.com/profile/16441901713967254504noreply@blogger.comtag:blogger.com,1999:blog-15136575.post-73636488016003490732011-02-13T03:30:08.932+00:002011-02-13T03:30:08.932+00:00Based on this example, it doesn't seem that a ...Based on this example, it doesn't seem that a monadic parser could be as powerful as shift/reduce. I can maybe see it handling LL(1) grammars, but can a monadic parser do LALR?<br /><br />Still, a useful example. I might choose this technique for those cases where Antler or Yacc would be overkill.Anonymousnoreply@blogger.com