Skip to content

Commit 41e1b74

Browse files
authored
Document GHC-88933 (#471)
1 parent b8927e5 commit 41e1b74

File tree

7 files changed

+84
-0
lines changed

7 files changed

+84
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module BoolDefaultDeclaration where
2+
3+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module BoolDefaultDeclaration where
2+
3+
default (Bool)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
title: A default declaration was provided for Bool
3+
order: 1
4+
---
5+
6+
In this example a default declaration was provided for the type `Bool`.
7+
Since `Bool` does not implement the `Num` type class, we have to remove
8+
the default declaration if we want to compile the program.
9+
10+
# Error message
11+
12+
```
13+
BoolDefaultDeclaration.hs:3:1: error: [GHC-88933]
14+
• The default type ‘Bool’ is not an instance of ‘Num’
15+
• When checking the types in a default declaration
16+
|
17+
3 | default (Bool)
18+
| ^^^^^^^^^^^^^^
19+
```
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
title: Non-numeric type in default declaration
3+
summary: Default declarations may only mention types which implement the Num type class
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+
23+
Every type which appears in a default declaration must implement the `Num` type class.
24+
If a type which occurs in a default declaration does not have such an instance, then this error
25+
is thrown by GHC.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{-# LANGUAGE OverloadedStrings #-}
2+
module StringDefaultDeclaration where
3+
4+
default (String)
5+
6+
main :: IO ()
7+
main = print "Hello, World!"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
module StringDefaultDeclaration where
3+
4+
default (String)
5+
6+
main :: IO ()
7+
main = print "Hello, World!"
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
title: A default declaration was provided for String
3+
order: 2
4+
---
5+
6+
In this example, the programmer wrote a default declaration for the type `String`, which does not implement the type class `Num`.
7+
If we enable the `OverloadedStrings` extension, then the rules about which types are allowed in default declarations are relaxed.
8+
Every type which implements either the `Num` or the `IsString` type class is now allowed in default declarations.
9+
10+
# Error message
11+
12+
13+
```
14+
StringDefaultDeclaration.hs:4:1: error: [GHC-88933]
15+
• The default type ‘String’ is not an instance of ‘Num’
16+
• When checking the types in a default declaration
17+
|
18+
4 | default (String)
19+
20+
```

0 commit comments

Comments
 (0)