Skip to content

Commit b8927e5

Browse files
authored
Document GHC-99565 (#468)
1 parent 8d60ff4 commit b8927e5

File tree

4 files changed

+54
-0
lines changed

4 files changed

+54
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
title: Multiple default declarations
3+
summary: Multiple default declarations are not allowed
4+
severity: error
5+
introduced: 9.6.1
6+
---
7+
8+
A numeric literal such as `5` is overloaded in Haskell and has the type `forall a. Num a => a`.
9+
This allows the programmer to use numeric literals in many contexts without having a separate literal syntax for different numeric types.
10+
By extension, many numeric functions have a polymorphic type involving the `Num` type class.
11+
12+
A downside of using overloaded arithmetic expressions is that GHC is not always able to non-ambiguously infer a concrete type, such as `Int` or `Double`.
13+
This is the case, for example, in the expression `show 5`.
14+
The compiler cannot infer if the programmer means `show (5 :: Int)`, `show (5 :: Double)` or any other numeric type.
15+
16+
Since this case is very common, the Haskell report specifies a default behaviour for overloaded numeric expressions which involve the `Num` typeclass.
17+
Numeric types are defaulted to `Integer`, and if that is not possible to `Double`.
18+
19+
This defaulting behaviour can be customized with "default declarations".
20+
A default declaration uses the syntax `default (...)` with a list of comma-separated types.
21+
These types are tried in the order in which they occur in the default declaration.
22+
However, every Haskell module may contain at most one such default declaration.
23+
If a module contains more that one default declaration, then this error is thrown by GHC.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module MultipleDefaults where
2+
3+
default (Double, Int)
4+
5+
6+
main :: IO ()
7+
main = print 5
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module MultipleDefaults where
2+
3+
default (Double)
4+
default (Int)
5+
6+
main :: IO ()
7+
main = print 5
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
title: Multiple default declarations in single module
3+
---
4+
5+
In this example the programmer tries to customize the defaulting behaviour for numeric types to allow defaulting to both `Double` and `Int`.
6+
In order to do this both defaults have to be specified in a comma-separated list instead of two separate default declarations.
7+
8+
# Error Message
9+
10+
```
11+
messages/GHC-99565/multiple-defaults/before/MultipleDefaults.hs:4:1: error:
12+
Multiple default declarations
13+
here was another default declaration messages/GHC-99565/multiple-defaults/before/MultipleDefaults.hs:3:1-16
14+
|
15+
4 | default (Double)
16+
| ^^^^^^^^^^^^^^^^
17+
```

0 commit comments

Comments
 (0)