{"id":47,"date":"2015-10-13T13:03:32","date_gmt":"2015-10-13T13:03:32","guid":{"rendered":"http:\/\/www.rangakrish.com\/?p=47"},"modified":"2015-11-14T23:53:57","modified_gmt":"2015-11-14T23:53:57","slug":"multimethods-in-lisp","status":"publish","type":"post","link":"https:\/\/www.rangakrish.com\/index.php\/2015\/10\/13\/multimethods-in-lisp\/","title":{"rendered":"Multimethods in Lisp"},"content":{"rendered":"<p>In object-oriented languages such as C++ and Java, virtual functions are dynamically dispatched based on the runtime type of the receiving object. But if such virtual functions have an argument that is itself based on a class hierarchy, there is no way to associate dynamic dispatch based on both the receiver and the argument. In other words, the call<\/p>\n<p style=\"padding-left: 30px;\"><em><strong>obj.foo(arg)<\/strong><\/em><\/p>\n<p>will invoke the correct foo() based only on the runtime type of <em><strong>obj<\/strong><\/em>. The runtime type of <em><strong>arg<\/strong><\/em> is never considered.<\/p>\n<p>In my <strong><em>Advanced C++<\/em><\/strong> course, I discuss a programming technique that shows how we can use templates to simulate multiple dispatch in C++ (inspired by an article that appeared in C++ Report, 1998). Multimethods have been part of Lisp for a long time\u00a0(a multimethod is a method that is selected based on the runtime types of two or more of its arguments).<\/p>\n<p>Consider a hierarchy of space objects as shown in the following figure.<\/p>\n<figure id=\"attachment_55\" aria-describedby=\"caption-attachment-55\" style=\"width: 383px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2015\/10\/Multimethods.png\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" data-attachment-id=\"55\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2015\/10\/13\/multimethods-in-lisp\/multimethods-2\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2015\/10\/Multimethods.png\" data-orig-size=\"617,349\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Multimethods\" data-image-description=\"\" data-image-caption=\"&lt;p&gt;Multimethods Example&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2015\/10\/Multimethods.png\" class=\" wp-image-55\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2015\/10\/Multimethods.png?resize=383%2C217\" alt=\"Multimethods Example\" width=\"383\" height=\"217\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2015\/10\/Multimethods.png?w=617&amp;ssl=1 617w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2015\/10\/Multimethods.png?resize=300%2C170&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2015\/10\/Multimethods.png?resize=352%2C198&amp;ssl=1 352w\" sizes=\"(max-width: 383px) 100vw, 383px\" \/><\/a><figcaption id=\"caption-attachment-55\" class=\"wp-caption-text\"><strong>Multimethods Example<\/strong><\/figcaption><\/figure>\n<p>We want to model collision between various space objects, for example, between SpaceShip and Asteroid, Asteroid and Mars, etc. We want to associate default collision behaviour between objects and at the same time handle specific cases where appropriate. The following code fragment (I have tested it in <a href=\"http:\/\/franz.com\/products\/allegrocl\/\" target=\"_blank\">Allegro Common Lisp<\/a>) shows how this might be implemented.<\/p>\n<figure id=\"attachment_50\" aria-describedby=\"caption-attachment-50\" style=\"width: 546px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2015\/10\/Multimethods.jpg\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"50\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2015\/10\/13\/multimethods-in-lisp\/multimethods\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2015\/10\/Multimethods.jpg\" data-orig-size=\"546,606\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Rangarajan&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1444755531&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Multimethods in Lisp\" data-image-description=\"\" data-image-caption=\"&lt;p&gt;Multimethods in Lisp&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2015\/10\/Multimethods.jpg\" class=\"size-full wp-image-50\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2015\/10\/Multimethods.jpg?resize=546%2C606\" alt=\"Multimethods in Lisp\" width=\"546\" height=\"606\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2015\/10\/Multimethods.jpg?w=546&amp;ssl=1 546w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2015\/10\/Multimethods.jpg?resize=270%2C300&amp;ssl=1 270w\" sizes=\"(max-width: 546px) 100vw, 546px\" \/><\/a><figcaption id=\"caption-attachment-50\" class=\"wp-caption-text\"><strong>Multimethods in Lisp<\/strong><\/figcaption><\/figure>\n<p>In this example, I have defined a method to handle collision between two SpaceObjects in general, and between Asteroid and Planet, as a special case. You can see that Lisp correctly chooses the most specific method based on both the arguments at runtime.<\/p>\n<p>Although I have discussed an example with two objects, this works with any number of arguments! The example code can be downloaded <a href=\"http:\/\/www.rangakrish.com\/downloads\/Multimethod.lisp\" target=\"_blank\">here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In object-oriented languages such as C++ and Java, virtual functions are dynamically dispatched based on the runtime type of the receiving object. But if such virtual functions have an argument that is itself based on a class hierarchy, there is no way to associate dynamic dispatch based on both the receiver and the argument. In [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[18,17],"tags":[21],"class_list":["post-47","post","type-post","status-publish","format-standard","hentry","category-lisp","category-programming","tag-multimethods"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9OLnF-L","jetpack-related-posts":[{"id":278,"url":"https:\/\/www.rangakrish.com\/index.php\/2016\/07\/07\/multimethods-in-julia\/","url_meta":{"origin":47,"position":0},"title":"Multimethods in Julia","author":"admin","date":"July 7, 2016","format":false,"excerpt":"I got interested in Julia programming language quite recently, primarily because of a project involving image processing and machine learning. The language is still evolving, but already has a rich set of features and a good collection of external libraries\u00a0covering many areas. One of the highlights of the language is\u2026","rel":"","context":"In &quot;Julia&quot;","block_context":{"text":"Julia","link":"https:\/\/www.rangakrish.com\/index.php\/category\/julia\/"},"img":{"alt_text":"Multimethods Example","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2015\/10\/Multimethods.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":3350,"url":"https:\/\/www.rangakrish.com\/index.php\/2024\/03\/09\/the-hy-programming-language-part-2\/","url_meta":{"origin":47,"position":1},"title":"The Hy Programming Language &#8211; Part 2","author":"admin","date":"March 9, 2024","format":false,"excerpt":"I wrote about \u201cHy\u201d language in an earlier article. Since then I have been exploring the language some more, trying to get a better understanding of its features.\u00a0 The following example uses inheritance to model different types of \u201cSpace\u201d objects such as Asteroid, SpaceShip and Planet. It is taken from\u2026","rel":"","context":"In &quot;Hy Language&quot;","block_context":{"text":"Hy Language","link":"https:\/\/www.rangakrish.com\/index.php\/category\/hy-language\/"},"img":{"alt_text":"Inheritance Example","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2024\/03\/Inheritance-Example-201x300.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2024\/03\/Inheritance-Example-201x300.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2024\/03\/Inheritance-Example-201x300.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":3555,"url":"https:\/\/www.rangakrish.com\/index.php\/2024\/11\/22\/multiobjective-optimization-moo-in-lisp-and-prolog\/","url_meta":{"origin":47,"position":2},"title":"Multiobjective Optimization (MOO) in Lisp and Prolog","author":"admin","date":"November 22, 2024","format":false,"excerpt":"Recently I came across a nice article\u00a0by Jose Crespo, where the author stresses that the future of programming revolves around the application of math concepts such as Functor, Monads, Folds, etc. In addition, he argues that familiarity with C\/C++ is essential in this modern AI age. The author goes through\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"Lisp Code for Pareto Front","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2024\/11\/lisp-code-249x300.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":2815,"url":"https:\/\/www.rangakrish.com\/index.php\/2022\/05\/29\/setting-up-function-hooks-in-lisp\/","url_meta":{"origin":47,"position":3},"title":"Setting Up Function Hooks in Lisp","author":"admin","date":"May 29, 2022","format":false,"excerpt":"Lisp is known to be a highly dynamic language, where functions are first-class\u00a0objects. It is possible to define and undefine functions on the fly as well as attach hooks to existing functions. These are in addition to the ability to pass functions as parameters to other functions and returning a\u2026","rel":"","context":"In &quot;LISP&quot;","block_context":{"text":"LISP","link":"https:\/\/www.rangakrish.com\/index.php\/category\/lisp\/"},"img":{"alt_text":"Function Object","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/Function-Object1-300x58.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/Function-Object1-300x58.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/Function-Object1-300x58.jpg?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":1091,"url":"https:\/\/www.rangakrish.com\/index.php\/2018\/10\/14\/c17-stdapply-and-stdinvoke\/","url_meta":{"origin":47,"position":4},"title":"C++17 &#8211; std::apply() and std::invoke()","author":"admin","date":"October 14, 2018","format":false,"excerpt":"Calling a function (or function object) dynamically, through a pointer known at runtime, is a common programming scenario. Almost all languages support this use case. Lisp, for example, has apply and funcall. When using apply, you can see that the arguments are passed via a separate list object. With funcall,\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"Calling Member Functions","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/10\/Code5.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/10\/Code5.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/10\/Code5.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/10\/Code5.png?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":2334,"url":"https:\/\/www.rangakrish.com\/index.php\/2021\/02\/28\/calling-c-methods-from-lispworks-lisp-through-com-interface\/","url_meta":{"origin":47,"position":5},"title":"Calling C# Methods from LispWorks Lisp through COM Interface","author":"admin","date":"February 28, 2021","format":false,"excerpt":"In an earlier article, I showed how to invoke C# methods from Sicstus Prolog. Today, I would like to detail the steps for doing the same from LispWorks Lisp. LispWorks Enterprise Edition comes budled with support for COM and Automation. This is what we will be using. What this means\u2026","rel":"","context":"In &quot;LISP&quot;","block_context":{"text":"LISP","link":"https:\/\/www.rangakrish.com\/index.php\/category\/lisp\/"},"img":{"alt_text":"The COM Server in C#","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/02\/C-Code-300x267.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/47","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/comments?post=47"}],"version-history":[{"count":0,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/47\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/media?parent=47"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/categories?post=47"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/tags?post=47"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}