Dfn to/from TradFn converter

APL-related discussions - a stream of APL consciousness.
Not sure where to start a discussion ? Here's the place to be
Forum rules
This forum is for discussing APL-related issues. If you think that the subject is off-topic, then the Chat forum is probably a better place for your thoughts !

Dfn to/from TradFn converter

Postby rbe on Fri May 17, 2019 9:28 pm

Does anybody have a Dfn to TradFn converter kicking around, or
a TradFn to Dfn converter?

Posts: 8
Joined: Fri Apr 13, 2018 7:55 pm

Re: Dfn to/from TradFn converter

Postby Adam|Dyalog on Sun May 19, 2019 6:18 am

Hi Bob,

May I ask what you need this for?

For example, I have sometimes wanted to exposed a dfn as a public method in a class, but dfn syntax does not allow :Access Public statements. However, it is easy to "wrap" a dfn in a tradfn to achieve this:
      z←x Tradfn y
:Access Public

While I don't have a program to do it, conversion for dfn to tradfn is not too complicated. If I got it all right, it is:
  1. Create the header: Fn←{ becomes z←{x} Fn y
  2. Replace argument names and with x and y
  3. Handle missing left arguments if necessary: ⍺←def becomes :If 900⌶⍬ ⋄ x←def ⋄ :EndIf
  4. Convert guards: cond:res becomes :If cond ⋄ z←res ⋄ :Return ⋄ :EndIf
  5. Convert error guards: errno::res becomes :Trap errno ⋄ rest of code ⋄ :Else ⋄ z←res ⋄ :EndTrap
  6. If desired, break out inner dfns into external sub-functions, but watch out for scoping rules!
  7. Insert z← on the last code line
The opposite, from tradfn to dfn, is not universally possible. There are plenty of tradfn features that cannot (easily) be replicated in dfns, including dynamic localisation, returning functions as result, dynamic scoping, calling result-less functions, branching, access statements, class constructors, etc. etc.
User avatar
Posts: 56
Joined: Thu Jun 25, 2015 1:13 pm

Re: Dfn to/from TradFn converter

Postby rbe on Mon May 27, 2019 9:27 pm

Hi, Adam,

As written, the APEX compiler handled TradFns only. Dfns came along
a wee bit later, and Dyalog did not have a CRAY distribution, so it
was not of great interest to me at the time, as the CRAYs were my
major target systems.

I am currently dealing with a small application that is written
in Dfns, so to compile it, I have two choices: Modify APEX to
support Dfns directly, or modify the incoming source functions to
turn them into Dfns. I took the latter approach because I have
an aversion to even looking at syntax analyzers. It is not
the direction would go in were I designing APEX now, of course.

I have the converter mostly working now, and will leave the boring
parts as ISMOP. Your overview for conversion left out, as all good
specifications do, things like: How do I detect where I have to
insert "z←" in lines of source code? [I cheat here, because I can.]
How do I handle nested Dfns: Note that I have to change ⍺ and ⍵ to
alpha and omega (What is the rationale for Dyalog not supporting
⍺ and ⍵ in TradFns?) BUT, I have to be careful about changing them
in nested Dfns, and probably do not want to change ⎕←'Is this an ⍺?'.

At any rate, I'll make the converter open source when I have a bit
of time in the next few weeks.

Posts: 8
Joined: Fri Apr 13, 2018 7:55 pm

Return to APL Chat

Who is online

Users browsing this forum: No registered users and 1 guest