{"id":148,"date":"2009-03-09T06:21:03","date_gmt":"2009-03-09T05:21:03","guid":{"rendered":"http:\/\/fiber-space.de\/wordpress\/?p=148"},"modified":"2009-03-09T06:22:33","modified_gmt":"2009-03-09T05:22:33","slug":"the-thunk_stmt-and-programmable-semantics-in-python","status":"publish","type":"post","link":"http:\/\/fiber-space.de\/wordpress\/2009\/03\/09\/the-thunk_stmt-and-programmable-semantics-in-python\/","title":{"rendered":"The thunk_stmt and programmable semantics In Python"},"content":{"rendered":"<p>Seems as if there was just a single direction of syntactical improvement of Python these days which is the introduction of Ruby style blocks. Tav <a href=\"http:\/\/tav.espians.com\/ruby-style-blocks-in-python.html\">suggests<\/a> to reuse Pythons with-statement:<\/p>\n<p\/>\n<pre lang=\"bnf\">with_stmt: \"with\" expression ([\"as\" target] | [\"do\" [parameter_list]]) \":\" suite<\/pre>\n<p>Guido responds promptly and rejects the semantic dispersion of the with-statement. No meaning changer by using just a magical `do` keyword. In Tavs vision the do-branch of the with-statement is turned into an anonymous closure that is called by a special `__do__` function. The `__do__` shall also be customizable which finally leads to DSLs:<\/p>\n<blockquote><p>But more enterprising __do__ functions could do interesting things like rewriting the AST of the BLOCK_CODE. Given that this is already done by many many Python libraries, it&#8217;s not much of a stretch of the imagination. The benefit? Super nice Pythonic DSLs!<\/p><\/blockquote>\n<p\/>\nWell it is quite easy to put this idea upside down. Instead of re-purposing a special purpose statement like `with` one can simply turn each expression and &#8220;small statement&#8221; e.g. raise, assert, return, assignment etc. into a compound statement using following definition<\/p>\n<p\/>\n<pre lang=\"bnf\">thunk_stmt: small_stmt ':' suite<\/pre>\n<p>One has to elaborate on it a little and change rules of Pythons existing grammar for LL(1) conformance<\/p>\n<p\/>\n<pre lang=\"bnf\">stmt: compound_stmt\r\nsingle_input: NEWLINE | compound_stmt NEWLINE\r\ncompound_stmt: ( if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | \r\n                 funcdef | classdef | thunk_stmt )\r\nthunk_stmt: small_stmt ( ':' suite | (';' small_stmt)* [';'] NEWLINE )<\/pre>\n<p>Here the `thunk_stmt` is a fusion of the definition stated above and the `simple_stmt` of Pythons grammar. This definition doesn&#8217;t lead to <em>programmable syntax<\/em> but programmable semantics which is what Tav intends to support with a customizable `__do__`.<\/p>\n<p>An obvious use case would be property definitions:<\/p>\n<p\/>\n<pre lang=\"python\">x = property():\r\n    def get(self):\r\n        return self._x\r\n\r\n    def set(self, value):\r\n        self._x = value<\/pre>\n<p>It simply appends a block of code to a valid assignment `x = property()`.<\/p>\n<p>Same goes with<\/p>\n<p\/>\n<pre lang=\"python\">employees.select.do(emp)<\/pre>\n<p>which might be extended to<\/p>\n<p\/>\n<pre lang=\"python\">employees.select.do(emp):\r\n    if emp.salary &gt; developer.salary:\r\n            return fireEmployee(emp)\r\n    else:\r\n        return extendContract(emp)<\/pre>\n<p>Without an appended block the expression<\/p>\n<p\/>\n<pre lang=\"python\">employees.select.do(emp)<\/pre>\n<p>might simply mean<\/p>\n<p\/>\n<pre lang=\"python\">employees.select(lambda emp: emp)<\/pre>\n<p>It should be clear that Python needs a protocol to support this which means here that a `__special__` function is attached to an object and that for user defined classes this `__special__` function can be overwritten.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Seems as if there was just a single direction of syntactical improvement of Python these days which is the introduction of Ruby style blocks. Tav suggests to reuse Pythons with-statement: with_stmt: &#8220;with&#8221; expression ([&#8220;as&#8221; target] | [&#8220;do&#8221; [parameter_list]]) &#8220;:&#8221; suite &hellip; <a href=\"http:\/\/fiber-space.de\/wordpress\/2009\/03\/09\/the-thunk_stmt-and-programmable-semantics-in-python\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[5,12,4],"tags":[],"_links":{"self":[{"href":"http:\/\/fiber-space.de\/wordpress\/wp-json\/wp\/v2\/posts\/148"}],"collection":[{"href":"http:\/\/fiber-space.de\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/fiber-space.de\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/fiber-space.de\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/fiber-space.de\/wordpress\/wp-json\/wp\/v2\/comments?post=148"}],"version-history":[{"count":19,"href":"http:\/\/fiber-space.de\/wordpress\/wp-json\/wp\/v2\/posts\/148\/revisions"}],"predecessor-version":[{"id":167,"href":"http:\/\/fiber-space.de\/wordpress\/wp-json\/wp\/v2\/posts\/148\/revisions\/167"}],"wp:attachment":[{"href":"http:\/\/fiber-space.de\/wordpress\/wp-json\/wp\/v2\/media?parent=148"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/fiber-space.de\/wordpress\/wp-json\/wp\/v2\/categories?post=148"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/fiber-space.de\/wordpress\/wp-json\/wp\/v2\/tags?post=148"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}