Opened 4 years ago

Closed 3 years ago

#8753 closed task (fixed)

Import constructor but not the data type

Reported by: andreas.abel Owned by:
Priority: normal Milestone:
Component: Documentation Version: 7.6.3
Keywords: Cc: cactus
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Test Case:
Blocked By: Blocking:
Related Tickets: Differential Rev(s):
Wiki Page:

Description (last modified by hvr)

How do I import a constructor but not its data type? The constructor might have the name of a data type which I do not want to bring into scope.

Use case: Library (A) defines a parametrized data type (D a) which I want to use in my code (B) in instantiated form (D Int) but with the same name (D). I want to pattern match against inhabitants of type D, so I need the constructor(s) in scope.

Here is how it could look like if import lists let me specify whether I want to import a type or a constructor with name "D".

module A where

data D a = D a
module B where

import A (constructor D)
import qualified A
D = A.D Int

f :: D -> Int
f (D x) = x

Haskell has different name spaces for type and constructors, but does not let the user talk about these name spaces where it matters, namely in import/export lists. At least I found no documentation instructing me how to do this.

Change History (11)

comment:1 Changed 4 years ago by carter

Andreas, I think pattern synonyms cover this need. Have you tried them out yet? :)

comment:2 Changed 4 years ago by hvr

Description: modified (diff)

(fixed-up wiki markup in ticket description)

comment:3 Changed 4 years ago by andreas.abel

Indeed, they could do the job, but are they in ghc 7.6? I have not tried ghc 7.7.

comment:4 Changed 4 years ago by simonpj

Cc: cactus added

You are right that there is no good way to do this, and that the lack is unfortunate.

It is, however, possible in GHC 7.8. You can say

import A( pattern D )

provided you use LANGUAGE PatternSynonyms. Here D is a data constructor, but that's just a degenerate pattern synonym. Similarly in hiding and export lists.

Does that help?

I see that this point does not (yet) show up in the user manual, and it should. Gergo, might you fix that?


comment:5 Changed 4 years ago by andreas.abel

That certainly addresses the issue. No need to for a separate syntax for constructors, if it works for patterns in general. Thanks!

comment:6 Changed 3 years ago by nomeata

Resolution: fixed
Status: newclosed

comment:7 Changed 3 years ago by goldfire

Is this in the manual?

comment:8 Changed 3 years ago by nomeata

Component: CompilerDocumentation
Resolution: fixed
Status: closednew
Type: feature requesttask

Doesn’t seem like it. mentions pattern Foo in export/import list, but does not say that proper constructors can also be addressed this way.

I guess it should also be mentioned in

Anywhere else where this should be mentioned?

(Reopening as a task.)

comment:9 Changed 3 years ago by goldfire

That looks sufficient to me...

comment:10 Changed 3 years ago by Simon Peyton Jones <simonpj@…>

In 59cdb992df5bd8bdc563b30c2103c323a7d57f15/ghc:

Document explicit import/export of data constructors (Trac #8753)

I also added sub-sections to the pattern synonym documentation

comment:11 Changed 3 years ago by simonpj

Resolution: fixed
Status: newclosed

Good idea, done.

Note: See TracTickets for help on using tickets.