Resource request: dsls via ppx?

Is there a good resource on building DSLs in OCaml via using/abusing ppx ? I’m primarily looking for a “by example” type resource that covers the common techniques / tricks / idioms.

Thanks!

I wrote this early on in my PPX journey: Learning to write PPX extenders: some links - Learning - OCaml (I’m mistakenly calling out the blog post linked on an error handling issue – was just my confusion.)

care to share what you’re contemplating, in a DSL?

In the Lisp community, there are books like:

https://beautifulracket.com/
https://letoverlambda.com/
On Lisp

I don’t have anything particular in mind, just wanting to read impressive examples that makes one go “oh wow, I did not realize that was possible”

1 Like

Many of us here are experienced LISP/Scheme programmers, so it’s not that we don’t have ideas of DSLs in our heads, but rather, that we have too many ideas, and they’re all slightly different. If you have one or two examples which you could cite (maybe page references from these books?) then that could help move the conversation forward.

I mention this b/c writing a DSL in a language is more-involved than writing macros for that language. You have to choose the level of embedding into the host language (compiler? interpreter? bytecode?) and probably other things, all of which depend on the language you’re trying to embed.

I think what you’re really looking for is MetaOCaml: BER MetaOCaml

It’s begun to lag mainline OCaml more and more, so I’m not entirely sure of its status, but it has the sorts of facilities you’d expect for lisp-esque metaprogramming and code generation.

Alternatively, @Chet_Murthy posts frequently re: his experiments with more powerful/integrated quasiquoting and such in camlp5; I haven’t tinkered in that direction before, but it might be of interest.

I have a bunch of half baked (maybe even only 1% baked) ideas; they’re not things I absolutely need, i.e. “hell or high water, we make to make this work”; more like things that would be fun to goof around with. For example:

  • mini subset of J/K/APL for ND-array / tensor ops . in particular, I like J’s notion of ranks ( Loopless Code I: Verbs Have Rank ); I’m also interested in the interaction of “adjectives , adverbs, monads, dyads” with currying / point-free programming

  • embedding logic programming; http://minikanren.org/ embeds nicely in Lisp; is there something similar with OCaml ?

  • datalog/SQL like language for querying/updating in-memory tables (typed arrays)

  • mini-subset that compiles to wasm; one cool thing would be OCaml code that dynamically generates wasm code, executes it on wasmtime (or in browser), and passes data back & forth

ML stands for “Meta Language” right? I feel like I’ve been playing a lot with the “Language” part and very little with the “Meta” part; so I’m looking for impressive/inspiring things others have done.

Re: embedded logic programming, there’s elpi perhaps ?

the “Meta” in “ML” referred originally to a meta-language for a theorem-prover over the language PCF (IIRC).

1 Like

opam search kanren gives me 3 hits.

SWI Prolog comes with an embedding library which has an Ocaml binding.

Hi @zeroexcuses ,

If you want to inject some items written in a DSL into code that’s generally written in OCaml, you can use extension nodes for those items (extension nodes are generic placeholders in the OCaml syntax) .

If you want to turn a whole DSL into OCaml code, the OCaml PPX syntax extensions (extension nodes or attributes) won’t be of much help. However, you can still use ppxlib for it: you can turn your DSL items into OCaml parsetree items using metaquot and Ast_builder and then you can use Pprintast to turn the parstree you’ve generated into an OCaml module, which can form part of the rest of your code base.

If any of those two things is what you had in mind, I could point to some example.