Generalized Default Rules

Rules Haskell-default-default by default uses a variable of type a with constraints (C1 a, ..., Cn a) if:

  • Variable type a is not displayed in any other constraints
  • All Ci classes are standard.
  • At least one of the classes Ci is numeric

This makes sense to me, but it is also very limited: in practice, this means that you always need to specify a type if it is ambiguous when you work with custom classes. For example, this does not compile:

class (Show a) => MyShow a where myShow :: a -> String myShow = show instance (MyShow a) => MyShow (Maybe a) where myShow Nothing = "Nothing" myShow (Just x) = "Just " ++ (myShow x) main = print $ myShow Nothing -- ambiguous 

GHCi extends this rule set to:

  • All Ci classes are single parameter classes.
  • At least one of the Ci classes is numeric or is Show, Eq, or Ord.

and the GHC has an extension called ExtendedDefaultRules that allows these rules. However, this extension is very specific: it works only on GHC and with standard classes. For example, you might think of libraries that do not use the standard library. In this case, the GHCi extension will not work.

My question is: is there a single way to extend the default Haskell rules, such as GHCi, with compiler extensions? And more general: is there a way to define a default type based on some restriction rules in Haskell 98?

+6
source share
1 answer

There is no way to do what you want.

The current situation is poor in design. The idea was to do something minimal that could be expanded in the future when we have a good design.

+6
source

Source: https://habr.com/ru/post/950201/


All Articles