ocaml - Ocamllex refill-handler example -


in chapter 12 lexer , parser generators, fail compile following example :

{  type token = eol | int of int | plus  module make (m : sig                type 'a t                val return: 'a -> 'a t                val bind: 'a t -> ('a -> 'b t) -> 'b t                val fail : string -> 'a t                 (* set lexbuf *)                val on_refill : lexing.lexbuf -> unit t              end) = struct  let refill_handler k lexbuf arg =     m.bind (m.on_refill lexbuf) (fun () -> k lexbuf arg)  }  refill {refill_handler}  rule token = parse | [' ' '\t']     { token lexbuf } | '\n'     { m.return eol } | ['0'-'9']+     { m.return (int (int_of_string i)) } | '+'     { m.return plus } | _     { m.fail "unexpected character" } { end } 

i not understand how module make working, , comes from. note : using 4.02.1 ocaml compiler.

this code defines module make, functor. is, takes module parameter , returns module.

the module parameter accepts arbitrary monad, in essence way of saying happens when followed else.

you can find description of monads here: http://blog.enfranchisedmind.com/2007/08/a-monad-tutorial-for-ocaml

i got code compile changing refill function this:

let refill_handler k lexbuf =     m.bind (m.on_refill lexbuf) (fun () -> k lexbuf) 

the original definition doesn't seem match type of refill handler. (but missing something; kind of code takes lot of getting used to.)


Comments