Controlando o escopo de instâncias em Haskell - Marco Túlio Gontijo e Silva (2012)
The Haskell module system aims for simplicity and has a notableadvantage of being easy to learn and use. However, type classinstances in Haskell are always exported and imported betweenmodules. This breaches uniformity and simplicity of the module systemand introduces practical problems. instances created in different modules can conflict with each other, and can make it impossible to import two modules that contain instance definitions if this instance is used. Because of this, it is not very incovenient to define two distinct instances of the same type class for the same type in a program. As a workaround for this problem, the definition of instances in modules where the data type or the type class are not defined became a bad practice, and these instances were called orphans. Only these instances can cause the conflict since, as non-orphan instances are defined in the same module of the type or of the type class, there will be only one instance for each class and each type. In this dissertation we present and discuss a solution to these problems that simply allows control over importation and exportation of instances between modules, through a small change in the language. The solution is presented in two versions. The final version, more consistent, is not compatible with Haskell, that is, programs that work on Haskell may not work with this change. The intermediate version, on the other hand, brings the benefits of the proposal while being compatible with Haskell, although it is less consistent. In order to avoid that the programmer needs to write very long names for the instances in the importation and exportation control lists on modules, we propose another small change in the language in which it is possible to give shorter names to instances. We also show how a formal specification of the module system must be adapted to include our proposal. As the formal specification didn't handle instances in general, we adapt this specification to handle instances at first, and then show how our proposal can be formally specified.