{"id":42,"date":"2005-12-21T23:41:43","date_gmt":"2005-12-21T21:41:43","guid":{"rendered":"http:\/\/ken.friislarsen.net\/blog\/2005\/12\/21\/refactoring-sml-quiz-part-2\/"},"modified":"2005-12-21T23:41:43","modified_gmt":"2005-12-21T21:41:43","slug":"refactoring-sml-quiz-part-2","status":"publish","type":"post","link":"http:\/\/ken.friislarsen.net\/blog\/2005\/12\/21\/refactoring-sml-quiz-part-2\/","title":{"rendered":"Refactoring SML Quiz, Part 2"},"content":{"rendered":"<p>The answer to yesterdays quiz is: Yes, types are necessary for lambda-lifting refactoring. Namely, if the lifted function contains an overloaded operator such as, e.g., <code>+<\/code>.<\/p>\n<p>For example, given the program:<\/p>\n<pre>\nfun foo x =\n    let fun add y = x + y\n    in  add 5.0 end\n<\/pre>\n<p>where we want to lift the function <code>add<\/code>.  Our first attempt might be to transform the program into something like (refactoring is a source code to source code transformation):<\/p>\n<pre>\nfun add x y = x + y\nfun foo x = add x 5.0\n<\/pre>\n<p>However, this might not compile with every SML compiler.  It depends on how much local context the compiler uses to resolve overloading.<\/p>\n<p>Moscow ML compiles the transformed program above just fine.  But Moscow ML complains about the following version:<\/p>\n<pre>\nfun add x y = x + y<strong>;<\/strong>\nfun foo x = add x 5.0\n<\/pre>\n<p>Notice the extra semi-colon after the declaration of <code>add<\/code>.<\/p>\n<p><strong>Update 2005-12-22:<\/strong><br \/>\nJust for good messure.  Here is one possible result of refactoring with necessary type information:<\/p>\n<pre>\nfun add (x : real) y = x + y<strong>;<\/strong>\nfun foo x = add x 5.0\n<\/pre>\n<p>I believe that only captured variables needs to be typed.<\/p>\n<p>Also note, that overloading is not the only problem.  It is possible to construct a similar example using records, but I&#8217;ll leave that to the reader (unless there is a popular and desperate demand, then I can provide an example).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The answer to yesterdays quiz is: Yes, types are necessary for lambda-lifting refactoring. Namely, if the lifted function contains an overloaded operator such as, e.g., +. For example, given the program: fun foo x = let fun add y = x + y in add 5.0 end where we want to lift the function add. <a class=\"read-more\" href=\"http:\/\/ken.friislarsen.net\/blog\/2005\/12\/21\/refactoring-sml-quiz-part-2\/\">[&hellip;]<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,3,7],"tags":[],"class_list":["post-42","post","type-post","status-publish","format-standard","hentry","category-coding","category-general","category-sml"],"_links":{"self":[{"href":"http:\/\/ken.friislarsen.net\/blog\/wp-json\/wp\/v2\/posts\/42","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/ken.friislarsen.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/ken.friislarsen.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/ken.friislarsen.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/ken.friislarsen.net\/blog\/wp-json\/wp\/v2\/comments?post=42"}],"version-history":[{"count":0,"href":"http:\/\/ken.friislarsen.net\/blog\/wp-json\/wp\/v2\/posts\/42\/revisions"}],"wp:attachment":[{"href":"http:\/\/ken.friislarsen.net\/blog\/wp-json\/wp\/v2\/media?parent=42"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/ken.friislarsen.net\/blog\/wp-json\/wp\/v2\/categories?post=42"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/ken.friislarsen.net\/blog\/wp-json\/wp\/v2\/tags?post=42"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}