# Questions on processing DSL in tagless-final

**URL:** <https://discuss.ocaml.org/t/questions-on-processing-dsl-in-tagless-final/4429>\
**Category:** Learning\
**Tags:** tagless-final\
**Created:** [September 25, 2019, 5:09pm UTC](https://discuss.ocaml.org/t/questions-on-processing-dsl-in-tagless-final/4429 "2019-09-25T17:09:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![arbipher](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/arbipher/32/3544_2.png) [@arbipher](https://discuss.ocaml.org/u/arbipher)\
**Post date:** [September 25, 2019, 5:09pm UTC](https://discuss.ocaml.org/t/questions-on-processing-dsl-in-tagless-final/4429/1 "2019-09-25T17:09:03Z")

</div>

Hi, I am thinking to use tagless-final style to rewrite a small project on DSL. Actually it’s a set of DSLs with small variants. For each of them, I have already `check` `interpret` `pp` functions for them. As you might see, there is lots of code duplication.

I start to read Oleg tutorials and papers ([http://okmij.org/ftp/tagless-final/](http://okmij.org/ftp/tagless-final/)). I have two questions to ask before reading many details in them.

1. The program of that DSL is written in text and I have a parser to get an `Ast.t` for it in my parser.  
if I use tagless-final style, what will the parser be? Instead of an `Ast.t`, the parser builds a function e.g. `fun () -> plus (int 1) (int 1)` where the `plus` and `int` can be defined in some Semantics module? If I hope to get a syntax tree, it seems I need a representation of the `Ast.t`?

2. I have some processing functions e.g. `constant_folding : Ast.t -> Ast.t` which I can get another valid(need checking…) Ast.t. How should I write this kind of function in the tagless-final style?

---

<div class="post-metadata">

**Author:** ![Kakadu](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/kakadu/32/1204_2.png) [@Kakadu](https://discuss.ocaml.org/u/Kakadu)\
**Post date:** [September 25, 2019, 7:47pm UTC](https://discuss.ocaml.org/t/questions-on-processing-dsl-in-tagless-final/4429/2 "2019-09-25T19:47:33Z")

</div>

1. I think that your intuituion about `fun () -> plus (int 1) (int 1)` is right
2. [http://okmij.org/ftp/tagless-final/course/lecture.pdf](http://okmij.org/ftp/tagless-final/course/lecture.pdf) page 15

---

<div class="post-metadata">

**Author:** ![grayswandyr](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/grayswandyr/32/94_2.png) [@grayswandyr](https://discuss.ocaml.org/u/grayswandyr)\
**Post date:** [September 26, 2019, 6:47am UTC](https://discuss.ocaml.org/t/questions-on-processing-dsl-in-tagless-final/4429/3 "2019-09-26T06:47:45Z")

</div>

> [@arbipher](#):
>
> The program of that DSL is written in text and I have a parser to get an `Ast.t` for it in my parser.  
> if I use tagless-final style, what will the parser be? Instead of an `Ast.t` , the parser builds a function e.g. `fun () -> plus (int 1) (int 1)` where the `plus` and `int` can be defined in some Semantics module? If I hope to get a syntax tree, it seems I need a representation of the `Ast.t` ?

Oleg addresses the so-called de-serialization problem at Sect. 2.3 of [http://okmij.org/ftp/tagless-final/course/lecture.pdf](http://okmij.org/ftp/tagless-final/course/lecture.pdf) . It all boils down to how abstract your AST is. I tend to think that, in most situations, the AST will be complex enough that you will need a pass transforming the AST in your DSL. Considering that you may have to perform quite a bit of analyses on the structure during this pass and that the corresponding function will be partial (due to possible non-syntactic errors in the AST), I think it makes sense for the AST to inhabit an _algebraic_ type (“initial” style), and only the DSL to be in final style.
