[erlang-questions] Core Erlang apply target expression behaviour

Robert Virding rvirding@REDACTED
Sun Apr 23 02:07:30 CEST 2017


I have been thinking about f# as well, but from a different direction. I
would want to use as much as possible of the existing f# compiler which is
written in f# so I would write an interpreter in Erlang for AST generated
by f# parser which you could then use that to run all the early parts of
the compiler, like type-checking and such. Then hopefully you could replace
the backend with a new bit which generates core erlang and use the erlang
compiler for the rest.

I fully realise it would not be as simple as it sounds :-) but I think it
would be a doable of reusing as much as possible of what is written in f#.
For me time is the limiting factor.

Robert


On 22 April 2017 at 21:52, Karl Nilsson <kjnilsson@REDACTED> wrote:

> Thanks for the advice. A few things to think about there.
> I have been writing things in erlang, compiling and comparing but rather
> than writing a language from scratch I am working from the AST (or rather
> the TAST) from another language, fsharp. As fsharp is an ML and core erlang
> is ML-ish I was hoping to avoid having to do too much mangling but
> flattening out applies and mod calls with let expressions is easy enough so
> I'll definitely do that.
>
> Cheers
> Karl
>
> On Sat, 22 Apr 2017 at 20:19 Robert Virding <rvirding@REDACTED> wrote:
>
>> 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 <kjnilsson@REDACTED> 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
>>> erlang-questions@REDACTED
>>> http://erlang.org/mailman/listinfo/erlang-questions
>>>
>>>
>>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20170423/432dfe3f/attachment.htm>


More information about the erlang-questions mailing list