Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting OCaml to F#: Converting OCaml quotation add and quotation expander to F#

Tags:

f#

ocaml

I am converting several modules based on OCaml to F# and ran into the OCaml ''Quotation.add'' compiler directive for a quotation expander.

A quotation expander is a function written in OCaml. A call to the Camlp4 library function ''Quotation.add'' adds the quotation expander.

Camlp4 - Tutorial, Ch. 3

After searching for both a quotation expander and the ''Quotation.add'' compiler directive in F# books, the F# site, Google and here, the answer I get is no.

Can someone confirm that F# does not support the concept of the OCaml ''Quotation.add'' compiler directive or the concept of OCaml quotation expanders.

EDIT

Note: I just learned that Camlp4 is a preprocessor-pretty-printer of OCaml, I thought it was a separate library for OCaml when I asked the question; now it makes sense.

like image 440
Guy Coder Avatar asked Dec 28 '22 01:12

Guy Coder


1 Answers

You're correct - the camlp4 library is not available for F#, so I'm afraid you'll need to use another approach to re-implement the required functionality in F#.

In general, there are a couple of related technologies that you can use from F# (but it is hard to tell if any of them is useful for you without knowing more about the specific problem):

  • F# Quotations allow you to manipulate F# code, but have limited compilation capabilities (are good i.e. for translating F# to SQL, JavaScript or perhaps GPU)
  • F# CodeDom (from F# PowerPack) allows you to generate code and compile it using F# compiler, but you can either generate code as text or using .NET object-oriented style.
  • T4 Templating is a .NET templating mechanism, though last time I checked, it did not support F# (but I think the Mono version might work)
  • F# Type Providers (thanks to kvb) make it possible to generate types by F# plugin for a compiler. This handles cases where you're using camplp4 to generate some types to be used later (i.e. from a shorter specification), but they have only limited use if you need to generate some code
like image 167
Tomas Petricek Avatar answered May 09 '23 02:05

Tomas Petricek