{"id":2584,"date":"2015-05-27T07:40:51","date_gmt":"2015-05-27T07:40:51","guid":{"rendered":"http:\/\/www.dyalog.com\/blog\/?p=2584"},"modified":"2015-05-27T08:03:00","modified_gmt":"2015-05-27T08:03:00","slug":"name-colouring-for-dfns","status":"publish","type":"post","link":"https:\/\/www.dyalog.com\/blog\/2015\/05\/name-colouring-for-dfns\/","title":{"rendered":"Name Colouring for Dfns"},"content":{"rendered":"<p>APL is sometimes criticised because expressions that include names cannot, in general, be parsed without knowing whether the names represent functions or variables. For example, the name <code>thing<\/code> in the expression <code>thing\u23733<\/code> could reference an array (in which case the <code>\u2373<\/code> is dyadic) or it could reference a function (making the <code>\u2373<\/code> monadic).<\/p>\n<p>An APL expression becomes completely parsable if we distinguish each name with one of four colours, depending on the &#8220;kind&#8221; of its referent: <span style=\"color: green\">array<\/span>, <span style=\"color: blue\">function<\/span>, <span style=\"color: navy\">monadic operator<\/span>, <span style=\"color: purple\">dyadic operator<\/span>. Now, with a bit of practice, we can at least <em>parse<\/em> <code><span style=\"color: green\">thing<\/span>\u23733<\/code> vs <code><span style=\"color: blue\">thing<\/span>\u23733<\/code> without recourse to the definition of <code>thing<\/code>. Notice how <em>kind<\/em> differs slightly from APL&#8217;s <em>name-class<\/em>, which does not distinguish monadic from dyadic operators.<\/p>\n<p>Names whose definitions are external to the code under consideration and so cannot be classified would be given a distinct colour, say <span style=\"color: red\">red<\/span>, which would at least draw attention to them. Colouring a number of interdependent functions at the same time should help with such issues.<\/p>\n<p>Name-colouring can co-exist with the more superficial <em>token-colouring<\/em> (<span style=\"color: green\"><code>\u235d<\/code> green for comments and so forth<\/span>) though we would probably want to configure the two schemes separately.<\/p>\n<p>There&#8217;s a related argument for colouring <em>parentheses<\/em> to show the kind of value they contain: <span style=\"color: blue\">(<\/span><code>~\u2218<\/code><span style=\"color: green\">(<\/span><code>\u2282''<\/code><span style=\"color: green\">)<\/span><span style=\"color: blue\">)<\/span>. This would mean that we could always determine the valency of a function call, or the kind of a hybrid token, such as <code>\/<\/code>, by looking at the colour of the single token immediately to its left. Finally, we should probably kind-colour curly braces: <code><span style=\"color: blue\">{<\/span>\u2375<span style=\"color: blue\">}<\/span><\/code>, <code><span style=\"color: navy\">{<\/span>\u237a\u237a \u2375<span style=\"color: navy\">}<\/span><\/code>, <code><span style=\"color: purple\">{<\/span>\u2375\u2375 \u2375<span style=\"color: purple\">}<\/span><\/code>.<\/p>\n<p><strong>Yeah but how?<\/strong><\/p>\n<p><em>In the following, where appropriate, &#8220;function&#8221; is a shorthand for &#8220;function or operator&#8221;.<\/em><\/p>\n<p>Most generally, we want to process a number of definitions at the same time, so that inter-function references can be coloured. Such a set of functions may be considered as internal definitions in an anonymous outer function so, without loss of generality, we need consider only the case of a single multi-line nested function.<\/p>\n<p>A function can be viewed as a tree, with nested subfunctions as subtrees. The definitions at each lexical level in the function comprise the information stored at each node of the tree.<\/p>\n<p>The colouring process traverses the tree, accumulating a dictionary of (name kind) pairs at each level. At a particular level, definitions are processed in the same order as they would be executed, so that the (name kind) entries from earlier definitions are available to inform later expressions. Visiting each node before traversing its subtrees ensures that outer dictionary entries are available to lexically inner functions. <em>Prefixing<\/em> dictionary entries for inner functions ensures that a left-to-right search (\u00e0 la dyadic iota) finds those names first, thus modelling lexical name-shadowing.<\/p>\n<p>Each assignment expression adds one or more names to the dictionary. The kind of the assignment is inferred by looking at the defining expression to the right of the assignment arrow. This sounds heavy, but if we examine the expression from right-to-left then we can often stop after examining very few tokens. For example, an arbitrarily long expression ending in (&#8230; function array) must, if it is syntactically correct, reduce to an array value, while (&#8230; function function) must be a function (train). A sequence in braces {&#8230;} resolves to a function, monadic operator or dyadic operator, depending only on whether there are occurrences of <code>\u237a\u237a<\/code> or <code>\u2375\u2375<\/code> at its outermost level.<\/p>\n<p>This approach, while relatively simple, is not perfect. It takes no account of the <em>order<\/em> in which names are defined relative to their being referenced. The assumption is that all definitions at an outer level are available to its inner levels. The following small function illustrates the problem:<br \/>\n<code><br \/>\n\u00a0\u00a0\u00a0\u00a0<span style=\"color: blue\">{<\/span><br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: blue\">M<\/span>\u2190<span style=\"color: blue\">{A<\/span> \u2375<span style=\"color: blue\">}<\/span>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u235d this <span style=\"color: blue\">A<\/span> correctly coloured <span style=\"color: blue\">function<\/span><br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: green\">N<\/span>\u2190<span style=\"color: blue\">{A<\/span> \u2375<span style=\"color: blue\">}<\/span>\u2375\u00a0\u00a0\u00a0\u00a0\u00a0\u235d this <span style=\"color: blue\">A<\/span> should be coloured <span style=\"color: red\">unclassified<\/span><br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: blue\">A<\/span>\u2190\u00f7\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u235d <span style=\"color: blue\">A<\/span> defined as a <span style=\"color: blue\">function<\/span><br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: blue\">M<\/span> <span style=\"color: green\">N<\/span>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u235d application of <span style=\"color: blue\">function<\/span> to <span style=\"color: green\">array<\/span><br \/>\n\u00a0\u00a0\u00a0\u00a0<span style=\"color: blue\">}<\/span><br \/>\n<\/code><br \/>\nIt remains to be seen whether this would be a problem in practice or whether, if name-colouring proved popular, people might adjust their code to avoid such anomalies. The problem does not occur if we avoid re-using the same name for items of different kinds at the same lexical level &#8211; which IMHO is not a bad rule, anyway.<\/p>\n<h2>Implementation Status<\/h2>\n<p>I would like to experiment with name-kind colouring for the dfns <a href=\"http:\/\/dfns.dyalog.com\/c_display.htm\" target=\"_blank\">code<\/a> samples on the Dyalog website.<\/p>\n<p>This project is under way, but has rather a low priority and so keeps stalling. In addition, I keep wavering between processing the <a href=\"http:\/\/dfns.dyalog.com\/n_tokens.htm\" target=\"_blank\">tokens<\/a> as a <a href=\"http:\/\/dfns.dyalog.com\/n_list.htm\" target=\"_blank\">list<\/a> with tail calling and the dictionary as an accumulating left argument, or in more classic APL style as a <em>vector<\/em>, developing parallel vectors for lexical depth and masks for assignment arrow positions and expression boundaries, etc.<\/p>\n<p>In the meantime, here is an artist&#8217;s impression of what name- and bracket- coloured code might look like, with colours: <span style=\"color: green\">array<\/span>, <span style=\"color: blue\">function<\/span>, <span style=\"color: purple\">dyadic-operator<\/span>.<br \/>\n<code><br \/>\n\u00a0\u00a0\u00a0\u00a0<span style=\"color: blue\">eval<\/span>\u2190<span style=\"color: blue\">{<\/span><br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: green\">stk I(op ops)<\/span>\u2190\u2375<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: green\">op<\/span> <span style=\"color: blue\">in<\/span>\u2283\u237a:\u237a <span style=\"color: blue\">prt<\/span> <span style=\"color: green\">stk<\/span> <span style=\"color: green\">I((<\/span>\u237a <span style=\"color: blue\">dref<\/span> <span style=\"color: green\">op)<\/span><span style=\"color: blue\">cat<\/span> <span style=\"color: green\">ops)<\/span><br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: green\">f(fr rr)<\/span>\u2190<span style=\"color: green\">f r<\/span>\u2190<span style=\"color: green\">stk<\/span><br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: blue\">c<\/span>\u2190<span style=\"color: blue\">{<\/span><span style=\"color: green\">op<\/span>\u2261\u2375~' '<span style=\"color: blue\">}<\/span><br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: blue\">c<\/span>'dip ':\u237a<span style=\"color: blue\">(<\/span>'*\u2337'<span style=\"color: purple\">rgs<\/span><span style=\"color: blue\">{<\/span>\u237a <span style=\"color: blue\">prt<\/span> <span style=\"color: green\">rr I(f<\/span> <span style=\"color: blue\">cat<\/span> <span style=\"color: green\">fr ops)<\/span><span style=\"color: blue\">})<\/span>\u2375<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0...<br \/>\n<\/code><\/p>\n<p>Notice how functions stand out from their arguments. For example, at the start of the third line: <code><span style=\"color: green\">op<\/span> <span style=\"color: blue\">in<\/span>\u2283\u237a:<\/code>, it is clear that <code><span style=\"color: blue\">in<\/span><\/code> is a function taking <code>\u2283\u237a<\/code> (first of alpha) as right argument and <code><span style=\"color: green\">op<\/span><\/code> as left argument, rather than a monadic function <code><span style=\"color: blue\">op<\/span><\/code> taking <code><span style=\"color: green\">in<\/span>\u2283\u237a<\/code> (<span style=\"color: green\">in<\/span> pick of alpha) as right argument. Other interpretations of the uncoloured expression <code>op in\u2283\u237a:<\/code> include:<\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0<span style=\"color: green\">op in<\/span>\u2283\u237a:<\/code>\u00a0\u00a0\u00a0\u00a0<code><span style=\"color: green\">op in<\/span><\/code> is a vector (<span style=\"color: green\">array<\/span>), so <code>\u2283<\/code> is pick.<br \/>\n<code>\u00a0\u00a0\u00a0\u00a0<span style=\"color: blue\">op in<\/span>\u2283\u237a:<\/code>\u00a0\u00a0\u00a0\u00a0both <code><span style=\"color: blue\">op<\/span><\/code> and <code><span style=\"color: blue\">in<\/span><\/code> are monadic <span style=\"color: blue\">function<\/span> calls, so <code>\u2283<\/code> is first.<br \/>\n<code>\u00a0\u00a0\u00a0\u00a0<span style=\"color: green\">op<\/span> <span style=\"color: navy\">in<\/span>\u2283\u237a:<\/code>\u00a0\u00a0\u00a0\u00a0<code><span style=\"color: navy\">in<\/span><\/code> is a <span style=\"color: navy\">monadic operator<\/span> with <span style=\"color: green\">array<\/span> operand <code><span style=\"color: green\">op<\/span><\/code>.<br \/>\n<code>\u00a0\u00a0\u00a0\u00a0<span style=\"color: blue\">op<\/span> <span style=\"color: purple\">in<\/span>\u2283\u237a:<\/code>\u00a0\u00a0\u00a0\u00a0<code><span style=\"color: purple\">in<\/span><\/code> is a <span style=\"color: purple\">dyadic operator<\/span> with <span style=\"color: blue\">function<\/span> operands <code><span style=\"color: blue\">op<\/span><\/code> and <code>\u2283<\/code>.<\/p>\n<p>I&#8217;m keen to hear any feedback and suggestions about name-colouring for dfns. What d&#8217;ya think?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>APL is sometimes criticised because expressions that include names cannot, in general, be parsed without knowing whether the names represent functions or variables. For example, the name thing in the expression thing\u23733 could reference an array (in which case the &hellip; <a href=\"https:\/\/www.dyalog.com\/blog\/2015\/05\/name-colouring-for-dfns\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":12,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8,23],"tags":[],"class_list":["post-2584","post","type-post","status-publish","format-standard","hentry","category-apl-language","category-musings"],"_links":{"self":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/2584","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/users\/12"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/comments?post=2584"}],"version-history":[{"count":262,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/2584\/revisions"}],"predecessor-version":[{"id":2877,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/2584\/revisions\/2877"}],"wp:attachment":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/media?parent=2584"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/categories?post=2584"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/tags?post=2584"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}