{"id":2173,"date":"2015-07-09T08:26:48","date_gmt":"2015-07-09T08:26:48","guid":{"rendered":"http:\/\/www.dyalog.com\/blog\/?p=2173"},"modified":"2015-07-09T08:50:20","modified_gmt":"2015-07-09T08:50:20","slug":"type-comments","status":"publish","type":"post","link":"https:\/\/www.dyalog.com\/blog\/2015\/07\/type-comments\/","title":{"rendered":"Type Comments"},"content":{"rendered":"<p>I&#8217;ve taken to commenting the closing brace of my inner dfns with a home-grown type notation pinched from the Functional Programming community:<\/p>\n<pre>    dref\u2190{                  \u235d Value for name \u2375 in dictionary \u237a \r\n        names values\u2190\u237a      \u235d dictionary pair\r\n        (names\u2373\u2282\u2375)\u2283values   \u235d value corresponding to name \u2375\r\n    }                       <span style=\"color: red;\">\u235d :: Value \u2190 Dict \u2207 Name<\/span>\r\n<\/pre>\n<p>I keep changing my mind about whether the result type should be to the left (<span style=\"color: red;\"><code>Value \u2190 ...<\/code><\/span>) or to the right (<span style=\"color: red;\"><code>... \u2192 Value<\/code><\/span>). The FP crowd favours <code>\u2192 Value<\/code> but I&#8217;m coming around to <code>Value \u2190<\/code> because:<\/p>\n<p>* In contrast to (say) Haskell, APL&#8217;s function\/argument sequences associate right.<br \/>\n* <code>Value \u2190<\/code> mirrors the result pattern in a tradfn header and so looks familiar.<br \/>\n* The type of function composition <code>f\u2218g<\/code> is simpler this way round.<\/p>\n<p>Such comments serve as an aide-m\u00e9moire when I later come to read the code though, with some ingenuity, the notation might possibly be extended to a more formal system, which could have value to a compiler or code-checker. We would need:<\/p>\n<p><strong>Glyphs for Dyalog&#8217;s three primitive atomic data types.<\/strong> For no particularly good reason, I&#8217;ve been using:<br \/>\n<code><br \/>\n# number<br \/>\n' character<br \/>\n. ref<br \/>\n<\/code><br \/>\n<strong>Glyphs for a few generic (polymorphic) types.<\/strong> These could be just regular lower-case letters <strong>a b c &#8230;<\/strong> though I currently prefer greek letters:<br \/>\n<code><br \/>\n\u237a \u220a \u2373 \u2374 \u2375 ...<br \/>\n<\/code><\/p>\n<p><strong>Some constructors for type expressions.<\/strong> This is the most contentious part. For what it&#8217;s worth, I&#8217;ve been using:<br \/>\n<code><br \/>\n::\u00a0\u00a0is of type ...<br \/>\n\u2207\u00a0\u00a0function<br \/>\n\u2207\u2207\u00a0\u00a0operator<br \/>\n\u2190\u00a0\u00a0returns<br \/>\n[\u237a]\u00a0vector of \u237as<br \/>\n{\u237a}\u00a0optional left argument \u237a<br \/>\n<\/code><br \/>\nFor example:<br \/>\n<code><br \/>\nfoo :: \u2375 \u2190 {\u237a} \u2207 \u2375<br \/>\n<\/code><br \/>\nimplies:<br \/>\n&#8211; <code>foo<\/code> is an ambi-valent function whose<br \/>\n&#8211; result is of the same type (<code>\u2375<\/code>) as its right argument and whose<br \/>\n&#8211; optional left argument <em>may<\/em> be of a different type (<code>\u237a<\/code>).<\/p>\n<p>I can abstract\/name type expressions with (capitalised) identifiers using <code>:=<\/code>. For example:<br \/>\n<code><br \/>\nDict\u00a0\u00a0\u00a0:=\u00a0[Name][Value]\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u235d dictionary name and value vectors<br \/>\nEval\u00a0\u00a0\u00a0:=\u00a0Expr\u00a0\u2190\u00a0Dict\u00a0\u2207\u00a0Expr\u00a0\u00a0\u00a0\u235d expression reduction<br \/>\nList\u00a0\u2375\u00a0:=\u00a0'\u2218'\u00a0|\u00a0\u2375\u00a0(List\u00a0\u2375)\u00a0\u00a0\u00a0\u00a0\u00a0\u235d recursive pairs. See <\/code><a title=\"list\" href=\"http:\/\/dfns.dyalog.com\/n_list.htm\" target=\"_blank\">list<\/a><code><br \/>\nName\u00a0\u00a0\u00a0:=\u00a0\u235e\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u235d primitive type: character vector<br \/>\n<\/code><br \/>\nThe type: character vector <code>[']<\/code> is used so frequently that the three glyphs fuse into: <code>\u235e<\/code>. This means that a vector-of-character-vectors, also a common type, is <code>[\u235e]<\/code>.<\/p>\n<p><strong>Primitive and derived function types.<\/strong><br \/>\nIf we&#8217;re not too nit-picky and ignore issues such as single extension and rank conformability, we can give at least hints for the types of some primitive functions and operators.<\/p>\n<p><code>\u00a0\u2373 :: # \u2190 \u237a \u2207 \u237a\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u235d dyadic index-of<\/code><br \/>\n<code>\u00a0\u2374 :: \u237a \u2190 [#] \u2207 \u237a\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u235d reshape (also take, transpose, ...)<\/code><\/p>\n<p>The three forms of primitive composition have interesting types:<br \/>\n<code><br \/>\n\u2218 :: \u2374 \u2190 {\u237a} (\u2374 \u2190 {\u237a} \u2207 \u2373) \u2207\u2207 (\u2373 \u2190 \u2207 \u2375 ) \u2375\u00a0\u00a0\u00a0\u00a0\u00a0\u235d {\u237a}f\u2218g \u2375<br \/>\n:: \u2374 \u2190\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u237a \u2207\u2207 (\u2374 \u2190 \u237a \u2207 \u2375 ) \u2375\u00a0\u00a0\u00a0\u235d A\u2218g \u2375<br \/>\n:: \u2374 \u2190\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0((\u2374 \u2190 \u237a \u2207 \u2375) \u2207\u2207 \u2375 )\u237a\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u235d (f\u2218B)\u2375<br \/>\n<\/code><br \/>\nIt follows that:<br \/>\n<code><br \/>\nf :: \u2374 \u2190 {\u237a} \u2207 \u2373<br \/>\ng :: \u2373 \u2190\u00a0\u00a0\u00a0\u00a0\u00a0\u2207 \u2375<br \/>\n=&gt; f\u2218g :: \u2374 \u2190 {\u237a} \u2207 \u2375\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u235d intermediate type \u2373 cancels out<br \/>\n<\/code><br \/>\nand for trains:<br \/>\n<code><br \/>\nA :: \u2373\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u235d A is an array of type \u2373<br \/>\nf :: \u2373 \u2190 {\u237a} \u2207 \u2375<br \/>\ng :: \u2374 \u2190 {\u2373} \u2207 \u220a<br \/>\nh :: \u220a \u2190 {\u237a} \u2207 \u2375<br \/>\n=&gt;\u00a0f g h :: \u2374 \u2190 {\u237a} \u2207 \u2375\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u235d fgh fork<br \/>\n=&gt;\u00a0A g h :: \u2374 \u2190\u00a0\u00a0\u00a0\u00a0\u00a0\u2207 \u2375\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u235d Agh fork<br \/>\n=&gt;\u00a0\u00a0\u00a0g h :: \u2374 \u2190 {\u237a} \u2207 \u2375\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u235d gh atop<br \/>\n<\/code><br \/>\nFor a more substantial example, search function <a title=\"joy code\" href=\"http:\/\/dfns.dyalog.com\/c_joy.htm\" target=\"_blank\">joy<\/a> for <code>::<\/code> and <code>:=<\/code> in a recent <a title=\"download dfns workspaces\" href=\"http:\/\/dfns.dyalog.com\/downloads\/dsamples.zip\" target=\"_blank\">download<\/a> of <a title=\"view sample dfns\" href=\"http:\/\/dfns.dyalog.com\" target=\"_blank\">dfns.dws<\/a>.<\/p>\n<p><strong>Muse:<\/strong><br \/>\nThis notation is not yet complete or rigorous enough to be of much use to a compiler but there may already be enough to allow the writing of a dfn, which checks its own and others internal consistency. In the long term, if a notation similar to this evolved into something useful, it might be attractive to allow <em>optional<\/em> type specification as part of the function definition: without the comment symbol:<\/p>\n<pre>    dref\u2190{                  \u235d Value for name \u2375 in dictionary \u237a \r\n        names values\u2190\u237a      \u235d dictionary pair\r\n        (names\u2373\u2282\u2375)\u2283values   \u235d value corresponding to name \u2375\r\n    } :: Value \u2190 Dict \u2207 Name\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve taken to commenting the closing brace of my inner dfns with a home-grown type notation pinched from the Functional Programming community: dref\u2190{ \u235d Value for name \u2375 in dictionary \u237a names values\u2190\u237a \u235d dictionary pair (names\u2373\u2282\u2375)\u2283values \u235d value corresponding &hellip; <a href=\"https:\/\/www.dyalog.com\/blog\/2015\/07\/type-comments\/\">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-2173","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\/2173","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=2173"}],"version-history":[{"count":275,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/2173\/revisions"}],"predecessor-version":[{"id":3092,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/2173\/revisions\/3092"}],"wp:attachment":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/media?parent=2173"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/categories?post=2173"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/tags?post=2173"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}