Generating type specs programmatically

Olivier Boudeville olivier.boudeville@REDACTED
Mon Dec 7 09:34:07 CET 2020


Hi Guilherme,

I may understand incorrectly your question, but type specs are 
well-defined in ASTs and stored appropriately in them (see 
https://erlang.org/doc/apps/erts/absform.html for a mostly accurately 
view of the AST grammar), so they can be freely read from there and 
written there as well, either directly (as raw terms) or through 
appropriate, "standard" Erlang API functions.

(I preferred the former method and apparently it works satisfactorily, 
for example it is used in 
https://github.com/Olivier-Boudeville/Ceylan-WOOPER/blob/712bcc587e73f2da1529b175dfb1d458be36e983/src/wooper_instance_construction.erl#L308 
; I suppose the AST structure is not likely to change a lot anytime 
soon, and, should this happen, then it will be just a matter of updating 
accordingly the generation logic; I found it clearer to generate forms 
through terms rather than thanks an abstracting API)

Hope this helps,

Olivier.



Le 12/7/20 à 12:52 AM, Leonard B a écrit :
> Hi Guilherme,
>
> I may be wrong, but I'm pretty sure specs are just syntactic sugar for 
> dialyzer (and devs) and are not actually included in the AST. Just 
> sounds like you may be looking for something that's not there.
>
> Kind regards,
> Leonard
>
> On Sun, Dec 6, 2020, 14:13 Guilherme Andrade <g@REDACTED 
> <mailto:g@REDACTED>> wrote:
>
>     Thanks for the suggestion!
>
>     After a quick glance, Typer seems to be directly generating string
>     representations of the specs - that is, not actually manipulating
>     the AST of the modules it works on by inserting nodes that
>     represent said specs.
>
>     That's a shame, but it's what I will also do if I don't find a
>     more elegant way to do it through `erl_syntax`.
>
>     Oh well.
>
>     On Sat, 5 Dec 2020 at 22:29, Fred Hebert <mononcqc@REDACTED
>     <mailto:mononcqc@REDACTED>> wrote:
>
>         You might want to look at Typer, since it's whole purpose is
>         to generate type specs for code that you can directly put in code.
>
>         On Sat, Dec 5, 2020 at 3:25 PM Guilherme Andrade
>         <g@REDACTED <mailto:g@REDACTED>> wrote:
>
>             Hello list,
>
>             I've been working on a code-generating tool, leveraging
>             `erl_syntax`[1] to build an AST containing arbitrary
>             Erlang functions; I then use `erl_pp`[2] to generate the
>             corresponding Erlang code and write the result onto a
>             source code file.
>
>             All is well so far, but now I want to generate type specs
>             and function specs with which to annotate the generated
>             code - and I can't for the life of me figure out how to do
>             it within `erl_syntax`.
>
>             All of the different attempts I've made end up either
>             crashing within `erl_syntax` or `erl_pp`, or at best
>             result in INVALID-FORM literals within the generated code.
>
>             Would any of you be able to point me in the right direction?
>
>             Thanks in advance!
>
>             [1]: http://erlang.org/doc/man/erl_syntax.html
>             <http://erlang.org/doc/man/erl_syntax.html>
>             [2]: https://erlang.org/doc/man/erl_pp.html
>             <https://erlang.org/doc/man/erl_pp.html>
>
>             -- 
>             Guilherme
>
>
>
>     -- 
>     Guilherme
>

-- 
Olivier Boudeville

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20201207/ee0b66bb/attachment.htm>


More information about the erlang-questions mailing list