SlideShare a Scribd company logo
1 of 17
Download to read offline
Property-based dispatch in functional languages

                                      Christopher Chedeau


                              Technical Report no 1116, Décembre 2011
                                           revision 2187



Climb is a generic image processing library. A generic algorithm interface often requires several different
specialized implementations. Olena, a C++ library, solves this using properties.
   We present a way to dispatch a function call to the best specialized implementation using properties
in a dynamic programming language: Common Lisp. Then, we introduce examples of algorithms and
properties used in image processing.
Climb est une bibliothèque de traitement d’image générique. L’interface générique d’un algorithme né-
cessite souvent plusieurs implémentations différentes spécialisées. Olena, une bibliothèque C++, résoud
ce problème en rajoutant des propriétés.
   Nous présentons un moyen de rediriger un appel de fonction vers la meilleure implémentation spé-
cialisée grâce aux propriétés dans un langage de programmation dynamique: Common Lisp. Nous al-
lons ensuite montrer des exemples d’algorithmes et de propriétés utilisées dans le domaine du traitement
d’image.

Keywords
Climb, Common Lisp, Dispatch, Properties, Image Processing




                         Laboratoire de Recherche et Développement de l’Epita
                     14-16, rue Voltaire – F-94276 Le Kremlin-Bicêtre cedex – France
                              Tél. +33 1 53 14 59 47 – Fax. +33 1 53 14 59 22
                         lrde@lrde.epita.fr – http://www.lrde.epita.fr/
2


Copying this document
Copyright © 2011 LRDE.
  Permission is granted to copy, distribute and/or modify this document under the terms of
the GNU Free Documentation License, Version 1.2 or any later version published by the Free
Software Foundation; with the Invariant Sections being just “Copying this document”, no Front-
Cover Texts, and no Back-Cover Texts.
  A copy of the license is provided in the file COPYING.DOC.
Contents

1 Introduction                                                                                                                                                 4

2   Olena Properties                                                                                                                                          5
    2.1 Properties used for Dispatch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .                                                            5
    2.2 C++ Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .                                                              6
    2.3 Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .                                                          8

3   Peroperties in Lisp                                                                                                                                        9
    3.1 Shift example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .                                                            9
    3.2 Generalized Dispatch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .                                                            10
    3.3 Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .                                                          11

4   Other Dispatch Implementations                                                                                                                            12
    4.1 Javascript Full Dispatch . . . . . .      .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   12
    4.2 Python Generic Dispatch . . . . . .       .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   12
    4.3 MetaObject Protocol Dispatch . . .        .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   13
    4.4 MOP Filtered Dispatch . . . . . . .       .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   14
    4.5 Javascript Multimethod . . . . . .        .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   14
    4.6 Haskell Function Pattern Matching         .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   15

5   Conclusion                                                                                                                                                16

6   Bibliography                                                                                                                                              17
Chapter 1

Introduction

It is often not possible to write one generic and optimized version of an algorithm that fits all
inputs. Instead, we often write multiple specialized versions based on inputs characteristics.
A dispatch technique is required to chose the best implementation. The implementation of
the dispatch mechanism is closely tied to the programming language capabilities. This reports
attempts to see how languages impacts dispatch implementation and philosophy.
   The usual characteristic used for dispatch is the type of the argument. Some languages also
support equality with a constant but it is about it. While they have proven to be good to express
a wide range of use cases, they do not properly fit our needs for image processing algorithms.
We want to be able to express complex characteristics with all the possibilities of the program-
ming language, not only class hierarchy and equality.
   In the first part, we explain how Olena represents inputs characteristics with properties and
implements a compile-time dispatch algorithm using C++ metaprogramming techniques. Then,
we propose an alternative dispatch algorithm that uses Common Lisp strengths. It provides bet-
ter expressiveness thanks to lambda functions along with a handy syntax powered by macros.
Finally, we make a round-up of different generic dispatch techniques in various programming
languages in order to understand what are the common dispatch concerns.
Chapter 2

Olena Properties

Properties are used in order to write specialized implementations of a function. It is the case
when you cannot write a single generic function that handles all the inputs. Specialized imple-
mentations are also written in order to improve performance for property combinations where
there are known optimizations.


2.1 Properties used for Dispatch
In Olena, there are 37 properties with a total of 141 different values. Around half of them
are about images, the other half describing other concepts of the library such as accumulators,
functions, operators, site sets, windows and values. A listing of some of those properties is
available in Figure 2.1.

       Data Type     Property Name      Property Values
           image     category           any, morpher, primary
                     dimension          any, one_d, three_d, two_d
                     ext_domain         any, extendable, none, some
                     ext_io             any, read_only, read_write
                     kind               any, logic
                     quant              any, low
                     speed              any, fastest
                     value_access       any, direct
                     value_alignment    any, with_grid
                     value_storage      any, disrupted, one_block, piecewise, singleton
                     vw_io              any, read_write
                     vw_set             any, uni
          site_set   arity              any, multiple, unique
                     nsites             any, known
            value    nature             any, floating, scalar, vectorial
                     quant              any, high, low
         window      definition          any, multiple, unique, varying
                     support            any, regular

                           Figure 2.1: Example of Olena Properties
2.2 C++ Implementation                                                                       6


  Properties in Olena can be seen as a combination of three parts:

Data Type Properties are specific to a data type.

Property Name The main goal of a property is to decide what version of the algorithm to use.
     It describes low-level implementation details of data types.
Property Value Unless the data type implementation has this property, the value is set to any.


2.2 C++ Implementation
Olena resolves the specialized implementation at compile time thanks to the SCOOP2 paradigm
(Géraud and Levillain (2008)). Through an example, we are going to show the code required to
write specializations of a method. The shift operation takes a window and an offset. It moves
every point of the window with the offset. For example, Figure 2.2 shows a 4-connectivity
window shifted by the point (3, 3).




                                   Figure 2.2: Shift Example

   The shift operation has two distinct specializations based on the window argument properties
as described in Figure 2.3. In order to shift a window with a point, you must be able to do
addition between coordinates of points. The property support with the value regular express
this hypothesis. Hence, all the specialization require the window to be on a regular support.
   A window can be defined in multiple ways. The most common is to give a list of points stored
in a site set. The window will be said to have a unique definition. When the window is defined
a combination of several site sets, it will be said to have a multiple definition.
   The specialization (1) only works on a unique definition and also requires the fixed size (the
number of points must not change during the execution of the program). The specialization (2)
works on multiple definitions and has no constraint on the size.

                                 definition                     support           size
                      any   unique multiple        varying   any regular      any fixed
 Specialization (1)
 Specialization (2)

                  Figure 2.3: Specialization constraints for the shift algorithm
7                                                                                      Olena Properties


     In order to implement this in C++, properties are separated into two categories.

Discriminant Properties The definition property can discriminate between the two specializa-
     tions. This is useful to know as we can use the C++ dispatch mechanism based on this
     property to chose the right specialization.
To-Check Properties In order to have meaningful error message when there is no matching
     specialization, we need to check for all the combination of properties that are not valid
     specializations.

  Once this categorization work is done, the implementation is straightforward as seen in Fig-
ure 2.4. It is just a matter of writing the proper dispatch method and property checks. However,
this is a repetitive and error-prone operation.

 1   template <typename W> mln_regular(W)
 2   shift_(trait::window::definition::unique, W& win, mln_dpsite(W)& dp)
 3   {
 4            // Checks for non−valid property combination
 5            mlc_is(mln_trait_window_size(W), trait::window::size::fixed)::check();
 6
 7           /* Specialized implementation (1) */
 8   }
 9
10   template <typename W> mln_regular(W)
11   shift_(trait::window::definition::multiple, W& win, mln_dpsite(W)& dp)
12   {
13            /* Specialized implementation (2) */
14   }
15
16   template <typename W> mln_regular(W)
17   shift(Window<W>& win, mln_dpsite(W)& dp)
18   {
19           // Checks for non−valid property combination
20           mlc_is(mln_trait_window_support(W), trait::window::support::regular)::check();
21           mlc_is_not(mln_trait_window_definition(W), trait::window::definition::any)::check();
22           mlc_is_not(mln_trait_window_definition(W), trait::window::definition::varying)::check();
23
24           // Dispatch on definition property
25           return shift_(mln_trait_window_definition(W)(), exact(win), dp);
26   }


                                Figure 2.4: Shift specialization in Olena
2.3 Analysis                                                                                    8


2.3    Analysis
The dispatch by property implemented in Olena has one fundamental characteristic: it is done
at compile time. It means that there is no associated runtime cost which is an crucial factor in a
performance oriented library. In order to achieve a static dispatch in C++, some trade-offs are
made.

   • The implementation of the framework required to make the dispatch work is not trivial,
     it adds a new layer to maintain to the project. Many macros are added in order to hide the
     complexity and provide a friendly syntax. It also increases compilation time.
   • Because properties are divided into two groups and used in two different contexts, finding
     the list of property values accepted by a specialization requires some work.
   • Adding new specializations may add new discriminant properties and therefore require
     to alter the prototype of previously written specializations. Adding new values for a prop-
     erty requires to add checks for all the specializations that uses the property. This makes
     property and specialization management not scalable.
Chapter 3

Peroperties in Lisp

The implementation of a static dispatch is a difficult task. The implementation in Olena is a feat
of strength that extensively uses C++ type checking and meta-programing techniques to do it.
Common Lisp is a functional language with the ability for the library developer to sculpt the
best syntax for the problem. With those features in mind, we propose a more generic and easy
to implement dispatch system based on Denuzière (2011).


3.1 Shift example
Let us get back to the shift example. So far, we have been reading Figure 2.3 from a property
perspective. We were focused on the characteristics of the properties in order to translate them
in C++. But the intuitive way is to read the table line by line. We want to know for each
specialization what properties it accepts. This is also what we expect to see in the code.

 1   (defalgo shift (
 2     (win
 3        :properties (
 4          :support :regular
 5          :definition :unique
 6          :size :fixed)
 7        window)
 8     (dp dpsite))
 9     ; Specialized implementation for definition::unique
10   )
11   (defalgo shift (
12     (win
13        :properties (
14          :support :regular
15          :definition :multiple)
16        window)
17     (dp dpsite))
18     ; Specialized implementation for definition::multiple
19   )


                                    Figure 3.1: Lisp Implementation
3.2 Generalized Dispatch                                                                           10


   Given this requirement, we can create a helper called defalgo that lets us declare our spe-
cializations as seen in Figure 3.1. It has the same syntax has a usual Common Lisp method
declaration but accepts a wider range of argument specializer, in this case, the prop specializer.
   defalgo implementation is straightforward. It keeps an internal structure that contains the
list of all the specializations of an algorithm. Each specialization has a list of tests to be applied
on the arguments. The first specialization with arguments passing all the tests is going to be
called. Figure 3.2 illustrates the structure generated by the shift example. Lines in red are tests
that differ from both specializations.




                                  Figure 3.2: Shift Specializations



3.2 Generalized Dispatch
The strength of the method is the ability to use any lambda function as argument special-
izer. We are no longer constrained by a language feature such as C++ that imposes special-
izers to be types. In the shift example, we’ve been using two specializers: instance-of and
has-property. Since we control defalgo, we can wrap an implicit instance-of around
an argument specializer that is a class.
11                                                                              Peroperties in Lisp


  In order to present the different ways to specify an arbitrary argument specializer, we are
going to use as an example the Fibonacci numbers. It is a sequence defined such as:
                                                 F0 = 0,                                           (3.1)
                                                 F1 = 1,                                           (3.2)
                                           Fn = Fn−1 + Fn−2                                        (3.3)
  Figure 3.3 shows three ways to encode a specializer.
A lambda expression It takes one argument and returns a boolean telling whether the special-
     ization is accepted or not.
A reference to a specializer It is often handy to define a generic specializer elsewhere and use
     a reference to it in the algorithm declaration.
Any expression that evaluates to a specializer The expression will be evaluated once and the
     result will be used as specializer.

 1 (defalgo fibo ((n                   1 (defun <2 (n) (< n 2))       1 (defun is (a)
 2                 (lambda (n)        2                              2 (lambda (b)
 3                    (< n 2))))      3 (defalgo fibo ((n #’<2))      3     (eq a b)))
 4 n)                                 4 n)                           4
                                                                     5 (defalgo fibo ((n (is 0)))
                                                                     6 0)
                                                                     7 (defalgo fibo ((n (is 1)))
                                                                     8 1)

 1 (defalgo fibo (n)
 2 (+ (fibo (− n 2)) (fibo (− n 1))))


                     Figure 3.3: Various ways to encode Fibonnaci special cases



3.3 Analysis
The generic dispatch provides a huge expressive power since it can accept any user function as
argument specializer. However, this comes at both at a cost of performance and integration to
common development chain.
     • This generic dispatch has a runtime cost. Every time you call a function, you have to
       go through the list of all argument specializers until you find a matching specialization.
       Since it is ultra-generic, it is hard to write an efficient caching mechanism for the dispatch
       lookup.
     • One way to develop code is to keep a Common Lisp instance running and evaluate the
       current file every time we make a change. This does not generate the intended result.
       Instead of having the newest version available in the environment, the old version is still
       at the top of the specialization list and is being used.
     • In the same vein, the order of the specialization declarations is important. You must al-
       ways declare the most general version last, or it will shadow all the other specializations.
Chapter 4

Other Dispatch Implementations

In order to see why generic dispatch using Common Lisp is a great fit, we are going to com-
pare it against other dispatch methodologies and implementation of generic dispatch in other
languages.


4.1      Javascript Full Dispatch
Before writing the Common Lisp implementation of generic dispatch, we wrote one in Javascript
called Full Dispatch (Chedeau (2010b)). The implementation is very similar using functions as
specializers. However, the syntax is not nearly as good as in Common Lisp. Javascript does not
have a macro facility that let us transform the syntax into something intuitive.
  As an example, we implement the Ackermann function in Figure 4.1 defined as following:

                                        A(m, n) = n + 1, if m = 0                          (4.1)
                               A(m, n) = A(m − 1, 1), if m > 0 and n = 0                   (4.2)
                         A(m, n) = A(m − 1, A(m, n − 1)), if m > 0 and n > 0               (4.3)


 1   ack = FullDispatch()
 2   ack.add [Zero, null], (m, n) −> n + 1
 3   ack.add [StrictPositive, Zero], (m, n) −> ack(m − 1, 1)
 4   ack.add [StrictPositive, StrictPositive], (m, n) −> ack(m − 1, ack(m, n − 1))


                   Figure 4.1: Ackermann function using Javascript Full Dispatch



4.2      Python Generic Dispatch
Michael Axiak implemented a generic dispatch method in Python (Axiak (2010)). He managed
to improve the syntax compared to the Javascript version using Python decorators. It looks
better but there is a problem, the argument name and specializers are not next to each other. It
makes it hard to visually find the link when there are more than one argument.
13                                                            Other Dispatch Implementations


  Figure 4.2 demonstrate how to implement Fibonacci Numbers using a dispatch based on
Python decorators.

 1   @dispatch(inside(0, 1))
 2   def fibo(n):
 3       return n
 4
 5   @dispatch(int)
 6   def fibo(n):
 7       return fibo(n − 1) + fibo(n − 2)


           Figure 4.2: Fibonacci Numbers using a dispatch based on Python decorators



4.3 MetaObject Protocol Dispatch
Common Lisp is bundled with an object oriented layer called CLOS that implements the MetaOb-
ject Protocol (Keene and Gerson (1989)) (MOP). Along with all the class definition, it provides a
traditional dispatch mechanism. The argument specializers are limited to class inheritance and
exact value match through eql specializers.
   The main conceptual difference between MOP dispatch and our generic dispatch is the fact
that the matched function will be the most specialized specialization instead of the first special-
ization found that matches. By changing this behavior, it allows to easily implement arbitrary
lambda as argument specializers and specialization on any amount of arguments.
   A summary of the differences between our generic dispatch and MOP dispatch is available
in Figure 4.3.

                                Type                defalgo     defmethod
                                      Specialization Behaviors
                     Specialization Selection     First found Most specialized
                    Specialization on arbitrary
                                                                     ×
                      amount of arguments
                                        Allowed Specializers
                        Instance of Class
                         EQL Specializer
                       Arbritrary Lambda                             ×

                                  Figure 4.3: Differences with MOP

  We took a lot of inspiration from MOP in order to design the syntax of the generic dispatch.
In Figure 4.4 you can see the implementation of Fibonacci Numbers using MOP. It only requires
to rename defmethod by defalgo, eql by is and to reorder the declarations in order to fall
back to the generic implementation of Figure 3.3.
4.4 MOP Filtered Dispatch                                                                                                 14



 1 (defmethod fibo (n)
 2 (+ (fibo (− n 1)) (fibo (− n 2))))
 3
 4 (defmethod fibo ((n (eql 1)))
 5 n)
 6
 7 (defmethod fibo ((n (eql 0)))
 8 n)


                                           Figure 4.4: Fibonnaci using MOP


4.4 MOP Filtered Dispatch
The idea behind Pascal Costanza Filtered Dispatch (Costanza et al. (2008)) is to enhance MOP
dispatch mechanism with filters. Instead of doing an eql specializer directly on the argument,
we are going to apply it on the argument filtered by a function. This way, the whole MOP
framework with specialization selection is still working, while providing more expressiveness
to the dispatch.
   Figure 4.5 is an example of the factorial implementation. The eql specializer is used on the
sign of the argument instead of the argument itself.

 1    (defun sign (n)
 2      (cond
 3        ((< n 0) ’neg)
 4        ((= n 0) ’zero)
 5        ((> n 0) ’pos)))
 6
 7    (defmethod factorial :filter :sign ((n (eql ’pos)))
 8      (* n (factorial (− n 1))))
 9
10    (defmethod factorial :filter :sign ((n (eql ’zero)))
11      1)
12
13    (defmethod factorial :filter :sign ((n (eql ’neg)))
14      (error "Factorial not defined for negative numbers."))


                                 Figure 4.5: Factorial using MOP Filtered Dispatch



4.5           Javascript Multimethod
Kris Jordan implemented a similar idea in the multimethod library. An example is available in
Figure 4.61 where the objective is to get the service identifier of the contact based on its service
field.
  To make a parallel with the previous dispatch, dispatch is the equivalent of a filter and
when of an eql specializer. The implementation takes advantage of the method chaining design
pattern in order to specify the different specializations.
     1 All   Javascript examples from now on will be written using CoffeeScript syntax to make it easier to understand.
15                                                                      Other Dispatch Implementations



 1 contacts = [
 2 { name:’Jack’, service:’Twitter’, handle: ’@jack’ },
 3 { name:’Diane’, service:’Email’, address:’diane@gmail.com’ },
 4 { name:’John’, service:’Phone’, number: ’919−919−9191’ }
 5 ]
 6
 7 getId = multimethod()
 8 .dispatch((contact) −> contact.service)
 9 .when(’Twitter’,
10     (contact) −> contact.handle)
11 .when(’Email’,
12     (contact) −> contact.address)
13 .default(
14     (contact) −> contact.name)


                          Figure 4.6: Example use of multimethod.js library


4.6 Haskell Function Pattern Matching
Haskell provides pattern matching at function level. You can write multiple specializations, the
arguments will be evaluated in order and the first specialization that matches will be used. One
interesting aspect is the ability to destructure an argument into different variables within the
specialization prototype.
  Figure 4.7 shows how to write Quicksort algorithm using function pattern matching in Haskell.

 1 quicksort [ ] = [ ]
 2 quicksort (p:xs) = (quicksort (filter (< p) xs)) ++ [p] ++ (quicksort (filter (>= p) xs))


                     Figure 4.7: Example of Haskell Function Pattern Matching
Chapter 5

Conclusion

In this report, we first showed an implementation of static property dispatch in C++. It ex-
tensively uses C++ features in order to dispatch for a very specific need. Then we introduce
a generic dispatch implementation that removes the compile-time constraint but which is able
to express a huge range of dispatch behaviors. In particular, the previous dispatch is trivial
to implement. Finally, we compare our generic dispatch to dispatch mechanisms from various
languages and libraries both in term of syntax and expressiveness power.
   This reports concludes a serie of three reports where we implemented all the generic aspects
that Olena provides for image processing taking advantage of Common Lisp dynamic aspects.
In Chedeau (2010a), we introduced genericity on data structures. An image can not only be
represented by a regular 2d grid but also as a 3d hexagonal grid or even as a graph. In Chedeau
(2011), we have seen that values are also subject to genericity. It is common to see intensity
values represented with 8-bit integer and floating point, Red-Green-Blue channels. Sometimes
it is convenient to hold other value types such as booleans or even images. Eventually, this
report completes the library including genericity on implementations.
Chapter 6

Bibliography

Axiak, M. (2010). Python generic dispatch. http://mike.axiak.net/blog/2010/06/
25/python-generic-dispatch/.
Chedeau, C. (2010a). Functionnal approach of image processing genericity. Technical Report
1001, EPITA Research and Development Laboratory (LRDE).

Chedeau, C. (2010b). Javascript - full dispatch (extended form of multimethod). http://
blog.vjeux.com/2010/javascript/javascript-full-dispatch-multimethod.
html.
Chedeau, C. (2011). Component trees and chaining operators in climb. Technical Report 1108,
EPITA Research and Development Laboratory (LRDE).

Costanza, P., Herzeel, C., Vallejos, J., and D’Hondt, T. (2008). Filtered dispatch. In Proceedings of
the 2008 symposium on Dynamic languages, DLS ’08, pages 4:1–4:10, New York, NY, USA. ACM.
Denuzière, L. (2011). Designing the user interface for a common lisp generic library. Technical
report, EPITA Research and Development Laboratory (LRDE).

Géraud, Th. and Levillain, R. (2008). Semantics-driven genericity: A sequel to the static C++
object-oriented programming paradigm (SCOOP 2). In Proceedings of the 6th International Work-
shop on Multiparadigm Programming with Object-Oriented Languages (MPOOL), Paphos, Cyprus.
Keene, S. and Gerson, D. (1989). Object-oriented programming in Common LISP: a programmer’s
guide to CLOS. Addison-Wesley.

More Related Content

What's hot

[Harvard CS264] 11a - Programming the Memory Hierarchy with Sequoia (Mike Bau...
[Harvard CS264] 11a - Programming the Memory Hierarchy with Sequoia (Mike Bau...[Harvard CS264] 11a - Programming the Memory Hierarchy with Sequoia (Mike Bau...
[Harvard CS264] 11a - Programming the Memory Hierarchy with Sequoia (Mike Bau...npinto
 
Java 7 Dolphin manjula kollipara
Java 7 Dolphin manjula kolliparaJava 7 Dolphin manjula kollipara
Java 7 Dolphin manjula kolliparaManjula Kollipara
 
06 iec t1_s1_oo_ps_session_08
06 iec t1_s1_oo_ps_session_0806 iec t1_s1_oo_ps_session_08
06 iec t1_s1_oo_ps_session_08Niit Care
 
11 iec t1_s1_oo_ps_session_16
11 iec t1_s1_oo_ps_session_1611 iec t1_s1_oo_ps_session_16
11 iec t1_s1_oo_ps_session_16Niit Care
 
13 iec t1_s1_oo_ps_session_19
13 iec t1_s1_oo_ps_session_1913 iec t1_s1_oo_ps_session_19
13 iec t1_s1_oo_ps_session_19Niit Care
 
Extension and Evolution
Extension and EvolutionExtension and Evolution
Extension and EvolutionEelco Visser
 
12 iec t1_s1_oo_ps_session_17
12 iec t1_s1_oo_ps_session_1712 iec t1_s1_oo_ps_session_17
12 iec t1_s1_oo_ps_session_17Niit Care
 
What is new and cool j2se & java
What is new and cool j2se & javaWhat is new and cool j2se & java
What is new and cool j2se & javaEugene Bogaart
 
Java Performance Tuning
Java Performance TuningJava Performance Tuning
Java Performance TuningMinh Hoang
 
Java- Concurrent programming - Synchronization (part 2)
Java- Concurrent programming - Synchronization (part 2)Java- Concurrent programming - Synchronization (part 2)
Java- Concurrent programming - Synchronization (part 2)Riccardo Cardin
 
Towards JVM Dynamic Languages Toolchain
Towards JVM Dynamic Languages ToolchainTowards JVM Dynamic Languages Toolchain
Towards JVM Dynamic Languages ToolchainAttila Szegedi
 

What's hot (20)

[Harvard CS264] 11a - Programming the Memory Hierarchy with Sequoia (Mike Bau...
[Harvard CS264] 11a - Programming the Memory Hierarchy with Sequoia (Mike Bau...[Harvard CS264] 11a - Programming the Memory Hierarchy with Sequoia (Mike Bau...
[Harvard CS264] 11a - Programming the Memory Hierarchy with Sequoia (Mike Bau...
 
Java 7 Dolphin manjula kollipara
Java 7 Dolphin manjula kolliparaJava 7 Dolphin manjula kollipara
Java 7 Dolphin manjula kollipara
 
Gdb cheat sheet
Gdb cheat sheetGdb cheat sheet
Gdb cheat sheet
 
06 iec t1_s1_oo_ps_session_08
06 iec t1_s1_oo_ps_session_0806 iec t1_s1_oo_ps_session_08
06 iec t1_s1_oo_ps_session_08
 
PVS-Studio
PVS-Studio PVS-Studio
PVS-Studio
 
11 iec t1_s1_oo_ps_session_16
11 iec t1_s1_oo_ps_session_1611 iec t1_s1_oo_ps_session_16
11 iec t1_s1_oo_ps_session_16
 
13 iec t1_s1_oo_ps_session_19
13 iec t1_s1_oo_ps_session_1913 iec t1_s1_oo_ps_session_19
13 iec t1_s1_oo_ps_session_19
 
Simple insites into JVM
Simple insites into JVMSimple insites into JVM
Simple insites into JVM
 
Extension and Evolution
Extension and EvolutionExtension and Evolution
Extension and Evolution
 
12 iec t1_s1_oo_ps_session_17
12 iec t1_s1_oo_ps_session_1712 iec t1_s1_oo_ps_session_17
12 iec t1_s1_oo_ps_session_17
 
What is new and cool j2se & java
What is new and cool j2se & javaWhat is new and cool j2se & java
What is new and cool j2se & java
 
No Heap Remote Objects for Distributed real-time Java
No Heap Remote Objects for Distributed real-time JavaNo Heap Remote Objects for Distributed real-time Java
No Heap Remote Objects for Distributed real-time Java
 
Cheat Sheet java
Cheat Sheet javaCheat Sheet java
Cheat Sheet java
 
Java Performance Tuning
Java Performance TuningJava Performance Tuning
Java Performance Tuning
 
Java Performance Tuning
Java Performance TuningJava Performance Tuning
Java Performance Tuning
 
Java- Concurrent programming - Synchronization (part 2)
Java- Concurrent programming - Synchronization (part 2)Java- Concurrent programming - Synchronization (part 2)
Java- Concurrent programming - Synchronization (part 2)
 
Pointers c imp
Pointers c impPointers c imp
Pointers c imp
 
Towards JVM Dynamic Languages Toolchain
Towards JVM Dynamic Languages ToolchainTowards JVM Dynamic Languages Toolchain
Towards JVM Dynamic Languages Toolchain
 
Pointers In C
Pointers In CPointers In C
Pointers In C
 
Project02 wit
Project02 witProject02 wit
Project02 wit
 

Viewers also liked

Facebook Tackle Box: 10 Apps & Tools Every Brand Should Use
Facebook Tackle Box: 10 Apps & Tools Every Brand Should UseFacebook Tackle Box: 10 Apps & Tools Every Brand Should Use
Facebook Tackle Box: 10 Apps & Tools Every Brand Should UseFirebelly Marketing
 
CMG Recommendation Letter PSB JPG
CMG Recommendation Letter PSB JPGCMG Recommendation Letter PSB JPG
CMG Recommendation Letter PSB JPGPia Sanchez
 
Working capital
Working capitalWorking capital
Working capitalNits Kedia
 
How We're Using Data Consolidation to Gain X-Ray Vision Into Marketing Perfor...
How We're Using Data Consolidation to Gain X-Ray Vision Into Marketing Perfor...How We're Using Data Consolidation to Gain X-Ray Vision Into Marketing Perfor...
How We're Using Data Consolidation to Gain X-Ray Vision Into Marketing Perfor...Janessa Lantz
 
How to create the life you want
How to create the life you wantHow to create the life you want
How to create the life you wantSelf-employed
 
Best Practice Guideline to Managing On-site Vermiculture Technologies
Best Practice Guideline to Managing On-site Vermiculture TechnologiesBest Practice Guideline to Managing On-site Vermiculture Technologies
Best Practice Guideline to Managing On-site Vermiculture Technologiesx3G9
 
Yellow Slice Design Profile - 2016
Yellow Slice Design Profile - 2016Yellow Slice Design Profile - 2016
Yellow Slice Design Profile - 2016Yellow Slice
 
Aula 1 a obra de kant como síntese do nascente pensamento burguês
Aula 1   a obra de kant como síntese do nascente pensamento burguêsAula 1   a obra de kant como síntese do nascente pensamento burguês
Aula 1 a obra de kant como síntese do nascente pensamento burguêsLeandro Alano
 
Show me your hands
Show me your handsShow me your hands
Show me your handsTerry Penney
 
Overview on china's philanthropy for ACCP
Overview on china's philanthropy for ACCPOverview on china's philanthropy for ACCP
Overview on china's philanthropy for ACCPgive2asia
 

Viewers also liked (20)

Facebook Tackle Box: 10 Apps & Tools Every Brand Should Use
Facebook Tackle Box: 10 Apps & Tools Every Brand Should UseFacebook Tackle Box: 10 Apps & Tools Every Brand Should Use
Facebook Tackle Box: 10 Apps & Tools Every Brand Should Use
 
Img056
Img056Img056
Img056
 
Unit 1.my school
Unit 1.my schoolUnit 1.my school
Unit 1.my school
 
CMG Recommendation Letter PSB JPG
CMG Recommendation Letter PSB JPGCMG Recommendation Letter PSB JPG
CMG Recommendation Letter PSB JPG
 
Working capital
Working capitalWorking capital
Working capital
 
JS basics
JS basicsJS basics
JS basics
 
Ngaputaw ppt
Ngaputaw pptNgaputaw ppt
Ngaputaw ppt
 
A Career in Teaching - Isobel Kerr [Scottish Teacher Recruitment Team]
A Career in Teaching - Isobel Kerr [Scottish Teacher Recruitment Team]A Career in Teaching - Isobel Kerr [Scottish Teacher Recruitment Team]
A Career in Teaching - Isobel Kerr [Scottish Teacher Recruitment Team]
 
THE BOOK OF THE DEAD
THE BOOK OF THE DEAD THE BOOK OF THE DEAD
THE BOOK OF THE DEAD
 
How We're Using Data Consolidation to Gain X-Ray Vision Into Marketing Perfor...
How We're Using Data Consolidation to Gain X-Ray Vision Into Marketing Perfor...How We're Using Data Consolidation to Gain X-Ray Vision Into Marketing Perfor...
How We're Using Data Consolidation to Gain X-Ray Vision Into Marketing Perfor...
 
How to create the life you want
How to create the life you wantHow to create the life you want
How to create the life you want
 
Best Practice Guideline to Managing On-site Vermiculture Technologies
Best Practice Guideline to Managing On-site Vermiculture TechnologiesBest Practice Guideline to Managing On-site Vermiculture Technologies
Best Practice Guideline to Managing On-site Vermiculture Technologies
 
Info penting
Info pentingInfo penting
Info penting
 
Yellow Slice Design Profile - 2016
Yellow Slice Design Profile - 2016Yellow Slice Design Profile - 2016
Yellow Slice Design Profile - 2016
 
Transformator
TransformatorTransformator
Transformator
 
Aula 1 a obra de kant como síntese do nascente pensamento burguês
Aula 1   a obra de kant como síntese do nascente pensamento burguêsAula 1   a obra de kant como síntese do nascente pensamento burguês
Aula 1 a obra de kant como síntese do nascente pensamento burguês
 
Show me your hands
Show me your handsShow me your hands
Show me your hands
 
Overview on china's philanthropy for ACCP
Overview on china's philanthropy for ACCPOverview on china's philanthropy for ACCP
Overview on china's philanthropy for ACCP
 
Activities
ActivitiesActivities
Activities
 
Sun & VMware Desktop Training
Sun & VMware Desktop TrainingSun & VMware Desktop Training
Sun & VMware Desktop Training
 

Similar to Climb - Property-based dispatch in functional languages [Report]

Similar to Climb - Property-based dispatch in functional languages [Report] (20)

The Function Pointer Tutorials
The Function Pointer TutorialsThe Function Pointer Tutorials
The Function Pointer Tutorials
 
robert-kovacsics-part-ii-dissertation
robert-kovacsics-part-ii-dissertationrobert-kovacsics-part-ii-dissertation
robert-kovacsics-part-ii-dissertation
 
PerlIntro
PerlIntroPerlIntro
PerlIntro
 
PerlIntro
PerlIntroPerlIntro
PerlIntro
 
Thesis
ThesisThesis
Thesis
 
Perl-crash-course
Perl-crash-coursePerl-crash-course
Perl-crash-course
 
Perl-crash-course
Perl-crash-coursePerl-crash-course
Perl-crash-course
 
Perl-crash-course
Perl-crash-coursePerl-crash-course
Perl-crash-course
 
Perl-crash-course
Perl-crash-coursePerl-crash-course
Perl-crash-course
 
Climb - A Generic and Dynamic Approach to Image Processing
Climb - A Generic and Dynamic Approach to Image ProcessingClimb - A Generic and Dynamic Approach to Image Processing
Climb - A Generic and Dynamic Approach to Image Processing
 
MyShell - English
MyShell - EnglishMyShell - English
MyShell - English
 
PythonIntro
PythonIntroPythonIntro
PythonIntro
 
textconfig
textconfigtextconfig
textconfig
 
MicroFSharp
MicroFSharpMicroFSharp
MicroFSharp
 
Intro to embedded systems programming
Intro to embedded systems programming Intro to embedded systems programming
Intro to embedded systems programming
 
Generics Tutorial
Generics TutorialGenerics Tutorial
Generics Tutorial
 
Getting started erlang
Getting started erlangGetting started erlang
Getting started erlang
 
Zrp manual 2
Zrp manual 2Zrp manual 2
Zrp manual 2
 
117 A Outline 25
117 A Outline 25117 A Outline 25
117 A Outline 25
 
Generics Tutorial
Generics TutorialGenerics Tutorial
Generics Tutorial
 

More from Christopher Chedeau

Generic Image Processing With Climb – 5th ELS
Generic Image Processing With Climb – 5th ELSGeneric Image Processing With Climb – 5th ELS
Generic Image Processing With Climb – 5th ELSChristopher Chedeau
 
Climb - Property-based dispatch in functional languages [Slides]
Climb - Property-based dispatch in functional languages [Slides]Climb - Property-based dispatch in functional languages [Slides]
Climb - Property-based dispatch in functional languages [Slides]Christopher Chedeau
 
JSPP: Morphing C++ into Javascript
JSPP: Morphing C++ into JavascriptJSPP: Morphing C++ into Javascript
JSPP: Morphing C++ into JavascriptChristopher Chedeau
 
Climb – Chaining Operators - Report
Climb – Chaining Operators - ReportClimb – Chaining Operators - Report
Climb – Chaining Operators - ReportChristopher Chedeau
 
Climb - A Generic and Dynamic Approach to Image Processing
Climb - A Generic and Dynamic Approach to Image ProcessingClimb - A Generic and Dynamic Approach to Image Processing
Climb - A Generic and Dynamic Approach to Image ProcessingChristopher Chedeau
 

More from Christopher Chedeau (8)

Generic Image Processing With Climb – 5th ELS
Generic Image Processing With Climb – 5th ELSGeneric Image Processing With Climb – 5th ELS
Generic Image Processing With Climb – 5th ELS
 
Climb - Property-based dispatch in functional languages [Slides]
Climb - Property-based dispatch in functional languages [Slides]Climb - Property-based dispatch in functional languages [Slides]
Climb - Property-based dispatch in functional languages [Slides]
 
JSPP: Morphing C++ into Javascript
JSPP: Morphing C++ into JavascriptJSPP: Morphing C++ into Javascript
JSPP: Morphing C++ into Javascript
 
J
JJ
J
 
Morpho math
Morpho mathMorpho math
Morpho math
 
Slides
SlidesSlides
Slides
 
Climb – Chaining Operators - Report
Climb – Chaining Operators - ReportClimb – Chaining Operators - Report
Climb – Chaining Operators - Report
 
Climb - A Generic and Dynamic Approach to Image Processing
Climb - A Generic and Dynamic Approach to Image ProcessingClimb - A Generic and Dynamic Approach to Image Processing
Climb - A Generic and Dynamic Approach to Image Processing
 

Recently uploaded

How to Add a many2many Relational Field in Odoo 17
How to Add a many2many Relational Field in Odoo 17How to Add a many2many Relational Field in Odoo 17
How to Add a many2many Relational Field in Odoo 17Celine George
 
Ultra structure and life cycle of Plasmodium.pptx
Ultra structure and life cycle of Plasmodium.pptxUltra structure and life cycle of Plasmodium.pptx
Ultra structure and life cycle of Plasmodium.pptxDr. Asif Anas
 
Patterns of Written Texts Across Disciplines.pptx
Patterns of Written Texts Across Disciplines.pptxPatterns of Written Texts Across Disciplines.pptx
Patterns of Written Texts Across Disciplines.pptxMYDA ANGELICA SUAN
 
Practical Research 1: Lesson 8 Writing the Thesis Statement.pptx
Practical Research 1: Lesson 8 Writing the Thesis Statement.pptxPractical Research 1: Lesson 8 Writing the Thesis Statement.pptx
Practical Research 1: Lesson 8 Writing the Thesis Statement.pptxKatherine Villaluna
 
P4C x ELT = P4ELT: Its Theoretical Background (Kanazawa, 2024 March).pdf
P4C x ELT = P4ELT: Its Theoretical Background (Kanazawa, 2024 March).pdfP4C x ELT = P4ELT: Its Theoretical Background (Kanazawa, 2024 March).pdf
P4C x ELT = P4ELT: Its Theoretical Background (Kanazawa, 2024 March).pdfYu Kanazawa / Osaka University
 
Philosophy of Education and Educational Philosophy
Philosophy of Education  and Educational PhilosophyPhilosophy of Education  and Educational Philosophy
Philosophy of Education and Educational PhilosophyShuvankar Madhu
 
Clinical Pharmacy Introduction to Clinical Pharmacy, Concept of clinical pptx
Clinical Pharmacy  Introduction to Clinical Pharmacy, Concept of clinical pptxClinical Pharmacy  Introduction to Clinical Pharmacy, Concept of clinical pptx
Clinical Pharmacy Introduction to Clinical Pharmacy, Concept of clinical pptxraviapr7
 
CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 11 - GLOBAL SUCCESS - NĂM HỌC 2023-2024 - HK...
CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 11 - GLOBAL SUCCESS - NĂM HỌC 2023-2024 - HK...CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 11 - GLOBAL SUCCESS - NĂM HỌC 2023-2024 - HK...
CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 11 - GLOBAL SUCCESS - NĂM HỌC 2023-2024 - HK...Nguyen Thanh Tu Collection
 
Benefits & Challenges of Inclusive Education
Benefits & Challenges of Inclusive EducationBenefits & Challenges of Inclusive Education
Benefits & Challenges of Inclusive EducationMJDuyan
 
How to Solve Singleton Error in the Odoo 17
How to Solve Singleton Error in the  Odoo 17How to Solve Singleton Error in the  Odoo 17
How to Solve Singleton Error in the Odoo 17Celine George
 
The basics of sentences session 10pptx.pptx
The basics of sentences session 10pptx.pptxThe basics of sentences session 10pptx.pptx
The basics of sentences session 10pptx.pptxheathfieldcps1
 
HED Office Sohayok Exam Question Solution 2023.pdf
HED Office Sohayok Exam Question Solution 2023.pdfHED Office Sohayok Exam Question Solution 2023.pdf
HED Office Sohayok Exam Question Solution 2023.pdfMohonDas
 
How to Manage Cross-Selling in Odoo 17 Sales
How to Manage Cross-Selling in Odoo 17 SalesHow to Manage Cross-Selling in Odoo 17 Sales
How to Manage Cross-Selling in Odoo 17 SalesCeline George
 
In - Vivo and In - Vitro Correlation.pptx
In - Vivo and In - Vitro Correlation.pptxIn - Vivo and In - Vitro Correlation.pptx
In - Vivo and In - Vitro Correlation.pptxAditiChauhan701637
 
How to Add Existing Field in One2Many Tree View in Odoo 17
How to Add Existing Field in One2Many Tree View in Odoo 17How to Add Existing Field in One2Many Tree View in Odoo 17
How to Add Existing Field in One2Many Tree View in Odoo 17Celine George
 
The Singapore Teaching Practice document
The Singapore Teaching Practice documentThe Singapore Teaching Practice document
The Singapore Teaching Practice documentXsasf Sfdfasd
 
Practical Research 1 Lesson 9 Scope and delimitation.pptx
Practical Research 1 Lesson 9 Scope and delimitation.pptxPractical Research 1 Lesson 9 Scope and delimitation.pptx
Practical Research 1 Lesson 9 Scope and delimitation.pptxKatherine Villaluna
 
General views of Histopathology and step
General views of Histopathology and stepGeneral views of Histopathology and step
General views of Histopathology and stepobaje godwin sunday
 
CAULIFLOWER BREEDING 1 Parmar pptx
CAULIFLOWER BREEDING 1 Parmar pptxCAULIFLOWER BREEDING 1 Parmar pptx
CAULIFLOWER BREEDING 1 Parmar pptxSaurabhParmar42
 

Recently uploaded (20)

How to Add a many2many Relational Field in Odoo 17
How to Add a many2many Relational Field in Odoo 17How to Add a many2many Relational Field in Odoo 17
How to Add a many2many Relational Field in Odoo 17
 
Ultra structure and life cycle of Plasmodium.pptx
Ultra structure and life cycle of Plasmodium.pptxUltra structure and life cycle of Plasmodium.pptx
Ultra structure and life cycle of Plasmodium.pptx
 
Patterns of Written Texts Across Disciplines.pptx
Patterns of Written Texts Across Disciplines.pptxPatterns of Written Texts Across Disciplines.pptx
Patterns of Written Texts Across Disciplines.pptx
 
Practical Research 1: Lesson 8 Writing the Thesis Statement.pptx
Practical Research 1: Lesson 8 Writing the Thesis Statement.pptxPractical Research 1: Lesson 8 Writing the Thesis Statement.pptx
Practical Research 1: Lesson 8 Writing the Thesis Statement.pptx
 
P4C x ELT = P4ELT: Its Theoretical Background (Kanazawa, 2024 March).pdf
P4C x ELT = P4ELT: Its Theoretical Background (Kanazawa, 2024 March).pdfP4C x ELT = P4ELT: Its Theoretical Background (Kanazawa, 2024 March).pdf
P4C x ELT = P4ELT: Its Theoretical Background (Kanazawa, 2024 March).pdf
 
Philosophy of Education and Educational Philosophy
Philosophy of Education  and Educational PhilosophyPhilosophy of Education  and Educational Philosophy
Philosophy of Education and Educational Philosophy
 
Clinical Pharmacy Introduction to Clinical Pharmacy, Concept of clinical pptx
Clinical Pharmacy  Introduction to Clinical Pharmacy, Concept of clinical pptxClinical Pharmacy  Introduction to Clinical Pharmacy, Concept of clinical pptx
Clinical Pharmacy Introduction to Clinical Pharmacy, Concept of clinical pptx
 
CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 11 - GLOBAL SUCCESS - NĂM HỌC 2023-2024 - HK...
CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 11 - GLOBAL SUCCESS - NĂM HỌC 2023-2024 - HK...CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 11 - GLOBAL SUCCESS - NĂM HỌC 2023-2024 - HK...
CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 11 - GLOBAL SUCCESS - NĂM HỌC 2023-2024 - HK...
 
Finals of Kant get Marx 2.0 : a general politics quiz
Finals of Kant get Marx 2.0 : a general politics quizFinals of Kant get Marx 2.0 : a general politics quiz
Finals of Kant get Marx 2.0 : a general politics quiz
 
Benefits & Challenges of Inclusive Education
Benefits & Challenges of Inclusive EducationBenefits & Challenges of Inclusive Education
Benefits & Challenges of Inclusive Education
 
How to Solve Singleton Error in the Odoo 17
How to Solve Singleton Error in the  Odoo 17How to Solve Singleton Error in the  Odoo 17
How to Solve Singleton Error in the Odoo 17
 
The basics of sentences session 10pptx.pptx
The basics of sentences session 10pptx.pptxThe basics of sentences session 10pptx.pptx
The basics of sentences session 10pptx.pptx
 
HED Office Sohayok Exam Question Solution 2023.pdf
HED Office Sohayok Exam Question Solution 2023.pdfHED Office Sohayok Exam Question Solution 2023.pdf
HED Office Sohayok Exam Question Solution 2023.pdf
 
How to Manage Cross-Selling in Odoo 17 Sales
How to Manage Cross-Selling in Odoo 17 SalesHow to Manage Cross-Selling in Odoo 17 Sales
How to Manage Cross-Selling in Odoo 17 Sales
 
In - Vivo and In - Vitro Correlation.pptx
In - Vivo and In - Vitro Correlation.pptxIn - Vivo and In - Vitro Correlation.pptx
In - Vivo and In - Vitro Correlation.pptx
 
How to Add Existing Field in One2Many Tree View in Odoo 17
How to Add Existing Field in One2Many Tree View in Odoo 17How to Add Existing Field in One2Many Tree View in Odoo 17
How to Add Existing Field in One2Many Tree View in Odoo 17
 
The Singapore Teaching Practice document
The Singapore Teaching Practice documentThe Singapore Teaching Practice document
The Singapore Teaching Practice document
 
Practical Research 1 Lesson 9 Scope and delimitation.pptx
Practical Research 1 Lesson 9 Scope and delimitation.pptxPractical Research 1 Lesson 9 Scope and delimitation.pptx
Practical Research 1 Lesson 9 Scope and delimitation.pptx
 
General views of Histopathology and step
General views of Histopathology and stepGeneral views of Histopathology and step
General views of Histopathology and step
 
CAULIFLOWER BREEDING 1 Parmar pptx
CAULIFLOWER BREEDING 1 Parmar pptxCAULIFLOWER BREEDING 1 Parmar pptx
CAULIFLOWER BREEDING 1 Parmar pptx
 

Climb - Property-based dispatch in functional languages [Report]

  • 1. Property-based dispatch in functional languages Christopher Chedeau Technical Report no 1116, Décembre 2011 revision 2187 Climb is a generic image processing library. A generic algorithm interface often requires several different specialized implementations. Olena, a C++ library, solves this using properties. We present a way to dispatch a function call to the best specialized implementation using properties in a dynamic programming language: Common Lisp. Then, we introduce examples of algorithms and properties used in image processing. Climb est une bibliothèque de traitement d’image générique. L’interface générique d’un algorithme né- cessite souvent plusieurs implémentations différentes spécialisées. Olena, une bibliothèque C++, résoud ce problème en rajoutant des propriétés. Nous présentons un moyen de rediriger un appel de fonction vers la meilleure implémentation spé- cialisée grâce aux propriétés dans un langage de programmation dynamique: Common Lisp. Nous al- lons ensuite montrer des exemples d’algorithmes et de propriétés utilisées dans le domaine du traitement d’image. Keywords Climb, Common Lisp, Dispatch, Properties, Image Processing Laboratoire de Recherche et Développement de l’Epita 14-16, rue Voltaire – F-94276 Le Kremlin-Bicêtre cedex – France Tél. +33 1 53 14 59 47 – Fax. +33 1 53 14 59 22 lrde@lrde.epita.fr – http://www.lrde.epita.fr/
  • 2. 2 Copying this document Copyright © 2011 LRDE. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation; with the Invariant Sections being just “Copying this document”, no Front- Cover Texts, and no Back-Cover Texts. A copy of the license is provided in the file COPYING.DOC.
  • 3. Contents 1 Introduction 4 2 Olena Properties 5 2.1 Properties used for Dispatch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 2.2 C++ Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 2.3 Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 3 Peroperties in Lisp 9 3.1 Shift example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 3.2 Generalized Dispatch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 3.3 Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 4 Other Dispatch Implementations 12 4.1 Javascript Full Dispatch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 4.2 Python Generic Dispatch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 4.3 MetaObject Protocol Dispatch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 4.4 MOP Filtered Dispatch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 4.5 Javascript Multimethod . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 4.6 Haskell Function Pattern Matching . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 5 Conclusion 16 6 Bibliography 17
  • 4. Chapter 1 Introduction It is often not possible to write one generic and optimized version of an algorithm that fits all inputs. Instead, we often write multiple specialized versions based on inputs characteristics. A dispatch technique is required to chose the best implementation. The implementation of the dispatch mechanism is closely tied to the programming language capabilities. This reports attempts to see how languages impacts dispatch implementation and philosophy. The usual characteristic used for dispatch is the type of the argument. Some languages also support equality with a constant but it is about it. While they have proven to be good to express a wide range of use cases, they do not properly fit our needs for image processing algorithms. We want to be able to express complex characteristics with all the possibilities of the program- ming language, not only class hierarchy and equality. In the first part, we explain how Olena represents inputs characteristics with properties and implements a compile-time dispatch algorithm using C++ metaprogramming techniques. Then, we propose an alternative dispatch algorithm that uses Common Lisp strengths. It provides bet- ter expressiveness thanks to lambda functions along with a handy syntax powered by macros. Finally, we make a round-up of different generic dispatch techniques in various programming languages in order to understand what are the common dispatch concerns.
  • 5. Chapter 2 Olena Properties Properties are used in order to write specialized implementations of a function. It is the case when you cannot write a single generic function that handles all the inputs. Specialized imple- mentations are also written in order to improve performance for property combinations where there are known optimizations. 2.1 Properties used for Dispatch In Olena, there are 37 properties with a total of 141 different values. Around half of them are about images, the other half describing other concepts of the library such as accumulators, functions, operators, site sets, windows and values. A listing of some of those properties is available in Figure 2.1. Data Type Property Name Property Values image category any, morpher, primary dimension any, one_d, three_d, two_d ext_domain any, extendable, none, some ext_io any, read_only, read_write kind any, logic quant any, low speed any, fastest value_access any, direct value_alignment any, with_grid value_storage any, disrupted, one_block, piecewise, singleton vw_io any, read_write vw_set any, uni site_set arity any, multiple, unique nsites any, known value nature any, floating, scalar, vectorial quant any, high, low window definition any, multiple, unique, varying support any, regular Figure 2.1: Example of Olena Properties
  • 6. 2.2 C++ Implementation 6 Properties in Olena can be seen as a combination of three parts: Data Type Properties are specific to a data type. Property Name The main goal of a property is to decide what version of the algorithm to use. It describes low-level implementation details of data types. Property Value Unless the data type implementation has this property, the value is set to any. 2.2 C++ Implementation Olena resolves the specialized implementation at compile time thanks to the SCOOP2 paradigm (Géraud and Levillain (2008)). Through an example, we are going to show the code required to write specializations of a method. The shift operation takes a window and an offset. It moves every point of the window with the offset. For example, Figure 2.2 shows a 4-connectivity window shifted by the point (3, 3). Figure 2.2: Shift Example The shift operation has two distinct specializations based on the window argument properties as described in Figure 2.3. In order to shift a window with a point, you must be able to do addition between coordinates of points. The property support with the value regular express this hypothesis. Hence, all the specialization require the window to be on a regular support. A window can be defined in multiple ways. The most common is to give a list of points stored in a site set. The window will be said to have a unique definition. When the window is defined a combination of several site sets, it will be said to have a multiple definition. The specialization (1) only works on a unique definition and also requires the fixed size (the number of points must not change during the execution of the program). The specialization (2) works on multiple definitions and has no constraint on the size. definition support size any unique multiple varying any regular any fixed Specialization (1) Specialization (2) Figure 2.3: Specialization constraints for the shift algorithm
  • 7. 7 Olena Properties In order to implement this in C++, properties are separated into two categories. Discriminant Properties The definition property can discriminate between the two specializa- tions. This is useful to know as we can use the C++ dispatch mechanism based on this property to chose the right specialization. To-Check Properties In order to have meaningful error message when there is no matching specialization, we need to check for all the combination of properties that are not valid specializations. Once this categorization work is done, the implementation is straightforward as seen in Fig- ure 2.4. It is just a matter of writing the proper dispatch method and property checks. However, this is a repetitive and error-prone operation. 1 template <typename W> mln_regular(W) 2 shift_(trait::window::definition::unique, W& win, mln_dpsite(W)& dp) 3 { 4 // Checks for non−valid property combination 5 mlc_is(mln_trait_window_size(W), trait::window::size::fixed)::check(); 6 7 /* Specialized implementation (1) */ 8 } 9 10 template <typename W> mln_regular(W) 11 shift_(trait::window::definition::multiple, W& win, mln_dpsite(W)& dp) 12 { 13 /* Specialized implementation (2) */ 14 } 15 16 template <typename W> mln_regular(W) 17 shift(Window<W>& win, mln_dpsite(W)& dp) 18 { 19 // Checks for non−valid property combination 20 mlc_is(mln_trait_window_support(W), trait::window::support::regular)::check(); 21 mlc_is_not(mln_trait_window_definition(W), trait::window::definition::any)::check(); 22 mlc_is_not(mln_trait_window_definition(W), trait::window::definition::varying)::check(); 23 24 // Dispatch on definition property 25 return shift_(mln_trait_window_definition(W)(), exact(win), dp); 26 } Figure 2.4: Shift specialization in Olena
  • 8. 2.3 Analysis 8 2.3 Analysis The dispatch by property implemented in Olena has one fundamental characteristic: it is done at compile time. It means that there is no associated runtime cost which is an crucial factor in a performance oriented library. In order to achieve a static dispatch in C++, some trade-offs are made. • The implementation of the framework required to make the dispatch work is not trivial, it adds a new layer to maintain to the project. Many macros are added in order to hide the complexity and provide a friendly syntax. It also increases compilation time. • Because properties are divided into two groups and used in two different contexts, finding the list of property values accepted by a specialization requires some work. • Adding new specializations may add new discriminant properties and therefore require to alter the prototype of previously written specializations. Adding new values for a prop- erty requires to add checks for all the specializations that uses the property. This makes property and specialization management not scalable.
  • 9. Chapter 3 Peroperties in Lisp The implementation of a static dispatch is a difficult task. The implementation in Olena is a feat of strength that extensively uses C++ type checking and meta-programing techniques to do it. Common Lisp is a functional language with the ability for the library developer to sculpt the best syntax for the problem. With those features in mind, we propose a more generic and easy to implement dispatch system based on Denuzière (2011). 3.1 Shift example Let us get back to the shift example. So far, we have been reading Figure 2.3 from a property perspective. We were focused on the characteristics of the properties in order to translate them in C++. But the intuitive way is to read the table line by line. We want to know for each specialization what properties it accepts. This is also what we expect to see in the code. 1 (defalgo shift ( 2 (win 3 :properties ( 4 :support :regular 5 :definition :unique 6 :size :fixed) 7 window) 8 (dp dpsite)) 9 ; Specialized implementation for definition::unique 10 ) 11 (defalgo shift ( 12 (win 13 :properties ( 14 :support :regular 15 :definition :multiple) 16 window) 17 (dp dpsite)) 18 ; Specialized implementation for definition::multiple 19 ) Figure 3.1: Lisp Implementation
  • 10. 3.2 Generalized Dispatch 10 Given this requirement, we can create a helper called defalgo that lets us declare our spe- cializations as seen in Figure 3.1. It has the same syntax has a usual Common Lisp method declaration but accepts a wider range of argument specializer, in this case, the prop specializer. defalgo implementation is straightforward. It keeps an internal structure that contains the list of all the specializations of an algorithm. Each specialization has a list of tests to be applied on the arguments. The first specialization with arguments passing all the tests is going to be called. Figure 3.2 illustrates the structure generated by the shift example. Lines in red are tests that differ from both specializations. Figure 3.2: Shift Specializations 3.2 Generalized Dispatch The strength of the method is the ability to use any lambda function as argument special- izer. We are no longer constrained by a language feature such as C++ that imposes special- izers to be types. In the shift example, we’ve been using two specializers: instance-of and has-property. Since we control defalgo, we can wrap an implicit instance-of around an argument specializer that is a class.
  • 11. 11 Peroperties in Lisp In order to present the different ways to specify an arbitrary argument specializer, we are going to use as an example the Fibonacci numbers. It is a sequence defined such as: F0 = 0, (3.1) F1 = 1, (3.2) Fn = Fn−1 + Fn−2 (3.3) Figure 3.3 shows three ways to encode a specializer. A lambda expression It takes one argument and returns a boolean telling whether the special- ization is accepted or not. A reference to a specializer It is often handy to define a generic specializer elsewhere and use a reference to it in the algorithm declaration. Any expression that evaluates to a specializer The expression will be evaluated once and the result will be used as specializer. 1 (defalgo fibo ((n 1 (defun <2 (n) (< n 2)) 1 (defun is (a) 2 (lambda (n) 2 2 (lambda (b) 3 (< n 2)))) 3 (defalgo fibo ((n #’<2)) 3 (eq a b))) 4 n) 4 n) 4 5 (defalgo fibo ((n (is 0))) 6 0) 7 (defalgo fibo ((n (is 1))) 8 1) 1 (defalgo fibo (n) 2 (+ (fibo (− n 2)) (fibo (− n 1)))) Figure 3.3: Various ways to encode Fibonnaci special cases 3.3 Analysis The generic dispatch provides a huge expressive power since it can accept any user function as argument specializer. However, this comes at both at a cost of performance and integration to common development chain. • This generic dispatch has a runtime cost. Every time you call a function, you have to go through the list of all argument specializers until you find a matching specialization. Since it is ultra-generic, it is hard to write an efficient caching mechanism for the dispatch lookup. • One way to develop code is to keep a Common Lisp instance running and evaluate the current file every time we make a change. This does not generate the intended result. Instead of having the newest version available in the environment, the old version is still at the top of the specialization list and is being used. • In the same vein, the order of the specialization declarations is important. You must al- ways declare the most general version last, or it will shadow all the other specializations.
  • 12. Chapter 4 Other Dispatch Implementations In order to see why generic dispatch using Common Lisp is a great fit, we are going to com- pare it against other dispatch methodologies and implementation of generic dispatch in other languages. 4.1 Javascript Full Dispatch Before writing the Common Lisp implementation of generic dispatch, we wrote one in Javascript called Full Dispatch (Chedeau (2010b)). The implementation is very similar using functions as specializers. However, the syntax is not nearly as good as in Common Lisp. Javascript does not have a macro facility that let us transform the syntax into something intuitive. As an example, we implement the Ackermann function in Figure 4.1 defined as following: A(m, n) = n + 1, if m = 0 (4.1) A(m, n) = A(m − 1, 1), if m > 0 and n = 0 (4.2) A(m, n) = A(m − 1, A(m, n − 1)), if m > 0 and n > 0 (4.3) 1 ack = FullDispatch() 2 ack.add [Zero, null], (m, n) −> n + 1 3 ack.add [StrictPositive, Zero], (m, n) −> ack(m − 1, 1) 4 ack.add [StrictPositive, StrictPositive], (m, n) −> ack(m − 1, ack(m, n − 1)) Figure 4.1: Ackermann function using Javascript Full Dispatch 4.2 Python Generic Dispatch Michael Axiak implemented a generic dispatch method in Python (Axiak (2010)). He managed to improve the syntax compared to the Javascript version using Python decorators. It looks better but there is a problem, the argument name and specializers are not next to each other. It makes it hard to visually find the link when there are more than one argument.
  • 13. 13 Other Dispatch Implementations Figure 4.2 demonstrate how to implement Fibonacci Numbers using a dispatch based on Python decorators. 1 @dispatch(inside(0, 1)) 2 def fibo(n): 3 return n 4 5 @dispatch(int) 6 def fibo(n): 7 return fibo(n − 1) + fibo(n − 2) Figure 4.2: Fibonacci Numbers using a dispatch based on Python decorators 4.3 MetaObject Protocol Dispatch Common Lisp is bundled with an object oriented layer called CLOS that implements the MetaOb- ject Protocol (Keene and Gerson (1989)) (MOP). Along with all the class definition, it provides a traditional dispatch mechanism. The argument specializers are limited to class inheritance and exact value match through eql specializers. The main conceptual difference between MOP dispatch and our generic dispatch is the fact that the matched function will be the most specialized specialization instead of the first special- ization found that matches. By changing this behavior, it allows to easily implement arbitrary lambda as argument specializers and specialization on any amount of arguments. A summary of the differences between our generic dispatch and MOP dispatch is available in Figure 4.3. Type defalgo defmethod Specialization Behaviors Specialization Selection First found Most specialized Specialization on arbitrary × amount of arguments Allowed Specializers Instance of Class EQL Specializer Arbritrary Lambda × Figure 4.3: Differences with MOP We took a lot of inspiration from MOP in order to design the syntax of the generic dispatch. In Figure 4.4 you can see the implementation of Fibonacci Numbers using MOP. It only requires to rename defmethod by defalgo, eql by is and to reorder the declarations in order to fall back to the generic implementation of Figure 3.3.
  • 14. 4.4 MOP Filtered Dispatch 14 1 (defmethod fibo (n) 2 (+ (fibo (− n 1)) (fibo (− n 2)))) 3 4 (defmethod fibo ((n (eql 1))) 5 n) 6 7 (defmethod fibo ((n (eql 0))) 8 n) Figure 4.4: Fibonnaci using MOP 4.4 MOP Filtered Dispatch The idea behind Pascal Costanza Filtered Dispatch (Costanza et al. (2008)) is to enhance MOP dispatch mechanism with filters. Instead of doing an eql specializer directly on the argument, we are going to apply it on the argument filtered by a function. This way, the whole MOP framework with specialization selection is still working, while providing more expressiveness to the dispatch. Figure 4.5 is an example of the factorial implementation. The eql specializer is used on the sign of the argument instead of the argument itself. 1 (defun sign (n) 2 (cond 3 ((< n 0) ’neg) 4 ((= n 0) ’zero) 5 ((> n 0) ’pos))) 6 7 (defmethod factorial :filter :sign ((n (eql ’pos))) 8 (* n (factorial (− n 1)))) 9 10 (defmethod factorial :filter :sign ((n (eql ’zero))) 11 1) 12 13 (defmethod factorial :filter :sign ((n (eql ’neg))) 14 (error "Factorial not defined for negative numbers.")) Figure 4.5: Factorial using MOP Filtered Dispatch 4.5 Javascript Multimethod Kris Jordan implemented a similar idea in the multimethod library. An example is available in Figure 4.61 where the objective is to get the service identifier of the contact based on its service field. To make a parallel with the previous dispatch, dispatch is the equivalent of a filter and when of an eql specializer. The implementation takes advantage of the method chaining design pattern in order to specify the different specializations. 1 All Javascript examples from now on will be written using CoffeeScript syntax to make it easier to understand.
  • 15. 15 Other Dispatch Implementations 1 contacts = [ 2 { name:’Jack’, service:’Twitter’, handle: ’@jack’ }, 3 { name:’Diane’, service:’Email’, address:’diane@gmail.com’ }, 4 { name:’John’, service:’Phone’, number: ’919−919−9191’ } 5 ] 6 7 getId = multimethod() 8 .dispatch((contact) −> contact.service) 9 .when(’Twitter’, 10 (contact) −> contact.handle) 11 .when(’Email’, 12 (contact) −> contact.address) 13 .default( 14 (contact) −> contact.name) Figure 4.6: Example use of multimethod.js library 4.6 Haskell Function Pattern Matching Haskell provides pattern matching at function level. You can write multiple specializations, the arguments will be evaluated in order and the first specialization that matches will be used. One interesting aspect is the ability to destructure an argument into different variables within the specialization prototype. Figure 4.7 shows how to write Quicksort algorithm using function pattern matching in Haskell. 1 quicksort [ ] = [ ] 2 quicksort (p:xs) = (quicksort (filter (< p) xs)) ++ [p] ++ (quicksort (filter (>= p) xs)) Figure 4.7: Example of Haskell Function Pattern Matching
  • 16. Chapter 5 Conclusion In this report, we first showed an implementation of static property dispatch in C++. It ex- tensively uses C++ features in order to dispatch for a very specific need. Then we introduce a generic dispatch implementation that removes the compile-time constraint but which is able to express a huge range of dispatch behaviors. In particular, the previous dispatch is trivial to implement. Finally, we compare our generic dispatch to dispatch mechanisms from various languages and libraries both in term of syntax and expressiveness power. This reports concludes a serie of three reports where we implemented all the generic aspects that Olena provides for image processing taking advantage of Common Lisp dynamic aspects. In Chedeau (2010a), we introduced genericity on data structures. An image can not only be represented by a regular 2d grid but also as a 3d hexagonal grid or even as a graph. In Chedeau (2011), we have seen that values are also subject to genericity. It is common to see intensity values represented with 8-bit integer and floating point, Red-Green-Blue channels. Sometimes it is convenient to hold other value types such as booleans or even images. Eventually, this report completes the library including genericity on implementations.
  • 17. Chapter 6 Bibliography Axiak, M. (2010). Python generic dispatch. http://mike.axiak.net/blog/2010/06/ 25/python-generic-dispatch/. Chedeau, C. (2010a). Functionnal approach of image processing genericity. Technical Report 1001, EPITA Research and Development Laboratory (LRDE). Chedeau, C. (2010b). Javascript - full dispatch (extended form of multimethod). http:// blog.vjeux.com/2010/javascript/javascript-full-dispatch-multimethod. html. Chedeau, C. (2011). Component trees and chaining operators in climb. Technical Report 1108, EPITA Research and Development Laboratory (LRDE). Costanza, P., Herzeel, C., Vallejos, J., and D’Hondt, T. (2008). Filtered dispatch. In Proceedings of the 2008 symposium on Dynamic languages, DLS ’08, pages 4:1–4:10, New York, NY, USA. ACM. Denuzière, L. (2011). Designing the user interface for a common lisp generic library. Technical report, EPITA Research and Development Laboratory (LRDE). Géraud, Th. and Levillain, R. (2008). Semantics-driven genericity: A sequel to the static C++ object-oriented programming paradigm (SCOOP 2). In Proceedings of the 6th International Work- shop on Multiparadigm Programming with Object-Oriented Languages (MPOOL), Paphos, Cyprus. Keene, S. and Gerson, D. (1989). Object-oriented programming in Common LISP: a programmer’s guide to CLOS. Addison-Wesley.