SICP - Generic Operator


Generic function: a meta function with a set of attached methods (specializers).
A call to a generic function matches the types of the actual arguments to the signatures of the attached methods and dispatches to the most appropriate method following deterministic rules.

For instance, sum & multiplication can be defined generically for any calculable entities (ordinal numbers, complex numbers, matrices,.. ). The "hwo-to-calculate" is determined and applied based on the type of the entity (number, matrix ...).

This is called 'Dispatch type': Looking at the types on the data and then dispatches them to the right operation. It can be implemented in two different ways:
  • Data-directed programming: data holds its type without holding the operation to apply (the target operation is selected from a separate 'operator-table' based on the data type)
  • Message passing style: data holds the operation to apply (object oriented programming) (seems similar to "Operator overloading": define the action of some operators on self-defined types).



Margin:
Generics in Java: the ability of a class/interface/method to take a type parameter. It's not considered as a fundamental construct; It allows to have type transparency. I guess this kind of  type parametrizing don't make sense for dynamic languages (lisp, javascript ...). 



(Rule based substitution example)
Stylized programming (tight coupling with problem domain) leads to ad hoc abstraction.

Good Design should be additive (if existing components require to be modified to include a new component, then system design is not additive) .

Coercion : a way by which objects of one type may be viewed as being of another type.
e.g (+ a b), if a and b are different types, then one is converted to the type of the other.  
Coercion is even optimal if there is a hierarchy on types, e.g:
(f a b)  & a extends c & b extends c; then both a & b can be converted (raised) to c 

Inadequacies of hierarchies: Dealing with large numbers of interrelated types (i.e a subtypes may have many super types and vice versa) while still preserving modularity in the design of large systems is very difficult.
For example, much of the complexity of object-oriented programming languages centers on the treatment of generic operations on interrelated types (classes, inheritance).
 
Developing a useful, general framework for expressing the relations among different types of entities (ontology), cannot be adequately addressed in terms of computer-language design alone, without also drawing on work in knowledge representation and automated reasoning






 

No comments: