Personal tools

User:Zzo38/Proposal for instance disambiguation

From HaskellWiki

< User:Zzo38(Difference between revisions)
Jump to: navigation, search
(New page: This document has some of my ideas about instance disambiguations and related things how it could be implemented in Haskell. =Select instances from imported modules= Example: import A ()...)
 
Line 6: Line 6:
 
import B class (AAA as ZZZ qualified as HHH);
 
import B class (AAA as ZZZ qualified as HHH);
 
import C hiding instance (Num String, Num Bool);
 
import C hiding instance (Num String, Num Bool);
From module A imports nothing not even instances. From module B imports instances of class AAA to be instances of class ZZZ instead, where all its methods are prefixed with "HHH." at the front. From module C, does not import instances of Num class for type String (this is only valid if type synonym instances is enabled, and means the same thing here) and Bool.
+
import D type (T as TT, U as newtype UU);
  +
From module A imports nothing not even instances. From module B imports instances of class AAA to be instances of class ZZZ instead, where all its methods are prefixed with "HHH." at the front. From module C, does not import instances of Num class for type String (this is only valid if type synonym instances is enabled, and means the same thing here) and Bool. From module D, the type named T is named TT when imported, for all functions using it, all instances, etc. If T is a type from a different module, then all function and instance from D using type T will make a new type TT instead. Instances for the type U are instead for a new type UU which is U wrapped in a newtype.
  +
  +
=Override instances=
  +
Example:
  +
import E hiding module C;
  +
This hides instances in C which conflict with those of E, and hides names (except qualified)
  +
  +
Example:
  +
instance Num Int where {
  +
x + y = 42;
  +
};
  +
This, defined in a module, automatically overrides instances from imported modules.
  +
  +
Example:
  +
instance (instance IsString [Char] hiding) => IsString [x];
  +
It means that this is instance for any list but if there is instance for strings, that one will override this one.
  +
  +
Example:
  +
instance Num x => Monoid (x as newtype (Sum x)) where {
  +
mempty = 0;
  +
mappend = (+);
  +
};
  +
Allows you to define newtype as part of an instance declaration, doing wrapping and unwrapping automatically.

Revision as of 06:16, 20 November 2011

This document has some of my ideas about instance disambiguations and related things how it could be implemented in Haskell.

1 Select instances from imported modules

Example:

import A () instance ();
import B class (AAA as ZZZ qualified as HHH);
import C hiding instance (Num String, Num Bool);
import D type (T as TT, U as newtype UU);

From module A imports nothing not even instances. From module B imports instances of class AAA to be instances of class ZZZ instead, where all its methods are prefixed with "HHH." at the front. From module C, does not import instances of Num class for type String (this is only valid if type synonym instances is enabled, and means the same thing here) and Bool. From module D, the type named T is named TT when imported, for all functions using it, all instances, etc. If T is a type from a different module, then all function and instance from D using type T will make a new type TT instead. Instances for the type U are instead for a new type UU which is U wrapped in a newtype.

2 Override instances

Example:

import E hiding module C;

This hides instances in C which conflict with those of E, and hides names (except qualified)

Example:

instance Num Int where {
  x + y = 42;
};

This, defined in a module, automatically overrides instances from imported modules.

Example:

instance (instance IsString [Char] hiding) => IsString [x];

It means that this is instance for any list but if there is instance for strings, that one will override this one.

Example:

instance Num x => Monoid (x as newtype (Sum x)) where {
  mempty = 0;
  mappend = (+);
};

Allows you to define newtype as part of an instance declaration, doing wrapping and unwrapping automatically.