| | 17 | == Context reduction == |
| | 18 | Haskell computes a type for each variable bound by `let` or `where`, |
| | 19 | and then generalizes this type. |
| | 20 | In Haskell 98, the allowed contexts are restricted, so contexts are reduced using `instance` declarations (and duplicate assertions and those implied by `class` contexts are removed) until either they are in the allowed form or no instance is applicable, in which case an error is reported. For example, in the following |
| | 21 | {{{ |
| | 22 | module M where |
| | 23 | class C a where c_method :: a -> Bool |
| | 24 | foo xs = c_method (tail xs) |
| | 25 | }}} |
| | 26 | the context of the inferred type |
| | 27 | {{{ |
| | 28 | foo :: C [a] => [a] -> Bool |
| | 29 | }}} |
| | 30 | is neither allowed nor reducible, so a missing instance `C [a]` is reported. |
| | 31 | |
| | 32 | When contexts are unrestricted, context reduction is forced only by explicit signatures and the type of `main`. |
| | 33 | The above example becomes legal; if a matching instance is in scope when context reduction is forced on uses of `foo`, they will also typecheck: |
| | 34 | {{{ |
| | 35 | import M |
| | 36 | instance C [a] where c_method = null |
| | 37 | |
| | 38 | main :: IO () |
| | 39 | main = print (foo "abc") |
| | 40 | }}} |
| | 41 | |
| | 42 | Delaying context reduction: |
| | 43 | * can leave contexts more complex (could interact with the Monomorphism Restriction) |
| | 44 | * delays (and sometimes avoids) type errors |
| | 45 | * sometimes avoids nontermination of context reduction |
| | 46 | * is required by OverlappingInstances |
| | 47 | |