Monday, 15 August 2011

scala - Wrapping a Monad into a parser - do I need Monad Transformers and how to do it? -


I have a monadic type exp and I want to create such parser which Parsing in the value of The following code works, but can I do some cool / cooler?

  def grid (x: int, y: int): problem = ??? Def expInt: parser [EXP [int]] = ??? Def Grid: Parser [Excl. [Problem]] = For {ex ~ _ ~ ey & lt; - "Grid (" ~> expInt ~ "," ~ expInt <~ ")"} {x   

I have heard about Moned Transformer, but still is scared of Scalaz 7's wierd import system. Can anyone answer that

  1. I can use the moned transformer and
  2. how it looks like a principle, using Scalge 7, the scala combination parser Own your exp Monaad

    "Weird" import system for the first easy part:

      import scales ._, scales._   

    all this will always work (except for the name collision, but this is a possibility with any library And it's easy to solve), and no one is paying attention to you not to use the new la carte import, which is mainly about documentation.

    The basic pattern for the mondad transformer is that you have a whatevert [f [_], ...] type class whose name is runWhateverT (Or just play ) which f [whichever [...]] returns .

    In this case it seems that you want to end with a parser [XP] , which tells you that you have your own expT [f [ _]] needs transformers. I will not start with details about how to implement it (which will definitely depend on the words of your mona), but OptionT :

      Import scala.util.parsing.combinator._ import scalaz._, Scalaz._ Object Expands MyParser RegexParsers {Inherent Val Monad = Parser Monad (this) Def Aud Number: OptionT [Parser, Int] = OptionT.optionT ( "\ D +". ^ ^ (_.toInt) ^ ^ (i = & gt; (i% 2! = 0) option i) def pairop objects: OptionT [parser, (int, int)] = {_ & Lt; LiftM [Option]] - Lifel ("("). LiftM [Option +] X & Lt; - Strange Number _ - Literal (","). LiftM [Option +] Y & lt; - Strange Number _ & Lt - Literal (")"). LiftM [OptionT]} yield (X, Y) DEF applied (S: string) = purse (pairOfOdds.run, s)}   

    See some discussion about why Implicit Val monad = ... line.

    It works like this:

      scala> MyParser ("(13, 43)") res0: MyParser.ParseResult [option ([int, int]] = [1.9] Parsed: some ((13,43)) scale & gt; MyParser (", 13, 42)") res1: MyParser.ParseResult [option [(int, int)] [[1.8] parsed: Any   

    Note that < Code> oddNumber run gives us a parser [option [int]] which will parse the number of a string and return either some (i) ( If they are weird numbers) or none (if they are also).

    We are not actually going to call oddNumber.run , however, the fact is that the fact that we are using a moned transformer means that We raised oddNumber to parser verb ( literal (...) to understand a here ).

    The syntax here is ugly, we lose the good ~ combination and for example, string to parser [string] Built-in conversion, but if we want we can easily write the version of those things.

No comments:

Post a Comment