{"id":499,"date":"2003-06-03T11:44:06","date_gmt":"2003-06-03T20:44:06","guid":{"rendered":"\/?p=499"},"modified":"2018-02-21T21:42:35","modified_gmt":"2018-02-22T02:42:35","slug":"multimethods","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2003\/06\/03\/multimethods\/","title":{"rendered":"Multimethods"},"content":{"rendered":"<p>\n\n<a href=\"http:\/\/www.sidhe.org\/~dan\/blog\/archives\/000198.html\">Dan Sugalski<\/a> does a great job of explaining multimethods. And there&rsquo;s more:\n\n<\/p>\n\n\n\n<blockquote cite=\"http:\/\/www.sidhe.org\/~dan\/blog\/archives\/000198.html\">\n\nHaskell, for example, allows you to dispatch based on the values that are passed, as well as their types. This is a tremendously powerful mechanism, though one that&rsquo;s not often implemented. (I have a nagging suspicion that&rsquo;s as much because it becomes close to impossible to mathematically prove a program's correctness with this scheme as the cost, and the folks doing language research tend to be of a theoretical bent. I may be wrong, though) \n\n<\/blockquote>\n\n\n\n<p>\n\nI worked on a language that had <code>where<\/code> clauses, a slight generalization of his example. You could say stuff say <code>foo(a, b : Int) where a ~= b\/2<\/code> or <code>foo(e : E, l : Seq[E]) where e = head(l)<\/code>. The language was designed with theorem proving in mind. However, this <em>does<\/em> complicate some things. And what do you do if the <code>where<\/code> clauses of the different methods aren&rsquo;t disjoint?\n\n<\/p>\n\n\n\n<p>\n\nTwo more points:\n\n<\/p>\n\n\n\n<ul>\n\n\t<li>Objective-C&rsquo;s categories are half way to multimethods. Methods don&rsquo;t have to be locked up in the class definition; you can add them later.<\/li>\n\n\n\n\t<li>Apple&rsquo;s Dylan language supports <a href=\"http:\/\/monday.sourceforge.net\/wiki\/index.php\/SingletonTypes\">singleton types<\/a>. <code>singleton(5)<\/code> creates a type that&rsquo;s a subtype of <code>5<\/code>&rsquo;s type and whose only member is <code>5<\/code>. This makes it possible to dispatch by value. Dylan also lets you make new number types with limited ranges, but I don&rsquo;t recall exactly how that works; presumably those are also taken into account during dispatch. Another fun way to use singleton types is to let the multimethod dispatch system do <a href=\"http:\/\/www.cs.bham.ac.uk\/research\/poplog\/paradigms_lectures\/lecture13.html\">memoizing<\/a> for you. You create a new method specialized on singleton types for the arguments you received, and put it in the dispatch table.<\/li>\n\n<\/ul>","protected":false},"excerpt":{"rendered":"<p>Dan Sugalski does a great job of explaining multimethods. And there&rsquo;s more: Haskell, for example, allows you to dispatch based on the values that are passed, as well as their types. This is a tremendously powerful mechanism, though one that&rsquo;s not often implemented. (I have a nagging suspicion that&rsquo;s as much because it becomes close [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"","apple_news_api_id":"","apple_news_api_modified_at":"","apple_news_api_revision":"","apple_news_api_share_url":"","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[4],"tags":[296,46,71],"class_list":["post-499","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-dylan","tag-languagedesign","tag-programming"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/499","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=499"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/499\/revisions"}],"predecessor-version":[{"id":20653,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/499\/revisions\/20653"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=499"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=499"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=499"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}