caml-list AT inria.fr
Subject: Caml users' mailing list
List archive
- From: bob zhang <bobzhang1988 AT gmail.com>
- To: Caml List <caml-list AT inria.fr>
- Subject: [Caml-list] Fan, a promising replacement of camlp4
- Date: Mon, 21 Jan 2013 00:29:55 -0500
Dear Camlers,
Fan (https://github.com/bobzhang/Fan) is a dialect of ocaml for
meta-programming, though it's still under active development, but I
think I have done something right, so I would be happy to share some
encouraging results.
Besides the engineering part(Fan is much much faster than
camlp[4,5], with numerous bug fixes, nested quotation-antiquotation,
pluggable lexer, etc), the main contribution lies in 3 parts:
First, Fan uses polymorphic variants to encode the intermediate Ast;
Second, Fan supports first class programmable grammar/lexer,
Third, Fan has a strong built in customizable deriving support.
First. variants hit the sweet spot here, (don't worry about the
error message, since we provide a quasi-quotation mechanism, so for
a large ast, the user don't construct the Ast by hand).
The benefit is *huge*,
a. shared constructor branches
A lot of code re-use and much cleaner API here, think
about if you need a function which takes the location
from the Ast node, in Camlp4, you have to write loc_of_expr
or loc_of_patt, to make things worse, once you choose loc_of_expr,
the function can only apply to expr, no code re-use anymore,
now in Fan, you only need loc_of, it can apply to all
syntax categories,
(btw, loc_of is derived automatically :-))
b. subtyping makes program analysis easier
if you pick a subset of ocaml to mixin with your own
DSL,subtyping
makes you only need to analyze part of the language with much
more type safety.
c. overloaded quosi-quotation
We provides two Asts for quasi-quotation, one with the
location, the other without location(for code
generation), they works
well together. And the user can also enrich the
intermediate Ast with
a new constructor while re-use the existing quotation kit.
d. minimum dependency
Since the namespace of variants is global, we provide a
mechanism to store the ast in a normal ocaml value, this adds
reflection layer to ocaml automatically while without
introducing any
dependency. Besides, unlike camlp4,
the global namespace also makes the semantics correct
Second, we provides the first class programmable parser/lexer.
The grammar is not only dynamically extensible, but you can also
program the productions as well. This is much different from yacc or
menhir, here grammar is programmable means you can parameterize any
production.
Third, we provides a uniform deriving mechanism
Take a look at the code in src/FanAst.ml
{:fans|keep off; derive (Map2 Fold2 OIter MetaExpr
MetaPatt Map Fold Print OPrint OEq GenLoc Strip ); |};
{:ocaml|INCLUDE "src/Ast.ml"; |};
This will derive the map2, fold2,oiter objects, and metaexpr
metapatt lifting, map, fold, pretty printer, strip location for
FanAst, the deriving can be customized in tens of lines code.
There are a number of enhancements I don't mention here, it
would be very helpful that you could suggest what kind of
functionality you want, Fan is already much better than camlp4 in all
rounds, it is still at alpha stage, so your opinions matter a lot :-)
--
Regards
-- Bob
- [Caml-list] Fan, a promising replacement of camlp4, bob zhang, 01/21/2013
- Re: [Caml-list] Fan, a promising replacement of camlp4, Ivan Gotovchits, 01/21/2013
- Re: [Caml-list] Fan, a promising replacement of camlp4, bob zhang, 01/21/2013
- Re: [Caml-list] Fan, a promising replacement of camlp4, Török Edwin, 01/22/2013
- [Caml-list] Re: Fan, a promising replacement of camlp4, Hongbo Zhang, 01/23/2013
- [Caml-list] Re: Fan, a promising replacement of camlp4, Török Edwin, 01/23/2013
- [Caml-list] Re: Fan, a promising replacement of camlp4, bob zhang, 01/23/2013
- [Caml-list] Re: Fan, a promising replacement of camlp4, Török Edwin, 01/23/2013
- [Caml-list] Re: Fan, a promising replacement of camlp4, Hongbo Zhang, 01/23/2013
- Re: [Caml-list] Fan, a promising replacement of camlp4, Ivan Gotovchits, 01/21/2013
Archive powered by MHonArc 2.6.18.