[wg-camlp4] Monadic computation in Fan

Bob Zhang bobzhang1988 at gmail.com
Sun Dec 1 03:59:33 GMT 2013

Dear List

     I am very glad to let you know that I have incorporated a very
cool feature in Fan. a new DDSL l 'local_extend', which mutate the
parser inside the quotation, but restored all the parser it mutated
outside the quotation.
    The use case is when you really need to mutate a tiny part of the
grammar -- copy alone can not work, since parsers are mutually
recursive -- now we can limit  the mutation scope, however.

The ten lines below show how we introduce computation expression (F#
style) in Fan by *local_extend* DDSL, the introduced DDSL is called

  let f (loc:Locf.t) meta content =
    let module_name =
    match meta with
    | None -> Locf.failf loc "cexp module name must be specified via @"
    | Some x -> String.capitalize x in
    exp: 10 RA
    ["let"; "!" ; bind as bi ; "in"; exp as x %{
     let pat_exps  =  Ast_basic.list_of_and bi [] in
       (fun pat_exp acc ->
         match pat_exp with
         | %bind{ $p = $e} ->
             %exp{$uid:module_name.bind $e (fun $p -> $acc)}
         | _ -> assert false
                     ) pat_exps x }
    ${Gramlib.parse_string_eoi exp ~loc content}}

 let d = Ns.lang in
    Ast_quotation.add {domain = d; name="cexp";} Dyn_tag.exp f;

Test case
let v = %cexp at option{
 let! a = Some 3  in
 let! b = Some 4 in
 Some (a + b)
-- Bob

-- Bob

More information about the wg-camlp4 mailing list