[erlang-questions] Core Erlang apply target expression behaviour

Robert Virding <>
Sat Apr 22 21:19:37 CEST 2017


I would try doing 2 things:

- Write the equivalent Erlang code which does what you want the code to do,
compile it with the to_core0 option and see what it generates. This then
becomes your target code. This is how I did it with LFE.

- Or you can try flattening out your code to remove the nested calls. While
Core erlang in principle should be able to manage nested code the Erlang
compiler generally flattens things.

What I found when doing LFE was that while the language *in principle*
allows a lot of things *in practice* later passes in the compiler, for
example the optimisation passes, assume that the code looks like what the
Erlang compiler compiler generates. One example of this is how literal
structures are represented.

to_core0 returns the Core erlang which has been generated by the
erlang->core conversion pass which is then what is passed on to
optimisation and and code generation. This is what you should be targeting.

To check what the actual Core erlang data structures look like, not the
pretty printed code, in the shell try doing:

> c("foo", [binary,to_core0,return]).

This call will *return* the actual data structure so you can put it in
variable then pretty print it to a file so you can what you really need to
get. Note that some of the attributes are important in later passes.

What language are you working on?

Robert


On 21 April 2017 at 19:12, Karl Nilsson <> wrote:

> Whilst trying to compile some code to core erlang I came across a problem
> when the target expression (e0 in spec) to apply was another apply
> expression. According to the spec I thought that would be ok however when
> compiling the .core file with erlc I got an "no_file: Warning: invalid
> function call" error.
>
> What I then tried, not thinking it would work, was to wrap the inner apply
> expression in a let expression. This to my surprise worked just fine. I've
> included the code below. 'addSix` is the working function and `addSix2` is
> the dodgy one. Am I doing something wrong in `addSix2` to in terms of how I
> print my AST or is it simply that `addSix2` cannot be made to work? I also
> tried putting some parens around it but that also didn't work.
>
> 'add'/2 =
>     fun (_a0,_b0) ->
>         call 'erlang':'+'(_a0,_b0)
>
> 'addFive'/0 =
>     fun () ->
>         let <_a0> = 5
>         in fun (_b0) ->
>     apply 'add'/2 (_a0,_b0)
>
> 'addSix'/1 =
>     fun (_x0) ->
>         call 'erlang':'+'(let <_fez0> = apply 'addFive'/0 ()
> in apply _fez0 (_x0),1)
>
> 'addSix2'/1 =
>     fun (_x0) ->
>         call 'erlang':'+'(apply     apply 'addFive'/0 () (_x0),1)
>
>
> Cheers
> Karl
>
> _______________________________________________
> erlang-questions mailing list
> 
> http://erlang.org/mailman/listinfo/erlang-questions
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20170422/984118f3/attachment.htm>


More information about the erlang-questions mailing list