Skip to content

Commit 1d5612c

Browse files
committed
Document GHC-10498
1 parent 2c280d5 commit 1d5612c

File tree

13 files changed

+132
-0
lines changed

13 files changed

+132
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module Lib where
2+
3+
areDifferent :: Eq a => a -> a -> Maybe (a, a)
4+
areDifferent x y
5+
| x == y = Nothing
6+
areDifferent x y = Just (x, y)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module Lib where
2+
3+
areDifferent :: a -> a -> Maybe (a, a)
4+
areDifferent x x = Nothing
5+
areDifferent x y = Just (x, y)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
title: Duplicate function arguments
3+
---
4+
5+
```
6+
Lib.hs:4:14: error: [GHC-10498]
7+
• Conflicting definitions for ‘x’
8+
Bound at: Lib.hs:4:14
9+
Lib.hs:4:16
10+
• In an equation for ‘areDifferent’
11+
|
12+
4 | areDifferent x x = Nothing
13+
|
14+
```
15+
16+
*Advanced topic:* somewhat surprisingly, you are allowed
17+
to duplicate arguments of type-level functions as in
18+
19+
```haskell
20+
{-# LANGUAGE DataKinds, TypeFamilies #-}
21+
type family IsElem x xs where
22+
IsElem _ '[] = 'False
23+
IsElem x (x ': xs) = 'True
24+
IsElem x (_ ': xs) = IsElem x xs
25+
```
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
title: Conflicting definitions
3+
summary: The same entity has more than one definition
4+
severity: error
5+
introduced: 9.8.1
6+
---
7+
8+
GHC does not allow the same entity to have more than one
9+
definition. In some contexts it can figure out which one
10+
is supposed to shadow another: local definitions take
11+
priority over global definitions, etc. Shadowing is just a warning, GHC-63397. However, if the definitions are on the same
12+
level it becomes a fatal error.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module Lib where
2+
3+
data Pair a = Pair a a
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module Lib where
2+
3+
data Pair a a = Pair a a
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
title: Duplicate datatype parameters
3+
---
4+
5+
```
6+
Lib.hs:3:11: error: [GHC-10498]
7+
Conflicting definitions for ‘a’
8+
Bound at: Lib.hs:3:11
9+
Lib.hs:3:13
10+
|
11+
3 | data Pair a a = Pair a a
12+
|
13+
```
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module Lib where
2+
3+
function :: Int -> Int
4+
function 1 = 1
5+
function 2 = 2
6+
function 3 = 3
7+
function _ = 4
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module Lib where
2+
3+
function :: Int -> Int
4+
function 1 = 1
5+
function 2 = 2
6+
functlon 3 = 3
7+
function _ = 4
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
title: Just a typo
3+
---
4+
5+
```
6+
Lib.hs:7:1: error: [GHC-29916]
7+
Multiple declarations of ‘function’
8+
Declared at: Lib.hs:4:1
9+
Lib.hs:7:1
10+
|
11+
7 | function _ = 4
12+
|
13+
```
14+
15+
Because of a typo (`functlon` instead of `function`)
16+
GHC parses the third equation as a definition of
17+
`functlon` (without a type signature), while the fourth
18+
equation becomes the second, conflicting definition of `function`.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{-# LANGUAGE NamedFieldPuns #-}
2+
3+
module Lib where
4+
5+
data Foo = Foo { x :: Int, y :: Int }
6+
7+
foo :: Foo -> Int -> Int
8+
foo Foo{y} x = x + y
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{-# LANGUAGE RecordWildCards #-}
2+
3+
module Lib where
4+
5+
data Foo = Foo { x :: Int, y :: Int }
6+
7+
foo :: Foo -> Int -> Int
8+
foo Foo{..} x = x + y
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
title: Implicit duplication because of `RecordWildCards`
3+
---
4+
5+
```
6+
Lib.hs:8:9: error: [GHC-10498]
7+
• Conflicting definitions for ‘x’
8+
Bound at: Lib.hs:8:9-10
9+
Lib.hs:8:13
10+
• In an equation for ‘foo’
11+
|
12+
8 | foo Foo{..} x = x + y
13+
|
14+
```
15+
16+
Consider using `NamedFieldPuns` to be more explicit
17+
about which variables are bound where.

0 commit comments

Comments
 (0)